develooper Front page | perl.perl5.porters | Postings from July 2020

Newx()/sv_grow/newSV() that doesnt die on too much memory?

Thread Next
July 7, 2020 15:04
Newx()/sv_grow/newSV() that doesnt die on too much memory?
Message ID:
I noticed that the perldocs say we should use the Newx family of
functions for allocating buffers instead of malloc(), but none of them
have an api that allows for a NULL pointer return due to insufficient
memory.  They all die if too much memory is requested, in safemalloc()
I believe.

So what is the correct way to do this? A simple malloc() call works,
but I thought we used Newx() and friends to work around portability
issues with malloc.

So what is the correct way to deal with this? Should there be a
Newx_null() or something that deal with this?

A related issue is it would be nice to have an XS way to say "make
sure this PV contains X bytes, or return NULL", eg, something like:

ptr= sv_grow_null(pv,size);
if (!ptr) handle_not_enough_memory(size);

Currently if I want to do something like this I need to malloc the
buffer myself and then stitch it into the SV body afterwards, and
every time I do something like that I feel a bit dirty... Such
unwarranted chuminess with the existing implementation is not good.



perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About