summaryrefslogtreecommitdiff
path: root/av.c
diff options
context:
space:
mode:
authorRichard Leach <richardleach@users.noreply.github.com>2021-04-19 21:27:03 +0100
committerHugo van der Sanden <hv@crypt.org>2021-05-26 13:37:12 +0100
commit0b1c19ab1cbed9c221a41fca38580344778ce3a6 (patch)
tree57806b6c33ce1b88588f23a10c37fb3200ec4897 /av.c
parent9940d7c95f83dcd73b4b62eba7c1671f22ca8bf3 (diff)
downloadperl-0b1c19ab1cbed9c221a41fca38580344778ce3a6.tar.gz
Add Perl_av_new_alloc() function and newAV_alloc_x/z() macros
Diffstat (limited to 'av.c')
-rw-r--r--av.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/av.c b/av.c
index 03d793b31a..afac896dda 100644
--- a/av.c
+++ b/av.c
@@ -394,6 +394,44 @@ Perl_av_store(pTHX_ AV *av, SSize_t key, SV *val)
}
/*
+=for apidoc av_new_alloc
+
+Creates a new AV and allocates its SV* array.
+
+This is similar to but more efficient than doing:
+
+ AV *av = newAV();
+ av_extend(av, key);
+
+The zeroflag parameter controls whether the array is NULL initialized.
+
+Note that av_index() takes the desired AvMAX as its key parameter, but
+av_new_alloc() instead takes the desired size (so AvMAX + 1). This
+size must be at least 1.
+
+=cut
+*/
+
+AV *
+Perl_av_new_alloc(pTHX_ SSize_t size, bool zeroflag)
+{
+ AV * const av = newAV();
+ SV** ary;
+ PERL_ARGS_ASSERT_AV_NEW_ALLOC;
+ assert(size > 0);
+
+ Newx(ary, size, SV*); /* Newx performs the memwrap check */
+ AvALLOC(av) = ary;
+ AvARRAY(av) = ary;
+ AvMAX(av) = size - 1;
+
+ if (zeroflag)
+ Zero(ary, size, SV*);
+
+ return av;
+}
+
+/*
=for apidoc av_make
Creates a new AV and populates it with a list of SVs. The SVs are copied