/* allocate a context-id and associated context struct returns cid; a context in state zero is considered available for allocation, this corresponds to the C_FREE enumeration constant. */ static int xpost_interpreter_cid_init(unsigned int *cid) { unsigned int startid = nextid; /*printf("cid_init\n"); */ while ( xpost_interpreter_cid_get_context(++nextid)->state != 0 ) { if (nextid == startid + MAXCONTEXT) { XPOST_LOG_ERR("ctab full. cannot create new process"); return 0; } } *cid = nextid; return 1; }
static int init_test_garbage(int (*xpost_interpreter_cid_init)(unsigned int *cid), Xpost_Context *(*xpost_interpreter_cid_get_context)(unsigned int cid), int (*xpost_interpreter_get_initializing)(void), void (*xpost_interpreter_set_initializing)(int), Xpost_Memory_File *(*xpost_interpreter_alloc_global_memory)(void), Xpost_Memory_File *(*xpost_interpreter_alloc_local_memory)(void)) { int fd; unsigned int cid; char fname[] = "xmemXXXXXX"; unsigned int tadr; int ret; unsigned int ent; /* create interpreter and context */ itpdata = malloc(sizeof*itpdata); if (!itpdata) return 0; memset(itpdata, 0, sizeof*itpdata); ret = xpost_interpreter_cid_init(&cid); if (!ret) return 0; ctx = xpost_interpreter_cid_get_context(cid); ctx->id = cid; /* create global memory file */ ctx->gl = xpost_interpreter_alloc_global_memory(); if (ctx->gl == NULL) { return 0; } fd = mkstemp(fname); ret = xpost_memory_file_init(ctx->gl, fname, fd, xpost_interpreter_cid_get_context, xpost_interpreter_get_initializing, xpost_interpreter_set_initializing); if (!ret) { close(fd); return 0; } ret = xpost_memory_table_init(ctx->gl, &tadr); if (!ret) { xpost_memory_file_exit(ctx->gl); return 0; } ret = xpost_free_init(ctx->gl); if (!ret) { xpost_memory_file_exit(ctx->gl); return 0; } ret = xpost_save_init(ctx->gl); if (!ret) { xpost_memory_file_exit(ctx->gl); return 0; } ret = xpost_context_init_ctxlist(ctx->gl); if (!ret) { xpost_memory_file_exit(ctx->gl); return 0; } xpost_context_append_ctxlist(ctx->gl, ctx->id); ctx->gl->start = XPOST_MEMORY_TABLE_SPECIAL_OPERATOR_TABLE + 1; /* create local memory file */ ctx->lo = xpost_interpreter_alloc_local_memory(); if (ctx->lo == NULL) { xpost_memory_file_exit(ctx->gl); return 0; } strcpy(fname, "xmemXXXXXX"); fd = mkstemp(fname); ret = xpost_memory_file_init(ctx->lo, fname, fd, xpost_interpreter_cid_get_context, xpost_interpreter_get_initializing, xpost_interpreter_set_initializing); if (!ret) { close(fd); xpost_memory_file_exit(ctx->gl); return 0; } ret = xpost_memory_table_init(ctx->lo, &tadr); if (!ret) { xpost_memory_file_exit(ctx->gl); xpost_memory_file_exit(ctx->lo); return 0; } ret = xpost_free_init(ctx->lo); if (!ret) { xpost_memory_file_exit(ctx->gl); xpost_memory_file_exit(ctx->lo); return 0; } ret = xpost_save_init(ctx->lo); if (!ret) { xpost_memory_file_exit(ctx->gl); xpost_memory_file_exit(ctx->lo); return 0; } ret = xpost_context_init_ctxlist(ctx->lo); if (!ret) { xpost_memory_file_exit(ctx->gl); xpost_memory_file_exit(ctx->lo); return 0; } xpost_context_append_ctxlist(ctx->lo, ctx->id); ctx->lo->start = XPOST_MEMORY_TABLE_SPECIAL_BOGUS_NAME + 1; /* create names in both mfiles */ ret = xpost_name_init(ctx); if (!ret) { xpost_memory_file_exit(ctx->gl); xpost_memory_file_exit(ctx->lo); return 0; } /* create global OPTAB */ ctx->vmmode = GLOBAL; /*ret = initoptab(ctx);*/ /* NO! only need to allocate something in the table slot */ ret = xpost_memory_table_alloc(ctx->gl, 1024, 0, &ent); if (!ret) { xpost_memory_file_exit(ctx->gl); xpost_memory_file_exit(ctx->lo); return 0; } /* ... no initop(). don't need operators for this. */ /* only need one stack */ ctx->vmmode = LOCAL; xpost_stack_init(ctx->lo, &ctx->hold); ctx->os = ctx->ds = ctx->es = ctx->hold; ctx->gl->interpreter_set_initializing(0); /* garbage collector won't run otherwise */ return 1; }