/* 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;
/*
*/
void* malloc(size_t _size)
{
- return HeapAlloc(hHeap, 0, ROUND_UP(_size, 16));
+ return HeapAlloc(hHeap, 0, ROUND_SIZE(_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) );
}
/*
*/
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;
}
/*
*/
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));
}
/*