7 Create a new Imager context object.
13 im_context_new(void) {
14 im_context_t ctx = malloc(sizeof(im_context_struct));
20 ctx->error_sp = IM_ERROR_COUNT-1;
21 for (i = 0; i < IM_ERROR_COUNT; ++i) {
22 ctx->error_alloc[i] = 0;
23 ctx->error_stack[i].msg = NULL;
24 ctx->error_stack[i].code = 0;
32 ctx->max_bytes = DEF_BYTES_LIMIT;
36 #ifdef IMAGER_TRACE_CONTEXT
37 fprintf(stderr, "im_context: created %p\n", ctx);
44 =item im_context_refinc(ctx, where)
45 X<im_context_refinc API>
46 =section Context objects
47 =synopsis im_context_refinc(aIMCTX, "a description");
49 Add a new reference to the context.
55 im_context_refinc(im_context_t ctx, const char *where) {
58 #ifdef IMAGER_TRACE_CONTEXT
59 fprintf(stderr, "im_context:%s: refinc %p (count now %lu)\n", where,
60 ctx, (unsigned long)ctx->refcount);
65 =item im_context_refdec(ctx, where)
66 X<im_context_refdec API>
67 =section Context objects
68 =synopsis im_context_refdec(aIMCTX, "a description");
70 Remove a reference to the context, releasing it if all references have
77 im_context_refdec(im_context_t ctx, const char *where) {
80 im_assert(ctx->refcount > 0);
84 #ifdef IMAGER_TRACE_CONTEXT
85 fprintf(stderr, "im_context:%s: delete %p (count now %lu)\n", where,
86 ctx, (unsigned long)ctx->refcount);
89 if (ctx->refcount != 0)
92 for (i = 0; i < IM_ERROR_COUNT; ++i) {
93 if (ctx->error_stack[i].msg)
94 myfree(ctx->error_stack[i].msg);
105 =item im_context_clone(ctx)
107 Clone an Imager context object, returning the result.
113 im_context_clone(im_context_t ctx, const char *where) {
114 im_context_t nctx = malloc(sizeof(im_context_struct));
120 nctx->error_sp = ctx->error_sp;
121 for (i = 0; i < IM_ERROR_COUNT; ++i) {
122 if (ctx->error_stack[i].msg) {
123 size_t sz = ctx->error_alloc[i];
124 nctx->error_alloc[i] = sz;
125 nctx->error_stack[i].msg = mymalloc(sz);
126 memcpy(nctx->error_stack[i].msg, ctx->error_stack[i].msg, sz);
129 nctx->error_alloc[i] = 0;
130 nctx->error_stack[i].msg = NULL;
132 nctx->error_stack[i].code = ctx->error_stack[i].code;
135 nctx->log_level = ctx->log_level;
137 /* disable buffering, this isn't perfect */
138 setvbuf(ctx->lg_file, NULL, _IONBF, 0);
140 /* clone that and disable buffering some more */
141 nctx->lg_file = fdopen(fileno(ctx->lg_file), "a");
143 setvbuf(nctx->lg_file, NULL, _IONBF, 0);
146 nctx->lg_file = NULL;
149 nctx->max_width = ctx->max_width;
150 nctx->max_height = ctx->max_height;
151 nctx->max_bytes = ctx->max_bytes;
154 #ifdef IMAGER_TRACE_CONTEXT
155 fprintf(stderr, "im_context:%s: cloned %p to %p\n", where, ctx, nctx);