Proposed addition of malloc_size_np()

John-Mark Gurney gurney_j at resnet.uoregon.edu
Sat Mar 25 04:51:51 UTC 2006


Jason Evans wrote this message on Fri, Mar 24, 2006 at 15:17 -0800:
> === Proposal ===
> Add malloc_size_np() to libc, and provide the prototype in malloc_np.h:
> 
> 	size_t
> 	malloc_size_np(const void *ptr);
> 
> This function would return the usable size of the allocation pointed to 
> by ptr, which is always greater than or equal to the size that was 
> specified in the call to malloc(), calloc(), posix_memalign(), or 
> realloc(), regardless of allocator implementation.  It is up to the 
> implementation whether the value returned by malloc_size_np() differs 
> from the size specified during allocation, but the return value must be 
> consistent across multiple calls for the same allocation.

a) the idea of using memory beyond what you've allocated is bad...
b) it should return the maximum size you can realloc to w/o a copy
of the memory...  and then the way you use the extra memory is to
call realloc on the buffer to the value returned by malloc_size_np..

My vote is to never let allocations go outside their area even if
they could...  It'll just make new programmers lazy, and ready to
break something...

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."


More information about the freebsd-arch mailing list