summaryrefslogtreecommitdiffstats
path: root/depends/lzma/wrapper/common_internal.c
blob: c9213ef4ec4487a892b6662ca3c5d4a9ad486538 (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
/*
 * Written in 2009 by Lloyd Hilaiel
 *
 * License
 *
 * All the cruft you find here is public domain.  You don't have to credit
 * anyone to use this code, but my personal request is that you mention
 * Igor Pavlov for his hard, high quality work.
 */

#include "common_internal.h"

static void *elzmaAlloc(void *p, size_t size)
{
	struct elzma_alloc_struct *as = (struct elzma_alloc_struct *)p;
	if (as->clientMallocFunc)
	{
		return as->clientMallocFunc(as->clientMallocContext, size);
	}
	return malloc(size);
}

static void elzmaFree(void *p, void *address)
{
	struct elzma_alloc_struct *as = (struct elzma_alloc_struct *)p;
	if (as->clientFreeFunc)
	{
		as->clientFreeFunc(as->clientMallocContext, address);
	}
	else
	{
		free(address);
	}
}

void init_alloc_struct(struct elzma_alloc_struct *as, elzma_malloc clientMallocFunc,
					   void *clientMallocContext, elzma_free clientFreeFunc,
					   void *clientFreeContext)
{
	as->Alloc = elzmaAlloc;
	as->Free = elzmaFree;
	as->clientMallocFunc = clientMallocFunc;
	as->clientMallocContext = clientMallocContext;
	as->clientFreeFunc = clientFreeFunc;
	as->clientFreeContext = clientFreeContext;
}