EXIF library (libexif) Internals  0.6.24
exif-mem.c
Go to the documentation of this file.
1 #include <libexif/exif-mem.h>
2 
3 #include <stdlib.h>
4 
5 struct _ExifMem {
6  unsigned int ref_count;
10 };
11 
13 static void *
15 {
16  return calloc ((size_t) ds, 1);
17 }
18 
20 static void *
22 {
23  return realloc (d, (size_t) ds);
24 }
25 
27 static void
29 {
30  free (d);
31 }
32 
33 ExifMem *
35  ExifMemFreeFunc free_func)
36 {
37  ExifMem *mem;
38 
39  if (!alloc_func && !realloc_func)
40  return NULL;
41  mem = alloc_func ? alloc_func (sizeof (ExifMem)) :
42  realloc_func (NULL, sizeof (ExifMem));
43  if (!mem) return NULL;
44  mem->ref_count = 1;
45 
46  mem->alloc_func = alloc_func;
47  mem->realloc_func = realloc_func;
48  mem->free_func = free_func;
49 
50  return mem;
51 }
52 
53 void
55 {
56  if (!mem) return;
57  mem->ref_count++;
58 }
59 
60 void
62 {
63  if (!mem) return;
64  if (!--mem->ref_count)
65  exif_mem_free (mem, mem);
66 }
67 
68 void
69 exif_mem_free (ExifMem *mem, void *d)
70 {
71  if (!mem) return;
72  if (mem->free_func) {
73  mem->free_func (d);
74  return;
75  }
76 }
77 
78 void *
80 {
81  if (!mem) return NULL;
82  if (mem->alloc_func || mem->realloc_func)
83  return mem->alloc_func ? mem->alloc_func (ds) :
84  mem->realloc_func (NULL, ds);
85  return NULL;
86 }
87 
88 void *
89 exif_mem_realloc (ExifMem *mem, void *d, ExifLong ds)
90 {
91  return (mem && mem->realloc_func) ? mem->realloc_func (d, ds) : NULL;
92 }
93 
94 ExifMem *
96 {
99 }
static void exif_mem_free_func(void *d)
Default memory free function.
Definition: exif-mem.c:28
void * exif_mem_realloc(ExifMem *mem, void *d, ExifLong ds)
Definition: exif-mem.c:89
void exif_mem_free(ExifMem *mem, void *d)
Definition: exif-mem.c:69
ExifMem * exif_mem_new(ExifMemAllocFunc alloc_func, ExifMemReallocFunc realloc_func, ExifMemFreeFunc free_func)
Create a new ExifMem.
Definition: exif-mem.c:34
void exif_mem_unref(ExifMem *mem)
Unrefcount an ExifMem.
Definition: exif-mem.c:61
ExifMem * exif_mem_new_default(void)
Create a new ExifMem with default values for your convenience.
Definition: exif-mem.c:95
static void * exif_mem_realloc_func(void *d, ExifLong ds)
Default memory reallocation function.
Definition: exif-mem.c:21
void * exif_mem_alloc(ExifMem *mem, ExifLong ds)
Definition: exif-mem.c:79
static void * exif_mem_alloc_func(ExifLong ds)
Default memory allocation function.
Definition: exif-mem.c:14
void exif_mem_ref(ExifMem *mem)
Refcount an ExifMem.
Definition: exif-mem.c:54
Define the ExifMem data type and the associated functions.
void *(* ExifMemReallocFunc)(void *p, ExifLong s)
Should work like realloc()
Definition: exif-mem.h:47
void(* ExifMemFreeFunc)(void *p)
Free method for ExifMem.
Definition: exif-mem.h:54
void *(* ExifMemAllocFunc)(ExifLong s)
Should work like calloc()
Definition: exif-mem.h:39
uint32_t ExifLong
EXIF Unsigned Long data type.
Definition: exif-utils.h:54
ExifMemAllocFunc alloc_func
Definition: exif-mem.c:7
ExifMemReallocFunc realloc_func
Definition: exif-mem.c:8
unsigned int ref_count
Definition: exif-mem.c:6
ExifMemFreeFunc free_func
Definition: exif-mem.c:9

libexif Generated by doxygen