alloc at minimum 16 bytes (spotted by WaxDragon)

realloc should free passed mem if new size is 0

svn path=/trunk/; revision=18404
This commit is contained in:
Gunnar Dalsnes 2005-10-10 22:42:13 +00:00
parent 1fc06afd71
commit b1761726b2

View file

@ -29,6 +29,9 @@
/* fixme: should have this in common header */ /* fixme: should have this in common header */
#define ROUND_UP(a,b) ((a + (b-1)) & ~(b-1)) #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; extern HANDLE hHeap;
/* /*
@ -36,7 +39,7 @@ extern HANDLE hHeap;
*/ */
void* malloc(size_t _size) 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) 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) void* realloc(void* _ptr, size_t _size)
{ {
if (!_ptr) if (!_ptr) return malloc(_size);
return HeapAlloc(hHeap, 0, ROUND_UP(_size, 16)); if (_size) return HeapReAlloc(hHeap, 0, _ptr, ROUND_SIZE(_size));
return HeapReAlloc(hHeap, 0, _ptr, ROUND_UP(_size, 16)); free(_ptr);
return NULL;
} }
/* /*
@ -70,7 +74,7 @@ void* realloc(void* _ptr, size_t _size)
*/ */
void* _expand(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));
} }
/* /*