Эх сурвалжийг харах

ncd: NCDVal: remove NCDValMem_FreeExport and NCDValMem_InitImport in favor of NCDValMem_InitCopy

ambrop7 13 жил өмнө
parent
commit
4236bb6d04

+ 8 - 17
ncd/NCDInterpProcess.c

@@ -222,27 +222,18 @@ int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIn
             goto loop_fail0;
         }
         
-        NCDValMem mem;
-        NCDValMem_Init(&mem);
+        NCDValMem_Init(&e->arg_mem);
         
         NCDValRef val;
-        if (!convert_value_recurser(pdb, string_index, NCDStatement_RegArgs(s), &mem, &val)) {
+        if (!convert_value_recurser(pdb, string_index, NCDStatement_RegArgs(s), &e->arg_mem, &val)) {
             BLog(BLOG_ERROR, "convert_value_recurser failed");
-            NCDValMem_Free(&mem);
-            goto loop_fail0;
+            goto loop_fail1;
         }
         
         e->arg_ref = NCDVal_ToSafe(val);
         
         if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
             BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
-            NCDValMem_Free(&mem);
-            goto loop_fail0;
-        }
-        
-        if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
-            BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
-            NCDValMem_Free(&mem);
             goto loop_fail1;
         }
         
@@ -273,9 +264,9 @@ int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIn
     loop_fail3:
         BFree(e->objnames);
     loop_fail2:
-        BFree(e->arg_data);
-    loop_fail1:
         NCDValReplaceProg_Free(&e->arg_prog);
+    loop_fail1:
+        NCDValMem_Free(&e->arg_mem);
     loop_fail0:
         goto fail3;
     }
@@ -289,8 +280,8 @@ fail3:
     while (o->num_stmts-- > 0) {
         struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
         BFree(e->objnames);
-        BFree(e->arg_data);
         NCDValReplaceProg_Free(&e->arg_prog);
+        NCDValMem_Free(&e->arg_mem);
     }
     free(o->name);
 fail2:
@@ -308,8 +299,8 @@ void NCDInterpProcess_Free (NCDInterpProcess *o)
     while (o->num_stmts-- > 0) {
         struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
         BFree(e->objnames);
-        BFree(e->arg_data);
         NCDValReplaceProg_Free(&e->arg_prog);
+        NCDValMem_Free(&e->arg_mem);
     }
     
     free(o->name);
@@ -396,7 +387,7 @@ int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *o
     
     struct NCDInterpProcess__stmt *e = &o->stmts[i];
     
-    if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
+    if (!NCDValMem_InitCopy(out_valmem, &e->arg_mem)) {
         return 0;
     }
     

+ 1 - 2
ncd/NCDInterpProcess.h

@@ -51,8 +51,7 @@ struct NCDInterpProcess__stmt {
         const struct NCDModule *simple_module;
         int method_name_id;
     } binding;
-    char *arg_data;
-    size_t arg_len;
+    NCDValMem arg_mem;
     NCDValSafeRef arg_ref;
     NCDValReplaceProg arg_prog;
     int alloc_size;

+ 13 - 37
ncd/NCDVal.c

@@ -231,52 +231,28 @@ void NCDValMem_Free (NCDValMem *o)
     }
 }
 
