aboutsummaryrefslogtreecommitdiff
path: root/allocator.h
blob: fc28833ecd71969d58debbe745a4a0544a0060d6 (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
#ifndef ITS1_ALLOCATOR_H_INCLUDED
#define ITS1_ALLOCATOR_H_INCLUDED

#include <stddef.h> /* size_t */

struct its1_allocator {
	void *(*allocate)(struct its1_allocator *allocator, size_t n);
	void (*deallocate)(struct its1_allocator *allocator, void *ptr);
};

extern struct its1_allocator its1_default_allocator;

/* allocator interface */
void *its1_malloc(struct its1_allocator *allocator, size_t n);
void  its1_free(struct its1_allocator *allocator, void *ptr);

/* helper functions */
char *its1_strdup(struct its1_allocator *allocator, const char *str);
void *its1_malloc_zero(struct its1_allocator *allocator, size_t n);

#define ALLOCATE(n) \
	its1_malloc(&its1_default_allocator, (n))

#define ALLOCATE_ZERO(n) \
	its1_malloc_zero(&its1_default_allocator, (n))

#define FREE(ptr) \
	(its1_free(&its1_default_allocator, (ptr)), (ptr) = NULL)

#define NEW(ptr) ((ptr) = ALLOCATE(sizeof *(ptr)))
#define NEW0(ptr) ((ptr) = ALLOCATE_ZERO(sizeof *(ptr)))

#endif