/src/libexif/libexif/exif-mem.c
Line | Count | Source |
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 * |
36 | | exif_mem_alloc_func (ExifLong ds) |
37 | 1.52M | { |
38 | 1.52M | return calloc ((size_t) ds, 1); |
39 | 1.52M | } |
40 | | |
41 | | /*! Default memory reallocation function. */ |
42 | | static void * |
43 | | exif_mem_realloc_func (void *d, ExifLong ds) |
44 | 189k | { |
45 | 189k | return realloc (d, (size_t) ds); |
46 | 189k | } |
47 | | |
48 | | /*! Default memory free function. */ |
49 | | static void |
50 | | exif_mem_free_func (void *d) |
51 | 1.57M | { |
52 | 1.57M | free (d); |
53 | 1.57M | } |
54 | | |
55 | | ExifMem * |
56 | | exif_mem_new (ExifMemAllocFunc alloc_func, ExifMemReallocFunc realloc_func, |
57 | | ExifMemFreeFunc free_func) |
58 | 80.2k | { |
59 | 80.2k | ExifMem *mem; |
60 | | |
61 | 80.2k | if (!alloc_func && !realloc_func) |
62 | 0 | return NULL; |
63 | 80.2k | mem = alloc_func ? alloc_func (sizeof (ExifMem)) : |
64 | 80.2k | realloc_func (NULL, sizeof (ExifMem)); |
65 | 80.2k | if (!mem) return NULL; |
66 | 80.2k | mem->ref_count = 1; |
67 | | |
68 | 80.2k | mem->alloc_func = alloc_func; |
69 | 80.2k | mem->realloc_func = realloc_func; |
70 | 80.2k | mem->free_func = free_func; |
71 | | |
72 | 80.2k | return mem; |
73 | 80.2k | } |
74 | | |
75 | | void |
76 | | exif_mem_ref (ExifMem *mem) |
77 | 619k | { |
78 | 619k | if (!mem) return; |
79 | 619k | mem->ref_count++; |
80 | 619k | } |
81 | | |
82 | | void |
83 | | exif_mem_unref (ExifMem *mem) |
84 | 700k | { |
85 | 700k | if (!mem) return; |
86 | 700k | if (!--mem->ref_count) |
87 | 80.2k | exif_mem_free (mem, mem); |
88 | 700k | } |
89 | | |
90 | | void |
91 | | exif_mem_free (ExifMem *mem, void *d) |
92 | 1.57M | { |
93 | 1.57M | if (!mem) return; |
94 | 1.57M | if (mem->free_func) { |
95 | 1.57M | mem->free_func (d); |
96 | 1.57M | return; |
97 | 1.57M | } |
98 | 1.57M | } |
99 | | |
100 | | void * |
101 | | exif_mem_alloc (ExifMem *mem, ExifLong ds) |
102 | 1.44M | { |
103 | 1.44M | if (!mem) return NULL; |
104 | 1.44M | if (mem->alloc_func || mem->realloc_func) |
105 | 1.44M | return mem->alloc_func ? mem->alloc_func (ds) : |
106 | 1.44M | mem->realloc_func (NULL, ds); |
107 | 0 | return NULL; |
108 | 1.44M | } |
109 | | |
110 | | void * |
111 | | exif_mem_realloc (ExifMem *mem, void *d, ExifLong ds) |
112 | 189k | { |
113 | 189k | return (mem && mem->realloc_func) ? mem->realloc_func (d, ds) : NULL; |
114 | 189k | } |
115 | | |
116 | | ExifMem * |
117 | | exif_mem_new_default (void) |
118 | 80.2k | { |
119 | 80.2k | return exif_mem_new (exif_mem_alloc_func, exif_mem_realloc_func, |
120 | 80.2k | exif_mem_free_func); |
121 | 80.2k | } |