summaryrefslogtreecommitdiff
path: root/base/base_memory.c
blob: 1fa918c29c417241b63dd0a14d13c3e474f08d60 (plain)
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
#include "fitz-base.h"

/* Make this thread local storage if you wish.  */

static void *stdmalloc(fz_memorycontext *mem, int n)
{
	return malloc(n);
}

static void *stdrealloc(fz_memorycontext *mem, void *p, int n)
{
	return realloc(p, n);
}

static void stdfree(fz_memorycontext *mem, void *p)
{
	free(p);
}

static fz_memorycontext defmem = { stdmalloc, stdrealloc, stdfree };
static fz_memorycontext *curmem = &defmem;

fz_error fz_koutofmem = {
	-1,
	{"out of memory"},
	{"<internal>"},
	{"<internal>"},
	0, 0
};

fz_memorycontext *
fz_currentmemorycontext()
{
	return curmem;
}

void
fz_setmemorycontext(fz_memorycontext *mem)
{
	curmem = mem;
}

void *
fz_malloc(int n)
{
    fz_memorycontext *mem = fz_currentmemorycontext();
    void *p = mem->malloc(mem, n);
    if (!p)
	fz_warn("cannot malloc %d bytes", n);
    return p;
}

void *
fz_realloc(void *p, int n)
{
    fz_memorycontext *mem = fz_currentmemorycontext();
    void *np = mem->realloc(mem, p, n);
    if (np == nil)
	fz_warn("cannot realloc %d bytes", n);
    return np;
}

void
fz_free(void *p)
{
	fz_memorycontext *mem = fz_currentmemorycontext();
	mem->free(mem, p);
}

char *
fz_strdup(char *s)
{
	int len = strlen(s);
	char *ns = fz_malloc(len + 1);
	if (ns)
		strcpy(ns, s);
	return ns;
}