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

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

Thread Next
From:
demerphq
Date:
July 7, 2020 15:04
Subject:
Newx()/sv_grow/newSV() that doesnt die on too much memory?
Message ID:
CANgJU+UANUGTwp1ts37Dg8S8Kd5H9bSRiLfAN0FrtBFQ7iRpBA@mail.gmail.com
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.

Thoughts?

cheers,
Yves


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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About