File: iptc-mem.c

package info (click to toggle)
libiptcdata 1.0.4-6
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,088 kB
  • ctags: 767
  • sloc: sh: 9,668; ansic: 4,111; xml: 2,791; makefile: 123; python: 62; sed: 16
file content (95 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download | duplicates (6)
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
#include <libiptcdata/iptc-mem.h>
#include <stdlib.h>


struct _IptcMem {
	unsigned int ref_count;
	IptcMemAllocFunc alloc_func;
	IptcMemReallocFunc realloc_func;
	IptcMemFreeFunc free_func;
};

static void *
iptc_mem_alloc_func (IptcLong ds)
{
	return calloc ((size_t) ds, 1);
}

static void *
iptc_mem_realloc_func (void *d, IptcLong ds)
{
	return realloc (d, (size_t) ds);
}

static void
iptc_mem_free_func (void *d)
{
	free (d);
}

IptcMem *
iptc_mem_new (IptcMemAllocFunc alloc_func, IptcMemReallocFunc realloc_func,
	      IptcMemFreeFunc free_func)
{
	IptcMem *mem;

	if (!alloc_func || !realloc_func) return NULL;
	mem = alloc_func ? alloc_func (sizeof (IptcMem)) :
		           realloc_func (NULL, sizeof (IptcMem));
	if (!mem) return NULL;
	mem->ref_count = 1;

	mem->alloc_func   = alloc_func;
	mem->realloc_func = realloc_func;
	mem->free_func    = free_func;

	return mem;
}

void
iptc_mem_ref (IptcMem *mem)
{
	if (!mem) return;
	mem->ref_count++;
}

void
iptc_mem_unref (IptcMem *mem)
{
	if (!mem) return;
	if (!--mem->ref_count)
		iptc_mem_free (mem, mem);
}

void
iptc_mem_free (IptcMem *mem, void *d)
{
	if (!mem) return;
	if (mem->free_func) {
		mem->free_func (d);
		return;
	}
}

void *
iptc_mem_alloc (IptcMem *mem, IptcLong ds)
{
	if (!mem) return NULL;
	if (mem->alloc_func || mem->realloc_func)
		return mem->alloc_func ? mem->alloc_func (ds) :
					 mem->realloc_func (NULL, ds);
	return NULL;
}

void *
iptc_mem_realloc (IptcMem *mem, void *d, IptcLong ds)
{
	return (mem && mem->realloc_func) ? mem->realloc_func (d, ds) : NULL;
}

IptcMem *
iptc_mem_new_default (void)
{
	return iptc_mem_new (iptc_mem_alloc_func, iptc_mem_realloc_func,
			     iptc_mem_free_func);
}