{
/* Take the union of two inversion lists and point <output> to it. *output
* SHOULD BE DEFINED upon input, and if it points to one of the two lists,
- * the reference count to that list will be decremented. The first list,
- * <a>, may be NULL, in which case a copy of the second list is returned.
- * If <complement_b> is TRUE, the union is taken of the complement
- * (inversion) of <b> instead of b itself.
+ * the reference count to that list will be decremented if not already a
+ * temporary (mortal); otherwise *output will be made correspondingly
+ * mortal. The first list, <a>, may be NULL, in which case a copy of the
+ * second list is returned. If <complement_b> is TRUE, the union is taken
+ * of the complement (inversion) of <b> instead of b itself.
*
* The basis for this comes from "Unicode Demystified" Chapter 13 by
* Richard Gillam, published by Addison-Wesley, and explained at some
/* If either one is empty, the union is the other one */
if (a == NULL || ((len_a = _invlist_len(a)) == 0)) {
+ bool make_temp = FALSE; /* Should we mortalize the result? */
+
if (*output == a) {
if (a != NULL) {
- SvREFCNT_dec_NN(a);
+ if (! (make_temp = SvTEMP(a))) {
+ SvREFCNT_dec_NN(a);
+ }
}
}
if (*output != b) {
_invlist_invert(*output);
}
} /* else *output already = b; */
+
+ if (make_temp) {
+ sv_2mortal(*output);
+ }
return;
}
else if ((len_b = _invlist_len(b)) == 0) {
+ bool make_temp = FALSE;
if (*output == b) {
- SvREFCNT_dec_NN(b);
+ if (! (make_temp = SvTEMP(b))) {
+ SvREFCNT_dec_NN(b);
+ }
}
/* The complement of an empty list is a list that has everything in it,
* so the union with <a> includes everything too */
if (complement_b) {
if (a == *output) {
- SvREFCNT_dec_NN(a);
+ if (! (make_temp = SvTEMP(a))) {
+ SvREFCNT_dec_NN(a);
+ }
}
*output = _new_invlist(1);
_append_range_to_invlist(*output, 0, UV_MAX);
*output = invlist_clone(a);
}
/* else *output already = a; */
+
+ if (make_temp) {
+ sv_2mortal(*output);
+ }
return;
}
}
}
- /* We may be removing a reference to one of the inputs */
+ /* We may be removing a reference to one of the inputs. If so, the output
+ * is made mortal if the input was. (Mortal SVs shouldn't have their ref
+ * count decremented) */
if (a == *output || b == *output) {
assert(! invlist_is_iterating(*output));
- SvREFCNT_dec_NN(*output);
+ if ((SvTEMP(*output))) {
+ sv_2mortal(u);
+ }
+ else {
+ SvREFCNT_dec_NN(*output);
+ }
}
*output = u;
+
return;
}
{
/* Take the intersection of two inversion lists and point <i> to it. *i
* SHOULD BE DEFINED upon input, and if it points to one of the two lists,
- * the reference count to that list will be decremented.
+ * the reference count to that list will be decremented if not already a
+ * temporary (mortal); otherwise *i will be made correspondingly mortal.
* The first list, <a>, may be NULL, in which case an empty list is
* returned. If <complement_b> is TRUE, the result will be the
* intersection of <a> and the complement (or inversion) of <b> instead of
/* Special case if either one is empty */
len_a = (a == NULL) ? 0 : _invlist_len(a);
if ((len_a == 0) || ((len_b = _invlist_len(b)) == 0)) {
+ bool make_temp = FALSE;
if (len_a != 0 && complement_b) {
* simply 'a'. */
if (*i != a) {
if (*i == b) {
- SvREFCNT_dec_NN(b);
+ if (! (make_temp = SvTEMP(b))) {
+ SvREFCNT_dec_NN(b);
+ }
}
*i = invlist_clone(a);
}
/* else *i is already 'a' */
+
+ if (make_temp) {
+ sv_2mortal(*i);
+ }
return;
}
/* Here, 'a' or 'b' is empty and not using the complement of 'b'. The
* intersection must be empty */
if (*i == a) {
- SvREFCNT_dec_NN(a);
+ if (! (make_temp = SvTEMP(a))) {
+ SvREFCNT_dec_NN(a);
+ }
}
else if (*i == b) {
- SvREFCNT_dec_NN(b);
+ if (! (make_temp = SvTEMP(b))) {
+ SvREFCNT_dec_NN(b);
+ }
}
*i = _new_invlist(0);
+ if (make_temp) {
+ sv_2mortal(*i);
+ }
+
return;
}
}
}
- /* We may be removing a reference to one of the inputs */
+ /* We may be removing a reference to one of the inputs. If so, the output
+ * is made mortal if the input was. (Mortal SVs shouldn't have their ref
+ * count decremented) */
if (a == *i || b == *i) {
assert(! invlist_is_iterating(*i));
- SvREFCNT_dec_NN(*i);
+ if (SvTEMP(*i)) {
+ sv_2mortal(r);
+ }
+ else {
+ SvREFCNT_dec_NN(*i);
+ }
}
*i = r;
+
return;
}
{
/* Return a new inversion list that is a copy of the input one, which is
- * unchanged */
+ * unchanged. The new list will not be mortal even if the old one was. */
/* Need to allocate extra space to accommodate Perl's addition of a
* trailing NUL to SvPV's, since it thinks they are always strings */