/* ccpmem.h */ /* extensions by CCP for memory statistics */ #ifndef CCPMEM_H #define CCPMEM_H #include #include "stackless.h" /* must ensure that this is included before object.h" */ #include "object.h" #ifdef __cplusplus extern "C" { #endif /* memory usage statistics */ struct PyCCP_MemStatsItem_t { Py_ssize_t bytes; Py_ssize_t allocs; }; typedef struct PyCCP_MemStats_t { struct PyCCP_MemStatsItem_t mem; // allocs through PyMem_MALLOC struct PyCCP_MemStatsItem_t obj; // allocs through PyObject_Malloc (duplicated in raw and/or blc) struct PyCCP_MemStatsItem_t raw; // allocs through the raw interface struct PyCCP_MemStatsItem_t blc; // allocs in blocks } PyCCP_MemStats_t; PyAPI_FUNC(size_t) PyCCP_GetMemStats(PyCCP_MemStats_t *stats, size_t size); /* Support for custom allocators */ typedef void *(*PyCCP_Malloc_t)(size_t size, void *arg, const char *file, int line, const char *msg); typedef void *(*PyCCP_Realloc_t)(void *ptr, size_t size, void *arg, const char *file, int line, const char *msg); typedef void (*PyCCP_Free_t)(void *ptr, void *arg, const char *file, int line, const char *msg); typedef size_t (*PyCCP_Msize_t)(void *ptr, void *arg); typedef struct PyCCP_CustomAllocator_t { PyCCP_Malloc_t pMalloc; PyCCP_Realloc_t pRealloc; PyCCP_Free_t pFree; PyCCP_Msize_t pMsize; /* can be NULL, or return -1 if no size info is avail. */ void *arg; /* opaque argument for the functions */ } PyCCP_CustomAllocator_t; /* To set an allocator! use 0 for the regular allocator, 1 for the block allocator. * pass a null pointer to reset to internal default */ PyAPI_FUNC(void) PyCCP_SetAllocator(int which, const PyCCP_CustomAllocator_t *); /* for BLUE to set the current context */ /* internal data member */ extern PyCCP_CustomAllocator_t _PyCCP_CustomAllocator[]; /* CCP Addition: count allocated memory */ extern size_t PyCCP_memCounter; /* has to be exported for old compatibility */ /* context memory counting */ extern int PyCCP_memContextsActive; extern int PyCCP_memDynamicContexts; PyObject *PyCCP_MemGetContexts(void); void PyCCP_MemClearContexts(void); int PyCCP_MemGetContextByObj(PyObject *obj); PyAPI_FUNC(void) PyCCP_MemSetContext(int); /* for BLUE to set the current context */ /* interface to the ccp memory handling stuff. */ /* note, some sucky external modules use the memory allocation macros, so we must export some of these dudes */ PyAPI_FUNC(void *) PyCCP_MemMalloc(size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void *) PyCCP_MemRealloc(void *ptr, size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void) PyCCP_MemFree(void *ptr, const char *file, int line, const char *msg); size_t PyCCP_MemMsize(void *ptr); int PyCCP_MemMctxt(void *ptr); PyAPI_FUNC(void *) PyCCP_ObjMalloc(size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void *) PyCCP_ObjRealloc(void *ptr, size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void) PyCCP_ObjFree(void *ptr, const char *file, int line, const char *msg); size_t PyCCP_ObjMsize(void *ptr); PyAPI_FUNC(void *) PyCCP_RawMalloc(size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void *) PyCCP_RawRealloc(void *ptr, size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void) PyCCP_RawFree(void *ptr, const char *file, int line, const char *msg); size_t PyCCP_RawMsize(void *ptr); PyAPI_FUNC(void *) PyCCP_BlockMalloc(size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void *) PyCCP_BlockRealloc(void *ptr, size_t size, const char *file, int line, const char *msg); PyAPI_FUNC(void) PyCCP_BlockFree(void *ptr, const char *file, int line, const char *msg); size_t PyCCP_BlockMsize(void *ptr); //#define PyCCP_MEM_USE_HEAP_ALLOC #if defined PyCCP_MEM_USE_HEAP_ALLOC void *_PyMem_WinHeapAlloc(size_t n); void *_PyMem_WinHeapReAlloc(void *p, size_t n); void _PyMem_WinHeapFree(void *p); size_t _PyMem_WinHeapSize(void *p); /* actual malloc calls */ __inline void* PyMem_MALLOC_INNER(size_t n) { return _PyMem_WinHeapAlloc(n); } __inline void* PyMem_REALLOC_INNER(void *p, size_t n) { return _PyMem_WinHeapReAlloc(p, n); } __inline void PyMem_FREE_INNER(void *p) { _PyMem_WinHeapFree(p); } __inline size_t PyMem_MSIZE_INNER(void *p) { return _PyMem_WinHeapSize(p); } #else /* actual malloc calls */ __inline void* PyMem_MALLOC_INNER(size_t n) { //force pointer to 0 size to be valid return malloc(n?n:1); } __inline void* PyMem_REALLOC_INNER(void *p, size_t n) { //force c99 behaviour for n==0: return a valid pointer if (!p) return malloc(n?n:1); return realloc(p, n?n:1); } __inline void PyMem_FREE_INNER(void *p) { free(p); } #ifdef SN_TARGET_PS3 __inline size_t PyMem_MSIZE_INNER(void *p) { return malloc_usable_size(p); } #else #include __inline size_t PyMem_MSIZE_INNER(void *p) { return _msize(p); } #endif #endif #ifdef __cplusplus } // extern "C" #endif #endif /* CCPMEM_H */