- Cleanup the /lib directory, by putting more 3rd-party libs in /3rdparty, and by...
[reactos.git] / reactos / lib / sdk / crt / stdlib / malloc.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
29 /* round to 16 bytes + alloc at minimum 16 bytes */
30 #define ROUND_SIZE(size) (max(16, ROUND_UP(size, 16)))
31
32 extern HANDLE hHeap;
33
34 /*
35 * @implemented
36 */
37 void* malloc(size_t _size)
38 {
39 size_t nSize = ROUND_SIZE(_size);
40
41 if (nSize<_size)
42 return NULL;
43
44 return HeapAlloc(hHeap, 0, nSize);
45 }
46
47 /*
48 * @implemented
49 */
50 void free(void* _ptr)
51 {
52 HeapFree(hHeap,0,_ptr);
53 }
54
55 /*
56 * @implemented
57 */
58 void* calloc(size_t _nmemb, size_t _size)
59 {
60 size_t nSize = _nmemb * _size;
61 size_t cSize = ROUND_SIZE(nSize);
62
63 if ( (_nmemb > ((size_t)-1 / _size)) || (cSize<nSize))
64 return NULL;
65
66 return HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cSize );
67 }
68
69 /*
70 * @implemented
71 */
72 void* realloc(void* _ptr, size_t _size)
73 {
74 size_t nSize;
75
76 if (( _size == 0) && (_ptr !=NULL))
77 return NULL;
78
79 nSize = ROUND_SIZE(_size);
80
81 if (nSize<_size)
82 return NULL;
83
84 if (!_ptr) return malloc(_size);
85 if (_size) return HeapReAlloc(hHeap, 0, _ptr, nSize);
86 free(_ptr);
87 return NULL;
88 }
89
90 /*
91 * @implemented
92 */
93 void* _expand(void* _ptr, size_t _size)
94 {
95 size_t nSize;
96
97 nSize = ROUND_SIZE(_size);
98
99 if (nSize<_size)
100 return NULL;
101
102 return HeapReAlloc(hHeap, HEAP_REALLOC_IN_PLACE_ONLY, _ptr, nSize);
103 }
104
105 /*
106 * @implemented
107 */
108 size_t _msize(void* _ptr)
109 {
110 return HeapSize(hHeap, 0, _ptr);
111 }
112
113 /*
114 * @implemented
115 */
116 int _heapchk(void)
117 {
118 if (!HeapValidate(hHeap, 0, NULL))
119 return -1;
120 return 0;
121 }
122
123 /*
124 * @implemented
125 */
126 int _heapmin(void)
127 {
128 if (!HeapCompact(hHeap, 0))
129 return -1;
130 return 0;
131 }
132
133 /*
134 * @implemented
135 */
136 int _heapset(unsigned int unFill)
137 {
138 if (_heapchk() == -1)
139 return -1;
140 return 0;
141
142 }
143
144 /*
145 * @implemented
146 */
147 int _heapwalk(struct _heapinfo* entry)
148 {
149 return 0;
150 }
151