File: context.c

package info (click to toggle)
libimager-perl 1.005%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,308 kB
  • ctags: 4,067
  • sloc: perl: 30,915; ansic: 27,680; makefile: 55; cpp: 4
file content (289 lines) | stat: -rw-r--r-- 5,820 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "imageri.h"
#include <stdio.h>

static volatile im_slot_t slot_count = 1;
static im_slot_destroy_t *volatile slot_destructors;
static volatile i_mutex_t slot_mutex;

/*
=item im_context_new()

Create a new Imager context object.

=cut
*/

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->refcount = 1;

#ifdef IMAGER_TRACE_CONTEXT
  fprintf(stderr, "im_context: created %p\n", ctx);
#endif


  return ctx;
}

/*
=item im_context_refinc(ctx, where)
X<im_context_refinc API>
=section Context objects
=synopsis im_context_refinc(aIMCTX, "a description");

Add a new reference to the context.

=cut
*/

void
im_context_refinc(im_context_t ctx, const char *where) {
  ++ctx->refcount;

#ifdef IMAGER_TRACE_CONTEXT
  fprintf(stderr, "im_context:%s: refinc %p (count now %lu)\n", where,
	  ctx, (unsigned long)ctx->refcount);
#endif
}

/*
=item im_context_refdec(ctx, where)
X<im_context_refdec API>
=section Context objects
=synopsis im_context_refdec(aIMCTX, "a description");

Remove a reference to the context, releasing it if all references have
been removed.

=cut
*/

void
im_context_refdec(im_context_t ctx, const char *where) {
  int i;
  im_slot_t slot;

  im_assert(ctx->refcount > 0);

  --ctx->refcount;

#ifdef IMAGER_TRACE_CONTEXT
  fprintf(stderr, "im_context:%s: delete %p (count now %lu)\n", where,
	  ctx, (unsigned long)ctx->refcount);
#endif

  if (ctx->refcount != 0)
    return;

  /* lock here to avoid slot_destructors from being moved under us */
  i_mutex_lock(slot_mutex);
  for (slot = 0; slot < ctx->slot_alloc; ++slot) {
    if (ctx->slots[slot] && slot_destructors[slot])
      slot_destructors[slot](ctx->slots[slot]);
  }
  i_mutex_unlock(slot_mutex);

  free(ctx->slots);

  for (i = 0; i < IM_ERROR_COUNT; ++i) {
    if (ctx->error_stack[i].msg)
      myfree(ctx->error_stack[i].msg);
  }
#ifdef IMAGER_LOG
  if (ctx->lg_file && ctx->own_log)
    fclose(ctx->lg_file);
#endif

  free(ctx);
}

/*
=item im_context_clone(ctx)

Clone an Imager context object, returning the result.

The error stack is not copied from the original context.

=cut
*/

im_context_t
im_context_clone(im_context_t ctx, const char *where) {
  im_context_t nctx = malloc(sizeof(im_context_struct));
  int i;

  if (!nctx)
    return NULL;

  nctx->slot_alloc = slot_count;
  nctx->slots = calloc(sizeof(void *), nctx->slot_alloc);
  if (!nctx->slots) {
    free(nctx);
    return NULL;
  }

  nctx->error_sp = IM_ERROR_COUNT-1;
  for (i = 0; i < IM_ERROR_COUNT; ++i) {
    nctx->error_alloc[i] = 0;
    nctx->error_stack[i].msg = NULL;
  }
#ifdef IMAGER_LOG
  nctx->log_level = ctx->log_level;
  if (ctx->lg_file) {
    if (ctx->own_log) {
      int newfd = dup(fileno(ctx->lg_file));
      nctx->own_log = 1;
      nctx->lg_file = fdopen(newfd, "w");
      if (nctx->lg_file)
	setvbuf(nctx->lg_file, NULL, _IONBF, BUFSIZ);
    }
    else {
      /* stderr */
      nctx->lg_file = ctx->lg_file;
      nctx->own_log = 0;
    }
  }
  else {
    nctx->lg_file = NULL;
  }
#endif
  nctx->max_width = ctx->max_width;
  nctx->max_height = ctx->max_height;
  nctx->max_bytes = ctx->max_bytes;

  nctx->refcount = 1;

#ifdef IMAGER_TRACE_CONTEXT
  fprintf(stderr, "im_context:%s: cloned %p to %p\n", where, ctx, nctx);
#endif

  return nctx;
}

/*
=item im_context_slot_new(destructor)

Allocate a new context-local-storage slot.

C<desctructor> will be called when the context is destroyed if the
corresponding slot is non-NULL.

=cut
*/

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;
}

/*
=item im_context_slot_set(slot, value)

Set the value of a slot.

Returns true on success.

Aborts if the slot supplied is invalid.

If reallocation of slot storage fails, returns false.

=cut
*/

int
im_context_slot_set(im_context_t ctx, im_slot_t slot, void *value) {
  if (slot < 0 || slot >= slot_count) {
    fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n",
	    (int)slot, (int)slot_count-1);
    abort();
  }

  if (slot >= ctx->slot_alloc) {
    ssize_t i;
    size_t new_alloc = slot_count;
    void **new_slots = realloc(ctx->slots, sizeof(void *) * new_alloc);

    if (!new_slots)
      return 0;

    for (i = ctx->slot_alloc; i < new_alloc; ++i)
      new_slots[i] = NULL;

    ctx->slots = new_slots;
    ctx->slot_alloc = new_alloc;
  }

  ctx->slots[slot] = value;

  return 1;
}

/*
=item im_context_slot_get(ctx, slot)

Retrieve the value previously stored in the given slot of the context
object.

=cut
*/

void *
im_context_slot_get(im_context_t ctx, im_slot_t slot) {
  if (slot < 0 || slot >= slot_count) {
    fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n",
	    (int)slot, (int)slot_count-1);
    abort();
  }

  if (slot >= ctx->slot_alloc)
    return NULL;

  return ctx->slots[slot];
}