void NCDValue_Free (NCDValue *o) { switch (o->type) { case NCDVALUE_STRING: { free(o->string); } break; case NCDVALUE_LIST: { LinkedList1Node *n; while (n = LinkedList1_GetFirst(&o->list)) { struct NCDValue__list_element *e = UPPER_OBJECT(n, struct NCDValue__list_element, list_node); NCDValue_Free(&e->v); LinkedList1_Remove(&o->list, &e->list_node); free(e); } } break; case NCDVALUE_MAP: { LinkedList1Node *n; while (n = LinkedList1_GetFirst(&o->map_list)) { struct NCDValue__map_element *e = UPPER_OBJECT(n, struct NCDValue__map_element, list_node); LinkedList1_Remove(&o->map_list, &e->list_node); NCDValue_Free(&e->key); NCDValue_Free(&e->val); free(e); } } break; case NCDVALUE_VAR: { free(o->var_name); } break; case NCDVALUE_INVOC: { NCDValue_Free(o->invoc_arg); NCDValue_Free(o->invoc_func); free(o->invoc_arg); free(o->invoc_func); } break; default: ASSERT(0); } }
static void func_new (NCDModuleInst *i) { // allocate instance struct instance *o = malloc(sizeof(*o)); if (!o) { ModuleLog(i, BLOG_ERROR, "failed to allocate instance"); goto fail0; } NCDModuleInst_Backend_SetUser(i, o); // init arguments o->i = i; // check arguments NCDValue *template_name_arg; NCDValue *args_arg; if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail1; } if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail1; } // signal up. // Do it before creating the process so that the process starts initializing before our own process continues. NCDModuleInst_Backend_Up(o->i); // copy arguments NCDValue args; if (!NCDValue_InitCopy(&args, args_arg)) { ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed"); goto fail1; } // create process if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_event)process_handler_event)) { ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed"); NCDValue_Free(&args); goto fail1; } // set state working o->state = STATE_WORKING; return; fail1: free(o); fail0: NCDModuleInst_Backend_SetError(i); NCDModuleInst_Backend_Dead(i); }
static void func_new (NCDModuleInst *i) { // allocate instance struct instance *o = malloc(sizeof(*o)); if (!o) { ModuleLog(i, BLOG_ERROR, "failed to allocate instance"); goto fail0; } NCDModuleInst_Backend_SetUser(i, o); // init arguments o->i = i; // check arguments NCDValue *template_name_arg; NCDValue *args_arg; if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail1; } if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail1; } char *template_name = NCDValue_StringValue(template_name_arg); // calling none? if (!strcmp(template_name, "<none>")) { // signal up NCDModuleInst_Backend_Up(o->i); // set state none o->state = STATE_NONE; } else { // copy arguments NCDValue args; if (!NCDValue_InitCopy(&args, args_arg)) { ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed"); goto fail1; } // create process if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) { ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed"); NCDValue_Free(&args); goto fail1; } // set special functions NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj); // set callrefhere o->crh = (o->i->method_user ? ((NCDModuleInst *)i->method_user)->inst_user : NULL); // add to callrefhere's calls list if (o->crh) { LinkedList0_Prepend(&o->crh->calls_list, &o->calls_list_node); } // set state working o->state = STATE_WORKING; } return; fail1: free(o); fail0: NCDModuleInst_Backend_SetError(i); NCDModuleInst_Backend_Dead(i); }