summaryrefslogtreecommitdiff
path: root/babl/babl-memory.c
blob: b7d83ef73e28f0beeb8e97c3dcf30a5655b739ea (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* babl - dynamically extendable universal pixel conversion library.
 * Copyright (C) 2005, Øyvind Kolås.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "babl-internal.h"

static BablMallocFunc malloc_f = malloc;
static BablFreeFunc   free_f   = free;

static void *first_malloc_used = NULL;
static void *first_free_used   = NULL;

void
babl_set_malloc (BablMallocFunc malloc_function)
{
  malloc_f = malloc_function;
}

void
babl_set_free (BablFreeFunc free_function)
{
  free_f = free_function;
}

static char *signature = "babl-memory";
static char *freed = "So long and thanks for all the fish.";

typedef struct
{
  char  *signature;
  size_t size;
  int  (*destructor)(void *ptr);
} BablAllocInfo;

#define BABL_ALIGN     16
#define BABL_ALLOC     (sizeof (BablAllocInfo) + sizeof (void *))
#define BAI(ptr)       ((BablAllocInfo *) *((void **) ptr - 1))
#define IS_BAI(ptr)    (BAI (ptr)->signature == signature)

#if BABL_DEBUG_MEM

/* runtime statistics: */
static int mallocs  = 0;
static int frees    = 0;
static int strdups  = 0;
static int reallocs = 0;
static int callocs  = 0;
static int dups     = 0;

static const char *
mem_stats (void)
{
  static char buf[128];

  snprintf (buf, sizeof (buf), "mallocs:%i callocs:%i strdups:%i dups:%i allocs:%i frees:%i reallocs:%i\t|",
           mallocs, callocs, strdups, dups, mallocs + callocs + strdups + dups, frees, reallocs);
  return buf;
}

#endif

static void
functions_sanity (void)
{
  if (first_malloc_used != malloc_f ||
      first_free_used != free_f)
    {
      static int displayed = 0;

      if (first_malloc_used == NULL)
        {
          first_malloc_used = malloc_f;
          first_free_used   = free_f;
        }
      else if (!displayed)
        {
          fprintf (stderr, "HMM....\nSomething strange is happening,\n%s function pointer changing between invocations in babl.\n",
                   first_malloc_used == malloc_f ? "free" :
                   (first_free_used == free_f ? "malloc" : "malloc and free"));
          displayed = 1;
        }
    }
}

/* Allocate /size/ bytes of memory
 *
 * contents of memory undefined.
 */
void *
babl_malloc (size_t size)
{
  char *ret;
  int  offset;

  functions_sanity ();
  ret = malloc_f (BABL_ALLOC + BABL_ALIGN + size);
  if (!ret)
    babl_fatal ("args=(%i): failed", size);

  offset = BABL_ALIGN - ((uintptr_t) ret + BABL_ALLOC) % BABL_ALIGN;
  ret = ret + BABL_ALLOC + offset;

  *((void **) ret - 1) = ret - BABL_ALLOC - offset;
  BAI (ret)->signature = signature;
  BAI (ret)->size      = size;
  BAI (ret)->destructor = NULL;
#if BABL_DEBUG_MEM
  babl_mutex_lock (babl_debug_mutex); 
  mallocs++;
  babl_mutex_unlock (babl_debug_mutex); 
#endif
  return (void *) (ret);
}

/* set a callback to be called when the segment is freed.
 */
void
babl_set_destructor (void  *ptr,
                     int (*destructor)(void *ptr))
{
  babl_assert (IS_BAI (ptr));
  BAI(ptr)->destructor = destructor;
}

/* Create a duplicate allocation of the same size, note
 * that the exact location of the allocation needs to be
 * passed.
 */
void *
babl_dup (void *ptr)
{
  void *ret;

  babl_assert (IS_BAI (ptr));

  ret = babl_malloc (BAI (ptr)->size);
  memcpy (ret, ptr, BAI (ptr)->size);

#if BABL_DEBUG_MEM
  babl_mutex_lock (babl_debug_mutex); 
  dups++;
  mallocs--;
  babl_mutex_unlock (babl_debug_mutex); 
#endif
  return NULL;
}

/* Free memory allocated by a babl function (note: babl_free
 * will complain if memory not allocated by babl is passed.)
 *
 * Note: the function is made variadic to be a legal callback
 * function in some circumstances.
 */
void
babl_free (void *ptr,
           ...)
{
  functions_sanity ();
  if (!ptr)
    return;
  if (!IS_BAI (ptr))
    {
      #define IS_BAI(ptr)    (BAI (ptr)->signature == signature)
      if (freed == BAI (ptr)->signature)
        fprintf (stderr, "\nbabl:double free detected\n");
      else
        fprintf (stderr, "\nbabl_free passed unknown pointer, bailing and leaking it\n");
      return;
      //assert(0);
    }

  if (BAI (ptr)->destructor)
    if (BAI (ptr)->destructor (ptr))
      return; /* bail out on non 0 return from destructor */

  BAI (ptr)->signature = freed;
  free_f (BAI (ptr));
#if BABL_DEBUG_MEM
  babl_mutex_lock (babl_debug_mutex); 
  frees++;
  babl_mutex_unlock (babl_debug_mutex); 
#endif
}

/* reallocate allocation to be in size instead, contents of
 * common allocated memory between old and new size is preserved.
 */
void *
babl_realloc (void  *ptr,
              size_t size)
{
  void *ret = NULL;

  if (!ptr)
    {
      return babl_malloc (size);
    }

  babl_assert (IS_BAI (ptr));

  if (size == 0)
    {
      babl_free (ptr);
      return NULL;
    }
  if (babl_sizeof (ptr) >= size)
    {
      return ptr;
    }
  else if (babl_sizeof (ptr) < size)
    {
#ifdef USE_REALLOC_CLEAR
      /* not needed yet by babl, if aviodable, preferred, since
       * it has performance hits where it isn't wanted, a special
       * function might be better when needd.
       */
      ret = babl_calloc (size, 1);
#else
      ret = babl_malloc (size);
#endif
      memcpy (ret, ptr, babl_sizeof (ptr));
      BAI (ret)->destructor = BAI (ptr)->destructor;
      BAI (ptr)->destructor = NULL;
      babl_free (ptr);
#if BABL_DEBUG_MEM
      babl_mutex_lock (babl_debug_mutex); 
      reallocs++;
      babl_mutex_unlock (babl_debug_mutex); 
#endif
      return ret;
    }

  if (!ret)
    babl_fatal ("args=(%p, %i): failed", ptr, size);

  return NULL;
}

/* allocate nmemb*size bytes and set it to all zeros. */
void *
babl_calloc (size_t nmemb,
             size_t size)
{
  void *ret = babl_malloc (nmemb * size);

  if (!ret)
    babl_fatal ("args=(%i, %i): failed", nmemb, size);

  memset (ret, 0, nmemb * size);

#if BABL_DEBUG_MEM
  babl_mutex_lock (babl_debug_mutex); 
  callocs++;
  mallocs--;
  babl_mutex_unlock (babl_debug_mutex); 
#endif
  return ret;
}

/* Returns the size of an allocation.
 */
size_t
babl_sizeof (void *ptr)
{
  babl_assert (IS_BAI (ptr));
  return BAI (ptr)->size;
}

/*  duplicate allocation needed for a string, and
 *  copy string contents, string is zero terminated.
 */
char *
babl_strdup (const char *s)
{
  char *ret;

  ret = babl_malloc (strlen (s) + 1);
  if (!ret)
    babl_log ("args=(%s): failed", s);
  strcpy (ret, s);

#if BABL_DEBUG_MEM
  babl_mutex_lock (babl_debug_mutex); 
  strdups++;
  mallocs--;
  babl_mutex_unlock (babl_debug_mutex); 
#endif
  return ret;
}

/* append string to babl allocated string dest, the returned
 * string is the new canonical position with src added to dest
 * if the dest allocation needed to be resized. Passing NULL
 * causes a new allocation (thus babl-memory sees NULL as the empty
 * string).
 */
char *
babl_strcat (char       *dest,
             const char *src)
{
  char *ret;
  size_t src_len;
  size_t dst_len;

  if (NULL == src)
    return dest;

  src_len = strlen (src);
  if (!dest)
    {
      ret = babl_malloc (src_len + 1);
      strcpy (ret, src);
      return ret;
    }
  babl_assert (IS_BAI (dest));
  dst_len = strlen (dest);

  ret = dest;

  if (babl_sizeof (dest) < src_len + dst_len + 1)
    {
      size_t new_size = babl_sizeof (dest);
      while (new_size < src_len + dst_len + 1)
        new_size *= 2;
      ret = babl_realloc (dest, new_size);
    }

  strcpy (&ret[dst_len], src);
  return ret;
}

#if BABL_DEBUG_MEM
/* performs a sanity check on memory, (checks if number of
 * allocations and frees on babl memory evens out to zero).
 */
int
babl_memory_sanity (void)
{
  if (frees != mallocs + strdups + callocs)
    {
      babl_log ("memory usage does not add up!\n"
                "%s\n"
                "\tbalance: %i-%i=%i\n",
                mem_stats (), (strdups + mallocs + callocs), frees, (strdups + mallocs + callocs) - frees);
      return -1;
    }
  return 0;
}
#endif