1 /* exif-mem.c
2 *
3 * Copyright (c) 2004 Lutz Mueller <lutz@users.sourceforge.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 *
20 * SPDX-License-Identifier: LGPL-2.0-or-later
21 */
22
23 #include <libexif/exif-mem.h>
24
25 #include <stdlib.h>
26
27 struct _ExifMem {
28 unsigned int ref_count;
29 ExifMemAllocFunc alloc_func;
30 ExifMemReallocFunc realloc_func;
31 ExifMemFreeFunc free_func;
32 };
33
34 /*! Default memory allocation function. */
35 static void *
exif_mem_alloc_func(ExifLong ds)36 exif_mem_alloc_func (ExifLong ds)
37 {
38 return calloc ((size_t) ds, 1);
39 }
40
41 /*! Default memory reallocation function. */
42 static void *
exif_mem_realloc_func(void * d,ExifLong ds)43 exif_mem_realloc_func (void *d, ExifLong ds)
44 {
45 return realloc (d, (size_t) ds);
46 }
47
48 /*! Default memory free function. */
49 static void
exif_mem_free_func(void * d)50 exif_mem_free_func (void *d)
51 {
52 free (d);
53 }
54
55 ExifMem *
exif_mem_new(ExifMemAllocFunc alloc_func,ExifMemReallocFunc realloc_func,ExifMemFreeFunc free_func)56 exif_mem_new (ExifMemAllocFunc alloc_func, ExifMemReallocFunc realloc_func,
57 ExifMemFreeFunc free_func)
58 {
59 ExifMem *mem;
60
61 if (!alloc_func && !realloc_func)
62 return NULL;
63 mem = alloc_func ? alloc_func (sizeof (ExifMem)) :
64 realloc_func (NULL, sizeof (ExifMem));
65 if (!mem) return NULL;
66 mem->ref_count = 1;
67
68 mem->alloc_func = alloc_func;
69 mem->realloc_func = realloc_func;
70 mem->free_func = free_func;
71
72 return mem;
73 }
74
75 void
exif_mem_ref(ExifMem * mem)76 exif_mem_ref (ExifMem *mem)
77 {
78 if (!mem) return;
79 mem->ref_count++;
80 }
81
82 void
exif_mem_unref(ExifMem * mem)83 exif_mem_unref (ExifMem *mem)
84 {
85 if (!mem) return;
86 if (!--mem->ref_count)
87 exif_mem_free (mem, mem);
88 }
89
90 void
exif_mem_free(ExifMem * mem,void * d)91 exif_mem_free (ExifMem *mem, void *d)
92 {
93 if (!mem) return;
94 if (mem->free_func) {
95 mem->free_func (d);
96 return;
97 }
98 }
99
100 void *
exif_mem_alloc(ExifMem * mem,ExifLong ds)101 exif_mem_alloc (ExifMem *mem, ExifLong ds)
102 {
103 if (!mem) return NULL;
104 if (mem->alloc_func || mem->realloc_func)
105 return mem->alloc_func ? mem->alloc_func (ds) :
106 mem->realloc_func (NULL, ds);
107 return NULL;
108 }
109
110 void *
exif_mem_realloc(ExifMem * mem,void * d,ExifLong ds)111 exif_mem_realloc (ExifMem *mem, void *d, ExifLong ds)
112 {
113 return (mem && mem->realloc_func) ? mem->realloc_func (d, ds) : NULL;
114 }
115
116 ExifMem *
exif_mem_new_default(void)117 exif_mem_new_default (void)
118 {
119 return exif_mem_new (exif_mem_alloc_func, exif_mem_realloc_func,
120 exif_mem_free_func);
121 }
122