MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
memory.h
Go to the documentation of this file.
1 #ifndef _MEMORY_H_
2 #define _MEMORY_H_
3 
4 #include <stddef.h>
5 
6 typedef struct Allocator Allocator;
7 
8 #define ALLOC_ENOTALLOC -3
9 #define ALLOC_EMISMATCH -2
10 #define ALLOC_ENOMEMORY -1
11 
12 #define ALLOC_DIR_TOP -1
13 #define ALLOC_DIR_BOT +1
14 #define ALLOC_DIR_BOTH 0
15 
16 struct Allocator {
17  char name[12];
19 
20  char * rawbase;
21  char * base;
22  size_t size;
23 
24  size_t bottom;
25  size_t top;
26 
27  int refcount;
28  int use_malloc; /* only do the book keeping. delegate to libc malloc/free */
29 };
30 
31 typedef struct AllocatorIter AllocatorIter;
32 struct AllocatorIter {
34  size_t _bottom;
35  size_t _top;
36  int _ended;
37 
38  /* current block */
39  size_t size;
40  size_t request_size;
41  char * name;
42  int dir;
43  char * annotation;
44  char * ptr;
45 };
46 
47 int
48 allocator_init(Allocator * alloc, const char * name, const size_t size, const int zero, Allocator * parent);
49 
50 int
51 allocator_malloc_init(Allocator * alloc, const char * name, const size_t size, const int zero, Allocator * parent);
52 
53 int
54 allocator_split(Allocator * alloc, Allocator * parent, const char * name, const size_t request_size, const int zero);
55 
56 int
58 
59 void *
60 allocator_alloc(Allocator * alloc, const char * name, const size_t size, const int dir, const char * fmt, ...);
61 
62 void *
63 allocator_realloc_int(Allocator * alloc, void * ptr, const size_t size, const char * fmt, ...);
64 
65 #define allocator_alloc_bot(alloc, name, size) \
66  allocator_alloc(alloc, name, size, ALLOC_DIR_BOT, "%s:%d", __FILE__, __LINE__)
67 
68 #define allocator_alloc_top(alloc, name, size) \
69  allocator_alloc(alloc, name, size, ALLOC_DIR_TOP, "%s:%d", __FILE__, __LINE__)
70 
71 #define allocator_realloc(alloc, ptr, size) \
72  allocator_realloc_int(alloc, ptr, size, "%s:%d", __FILE__, __LINE__)
73 
74 /* free like API, will look up allocator pointer. */
75 void
76 allocator_free(void * ptr);
77 
78 int
79 allocator_dealloc (Allocator * alloc, void * ptr);
80 
81 size_t
83 
84 size_t
85 allocator_get_used_size(Allocator * alloc, int dir);
86 
87 int
89 
90 int
92  AllocatorIter * iter
93  );
94 
95 int
97  AllocatorIter * iter,
98  Allocator * alloc
99  );
100 
101 /* 0 for total */
102 size_t
103 allocator_get_used_size(Allocator * alloc, int dir);
104 
105 void
106 allocator_print(Allocator * alloc);
107 
108 int
109 allocator_reset(Allocator * alloc, int zero);
110 
111 #endif
const char * name
Definition: densitykernel.c:93
int allocator_iter_next(AllocatorIter *iter)
Definition: memory.c:209
int allocator_init(Allocator *alloc, const char *name, const size_t size, const int zero, Allocator *parent)
Definition: memory.c:24
void allocator_free(void *ptr)
Definition: memory.c:358
int allocator_split(Allocator *alloc, Allocator *parent, const char *name, const size_t request_size, const int zero)
int allocator_reset(Allocator *alloc, int zero)
Definition: memory.c:85
size_t allocator_get_free_size(Allocator *alloc)
Definition: memory.c:246
int allocator_dealloc(Allocator *alloc, void *ptr)
Definition: memory.c:376
void * allocator_realloc_int(Allocator *alloc, void *ptr, const size_t size, const char *fmt,...)
Definition: memory.c:312
void allocator_print(Allocator *alloc)
Definition: memory.c:284
int allocator_iter_start(AllocatorIter *iter, Allocator *alloc)
Definition: memory.c:190
int allocator_malloc_init(Allocator *alloc, const char *name, const size_t size, const int zero, Allocator *parent)
Definition: memory.c:54
int allocator_destroy(Allocator *alloc)
Definition: memory.c:176
size_t allocator_get_used_size(Allocator *alloc, int dir)
Definition: memory.c:256
void * allocator_alloc(Allocator *alloc, const char *name, const size_t size, const int dir, const char *fmt,...)
Definition: memory.c:166
int allocator_iter_ended(AllocatorIter *iter)
Definition: memory.c:240
size_t _bottom
Definition: memory.h:34
char * ptr
Definition: memory.h:44
Allocator * alloc
Definition: memory.h:33
char * name
Definition: memory.h:41
int _ended
Definition: memory.h:36
size_t _top
Definition: memory.h:35
size_t size
Definition: memory.h:39
char * annotation
Definition: memory.h:43
size_t request_size
Definition: memory.h:40
char name[12]
Definition: memory.h:17
Allocator * parent
Definition: memory.h:18
size_t top
Definition: memory.h:25
int refcount
Definition: memory.h:27
int use_malloc
Definition: memory.h:28
size_t bottom
Definition: memory.h:24
char * rawbase
Definition: memory.h:20
char * base
Definition: memory.h:21
size_t size
Definition: memory.h:22