alloc at minimum 16 bytes (spotted by WaxDragon)
realloc should free passed mem if new size is 0
Modified: trunk/reactos/lib/crt/stdlib/malloc.c
_____
Modified: trunk/reactos/lib/crt/stdlib/malloc.c
--- trunk/reactos/lib/crt/stdlib/malloc.c 2005-10-10 20:18:07 UTC
(rev 18403)
+++ trunk/reactos/lib/crt/stdlib/malloc.c 2005-10-10 22:42:13 UTC
(rev 18404)
@@ -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 @@
*/
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* 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* 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* _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));
}
/*
Show replies by date