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

ncd: NCDModule: make it possible to have the preallocated memory pointer passed as context for method calls, as
opposed to the NCDModuleInst pointer. This will simplify implementing method calls in the upcoming socket module, where
the "sys.socket" object type can either be an actual statement sys.connect(), or it can be an anonymous object _socket
for listening sockets.

ambrop7 13 жил өмнө
parent
commit
c29313f3c6
2 өөрчлөгдсөн 38 нэмэгдсэн , 4 устгасан
  1. 18 1
      ncd/NCDModule.c
  2. 20 3
      ncd/NCDModule.h

+ 18 - 1
ncd/NCDModule.c

@@ -101,6 +101,9 @@ void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *meth
     // clear error flag
     n->is_error = 0;
     
+    // give NCDModuleInst to methods, not mem
+    n->pass_mem_to_methods = 0;
+    
     DebugObject_Init(&n->d_obj);
     
     struct NCDModuleInst_new_params new_params;
@@ -166,7 +169,21 @@ NCDObject NCDModuleInst_Object (NCDModuleInst *n)
     DebugObject_Access(&n->d_obj);
     ASSERT(n->m->base_type_id >= 0)
     
-    return NCDObject_Build(n->m->base_type_id, n, (NCDObject_func_getvar)object_func_getvar, (NCDObject_func_getobj)object_func_getobj);
+    if (n->pass_mem_to_methods) {
+        return NCDObject_BuildMethodUser(n->m->base_type_id, n->mem, n, (NCDObject_func_getvar)object_func_getvar, (NCDObject_func_getobj)object_func_getobj);
+    } else {
+        return NCDObject_Build(n->m->base_type_id, n, (NCDObject_func_getvar)object_func_getvar, (NCDObject_func_getobj)object_func_getobj);
+    }
+}
+
+void NCDModuleInst_Backend_PassMemToMethods (NCDModuleInst *n)
+{
+    DebugObject_Access(&n->d_obj);
+    ASSERT(n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
+           n->state == STATE_UP ||
+           n->state == STATE_DYING)
+    
+    n->pass_mem_to_methods = 1;
 }
 
 static int can_resolve (NCDModuleInst *n)

+ 20 - 3
ncd/NCDModule.h

@@ -250,10 +250,15 @@ struct NCDModuleInst_new_params {
      * this pointer identifies the object it is being invoked with.
      * If the object is a statement (i.e. a {@link NCDModuleInst}), then this
      * will be the NCDModuleInst pointer, and {@link NCDModuleInst_Backend_GetUser}
-     * can be called on this to retrieve the user pointer of the object.
+     * can be called on this to retrieve the pointer to preallocated memory for
+     * the backend instance; *unless* {@link NCDModuleInst_Backend_PassMemToMethods}
+     * was called for the object on which the method is being called, in which case
+     * this will directly point to the preallocated memory.
      * On the other hand, if this is a method on an internal object built using
-     * only {@link NCDObject_Build} or {@link NCDObject_Build2}, this pointer will
-     * be whatever was passed as the "user" argument to those functions.
+     * only {@link NCDObject_Build} or {@link NCDObject_Build2},
+     * {@link NCDObject_BuildMethodUser} or {@link NCDObject_BuildMethodUser2},
+     * this pointer will be whatever was passed as the "user" argument, for the
+     * former two functions, and as "method_user", for the latter two.
      */
     void *method_user;
 };
@@ -349,6 +354,7 @@ typedef struct NCDModuleInst_s {
     void *mem; // not modified by NCDModuleInst (but passed to module)
     unsigned int state:3;
     unsigned int is_error:1;
+    unsigned int pass_mem_to_methods:1;
     unsigned int istate:3; // untouched by NCDModuleInst
     DebugObject d_obj;
 } NCDModuleInst;
@@ -451,6 +457,17 @@ void NCDModuleInst_Clean (NCDModuleInst *n);
  */
 NCDObject NCDModuleInst_Object (NCDModuleInst *n);
 
+/**
+ * If this is called, any methods called on this object will receive the preallocated
+ * memory pointer as the object state pointer. This means that in the
+ * {@link NCDModule_func_getvar2} function which is called when a method is created,
+ * the preallocated memory should be accessed as params->method_user.
+ * By default, however, params->method_user points to the NCDModuleInst of the base
+ * object, and {@link NCDModuleInst_Backend_GetUser} is needed to retrieve the
+ * preallocated memory pointer.
+ */
+void NCDModuleInst_Backend_PassMemToMethods (NCDModuleInst *n);
+
 /**
  * Retuns the state pointer passed to handlers of a module backend instance;
  * see {@link NCDModule_func_new2}.