Commit | Line | Data |
---|---|---|
74315ca9 | 1 | #include "imageri.h" |
31a13473 | 2 | #include <stdio.h> |
74315ca9 | 3 | |
fc02e376 TC |
4 | static im_slot_t slot_count; |
5 | static im_slot_destroy_t *slot_destructors; | |
6 | static i_mutex_t slot_mutex; | |
7 | ||
74315ca9 TC |
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) { | |
cf8c77ae | 18 | im_context_t ctx = malloc(sizeof(im_context_struct)); |
74315ca9 TC |
19 | int i; |
20 | ||
fc02e376 TC |
21 | if (!slot_mutex) |
22 | slot_mutex = i_mutex_new(); | |
23 | ||
cf8c77ae TC |
24 | if (!ctx) |
25 | return NULL; | |
26 | ||
74315ca9 TC |
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 | |
44d86483 TC |
37 | ctx->max_width = 0; |
38 | ctx->max_height = 0; | |
cf8c77ae | 39 | ctx->max_bytes = DEF_BYTES_LIMIT; |
74315ca9 | 40 | |
f4ced639 TC |
41 | ctx->slot_alloc = slot_count; |
42 | ctx->slots = calloc(sizeof(void *), ctx->slot_alloc); | |
fc02e376 TC |
43 | if (!ctx->slots) { |
44 | free(ctx); | |
45 | return NULL; | |
46 | } | |
47 | ||
31a13473 TC |
48 | ctx->refcount = 1; |
49 | ||
50 | #ifdef IMAGER_TRACE_CONTEXT | |
51 | fprintf(stderr, "im_context: created %p\n", ctx); | |
52 | #endif | |
53 | ||
fc02e376 | 54 | |
74315ca9 TC |
55 | return ctx; |
56 | } | |
57 | ||
58 | /* | |
31a13473 | 59 | =item im_context_refinc(ctx, where) |
abffffed TC |
60 | X<im_context_refinc API> |
61 | =section Context objects | |
62 | =synopsis im_context_refinc(aIMCTX, "a description"); | |
31a13473 TC |
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 | /* | |
abffffed TC |
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"); | |
74315ca9 | 84 | |
abffffed TC |
85 | Remove a reference to the context, releasing it if all references have |
86 | been removed. | |
74315ca9 TC |
87 | |
88 | =cut | |
89 | */ | |
90 | ||
91 | void | |
31a13473 | 92 | im_context_refdec(im_context_t ctx, const char *where) { |
74315ca9 | 93 | int i; |
fc02e376 | 94 | im_slot_t slot; |
74315ca9 | 95 | |
31a13473 TC |
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 | ||
fc02e376 TC |
108 | for (slot = 0; slot < ctx->slot_alloc; ++slot) { |
109 | if (ctx->slots[slot] && slot_destructors[slot]) | |
110 | slot_destructors[slot](ctx->slots[slot]); | |
111 | } | |
112 | ||
113 | free(ctx->slots); | |
114 | ||
74315ca9 TC |
115 | for (i = 0; i < IM_ERROR_COUNT; ++i) { |
116 | if (ctx->error_stack[i].msg) | |
117 | myfree(ctx->error_stack[i].msg); | |
118 | } | |
119 | #ifdef IMAGER_LOG | |
6ee0c41f | 120 | if (ctx->lg_file && ctx->own_log) |
74315ca9 TC |
121 | fclose(ctx->lg_file); |
122 | #endif | |
cf8c77ae TC |
123 | |
124 | free(ctx); | |
74315ca9 TC |
125 | } |
126 | ||
127 | /* | |
128 | =item im_context_clone(ctx) | |
129 | ||
130 | Clone an Imager context object, returning the result. | |
131 | ||
a6c8996f TC |
132 | The error stack is not copied from the original context. |
133 | ||
74315ca9 TC |
134 | =cut |
135 | */ | |
136 | ||
137 | im_context_t | |
31a13473 | 138 | im_context_clone(im_context_t ctx, const char *where) { |
cf8c77ae | 139 | im_context_t nctx = malloc(sizeof(im_context_struct)); |
74315ca9 TC |
140 | int i; |
141 | ||
cf8c77ae TC |
142 | if (!nctx) |
143 | return NULL; | |
144 | ||
fc02e376 TC |
145 | nctx->slots = calloc(sizeof(void *), slot_count); |
146 | if (!nctx->slots) { | |
147 | free(nctx); | |
148 | return NULL; | |
149 | } | |
150 | nctx->slot_alloc = slot_count; | |
151 | ||
a6c8996f | 152 | nctx->error_sp = IM_ERROR_COUNT-1; |
74315ca9 | 153 | for (i = 0; i < IM_ERROR_COUNT; ++i) { |
a6c8996f TC |
154 | nctx->error_alloc[i] = 0; |
155 | nctx->error_stack[i].msg = NULL; | |
74315ca9 TC |
156 | } |
157 | #ifdef IMAGER_LOG | |
158 | nctx->log_level = ctx->log_level; | |
159 | if (ctx->lg_file) { | |
6ee0c41f TC |
160 | if (ctx->own_log) { |
161 | int newfd = dup(fileno(ctx->lg_file)); | |
162 | nctx->own_log = 1; | |
163 | nctx->lg_file = fdopen(newfd, "w"); | |
164 | if (nctx->lg_file) | |
165 | setvbuf(nctx->lg_file, NULL, _IONBF, BUFSIZ); | |
166 | } | |
167 | else { | |
168 | /* stderr */ | |
169 | nctx->lg_file = ctx->lg_file; | |
170 | nctx->own_log = 0; | |
171 | } | |
74315ca9 TC |
172 | } |
173 | else { | |
44d86483 | 174 | nctx->lg_file = NULL; |
74315ca9 TC |
175 | } |
176 | #endif | |
44d86483 TC |
177 | nctx->max_width = ctx->max_width; |
178 | nctx->max_height = ctx->max_height; | |
179 | nctx->max_bytes = ctx->max_bytes; | |
fc02e376 | 180 | |
31a13473 | 181 | nctx->refcount = 1; |
74315ca9 | 182 | |
31a13473 TC |
183 | #ifdef IMAGER_TRACE_CONTEXT |
184 | fprintf(stderr, "im_context:%s: cloned %p to %p\n", where, ctx, nctx); | |
185 | #endif | |
186 | ||
187 | return nctx; | |
74315ca9 | 188 | } |
fc02e376 TC |
189 | |
190 | /* | |
191 | =item im_context_slot_new(destructor) | |
192 | ||
193 | Allocate a new context-local-storage slot. | |
194 | ||
195 | =cut | |
196 | */ | |
197 | ||
198 | im_slot_t | |
199 | im_context_slot_new(im_slot_destroy_t destructor) { | |
200 | im_slot_t new_slot; | |
201 | im_slot_destroy_t *new_destructors; | |
202 | if (!slot_mutex) | |
203 | slot_mutex = i_mutex_new(); | |
204 | ||
205 | i_mutex_lock(slot_mutex); | |
206 | ||
207 | new_slot = slot_count++; | |
208 | new_destructors = realloc(slot_destructors, sizeof(void *) * slot_count); | |
209 | if (!new_destructors) | |
210 | i_fatal(1, "Cannot allocate memory for slot destructors"); | |
211 | slot_destructors = new_destructors; | |
212 | ||
213 | slot_destructors[new_slot] = destructor; | |
214 | ||
215 | i_mutex_unlock(slot_mutex); | |
216 | ||
217 | return new_slot; | |
218 | } | |
219 | ||
220 | /* | |
221 | =item im_context_slot_set(slot, value) | |
222 | ||
223 | Set the value of a slot. | |
224 | ||
225 | Returns true on success. | |
226 | ||
227 | Aborts if the slot supplied is invalid. | |
228 | ||
229 | If reallocation of slot storage fails, returns false. | |
230 | ||
231 | =cut | |
232 | */ | |
233 | ||
234 | int | |
235 | im_context_slot_set(im_context_t ctx, im_slot_t slot, void *value) { | |
236 | if (slot < 0 || slot >= slot_count) { | |
237 | fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n", | |
238 | (int)slot, (int)slot_count-1); | |
239 | abort(); | |
240 | } | |
241 | ||
242 | if (slot >= ctx->slot_alloc) { | |
243 | ssize_t i; | |
244 | size_t new_alloc = slot_count; | |
245 | void **new_slots = realloc(ctx->slots, sizeof(void *) * new_alloc); | |
246 | ||
247 | if (!new_slots) | |
248 | return 0; | |
249 | ||
250 | for (i = ctx->slot_alloc; i < new_alloc; ++i) | |
251 | new_slots[i] = NULL; | |
252 | ||
253 | ctx->slots = new_slots; | |
254 | ctx->slot_alloc = new_alloc; | |
255 | } | |
256 | ||
257 | ctx->slots[slot] = value; | |
258 | ||
259 | return 1; | |
260 | } | |
261 | ||
262 | /* | |
263 | =item im_context_slot_get(ctx, slot) | |
264 | ||
265 | Retrieve the value previously stored in the given slot of the context | |
266 | object. | |
267 | ||
268 | =cut | |
269 | */ | |
270 | ||
271 | void * | |
272 | im_context_slot_get(im_context_t ctx, im_slot_t slot) { | |
273 | if (slot < 0 || slot >= slot_count) { | |
274 | fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n", | |
275 | (int)slot, (int)slot_count-1); | |
276 | abort(); | |
277 | } | |
278 | ||
279 | if (slot >= ctx->slot_alloc) | |
280 | return NULL; | |
281 | ||
282 | return ctx->slots[slot]; | |
283 | } |