nrt.h 6.3 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
All functions described here are threadsafe.
*/

#ifndef NUMBA_NRT_H_
#define NUMBA_NRT_H_


#include <stdlib.h>
#include <stdio.h>
#include "../../_numba_common.h"

#include "nrt_external.h"

#ifdef __cplusplus
extern "C"
{
#endif
/* Debugging facilities - enabled at compile-time */
/* #undef NDEBUG */
#if 0
#   define NRT_Debug(X) {X; fflush(stdout); }
#else
#   define NRT_Debug(X) if (0) { X; }
#endif

/* TypeDefs */
typedef void (*NRT_dtor_function)(void *ptr, size_t size, void *info);
typedef void (*NRT_dealloc_func)(void *ptr, void *dealloc_info);

typedef void *(*NRT_malloc_func)(size_t size);
typedef void *(*NRT_realloc_func)(void *ptr, size_t new_size);
typedef void (*NRT_free_func)(void *ptr);

/* Memory System API */

/* Initialize the memory system */
VISIBILITY_HIDDEN
void NRT_MemSys_init(void);

/* Shutdown the memory system */
VISIBILITY_HIDDEN
void NRT_MemSys_shutdown(void);

/*
 * Register the system allocation functions
 */
VISIBILITY_HIDDEN
void NRT_MemSys_set_allocator(NRT_malloc_func, NRT_realloc_func, NRT_free_func);

/*
 * Enable the internal statistics counters.
 */
VISIBILITY_HIDDEN
void NRT_MemSys_enable_stats(void);

/*
 * Disable the internal statistics counters.
 */
VISIBILITY_HIDDEN
void NRT_MemSys_disable_stats(void);

/*
 * Query whether the internal statistics counters are enabled.
 * Returns 1 if they are, 0 if they are not.
 */
VISIBILITY_HIDDEN
size_t NRT_MemSys_stats_enabled(void);

/*
 * The following functions get internal statistics of the memory subsystem.
 */
VISIBILITY_HIDDEN
size_t NRT_MemSys_get_stats_alloc(void);
VISIBILITY_HIDDEN
size_t NRT_MemSys_get_stats_free(void);
VISIBILITY_HIDDEN
size_t NRT_MemSys_get_stats_mi_alloc(void);
VISIBILITY_HIDDEN
size_t NRT_MemSys_get_stats_mi_free(void);

/* Memory Info API */

/* Create a new MemInfo for external memory
 *
 * data: data pointer being tracked
 * dtor: destructor to execute
 * dtor_info: additional information to pass to the destructor
 */
VISIBILITY_HIDDEN
NRT_MemInfo* NRT_MemInfo_new(void *data, size_t size,
                             NRT_dtor_function dtor, void *dtor_info);

/*
 * The `external_allocator` is for experimental API to customize the allocator.
 * Set to NULL to use the default builtin allocator.
 */
VISIBILITY_HIDDEN
void NRT_MemInfo_init(NRT_MemInfo *mi, void *data, size_t size,
                      NRT_dtor_function dtor, void *dtor_info,
                      NRT_ExternalAllocator *external_allocator);

/*
 * Returns the refcount of a MemInfo or (size_t)-1 if error.
 */
VISIBILITY_HIDDEN
size_t NRT_MemInfo_refcount(NRT_MemInfo *mi);

/*
 * Allocate memory of `size` bytes and return a pointer to a MemInfo structure
 * that describes the allocation
 */
VISIBILITY_HIDDEN
NRT_MemInfo *NRT_MemInfo_alloc(size_t size);

NRT_MemInfo *NRT_MemInfo_alloc_external(size_t size, NRT_ExternalAllocator *allocator);

/*
 * The "safe" NRT_MemInfo_alloc performs additional steps to help debug
 * memory errors.
 * It is guaranteed to:
 *   - zero-fill to the memory region after allocation and before deallocation.
 *   - may do more in the future
 */
VISIBILITY_HIDDEN
NRT_MemInfo *NRT_MemInfo_alloc_safe(size_t size);

/*
 * Similar to NRT_MemInfo_alloc_safe but with a custom dtor.
 */
VISIBILITY_HIDDEN
NRT_MemInfo* NRT_MemInfo_alloc_dtor_safe(size_t size, NRT_dtor_function dtor);

/*
 * Similar to NRT_MemInfo_alloc but with a custom dtor.
 */
VISIBILITY_HIDDEN
NRT_MemInfo* NRT_MemInfo_alloc_dtor(size_t size, NRT_dtor_function dtor);

/*
 * Aligned versions of the NRT_MemInfo_alloc and NRT_MemInfo_alloc_safe.
 * These take an additional argument `align` for number of bytes to align to.
 */
VISIBILITY_HIDDEN
NRT_MemInfo *NRT_MemInfo_alloc_aligned(size_t size, unsigned align);
VISIBILITY_HIDDEN
NRT_MemInfo *NRT_MemInfo_alloc_safe_aligned(size_t size, unsigned align);

/*
 * Experimental.
 * A variation to use an external allocator.
 */
NRT_MemInfo *NRT_MemInfo_alloc_safe_aligned_external(size_t size, unsigned align, NRT_ExternalAllocator *allocator);

/*
 * Internal API.
 * Release a MemInfo. Calls NRT_MemSys_insert_meminfo.
 */
VISIBILITY_HIDDEN
void NRT_MemInfo_destroy(NRT_MemInfo *mi);

/*
 * Acquire a reference to a MemInfo
 */
VISIBILITY_HIDDEN
void NRT_MemInfo_acquire(NRT_MemInfo* mi);

/*
 * Release a reference to a MemInfo
 */
VISIBILITY_HIDDEN
void NRT_MemInfo_release(NRT_MemInfo* mi);

/*
 * Internal/Compiler API.
 * Invoke the registered destructor of a MemInfo.
 */
VISIBILITY_HIDDEN
void NRT_MemInfo_call_dtor(NRT_MemInfo *mi);

/*
 * Returns the data pointer
 */
VISIBILITY_HIDDEN
void* NRT_MemInfo_data(NRT_MemInfo* mi);

/*
 * Returns the allocated size
 */
VISIBILITY_HIDDEN
size_t NRT_MemInfo_size(NRT_MemInfo* mi);


/*
 * Experimental.
 * Returns the external allocator
 */
VISIBILITY_HIDDEN
void* NRT_MemInfo_external_allocator(NRT_MemInfo* mi);

/*
 * Returns the parent MemInfo
 */
VISIBILITY_HIDDEN
void* NRT_MemInfo_parent(NRT_MemInfo* mi);


/*
 * NRT API for resizable buffers.
 */
VISIBILITY_HIDDEN
NRT_MemInfo *NRT_MemInfo_new_varsize(size_t size);
VISIBILITY_HIDDEN
NRT_MemInfo *NRT_MemInfo_new_varsize_dtor(size_t size, NRT_dtor_function dtor);
VISIBILITY_HIDDEN
void *NRT_MemInfo_varsize_alloc(NRT_MemInfo *mi, size_t size);
VISIBILITY_HIDDEN
void *NRT_MemInfo_varsize_realloc(NRT_MemInfo *mi, size_t size);
VISIBILITY_HIDDEN
void NRT_MemInfo_varsize_free(NRT_MemInfo *mi, void *ptr);

/*
 * Print debug info to FILE
 */
VISIBILITY_HIDDEN
void NRT_MemInfo_dump(NRT_MemInfo *mi, FILE *out);


/* Low-level allocation wrappers. */

/*
 * Allocate memory of `size` bytes.
 */
VISIBILITY_HIDDEN void* NRT_Allocate(size_t size);

/*
 * Experimental
 *
 * An alternative allocator that allows using an external allocator.
 */
VISIBILITY_HIDDEN void* NRT_Allocate_External(size_t size, NRT_ExternalAllocator *allocator);

/*
 * Deallocate memory pointed by `ptr`.
 */
VISIBILITY_HIDDEN void NRT_Free(void *ptr);

/*
 * Reallocate memory at `ptr`.
 */
VISIBILITY_HIDDEN void *NRT_Reallocate(void *ptr, size_t size);

/*
 * Debugging printf function used internally
 */
VISIBILITY_HIDDEN void nrt_debug_print(const char *fmt, ...);

/*
 * Get API function table.
 */
VISIBILITY_HIDDEN const NRT_api_functions* NRT_get_api(void);


/*
 * FOR INTERNAL USE ONLY.
 * Get a sample external allocator for testing
 */
VISIBILITY_HIDDEN NRT_ExternalAllocator* _nrt_get_sample_external_allocator(void);

#ifdef __cplusplus
}
#endif
#endif /* NUMBA_NRT_H_ */