diff --git a/reactos/lib/crt/stdlib/malloc.c b/reactos/lib/crt/stdlib/malloc.c index 44cf4e49dec..b1227a54772 100644 --- a/reactos/lib/crt/stdlib/malloc.c +++ b/reactos/lib/crt/stdlib/malloc.c @@ -29,6 +29,9 @@ /* fixme: should have this in common header */ #define ROUND_UP(a,b) ((a + (b-1)) & ~(b-1)) +/* round to 16 bytes + alloc at minimum 16 bytes */ +#define ROUND_SIZE(size) (max(16, ROUND_UP(size, 16))) + extern HANDLE hHeap; /* @@ -36,7 +39,7 @@ extern HANDLE hHeap; */ void* malloc(size_t _size) { - return HeapAlloc(hHeap, 0, ROUND_UP(_size, 16)); + return HeapAlloc(hHeap, 0, ROUND_SIZE(_size)); } /* @@ -52,7 +55,7 @@ void free(void* _ptr) */ void* calloc(size_t _nmemb, size_t _size) { - return HeapAlloc(hHeap, HEAP_ZERO_MEMORY, ROUND_UP(_nmemb*_size, 16) ); + return HeapAlloc(hHeap, HEAP_ZERO_MEMORY, ROUND_SIZE(_nmemb*_size) ); } /* @@ -60,9 +63,10 @@ void* calloc(size_t _nmemb, size_t _size) */ void* realloc(void* _ptr, size_t _size) { - if (!_ptr) - return HeapAlloc(hHeap, 0, ROUND_UP(_size, 16)); - return HeapReAlloc(hHeap, 0, _ptr, ROUND_UP(_size, 16)); + if (!_ptr) return malloc(_size); + if (_size) return HeapReAlloc(hHeap, 0, _ptr, ROUND_SIZE(_size)); + free(_ptr); + return NULL; } /* @@ -70,7 +74,7 @@ void* realloc(void* _ptr, size_t _size) */ void* _expand(void* _ptr, size_t _size) { - return HeapReAlloc(hHeap, HEAP_REALLOC_IN_PLACE_ONLY, _ptr, ROUND_UP(_size, 16)); + return HeapReAlloc(hHeap, HEAP_REALLOC_IN_PLACE_ONLY, _ptr, ROUND_SIZE(_size)); } /*