/* ccpmem_patch.h */ /* Patch the use of malloc with the proper python memory allocation macros */ #ifndef CCPMEM_PATCH_H #define CCPMEM_PATCH_H #ifdef PyCCP_MEM #include "pymem.h" #ifdef CCPMEM_PATCH_RAW /* use the raw api, suitable if the GIL isn't being held */ static void *ccp_malloc(size_t size, const char *file, int line) { return PyMem_MALLOC_RAW_EX(size, file, line, 0); } static void *ccp_realloc(void *ptr, size_t size, const char *file, int line) { return PyMem_REALLOC_RAW_EX(ptr, size, file, line, 0); } static void ccp_free(void *ptr, const char *file, int line) { PyMem_FREE_RAW_EX(ptr, file, line, 0); } #else /* use the regular, GIL dependent, api */ static void *ccp_malloc(size_t size, const char *file, int line) { return PyMem_MALLOC_EX(size, file, line, 0); } static void *ccp_realloc(void *ptr, size_t size, const char *file, int line) { return PyMem_REALLOC_EX(ptr, size, file, line, 0); } static void ccp_free(void *ptr, const char *file, int line) { PyMem_FREE_EX(ptr, file, line, 0); } #endif static char *ccp_strdup(const char *s, const char *file, int line) { size_t len; char *dest; if (!s) return NULL; len = strlen(s)+1; dest = (char*)ccp_malloc(len, file, line); if (!dest) return NULL; memcpy(dest, s, len); return dest; } #undef malloc #undef realloc #undef free #undef strdup #undef _strdup #define malloc(s) ccp_malloc((s), __FILE__, __LINE__) #define realloc(p, s) ccp_realloc((p), (s), __FILE__, __LINE__) #define free(p) ccp_free((p), __FILE__, __LINE__) #define strdup(p) ccp_strdup((p), __FILE__, __LINE__) #define _strdup(p) ccp_strdup((p), __FILE__, __LINE__) /* End of CCP change */ #endif /* PyCCP_MEM */ #else /* CCPMEM_PATCH_H */ #ifdef CCPMEM_PATCH_DISABLE /* disable it */ #undef malloc #undef realloc #undef free #undef strdup #undef _strdup #undef CCPMEM_PATCH_DISABLE #else /* re-enable it */ #define malloc(s) ccp_malloc((s), __FILE__, __LINE__) #define realloc(p, s) ccp_realloc((p), (s), __FILE__, __LINE__) #define free(p) ccp_free((p), __FILE__, __LINE__) #define strdup(p) ccp_strdup((p), __FILE__, __LINE__) #define _strdup(p) ccp_strdup((p), __FILE__, __LINE__) #endif #endif /* CCPMEM_PATCH_H */