static knh_MailBox_t *knh_MailBox_new(CTX ctx) { knh_MailBox_t *mailbox = (knh_MailBox_t *)KNH_MALLOC(ctx, sizeof(knh_MailBox_t)); mailbox->end = 0; mailbox->msgs = (knh_Message_t **)KNH_MALLOC(ctx, sizeof(knh_Message_t *) * MAX_MAIL_NUM); int i = 0; for (i = 0; i < MAX_MAIL_NUM; i++) { mailbox->msgs[i] = (knh_Message_t *)KNH_MALLOC(ctx, sizeof(knh_Message_t)); } return mailbox; }
static knh_Actor_t *knh_Actor_new(CTX ctx) { knh_Actor_t *actor = (knh_Actor_t *)KNH_MALLOC(ctx, sizeof(knh_Actor_t)); knh_ActorEX_t *b = (knh_ActorEX_t *)KNH_MALLOC(ctx, sizeof(knh_ActorEX_t)); actor->mtd_info = (knh_MethodInfo_t **)KNH_MALLOC(ctx, sizeof(knh_MethodInfo_t *) * MAX_METHOD_NUM); int i = 0; for (i = 0; i < MAX_METHOD_NUM; i++) { actor->mtd_info[i] = (knh_MethodInfo_t *)KNH_MALLOC(ctx, sizeof(knh_MethodInfo_t)); } actor->mailbox = knh_MailBox_new(ctx); actor->b = b; return actor; }
static void knh_actor_init(CTX ctx, knh_actor_t *a, const char *name, const char *host, int port) { if (name != NULL) { size_t len = knh_strlen(name); a->name = (const char *)KNH_MALLOC(ctx, len); strncpy((char *)a->name, name, len); } if (host != NULL) { size_t len = knh_strlen(host); a->host = (const char *)KNH_MALLOC(ctx, len); strncpy((char *)a->host, host, len); } a->port = port; }
KNHAPI(void) knh_ResultSet_initColumn(Ctx *ctx, knh_ResultSet_t *o, size_t column_size) { size_t i; if(DP(o)->column_size != 0) { for(i = 0; i < DP(o)->column_size; i++) { KNH_FINALv(ctx, DP(o)->column[i].name); } KNH_FREE(ctx, DP(o)->column, sizeof(knh_dbschema_t) * DP(o)->column_size); DP(o)->column = NULL; if(DP(o)->qcur != NULL) { DP(o)->qcurfree(DP(o)->qcur); DP(o)->qcur = NULL; } } DP(o)->column_size = column_size; if(column_size > 0) { DP(o)->column = (knh_dbschema_t*)KNH_MALLOC(ctx, sizeof(knh_dbschema_t) * DP(o)->column_size); for(i = 0; i < DP(o)->column_size; i++) { DP(o)->column[i].type = CLASS_Object; KNH_INITv(DP(o)->column[i].name, TS_EMPTY); DP(o)->column[i].start = 0; DP(o)->column[i].len = 0; } } DP(o)->count = 0; }
const knh_dim_t *new_dim(CTX ctx, size_t capacity, size_t wsize) { knh_dim_t *dim = (knh_dim_t*)KNH_MALLOC(ctx, sizeof(knh_dim_t)); dim->capacity = capacity; dim->wsize = wsize; dim->dim = 1; dim->x = dim->xy = dim->xyz = capacity; return (const knh_dim_t*)dim; }
static knh_xblock_t *knh_generateWrapper(CTX ctx, void *callee, int argc, knh_ffiparam_t *argv) { knh_xblock_t *blk = get_unused_xblock(ctx); knh_xblock_t *function = blk->block; size_t fidx = 0; int i = 0; knh_ffiparam_t *t; // local ffiarguments; ffi_cif *cif = (ffi_cif*)KNH_MALLOC(ctx, sizeof(ffi_cif)); ffi_type **args = (ffi_type**)KNH_MALLOC(ctx, sizeof(ffi_type) * argc); void *values[1]; for (i = 0; i < argc; i++) { t = &argv[i]; if (t->sfpidx != -1) { // it means arguments switch(t->type) { case CLASS_Tvoid: // do nothing break; case CLASS_Int: args[i] = &ffi_type_uint64; break; case CLASS_Float: args[i] = &ffi_type_double; break; default: args[i] = &ffi_type_pointer; break; } } } if (ffi_prep_cif(cif, FFI_DEFAULT_ABI, argc, &ffi_type_void, args) == FFI_OK) { fprintf(stderr, "OKAY!\n"); } else { fprintf(stderr, "this is not okay!\n"); } }
static knh_map_t *hmap_init(CTX ctx, size_t init, const char *path, void *option) { knh_hmap_t *hmap = (knh_hmap_t*)KNH_MALLOC(ctx, sizeof(knh_hmap_t)); knh_bzero(hmap, sizeof(knh_hmap_t)); if(init < K_HASH_INITSIZE) init = K_HASH_INITSIZE; hmap->hentry = (knh_hentry_t**)KNH_REALLOC(ctx, hmap->DBGNAME, NULL, 0, init, sizeof(knh_hentry_t*)); hmap->hmax = init; hmap->size = 0; hmap->factor = KNH_HASH_FACTOR(hmap->hmax); return (knh_map_t*)hmap; }
static kcontext_t* new_hcontext(CTX ctx0) { kcontext_t *ctx; static volatile size_t ctxid_counter = 0; if(ctx0 == NULL) { ctxid_counter = 0; ctx = (kcontext_t*)malloc(sizeof(kcontext_t)); knh_bzero(ctx, sizeof(kcontext_t)); } else { KNH_ASSERT_CTX0(ctx0); ctx = (kcontext_t*)KNH_MALLOC(ctx0, sizeof(kcontext_t)); knh_bzero(ctx, sizeof(kcontext_t)); } ctx->flag = 0; ctx->ctxid = ctxid_counter; ctxid_counter++; // ctx->freeObjectList = NULL; // ctx->freeMemoryList = NULL; ctx->parent = ctx; ctx->api2 = getapi2(); { kuintptr_t i = 0, ch; kuint_t t = knh_rand(); ch = t % 26; ctx->trace[i] = 'A' + ch; for(i = 1; i < 9; i++) { t = t / 36; if (t == 0) t = knh_rand(); ch = t % 36; ctx->trace[i] = (ch < 10) ? '0' + ch : 'A' + (ch - 10); } } ctx->seq = 0; ctx->ctxobjNC = NULL; if(ctx0 == NULL) { const char *ptrace = knh_getenv(K_DEOS_TRACE); if(ptrace == NULL) { ptrace = "$(setenv " K_DEOS_TRACE ")"; } KNH_NTRACE2(ctx, "konoha:newtrace", K_NOTICE, KNH_LDATA( LOG_s("parent", ptrace) #if defined(K_USING_POSIX_) , LOG_u("ppid", getppid()) #endif /* !defined(K_USING_POSIX_) */ )); } else { KNH_NTRACE2(ctx, "konoha:newtrace", K_NOTICE, KNH_LDATA(LOG_s("parent", ctx0->trace), LOG_u("seq", ctx0->seq))); } return ctx; }
static void knh_Bytes_expands(Ctx *ctx, knh_Bytes_t *ba, size_t newsize) { if(ba->capacity == 0) { newsize = knh_good_size(newsize); ba->bu.ubuf = (knh_uchar_t*)KNH_MALLOC(ctx, newsize); ba->capacity = newsize; } else { knh_uchar_t *buf = ba->bu.ubuf; size_t capacity = ba->capacity; knh_uchar_t *newa = (knh_uchar_t*)KNH_MALLOC(ctx, newsize); knh_memcpy(newa, buf, capacity); knh_bzero(&newa[capacity], newsize - capacity); ba->bu.ubuf = newa; ba->capacity = newsize; KNH_FREE(ctx, buf, capacity); if(unlikely(ctx->bufa == ba)) { KNH_SYSLOG(ctx, LOG_INFO, "ExtendedContextBuffer", "*newsize=%ld, pointer=(%p => %p)", newsize, buf, newa); knh_Bytes_checkstack(ctx, buf, buf + ba->bu.len, newa); } } }
static void knh_Stmt_terms_expand(Ctx *ctx, Stmt *o) { size_t i, newcapacity = DP(o)->capacity * 2; Object **newterms = (Object**)KNH_MALLOC(ctx, sizeof(Object*) * newcapacity); knh_memcpy(newterms, DP(o)->terms, sizeof(Object*) * DP(o)->capacity); for(i = DP(o)->capacity; i < newcapacity; i++) { KNH_INITv(newterms[i], KNH_NULL); } KNH_FREE(ctx, DP(o)->terms, sizeof(Object*) * DP(o)->capacity); DP(o)->terms = newterms; DP(o)->capacity = newcapacity; KNH_ASSERT(DP(o)->size < DP(o)->capacity); }
void knh_Bytes_expands(CTX ctx, knh_Bytes_t *ba, size_t newsize) { if(ba->dim->capacity == 0) { newsize = k_goodsize(newsize); ba->bu.ubuf = (knh_uchar_t*)KNH_MALLOC(ctx, newsize); ba->dim = new_dim(ctx, newsize, 1); } else { knh_uchar_t *ubuf = ba->bu.ubuf; ba->bu.ubuf = (knh_uchar_t*)KNH_REALLOC(ctx, ba->DBG_name, ba->bu.ubuf, ba->dim->capacity, newsize, 1); ((knh_dim_t*)ba->dim)->capacity = newsize; if(unlikely(ctx->bufa == ba)) { KNH_INFO(ctx, "newsize=%ld, pointer=(%p => %p)", newsize, ubuf, ba->bu.ubuf); Bytes_checkstack(ctx, ubuf, ubuf + ba->bu.len, ba->bu.ubuf); } } }
/* ------------------------------------------------------------------------ */ /* Close an open blob */ //## @Native void GitBlob.close(); KMETHOD GitBlob_close(CTX ctx, ksfp_t *sfp _RIX) { kGitBlob_free(ctx, sfp[0].p); RETURNvoid_(); } /* Write an in-memory buffer to the ODB as a blob */ //## @Native @Static GitOid GitBlob.createFromBuffer(GitRepository repo, Bytes buffer); KMETHOD GitBlob_createFromBuffer(CTX ctx, ksfp_t *sfp _RIX) { git_oid *oid = (git_oid *)KNH_MALLOC(ctx, sizeof(git_oid)); git_repository *repo = RawPtr_to(git_repository *, sfp[1]); const void *buffer = (const void *)BA_totext(sfp[2].ba); size_t len = BA_size(sfp[2].ba); int error = git_blob_create_frombuffer(oid, repo, buffer, len); if (error < GIT_SUCCESS) { TRACE_ERROR(ctx, "git_blob_create_frombuffer", error); KNH_FREE(ctx, oid, sizeof(git_oid)); RETURN_(KNH_NULL); } RETURN_(new_ReturnRawPtr(ctx, sfp, oid)); } /* Read a file from the working folder of a repository and write it to the * Object Database as a loose blob */ //## @Native @Static GitOid GitBlob.createFromFile(GitRepository repo, String path);
static void WebserviceGlue_free(CTX ctx, void *ptr) { if (ptr != NULL) { ptr = NULL; } } /* ------------------------------------------------------------------------ */ /* [Webservice] */ #define WS_PATH_MAX (256) //@Native Webservice Webservice.new(String path, Webservice _); METHOD Webservice_new(CTX ctx, knh_sfp_t *sfp _RIX) { knh_Webservice_t *proc = (knh_Webservice_t*)KNH_MALLOC(ctx, sizeof(knh_Webservice_t)); char *pname = String_to(char *, sfp[1]); size_t path_size = knh_strlen(pname); if (pname != NULL && path_size < WS_PATH_MAX) { proc->path = KNH_MALLOC(ctx, path_size * sizeof(char)); knh_memcpy(proc->path, pname, path_size+1); proc->path_size = path_size; } RETURN_(new_RawPtr(ctx, sfp[2].p, proc)); } static METHOD Fmethod_wrapWebservice(CTX ctx, knh_sfp_t *sfp _RIX) { knh_type_t rtype = knh_ParamArray_rtype(DP(sfp[K_MTDIDX].mtdNC)->mp); knh_Func_t *fo = sfp[0].fo; knh_WebserviceGlue_t *wglue = (knh_WebserviceGlue_t*)(((fo->mtd)->b)->cfunc);
po->path = NULL; } static void ProcessGlue_free(CTX ctx, void *ptr) { if (ptr != NULL) { ptr = NULL; } } /* ------------------------------------------------------------------------ */ //@Native Clib Clib.new(String libname, Clib _); METHOD Clib_new(CTX ctx, knh_sfp_t *sfp _RIX) { const char *libname = String_to(const char *, sfp[1]); knh_CLib_t *clib = (knh_CLib_t*)KNH_MALLOC(ctx, sizeof(knh_CLib_t)); clib->handler = knh_dlopen(ctx, libname); knh_RawPtr_t *po = new_RawPtr(ctx, sfp[2].p, clib); RETURN_(po); } static METHOD Fmethod_wrapCLib(CTX ctx, knh_sfp_t *sfp _RIX) { knh_type_t rtype = knh_ParamArray_rtype(DP(sfp[K_MTDIDX].mtdNC)->mp); knh_Func_t *fo = sfp[0].fo; knh_ClibGlue_t *cglue = (knh_ClibGlue_t*)(((fo->mtd)->b)->cfunc); // fprintf(stderr, "fptr:%p, %p, %d, %p, %p\n", // dg->fptr, // &(dg->cif), dg->argCount, // dg->retT, dg->argT);
static knh_actor_t *knh_actor_malloc(CTX ctx) { return (knh_actor_t *)KNH_MALLOC(ctx, sizeof(knh_actor_t)); }
if(po->rawptr != NULL) { knh_mutex_free(ctx, (kmutex_t *)po->rawptr); po->rawptr = NULL; } } /* ======================================================================== */ // [KMETHODS] //## @Native Thread Thread.spawn(dynamic f, dynamic[] args) KMETHOD Thread_spawn(CTX ctx, ksfp_t *sfp _RIX) { kFunc *f = sfp[1].fo; kArray *args = sfp[2].a; if(IS_NOTNULL(((kObject *)f))) { Thread_t *t = (Thread_t *)KNH_MALLOC(ctx, sizeof(Thread_t)); kcontext_t *newCtx = new_ThreadContext(WCTX(ctx)); t->ctx = newCtx; t->func = f; t->args = args; kthread_create(ctx, &(t->thread), NULL, spawn_start, t); RETURN_(new_ReturnRawPtr(ctx, sfp, t)); } } //## @Native void Thread.join(); KMETHOD Thread_join(CTX ctx, ksfp_t *sfp _RIX) { Thread_t *t = RawPtr_to(Thread_t *, sfp[0]); void *v;