summaryrefslogtreecommitdiff
path: root/inline.h
diff options
context:
space:
mode:
authorRichard Leach <richardleach@users.noreply.github.com>2021-06-11 20:48:29 +0100
committerHugo van der Sanden <hv@crypt.org>2021-07-03 02:40:33 +0100
commit84c75204391437a2f1d2f579c545b4592014b1cb (patch)
treeae4dd6fe22fe647fab18b1c4dcfb5095e4bf2ea8 /inline.h
parentc683824536c9806b1b970459bf81690e006b59ae (diff)
downloadperl-84c75204391437a2f1d2f579c545b4592014b1cb.tar.gz
Add inline av_fetch_simple and av_store_simple functions
Diffstat (limited to 'inline.h')
-rw-r--r--inline.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/inline.h b/inline.h
index 65b2d9df0c..55b26e0d78 100644
--- a/inline.h
+++ b/inline.h
@@ -57,6 +57,89 @@ Perl_av_count(pTHX_ AV *av)
return AvFILL(av) + 1;
}
+/* ------------------------------- av.c ------------------------------- */
+
+/*
+=for apidoc av_store_simple
+
+This is a cut-down version of av_store that assumes that the array is
+very straightforward - no magic, not readonly, and AvREAL - and that
+C<key> is not negative. This function MUST NOT be used in situations
+where any of those assumptions may not hold.
+
+Stores an SV in an array. The array index is specified as C<key>. It
+can be dereferenced to get the C<SV*> that was stored there (= C<val>)).
+
+Note that the caller is responsible for suitably incrementing the reference
+count of C<val> before the call.
+
+Approximate Perl equivalent: C<splice(@myarray, $key, 1, $val)>.
+
+=cut
+*/
+
+PERL_STATIC_INLINE SV**
+Perl_av_store_simple(pTHX_ AV *av, SSize_t key, SV *val)
+{
+ SV** ary;
+
+ PERL_ARGS_ASSERT_AV_STORE_SIMPLE;
+ assert(SvTYPE(av) == SVt_PVAV);
+ assert(!SvMAGICAL(av));
+ assert(!SvREADONLY(av));
+ assert(AvREAL(av));
+ assert(key > -1);
+
+ ary = AvARRAY(av);
+
+ if (AvFILLp(av) < key) {
+ if (key > AvMAX(av)) {
+ av_extend(av,key);
+ ary = AvARRAY(av);
+ }
+ AvFILLp(av) = key;
+ } else
+ SvREFCNT_dec(ary[key]);
+
+ ary[key] = val;
+ return &ary[key];
+}
+
+/*
+=for apidoc av_fetch_simple
+
+This is a cut-down version of av_fetch that assumes that the array is
+very straightforward - no magic, not readonly, and AvREAL - and that
+C<key> is not negative. This function MUST NOT be used in situations
+where any of those assumptions may not hold.
+
+Returns the SV at the specified index in the array. The C<key> is the
+index. If lval is true, you are guaranteed to get a real SV back (in case
+it wasn't real before), which you can then modify. Check that the return
+value is non-null before dereferencing it to a C<SV*>.
+
+The rough perl equivalent is C<$myarray[$key]>.
+
+=cut
+*/
+
+PERL_STATIC_INLINE SV**
+Perl_av_fetch_simple(pTHX_ AV *av, SSize_t key, I32 lval)
+{
+ PERL_ARGS_ASSERT_AV_FETCH_SIMPLE;
+ assert(SvTYPE(av) == SVt_PVAV);
+ assert(!SvMAGICAL(av));
+ assert(!SvREADONLY(av));
+ assert(AvREAL(av));
+ assert(key > -1);
+
+ if ( (key > AvFILLp(av)) || !AvARRAY(av)[key]) {
+ return lval ? av_store_simple(av,key,newSV(0)) : NULL;
+ } else {
+ return &AvARRAY(av)[key];
+ }
+}
+
/* ------------------------------- cv.h ------------------------------- */
/*