ReactOS 0.4.15-dev-7953-g1f49173
malloc.c
Go to the documentation of this file.
1/*
2 * msvcrt.dll heap functions
3 *
4 * Copyright 2000 Jon Griffiths
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Note: Win32 heap operations are MT safe. We only lock the new
21 * handler and non atomic heap operations
22 */
23
24#include <precomp.h>
25#include <stdlib.h>
26#include <malloc.h>
27
28#define ROUND_DOWN(n, align) \
29 (((ULONG)n) & ~((align) - 1l))
30
31#define ROUND_UP(n, align) \
32 ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
33
34/* round to 16 bytes + alloc at minimum 16 bytes */
35#define ROUND_SIZE(size) (max(16, ROUND_UP(size, 16)))
36
37/*
38 * @implemented
39 */
40void* malloc(size_t _size)
41{
42 size_t nSize = ROUND_SIZE(_size);
43
44 if (nSize<_size)
45 return NULL;
46
47 return HeapAlloc(GetProcessHeap(), 0, nSize);
48}
49
50/*
51 * @implemented
52 */
53void free(void* _ptr)
54{
55 HeapFree(GetProcessHeap(),0,_ptr);
56}
57
58/*
59 * @implemented
60 */
61void* calloc(size_t _nmemb, size_t _size)
62{
63 size_t nSize = _nmemb * _size;
64 size_t cSize = ROUND_SIZE(nSize);
65
66 if ( (_nmemb > ((size_t)-1 / _size)) || (cSize<nSize))
67 return NULL;
68
69 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cSize );
70}
71
72/*
73 * @implemented
74 */
75void* realloc(void* _ptr, size_t _size)
76{
77 size_t nSize;
78
79 if (_ptr == NULL)
80 return malloc(_size);
81
82 if (_size == 0)
83 {
84 free(_ptr);
85 return NULL;
86 }
87
88 nSize = ROUND_SIZE(_size);
89
90 /* check for integer overflow */
91 if (nSize<_size)
92 return NULL;
93
94 return HeapReAlloc(GetProcessHeap(), 0, _ptr, nSize);
95}
96
97/*
98 * @implemented
99 */
100void* _expand(void* _ptr, size_t _size)
101{
102 size_t nSize;
103
104 nSize = ROUND_SIZE(_size);
105
106 if (nSize<_size)
107 return NULL;
108
110}
111
112/*
113 * @implemented
114 */
115size_t _msize(void* _ptr)
116{
117 return HeapSize(GetProcessHeap(), 0, _ptr);
118}
119
120/*
121 * @implemented
122 */
123int _heapchk(void)
124{
125 if (!HeapValidate(GetProcessHeap(), 0, NULL))
126 return -1;
127 return 0;
128}
129
130/*
131 * @implemented
132 */
133int _heapmin(void)
134{
135 if (!HeapCompact(GetProcessHeap(), 0))
136 return -1;
137 return 0;
138}
139
140/*
141 * @implemented
142 */
143int _heapset(unsigned int unFill)
144{
145 if (_heapchk() == -1)
146 return -1;
147 return 0;
148
149}
150
151/*
152 * @implemented
153 */
155{
156 return 0;
157}
158
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapReAlloc
Definition: compat.h:734
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
SIZE_T WINAPI HeapCompact(HANDLE hHeap, DWORD dwFlags)
Definition: heapmem.c:145
BOOL WINAPI HeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
Definition: heapmem.c:156
uint32_t entry
Definition: isohybrid.c:63
int _heapchk(void)
Definition: malloc.c:123
int _heapset(unsigned int unFill)
Definition: malloc.c:143
void * _expand(void *_ptr, size_t _size)
Definition: malloc.c:100
int _heapmin(void)
Definition: malloc.c:133
size_t _msize(void *_ptr)
Definition: malloc.c:115
#define ROUND_SIZE(size)
Definition: malloc.c:35
int _heapwalk(struct _heapinfo *entry)
Definition: malloc.c:154
#define HEAP_REALLOC_IN_PLACE_ONLY
Definition: nt_native.h:1696
#define calloc
Definition: rosglue.h:14
SIZE_T WINAPI HeapSize(HANDLE, DWORD, LPCVOID)
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2084