Memory management in NumPy*
*NumPy is a trademark owned by NumFOCUS.
Α metaclass is used to override the internal data memory routines. The metaclass has four optional fields:
>>> import ctypes
>>> import ctypes.util
>>> import numpy_allocator
>>> my = ctypes.CDLL(ctypes.util.find_library('my'))
>>> class my_allocator(metaclass=numpy_allocator.type):
... _calloc_ = ctypes.addressof(my.calloc_func)
... _free_ = ctypes.addressof(my.free_func)
... _malloc_ = ctypes.addressof(my.malloc_func)
... _realloc_ = ctypes.addressof(my.realloc_func)
...
>>> import numpy as np
>>> with my_allocator:
... a = np.array([1, 2, 3])
...
>>> my_allocator.handles(a)
True