EXIF library (libexif) Internals 0.6.26
exif-mem.c
Go to the documentation of this file.
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
27struct _ExifMem {
28 unsigned int ref_count;
32};
33
35static void *
37{
38 return calloc ((size_t) ds, 1);
39}
40
42static void *
44{
45 return realloc (d, (size_t) ds);
46}
47
49static void
51{
52 free (d);
53}
54
55ExifMem *
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
75void
77{
78 if (!mem) return;
79 mem->ref_count++;
80}
81
82void
84{
85 if (!mem) return;
86 if (!--mem->ref_count)
87 exif_mem_free (mem, mem);
88}
89
90void
91exif_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
100void *
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
110void *
112{
113 return (mem && mem->realloc_func) ? mem->realloc_func (d, ds) : NULL;
114}
115
116ExifMem *
118{
121}
static void exif_mem_free_func(void *d)
Default memory free function.
Definition exif-mem.c:50
void exif_mem_free(ExifMem *mem, void *d)
Definition exif-mem.c:91
void exif_mem_unref(ExifMem *mem)
Unrefcount an ExifMem.
Definition exif-mem.c:83
void * exif_mem_alloc(ExifMem *mem, ExifLong ds)
Definition exif-mem.c:101
void * exif_mem_realloc(ExifMem *mem, void *d, ExifLong ds)
Definition exif-mem.c:111
ExifMem * exif_mem_new(ExifMemAllocFunc alloc_func, ExifMemReallocFunc realloc_func, ExifMemFreeFunc free_func)
Create a new ExifMem.
Definition exif-mem.c:56
static void * exif_mem_realloc_func(void *d, ExifLong ds)
Default memory reallocation function.
Definition exif-mem.c:43
static void * exif_mem_alloc_func(ExifLong ds)
Default memory allocation function.
Definition exif-mem.c:36
ExifMem * exif_mem_new_default(void)
Create a new ExifMem with default values for your convenience.
Definition exif-mem.c:117
void exif_mem_ref(ExifMem *mem)
Refcount an ExifMem.
Definition exif-mem.c:76
Define the ExifMem data type and the associated functions.
void(* ExifMemFreeFunc)(void *p)
Free method for ExifMem.
Definition exif-mem.h:56
void *(* ExifMemAllocFunc)(ExifLong s)
Should work like calloc()
Definition exif-mem.h:41
void *(* ExifMemReallocFunc)(void *p, ExifLong s)
Should work like realloc()
Definition exif-mem.h:49
uint32_t ExifLong
EXIF Unsigned Long data type.
Definition exif-utils.h:56
ExifMemAllocFunc alloc_func
Definition exif-mem.c:29
ExifMemReallocFunc realloc_func
Definition exif-mem.c:30
unsigned int ref_count
Definition exif-mem.c:28
ExifMemFreeFunc free_func
Definition exif-mem.c:31

libexif Generated by doxygen