summaryrefslogtreecommitdiff
path: root/src/liballoc/allocator.h
blob: 48be179d4de464c560e00eb0d9dc6b02e4804aeb (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef NEOAS_ALLOCATOR_H_INCLUDED
# define NEOAS_ALLOCATOR_H_INCLUDED

struct memory_allocator
{
  void *(*allocate) (void *, size_t, size_t);
  void *(*reallocate) (void *, void *, size_t, size_t);
  void (*deallocate) (void *, void *);
  void *context;
};

static inline const struct memory_allocator *default_memory_allocator (void);

static inline int safe_allocate (const struct memory_allocator *allocator,
				 size_t n, size_t size, void **allocated);

static inline int safe_reallocate (const struct memory_allocator *allocator,
				   void **memory, size_t n, size_t size);

static inline void safe_deallocate (const struct memory_allocator *allocator,
				    void **allocated);

static inline void copy_allocator (struct memory_allocator *dest,
				   const struct memory_allocator *src);

# include <stdlib.h>

# define ALLOCATE(ptr, n) \
  (safe_allocate (allocator, n, sizeof (*ptr), (void **) (&ptr)))

# define REALLOCATE(ptr, n) \
  (safe_reallocate (allocator, (void **) (&ptr), n, sizeof (*ptr)))

# define DEALLOCATE(ptr) \
  (safe_deallocate (allocator, (void **) (&ptr)))

static void *
default_allocate (void *unused, size_t n, size_t type)
{
  (void) unused;
  return calloc (n, type);
}

static void *
default_reallocate (void *unused, void *mem, size_t n, size_t type)
{
  (void) unused;
  return realloc (mem, n * type);
}

static void
default_deallocate (void *unused, void *mem)
{
  (void) unused;
  free (mem);
}

static inline const struct memory_allocator *
default_memory_allocator (void)
{
  static const struct memory_allocator alloc = {
    .allocate = default_allocate,
    .reallocate = default_reallocate,
    .deallocate = default_deallocate,
    .context = NULL
  };
  return &alloc;
}

static inline int
safe_allocate (const struct memory_allocator *allocator,
	       size_t n, size_t size, void **allocated)
{
  if (allocator == NULL)
    {
      allocator = default_memory_allocator ();
    }
  *allocated = allocator->allocate ((void *) allocator->context, n, size);
  if (*allocated == NULL)
    {
      return -1;
    }
  return 0;
}

static inline int
safe_reallocate (const struct memory_allocator *allocator,
		 void **memory, size_t n, size_t size)
{
  if (allocator == NULL)
    {
      allocator = default_memory_allocator ();
    }
  if (n == 0)
    {
      n = 1;
    }
  if (size == 0)
    {
      size = 1;
    }
  void *reallocated =
    allocator->reallocate (allocator->context, *memory, n, size);
  if (reallocated == NULL)
    {
      return -1;
    }
  *memory = reallocated;
  return 0;
}

static inline void
safe_deallocate (const struct memory_allocator *allocator, void **allocated)
{
  if (allocator == NULL)
    {
      allocator = default_memory_allocator ();
    }
  void *ptr = *allocated;
  if (ptr)
    {
      allocator->deallocate (allocator->context, ptr);
    }
  *allocated = NULL;
}

static inline void
copy_allocator (struct memory_allocator *dest,
		const struct memory_allocator *src)
{
  if (src == NULL)
    {
      src = default_memory_allocator ();
    }
  dest->allocate = src->allocate;
  dest->reallocate = src->reallocate;
  dest->deallocate = src->deallocate;
  dest->context = src->context;
}

#endif /* not NEOAS_ALLOCATOR_H_INCLUDED */