void NRT_MemSys_process_defer_dtor(void) { MemInfo *mi; while ((mi = nrt_pop_meminfo_list(&TheMSys.mi_deferlist))) { NRT_Debug(nrt_debug_print("Defer dtor %p\n", mi)); nrt_meminfo_call_dtor(mi); } }
NRT_adapt_ndarray_from_python(PyObject *obj, arystruct_t* arystruct) { PyArrayObject *ndary; int i, ndim; npy_intp *p; void *data; if (!PyArray_Check(obj)) { return -1; } ndary = (PyArrayObject*)obj; ndim = PyArray_NDIM(ndary); data = PyArray_DATA(ndary); arystruct->meminfo = meminfo_new_from_pyobject((void*)data, obj); arystruct->data = data; arystruct->nitems = PyArray_SIZE(ndary); arystruct->itemsize = PyArray_ITEMSIZE(ndary); arystruct->parent = obj; p = arystruct->shape_and_strides; for (i = 0; i < ndim; i++, p++) { *p = PyArray_DIM(ndary, i); } for (i = 0; i < ndim; i++, p++) { *p = PyArray_STRIDE(ndary, i); } NRT_Debug(nrt_debug_print("NRT_adapt_ndarray_from_python %p\n", arystruct->meminfo)); return 0; }
static void nrt_internal_dtor(void *ptr, void *info) { NRT_Debug(nrt_debug_print("nrt_internal_dtor %p, %p\n", ptr, info)); if (info != NULL) { memset(ptr, 0xDE, (size_t)info); /* for safety */ } NRT_Free(ptr); }
MemInfo* NRT_MemInfo_alloc_safe(size_t size) { void *data = NRT_Allocate(size); /* Only fill up a couple cachelines with debug markers, to minimize overhead. */ memset(data, 0xCB, MIN(size, 256)); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc_safe %p %llu\n", data, size)); return NRT_MemInfo_new(data, size, nrt_internal_dtor_safe, (void*)size); }
static void nrt_internal_dtor_safe(void *ptr, void *info) { size_t size = (size_t) info; NRT_Debug(nrt_debug_print("nrt_internal_dtor_safe %p, %p\n", ptr, info)); /* See NRT_MemInfo_alloc_safe() */ memset(ptr, 0xDE, MIN(size, 256)); NRT_Free(ptr); }
void NRT_MemInfo_release(MemInfo *mi, int defer) { NRT_Debug(nrt_debug_print("NRT_release %p\n", mi)); assert (mi->payload.refct > 0 && "RefCt cannot be 0"); /* RefCt drop to zero */ if (TheMSys.atomic_dec(&mi->payload.refct) == 0) { NRT_MemInfo_call_dtor(mi, defer); } }
MemInfo* NRT_MemInfo_alloc_safe_aligned(size_t size, unsigned align) { void *data = NULL; void *base = NRT_MemAlign(&data, size, align); memset(data, 0xCB, size); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc_safe_aligned %p %llu\n", data, size)); return NRT_MemInfo_new(data, size, nrt_internal_aligned_safe_dtor, base); }
static void nrt_meminfo_call_dtor(MemInfo *mi) { NRT_Debug(nrt_debug_print("nrt_meminfo_call_dtor %p\n", mi)); /* call dtor */ mi->payload.dtor(mi->payload.data, mi->payload.dtor_info); /* Clear and release MemInfo */ NRT_MemInfo_destroy(mi); }
MemInfo* NRT_MemSys_pop_meminfo(void) { MemInfo *node = nrt_pop_meminfo_list(&TheMSys.mi_freelist); if (NULL == node) { node = meminfo_malloc(); } memset(node, 0, sizeof(MemInfo)); /* to catch bugs; not required */ NRT_Debug(nrt_debug_print("NRT_MemSys_pop_meminfo: return %p\n", node)); return node; }
static void nrt_internal_aligned_safe_dtor(void *ptr, void *info) { AlignHeader *header = info; NRT_Debug(nrt_debug_print("nrt_internal_aligned_safe_dtor %p, %p\n", ptr, info)); if (header->total_size) { memset(header, 0xDE, header->total_size); /* for safety */ } NRT_Free(info); }
void NRT_MemSys_insert_meminfo(MemInfo *newnode) { if (NULL == newnode) { newnode = meminfo_malloc(); } else { assert(newnode->payload.refct == 0 && "RefCt must be 0"); } NRT_Debug(nrt_debug_print("NRT_MemSys_insert_meminfo newnode=%p\n", newnode)); memset(newnode, 0, sizeof(MemInfo)); /* to catch bugs; not required */ nrt_push_meminfo_list(&TheMSys.mi_freelist, newnode); }
static MemInfo* meminfo_malloc(void) { void *p = malloc(sizeof(MemInfo));; NRT_Debug(nrt_debug_print("meminfo_malloc %p\n", p)); return p; }
void NRT_MemInfo_acquire(MemInfo *mi) { NRT_Debug(nrt_debug_print("NRT_acquire %p\n", mi)); TheMSys.atomic_inc(&mi->payload.refct); }
void NRT_Free(void *ptr) { NRT_Debug(nrt_debug_print("NRT_Free %p\n", ptr)); free(ptr); TheMSys.atomic_inc(&TheMSys.stats_free); }
void* NRT_Allocate(size_t size) { void *ptr = malloc(size); NRT_Debug(nrt_debug_print("NRT_Allocate bytes=%llu ptr=%p\n", size, ptr)); return ptr; }
void NRT_MemInfo_defer_dtor(MemInfo *mi) { NRT_Debug(nrt_debug_print("NRT_MemInfo_defer_dtor\n")); nrt_push_meminfo_list(&TheMSys.mi_deferlist, mi); }
void* NRT_Allocate(size_t size) { void *ptr = malloc(size); NRT_Debug(nrt_debug_print("NRT_Allocate bytes=%llu ptr=%p\n", size, ptr)); TheMSys.atomic_inc(&TheMSys.stats_alloc); return ptr; }
void NRT_MemInfo_acquire(MemInfo *mi) { NRT_Debug(nrt_debug_print("NRT_acquire %p refct=%zu\n", mi, mi->payload.refct)); assert(mi->payload.refct > 0 && "RefCt cannot be zero"); TheMSys.atomic_inc(&mi->payload.refct); }
void NRT_Free(void *ptr) { NRT_Debug(nrt_debug_print("NRT_Free %p\n", ptr)); free(ptr); }
MemInfo* NRT_MemInfo_alloc_safe(size_t size) { void *data = NRT_Allocate(size); memset(data, 0xCB, size); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc_safe %p %llu\n", data, size)); return NRT_MemInfo_new(data, size, nrt_internal_dtor, (void*)size); }
static void nrt_internal_aligned_dtor(void *ptr, void *info) { NRT_Debug(nrt_debug_print("nrt_internal_aligned_dtor %p, %p\n", ptr, info)); NRT_Free(info); }
MemInfo* NRT_MemInfo_alloc(size_t size) { void *data = NRT_Allocate(size); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc %p\n", data)); return NRT_MemInfo_new(data, size, nrt_internal_dtor, NULL); }