Derive-C
Loading...
Searching...
No Matches
null.h
Go to the documentation of this file.
1
2// and just uses malloc/calloc/free.
3
4#ifndef NULLALLOC_H
5#define NULLALLOC_H
6
7#include <derive-c/core.h>
8#include <derive-c/panic.h>
9#include <stdlib.h>
10
11#ifdef __cplusplus
12struct nullalloc {
13 char UNUSED(_dummy_cpp_object_size_compatibility);
14};
15#else
16typedef struct {
17} nullalloc;
18#endif
19
20static nullalloc NAME(nullalloc, get)() { return (nullalloc){}; }
21
22static void* NAME(nullalloc, malloc)(nullalloc* DEBUG_UNUSED(self), size_t UNUSED(size)) {
23 DEBUG_ASSERT(self);
24 return NULL;
25}
26
27static void* NAME(nullalloc, realloc)(nullalloc* DEBUG_UNUSED(self), void* DEBUG_UNUSED(ptr),
28 size_t UNUSED(size)) {
29 DEBUG_ASSERT(self);
30 DEBUG_ASSERT(ptr);
31 return NULL;
32}
33
34static void* NAME(nullalloc, calloc)(nullalloc* DEBUG_UNUSED(self), size_t UNUSED(count),
35 size_t UNUSED(size)) {
36 DEBUG_ASSERT(self);
37 return NULL;
38}
39
40static void NAME(nullalloc, free)(nullalloc* DEBUG_UNUSED(self), void* UNUSED(ptr)) {
41 DEBUG_ASSERT(self);
42 PANIC("Not possible to free memory from the null allocator, as it allocates nothing")
43}
44
45#endif
static void free(SELF *self, void *ptr)
Definition template.h:62
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:51
static void * malloc(SELF *self, size_t size)
Definition template.h:29
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:40
#define UNUSED(ident)
Definition core.h:13
#define DEBUG_UNUSED(ident)
Definition core.h:8
#define NAME(pre, post)
Definition core.h:5
static nullalloc get()
Definition null.h:20
#define DEBUG_ASSERT(expr)
Definition panic.h:23
#define PANIC(...)
Definition panic.h:8
An allocator for the standard allocator, which only has global state,.
Definition null.h:16
static INDEX_TYPE size(SELF const *self)
Definition template.h:207