Re: Proposed addition of malloc_size_np()



On Mar 27, 2006, at 12:20 PM, John Baldwin wrote:
On Monday 27 March 2006 11:34, Jason Evans wrote:
I can think of a few straightforward uses:

1) Suppose that we are about to write to a string that we know is
malloc()ed, and we want to be sure that we aren't overflowing the
allocated buffer. We can add an assertion that the buffer is indeed
large enough to contain what is about to be written to it.

In that case though you really want to know the size you allocated,
not necessarily the backing-store size, esp. if you are in a library
since your caller may (correctly) assume that if it allocates X
bytes for a string the library will only write up to X bytes. So, I
think in that case you want the requested size, not the backing store
size.

Yes, you're right that we actually want the requested size rather than the backing-store size. This is only a "better than nothing" use case.

4) Porting code from Linux. Case in point: Xara Xtreme, currently being
ported by Vasil Dimov. At the moment, he has to use dlmalloc.

Does it contain a G/C of its own or some such?

I don't know the answer to that.

Following is what I've written for the malloc(3) man page:
----
The malloc_usable_size() function returns the usable size of the
allocation pointed to by ptr. The return value may be larger than the
size that was requested during allocation. malloc_usable_size() is not
intended as a mechanism for in-place realloc(), though it can be abused
that way; rather it is primarily provided as a tool for introspection
purposes. Any discrepancy between the requested allocation size and the
size reported by malloc_usable_size() should not be depended on, since
such behavior is entirely implementation-dependent.
----

I would word it stronger: "malloc_usable_size() should not be used as a
mechanism for in-place realloc(). It is provided solely as a tool for
introspection purposes."

Okay, sounds fine to me.

Thanks,
Jason

_______________________________________________
freebsd-arch@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-arch
To unsubscribe, send any mail to "freebsd-arch-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Is PSHUFW instruction MMX or SSE or SSE2? Is NASM manual correct?
    ... Then it would do a "first fit" search of the FAQE chain. ... allocation, and update the preceding FAQE to point to the ... MOVSTR with the target string address in SI, ...
    (alt.lang.asm)
  • Re: Strings in C are less optimal than in (say) Pascal - correct?
    ... >provided a string type with these 3 parts: ... I have a library of macros and functions that I use ... a vector of char can be used as a string. ... Here are the macros that access the size and allocation (I'm assuming ...
    (comp.lang.c)
  • Re: TStreams
    ... A string is an array of characters with the first character ... TMyRec = packed record ... FS.Seek(0, soFromBeginning); ... Note the protection of the resource allocation by a try / finally / end ...
    (comp.lang.pascal.delphi.misc)
  • Re: TStreams
    ... A string is an array of characters with the first character ... TMyRec = packed record ... FS.Seek(0, soFromBeginning); ... Note the protection of the resource allocation by a try / finally / end ...
    (comp.lang.pascal.delphi.misc)
  • Re: safe strcpy()?
    ... >The problem arises if the memory allocation for from exceeds that for to. ... there is no way for the string copy function itself to determine the ... The other response would be to just ignore writes to arrays ... "surprising" incorrect behavior. ...
    (SecProg)