-int NCDValMem_FreeExport (NCDValMem *o, char **out_data, size_t *out_len)
+int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other)
 {
-    NCDVal__AssertMem(o);
-    ASSERT(out_data)
-    ASSERT(out_len)
-    
-    if (o->buf) {
-        *out_data = o->buf;
-    } else {
-        if (!(*out_data = BAlloc(o->used))) {
-            return 0;
-        }
-        memcpy(*out_data, o->fastbuf, o->used);
-    }
-    
-    *out_len = o->used;
+    NCDVal__AssertMem(other);
     
-    return 1;
-}
-
-int NCDValMem_InitImport (NCDValMem *o, const char *data, size_t len)
-{
-    ASSERT(data)
-    ASSERT(len <= NCDVAL_MAXIDX)
+    o->size = other->size;
+    o->used = other->used;
     
-    if (len <= NCDVAL_FASTBUF_SIZE) {
-        memcpy(o->fastbuf, data, len);
+    if (!other->buf) {
         o->buf = NULL;
-        o->size = NCDVAL_FASTBUF_SIZE;
+        memcpy(o->fastbuf, other->fastbuf, other->used);
     } else {
-        size_t cap = len;
-        if (cap < NCDVAL_FIRST_SIZE) {
-            cap = NCDVAL_FIRST_SIZE;
-        }
-        
-        if (!(o->buf = BAlloc(cap))) {
-            return 0;
+        o->buf = BAlloc(other->size);
+        if (!o->buf) {
+            goto fail0;
         }
-        
-        memcpy(o->buf, data, len);
-        o->size = cap;
+        memcpy(o->buf, other->buf, other->used);
     }
     
-    o->used = len;
-    
     return 1;
+    
+fail0:
+    return 0;
 }
 
 void NCDVal_Assert (NCDValRef val)

+ 10 - 23
ncd/NCDVal.h

@@ -167,26 +167,14 @@ void NCDValMem_Init (NCDValMem *o);
 void NCDValMem_Free (NCDValMem *o);
 
 /**
- * Attempts to free the value memory object, exporting its data to an external
- * memory block.
- * On success, 1 is returned, and *out_data and *out_len are set; *out_data
- * receives a memory block which should be freed using {@link BFree}.
- * After the memory object is exported, copies can be created using
- * {@link NCDValMem_InitImport}. Any value references needed from the original
- * should be turned to safe references using {@link NCDVal_ToSafe} before
- * exporting the block, and imported to copies using {@link NCDVal_FromSafe}.
- * On failure, 0 is returned, and the memory object is unchanged (it should
- * still be freed using {@link NCDValMem_Free}.
- */
-int NCDValMem_FreeExport (NCDValMem *o, char **out_data, size_t *out_len) WARN_UNUSED;
-
-/**
- * Initializes the value memory object as a copy of an external memory block,
- * which was obtained using {@link NCDValMem_FreeExport}.
- * The memory block provided is only read, and is copied into this memory object.
- * Returns 1 on success, 0 on failure.
+ * Initializes the memory object to be a copy of an existing memory object.
+ * Value references from the original may be used if they are first turned
+ * to {@link NCDValSafeRef} using {@link NCDVal_ToSafe} and back to
+ * {@link NCDValRef} using {@link NCDVal_FromSafe} with the new memory object
+ * specified. Alternatively, {@link NCDVal_Moved} can be used.
+ * Returns 1 on success and 0 on failure.
  */
-int NCDValMem_InitImport (NCDValMem *o, const char *data, size_t len) WARN_UNUSED;
+int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other) WARN_UNUSED;
 
 /**
  * Does nothing.
@@ -568,8 +556,8 @@ NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  * efficiently replacing placeholders in identical values in identical memory
  * objects.
  * To actually perform replacements, make copies of the memory object of this value
- * using {@link NCDValMem_FreeExport} and {@link NCDValMem_InitImport}, then call
- * {@link NCDValReplaceProg_Execute} on the copies.
+ * using {@link NCDValMem_InitCopy}, then call {@link NCDValReplaceProg_Execute}
+ * on the copies.
  * The value passed must be a valid value, and not a placeholder.
  * Returns 1 on success, 0 on failure.
  */
@@ -596,8 +584,7 @@ typedef int (*NCDVal_replace_func) (void *arg, int plid, NCDValMem *mem, NCDValR
 /**
  * Executes the replacement program, replacing placeholders in a value.
  * The memory object must given be identical to the memory object which was used in
- * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_FreeExport} and
- * {@link NCDValMem_InitImport}.
+ * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_InitCopy}.
  * This will call the callback 'replace', which should build the values to replace
  * the placeholders.
  * Returns 1 on success and 0 on failure. On failure, the entire memory object enters