]> git.vpit.fr Git - perl/modules/Sub-Op.git/blobdiff - Op.xs
Factor the string duplication logic into a new so_strndup
[perl/modules/Sub-Op.git] / Op.xs
diff --git a/Op.xs b/Op.xs
index 285c1e9768ae593cc7f2c51b7697ad88fad20f77..b5417de40a3b002e426f8a82019f17dcad661909 100644 (file)
--- a/Op.xs
+++ b/Op.xs
@@ -99,7 +99,7 @@ void sub_op_init(sub_op_config_t *c) {
  c->namelen  = 0;
  c->proto    = NULL;
  c->protolen = 0;
- c->check    = 0;
+ c->call     = 0;
  c->ref      = 0;
  c->ud       = NULL;
 
@@ -115,33 +115,32 @@ void sub_op_register(pTHX_ const sub_op_config_t *c, U32 flags) {
  (void) hv_store(MY_CXT.map, c->name, c->namelen, newSViv(PTR2IV(c)), 0);
 }
 
+STATIC const char *so_strndup(pTHX_ const char *s, STRLEN len) {
+#define so_strndup(S, L) so_strndup(aTHX_ (S), (L))
+ const char *d;
+
+ if (!s)
+  return NULL;
+
+ d = PerlMemShared_malloc(len + 1);
+ Copy(s, d, len, char);
+ ((char *) d)[len] = '\0';
+
+ return d;
+}
+
 sub_op_config_t *sub_op_dup(pTHX_ const sub_op_config_t *orig) {
- STRLEN len;
  sub_op_config_t *dupe = PerlMemShared_malloc(sizeof *dupe);
 
- len           = orig->namelen;
- if (orig->name) {
-  dupe->name   = PerlMemShared_malloc(len + 1);
-  Copy(orig->name, dupe->name, len, char);
-  ((char *) dupe->name)[len] = '\0';
- } else {
-  dupe->name   = NULL;
- }
- dupe->namelen = len;
-
- len            = orig->protolen;
- if (orig->proto) {
-  dupe->proto   = PerlMemShared_malloc(len + 1);
-  Copy(orig->proto, dupe->proto, len, char);
-  ((char *) dupe->proto)[len] = '\0';
- } else {
-  dupe->proto   = NULL;
- }
- dupe->protolen = len;
+ dupe->name    = so_strndup(orig->name, orig->namelen);
+ dupe->namelen = orig->namelen;
+
+ dupe->proto    = so_strndup(orig->proto, orig->protolen);
+ dupe->protolen = orig->protolen;
 
- dupe->check = orig->check;
- dupe->ref   = orig->ref;
- dupe->ud    = orig->ud;
+ dupe->call = orig->call;
+ dupe->ref  = orig->ref;
+ dupe->ud   = orig->ud;
 
  return dupe;
 }
@@ -288,8 +287,8 @@ STATIC OP *so_ck_entersub(pTHX_ OP *o) {
     GvCV(gv) = NULL;
    }
 
-   if (c->check)
-    o = CALL_FPTR(c->check)(aTHX_ o, c->ud);
+   if (c->call)
+    o = CALL_FPTR(c->call)(aTHX_ o, c->ud);
   }
  }