]> git.imager.perl.org - imager.git/blob - context.c
improved polygon support
[imager.git] / context.c
1 #include "imageri.h"
2 #include <stdio.h>
3
4 static volatile im_slot_t slot_count = 1;
5 static im_slot_destroy_t *volatile slot_destructors;
6 static volatile i_mutex_t slot_mutex;
7
8 /*
9 =item im_context_new()
10
11 Create a new Imager context object.
12
13 =cut
14 */
15
16 im_context_t
17 im_context_new(void) {
18   im_context_t ctx = malloc(sizeof(im_context_struct));
19   int i;
20
21   if (!slot_mutex)
22     slot_mutex = i_mutex_new();
23
24   if (!ctx)
25     return NULL;
26   
27   ctx->error_sp = IM_ERROR_COUNT-1;
28   for (i = 0; i < IM_ERROR_COUNT; ++i) {
29     ctx->error_alloc[i] = 0;
30     ctx->error_stack[i].msg = NULL;
31     ctx->error_stack[i].code = 0;
32   }
33 #ifdef IMAGER_LOG
34   ctx->log_level = 0;
35   ctx->lg_file = NULL;
36 #endif
37   ctx->max_width = 0;
38   ctx->max_height = 0;
39   ctx->max_bytes = DEF_BYTES_LIMIT;
40
41   ctx->slot_alloc = slot_count;
42   ctx->slots = calloc(sizeof(void *), ctx->slot_alloc);
43   if (!ctx->slots) {
44     free(ctx);
45     return NULL;
46   }
47
48   ctx->refcount = 1;
49
50 #ifdef IMAGER_TRACE_CONTEXT
51   fprintf(stderr, "im_context: created %p\n", ctx);
52 #endif
53
54
55   return ctx;
56 }
57
58 /*
59 =item im_context_refinc(ctx, where)
60 X<im_context_refinc API>
61 =section Context objects
62 =synopsis im_context_refinc(aIMCTX, "a description");
63
64 Add a new reference to the context.
65
66 =cut
67 */
68
69 void
70 im_context_refinc(im_context_t ctx, const char *where) {
71   ++ctx->refcount;
72
73 #ifdef IMAGER_TRACE_CONTEXT
74   fprintf(stderr, "im_context:%s: refinc %p (count now %lu)\n", where,
75           ctx, (unsigned long)ctx->refcount);
76 #endif
77 }
78
79 /*
80 =item im_context_refdec(ctx, where)
81 X<im_context_refdec API>
82 =section Context objects
83 =synopsis im_context_refdec(aIMCTX, "a description");
84
85 Remove a reference to the context, releasing it if all references have
86 been removed.
87
88 =cut
89 */
90
91 void
92 im_context_refdec(im_context_t ctx, const char *where) {
93   int i;
94   im_slot_t slot;
95
96   im_assert(ctx->refcount > 0);
97
98   --ctx->refcount;
99
100 #ifdef IMAGER_TRACE_CONTEXT
101   fprintf(stderr, "im_context:%s: delete %p (count now %lu)\n", where,
102           ctx, (unsigned long)ctx->refcount);
103 #endif
104
105   if (ctx->refcount != 0)
106     return;
107
108   /* lock here to avoid slot_destructors from being moved under us */
109   i_mutex_lock(slot_mutex);
110   for (slot = 0; slot < ctx->slot_alloc; ++slot) {
111     if (ctx->slots[slot] && slot_destructors[slot])
112       slot_destructors[slot](ctx->slots[slot]);
113   }
114   i_mutex_unlock(slot_mutex);
115
116   free(ctx->slots);
117
118   for (i = 0; i < IM_ERROR_COUNT; ++i) {
119     if (ctx->error_stack[i].msg)
120       myfree(ctx->error_stack[i].msg);
121   }
122 #ifdef IMAGER_LOG
123   if (ctx->lg_file && ctx->own_log)
124     fclose(ctx->lg_file);
125 #endif
126
127   free(ctx);
128 }
129
130 /*
131 =item im_context_clone(ctx)
132
133 Clone an Imager context object, returning the result.
134
135 The error stack is not copied from the original context.
136
137 =cut
138 */
139
140 im_context_t
141 im_context_clone(im_context_t ctx, const char *where) {
142   im_context_t nctx = malloc(sizeof(im_context_struct));
143   int i;
144
145   if (!nctx)
146     return NULL;
147
148   nctx->slot_alloc = slot_count;
149   nctx->slots = calloc(sizeof(void *), nctx->slot_alloc);
150   if (!nctx->slots) {
151     free(nctx);
152     return NULL;
153   }
154
155   nctx->error_sp = IM_ERROR_COUNT-1;
156   for (i = 0; i < IM_ERROR_COUNT; ++i) {
157     nctx->error_alloc[i] = 0;
158     nctx->error_stack[i].msg = NULL;
159   }
160 #ifdef IMAGER_LOG
161   nctx->log_level = ctx->log_level;
162   if (ctx->lg_file) {
163     if (ctx->own_log) {
164       int newfd = dup(fileno(ctx->lg_file));
165       nctx->own_log = 1;
166       nctx->lg_file = fdopen(newfd, "w");
167       if (nctx->lg_file)
168         setvbuf(nctx->lg_file, NULL, _IONBF, BUFSIZ);
169     }
170     else {
171       /* stderr */
172       nctx->lg_file = ctx->lg_file;
173       nctx->own_log = 0;
174     }
175   }
176   else {
177     nctx->lg_file = NULL;
178   }
179 #endif
180   nctx->max_width = ctx->max_width;
181   nctx->max_height = ctx->max_height;
182   nctx->max_bytes = ctx->max_bytes;
183
184   nctx->refcount = 1;
185
186 #ifdef IMAGER_TRACE_CONTEXT
187   fprintf(stderr, "im_context:%s: cloned %p to %p\n", where, ctx, nctx);
188 #endif
189
190   return nctx;
191 }
192
193 /*
194 =item im_context_slot_new(destructor)
195
196 Allocate a new context-local-storage slot.
197
198 C<desctructor> will be called when the context is destroyed if the
199 corresponding slot is non-NULL.
200
201 =cut
202 */
203
204 im_slot_t
205 im_context_slot_new(im_slot_destroy_t destructor) {
206   im_slot_t new_slot;
207   im_slot_destroy_t *new_destructors;
208   if (!slot_mutex)
209     slot_mutex = i_mutex_new();
210
211   i_mutex_lock(slot_mutex);
212
213   new_slot = slot_count++;
214   new_destructors = realloc(slot_destructors, sizeof(void *) * slot_count);
215   if (!new_destructors)
216     i_fatal(1, "Cannot allocate memory for slot destructors");
217   slot_destructors = new_destructors;
218
219   slot_destructors[new_slot] = destructor;
220
221   i_mutex_unlock(slot_mutex);
222
223   return new_slot;
224 }
225
226 /*
227 =item im_context_slot_set(slot, value)
228
229 Set the value of a slot.
230
231 Returns true on success.
232
233 Aborts if the slot supplied is invalid.
234
235 If reallocation of slot storage fails, returns false.
236
237 =cut
238 */
239
240 int
241 im_context_slot_set(im_context_t ctx, im_slot_t slot, void *value) {
242   if (slot < 0 || slot >= slot_count) {
243     fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n",
244             (int)slot, (int)slot_count-1);
245     abort();
246   }
247
248   if (slot >= ctx->slot_alloc) {
249     ssize_t i;
250     size_t new_alloc = slot_count;
251     void **new_slots = realloc(ctx->slots, sizeof(void *) * new_alloc);
252
253     if (!new_slots)
254       return 0;
255
256     for (i = ctx->slot_alloc; i < new_alloc; ++i)
257       new_slots[i] = NULL;
258
259     ctx->slots = new_slots;
260     ctx->slot_alloc = new_alloc;
261   }
262
263   ctx->slots[slot] = value;
264
265   return 1;
266 }
267
268 /*
269 =item im_context_slot_get(ctx, slot)
270
271 Retrieve the value previously stored in the given slot of the context
272 object.
273
274 =cut
275 */
276
277 void *
278 im_context_slot_get(im_context_t ctx, im_slot_t slot) {
279   if (slot < 0 || slot >= slot_count) {
280     fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n",
281             (int)slot, (int)slot_count-1);
282     abort();
283   }
284
285   if (slot >= ctx->slot_alloc)
286     return NULL;
287
288   return ctx->slots[slot];
289 }