s***@casperkitty.com
2016-02-02 01:14:36 UTC
Depending upon how msize is defined, it may be possible for every possible
malloc implementation to implement it, though the usefulness of such
implementations could vary.
That definition is too weak as your dummy implementation shows.malloc implementation to implement it, though the usefulness of such
implementations could vary.
are other possible usage cases for which it is inadequate. Oh the other
hand, the way I have defined the function would allow it to be implemented
legitimately on every C implementation. Code which was written to take
advantage of the function to yield better performance on systems that could
implement it usefully would not receive those performance benefits on those
systems which didn't implement it usefully, but would still run correctly
regardless. Since most systems could implement it somewhat usefully, adding
the function to the Standard would make it possible for portable code to
receive performance improvement on most systems while remaining compatible
with all.
It is >= the allocation size passed to the allocation function.
Adding that to the standard would increase the storage cost of mallco() onsome platforms, even when using code which didn't take any advantage of it.
In some cases, when using code that allocates many small objects, the extra
cost could be very significant.
Note that the only requirement of this hypothetical implementation is
that memory passed to free() can be returned by later calls to malloc().
I would suggest that in many common usage scenarios a quick answer to thethat memory passed to free() can be returned by later calls to malloc().
question "Is block X guaranteed to be large enough to hold Y bytes" would
be more useful than a really slow answer to the question "Exactly how many
bytes are available in block X". There could be some benefits to having
a more precise estimate available, but in many cases there would be a
workable strategy which code could follow if it didn't know the block size
(e.g. use realloc). The only question would be whether a faster strategy
would be whether an alternative might be better (e.g. don't use realloc,
and avoid the need to regenerate any pointers which might have been changed
if code had used realloc).
I would suggest that a similar principle should be applied to things like
relational operators on pointers. I can't think of any implementations for
which efficient code generation would be impaired by a requirement that
use of relational operators on pointers which are either valid or null
will never do anything other than yield 0, yield 1, or raise an
implementation-defined signal, but there are many cases where adding such
a guarantee to the language would make it possible for portable code to
run much more efficiently on common implementations. To be sure, stronger
guarantees would be better still, but even a weak guarantee like the
above would be vastly more useful than having no sane way to check things
like whether two regions could be guaranteed not to overlap, except via:
for (int i=0; i<region_size; i++)
if ((char*)p1 == ((char*)p2+i) ||
(char*)p2 == ((char*)p1+i))
return 1;
which would be portable, but would on many implementations be absurdly
slow.