im_context_t im_context_new(void) { im_context_t ctx = malloc(sizeof(im_context_struct)); int i; if (!slot_mutex) slot_mutex = i_mutex_new(); if (!ctx) return NULL; ctx->error_sp = IM_ERROR_COUNT-1; for (i = 0; i < IM_ERROR_COUNT; ++i) { ctx->error_alloc[i] = 0; ctx->error_stack[i].msg = NULL; ctx->error_stack[i].code = 0; } #ifdef IMAGER_LOG ctx->log_level = 0; ctx->lg_file = NULL; #endif ctx->max_width = 0; ctx->max_height = 0; ctx->max_bytes = DEF_BYTES_LIMIT; ctx->slot_alloc = slot_count; ctx->slots = calloc(sizeof(void *), ctx->slot_alloc); if (!ctx->slots) { free(ctx); return NULL; } ctx->file_magic = NULL; ctx->refcount = 1; #ifdef IMAGER_TRACE_CONTEXT fprintf(stderr, "im_context: created %p\n", ctx); #endif return ctx; }
im_slot_t im_context_slot_new(im_slot_destroy_t destructor) { im_slot_t new_slot; im_slot_destroy_t *new_destructors; if (!slot_mutex) slot_mutex = i_mutex_new(); i_mutex_lock(slot_mutex); new_slot = slot_count++; new_destructors = realloc(slot_destructors, sizeof(void *) * slot_count); if (!new_destructors) i_fatal(1, "Cannot allocate memory for slot destructors"); slot_destructors = new_destructors; slot_destructors[new_slot] = destructor; i_mutex_unlock(slot_mutex); return new_slot; }
void i_t1_start(void) { mutex = i_mutex_new(); }