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#pragma once
5
6#include <stdio.h>
7#include <stdlib.h>
8
11
12DC_ZERO_SIZED(nullalloc);
13
14static nullalloc NS(nullalloc, get)() { return (nullalloc){}; }
15
16static void* NS(nullalloc, malloc)(nullalloc* self, size_t size) {
17 (void)size;
18 DC_ASSUME(self);
19 return NULL;
20}
21
22static void* NS(nullalloc, realloc)(nullalloc* self, void* ptr, size_t size) {
23 (void)size;
24 DC_ASSUME(self);
25 DC_ASSUME(ptr);
26 return NULL;
27}
28
29static void* NS(nullalloc, calloc)(nullalloc* self, size_t count, size_t size) {
30 (void)count;
31 (void)size;
32 DC_ASSUME(self);
33 return NULL;
34}
35
36static void NS(nullalloc, free)(nullalloc* self, void* ptr) {
37 (void)ptr;
38 DC_ASSUME(self);
39 DC_PANIC("Not possible to free memory from the null allocator, as it allocates nothing")
40}
41
42static void NS(nullalloc, debug)(nullalloc const* self, dc_debug_fmt fmt, FILE* stream) {
43 (void)fmt;
44 fprintf(stream, "nullalloc@%p { }", self);
45}
46
47DC_TRAIT_ALLOC(nullalloc);
static void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:62
static void free(SELF *self, void *ptr)
Definition template.h:56
static void * realloc(SELF *self, void *ptr, size_t size)
Definition template.h:45
static void * malloc(SELF *self, size_t size)
Definition template.h:23
static void * calloc(SELF *self, size_t count, size_t size)
Definition template.h:34
#define DC_TRAIT_ALLOC(SELF)
Definition trait.h:5
static INDEX_TYPE size(SELF const *self)
Definition template.h:252
#define NS(pre, post)
Definition namespace.h:4
static nullalloc get()
Definition null.h:14
#define DC_PANIC(...)
Definition panic.h:28
#define DC_ASSUME(expr,...)
Definition panic.h:56
Debug format helpers for debug printin data structures.
Definition fmt.h:10
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107
#define DC_ZERO_SIZED(TYPE)
Zero sized types are useful as markers (e.g. for gdb printing, or to replace debug info structs.
Definition zerosized.h:15