On Saturday 03 June 2006 14:39, Thomas Weidenmueller wrote:
Royce Mitchell III wrote:
@@ -60,6
+66,9 @@
*/
void* realloc(void* _ptr, size_t _size)
{
+ if ( _size == 0)
+ return NULL;
+
if (!_ptr) return malloc(_size);
if (_size) return HeapReAlloc(hHeap, 0, _ptr, ROUND_SIZE(_size));
free(_ptr);
@@ -71,6 +80,9 @@
*/
umm... shouldn't we call free() if we're going to return NULL because
size is 0?
realloc never frees the memory if it fails.
Calling realloc() sith size as 0 is supposed to free the buffer, not fail.
Heres how realloc() is defined in the man-page:
realloc() changes the size of the memory block pointed to by ptr to
size bytes. The contents will be unchanged to the minimum of the old
and new sizes; newly allocated memory will be uninitialized. If ptr is
NULL, the call is equivalent to malloc(size); if size is equal to zero,
the call is equivalent to free(ptr). Unless ptr is NULL, it must have
been returned by an earlier call to malloc(), calloc() or realloc().
If the area pointed to was moved, a free(ptr) is done.
void* _expand(void* _ptr, size_t _size)
{
+ if ( _size == 0)
+ return NULL;
+
return HeapReAlloc(hHeap, HEAP_REALLOC_IN_PLACE_ONLY, _ptr,
ROUND_SIZE(_size)); }
Again, shouldn't we free the existing buffer if we're returning NULL
because size is 0?
no, same as with realloc.
This call isn't in Posix or any C library I've ever used, but seems to be
correct. Expanding a buffer by 0 bytes, if called from userspace, should
return the original buffer. Called internally, like this appears to be,
returning NULL indicates the buffer wasn't touched.
DRH
(If you need help with how people will *expect* known functions to work
(specifically C library functions) just ask :)