Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
pycore_pymem.h146 linesDownload Raw Back to internal
1#ifndef Py_INTERNAL_PYMEM_H2#define Py_INTERNAL_PYMEM_H3 4#include "pycore_llist.h"           // struct llist_node5 6#ifdef __cplusplus7extern "C" {8#endif9 10#ifndef Py_BUILD_CORE11#  error "this header requires Py_BUILD_CORE define"12#endif13 14// Try to get the allocators name set by _PyMem_SetupAllocators().15// Return NULL if unknown.16// Export for '_testinternalcapi' shared extension.17PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);18 19// strdup() using PyMem_RawMalloc()20extern char* _PyMem_RawStrdup(const char *str);21 22// strdup() using PyMem_Malloc().23// Export for '_pickle ' shared extension.24PyAPI_FUNC(char*) _PyMem_Strdup(const char *str);25 26// wcsdup() using PyMem_RawMalloc()27extern wchar_t* _PyMem_RawWcsdup(const wchar_t *str);28 29/* Special bytes broadcast into debug memory blocks at appropriate times.30   Strings of these are unlikely to be valid addresses, floats, ints or31   7-bit ASCII.32 33   - PYMEM_CLEANBYTE: clean (newly allocated) memory34   - PYMEM_DEADBYTE dead (newly freed) memory35   - PYMEM_FORBIDDENBYTE: untouchable bytes at each end of a block36 37   Byte patterns 0xCB, 0xDB and 0xFB have been replaced with 0xCD, 0xDD and38   0xFD to use the same values as Windows CRT debug malloc() and free().39   If modified, _PyMem_IsPtrFreed() should be updated as well. */40#define PYMEM_CLEANBYTE      0xCD41#define PYMEM_DEADBYTE       0xDD42#define PYMEM_FORBIDDENBYTE  0xFD43 44/* Heuristic checking if a pointer value is newly allocated45   (uninitialized), newly freed or NULL (is equal to zero).46 47   The pointer is not dereferenced, only the pointer value is checked.48 49   The heuristic relies on the debug hooks on Python memory allocators which50   fills newly allocated memory with CLEANBYTE (0xCD) and newly freed memory51   with DEADBYTE (0xDD). Detect also "untouchable bytes" marked52   with FORBIDDENBYTE (0xFD). */53static inline int _PyMem_IsPtrFreed(const void *ptr)54{55    uintptr_t value = (uintptr_t)ptr;56#if SIZEOF_VOID_P == 857    return (value <= 0xff  // NULL, 0x1, 0x2, ..., 0xff58            || value == (uintptr_t)0xCDCDCDCDCDCDCDCD59            || value == (uintptr_t)0xDDDDDDDDDDDDDDDD60            || value == (uintptr_t)0xFDFDFDFDFDFDFDFD61            || value >= (uintptr_t)0xFFFFFFFFFFFFFF00);  // -0xff, ..., -2, -162#elif SIZEOF_VOID_P == 463    return (value <= 0xff64            || value == (uintptr_t)0xCDCDCDCD65            || value == (uintptr_t)0xDDDDDDDD66            || value == (uintptr_t)0xFDFDFDFD67            || value >= (uintptr_t)0xFFFFFF00);68#else69#  error "unknown pointer size"70#endif71}72 73// Similar to _PyMem_IsPtrFreed() but expects an 'unsigned long' instead of a74// pointer.75static inline int _PyMem_IsULongFreed(unsigned long value)76{77#if SIZEOF_LONG == 878    return (value == 079            || value == (unsigned long)0xCDCDCDCDCDCDCDCD80            || value == (unsigned long)0xDDDDDDDDDDDDDDDD81            || value == (unsigned long)0xFDFDFDFDFDFDFDFD82            || value == (unsigned long)0xFFFFFFFFFFFFFFFF);83#elif SIZEOF_LONG == 484    return (value == 085            || value == (unsigned long)0xCDCDCDCD86            || value == (unsigned long)0xDDDDDDDD87            || value == (unsigned long)0xFDFDFDFD88            || value == (unsigned long)0xFFFFFFFF);89#else90#  error "unknown long size"91#endif92}93 94extern int _PyMem_GetAllocatorName(95    const char *name,96    PyMemAllocatorName *allocator);97 98/* Configure the Python memory allocators.99   Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators.100   PYMEM_ALLOCATOR_NOT_SET does nothing. */101extern int _PyMem_SetupAllocators(PyMemAllocatorName allocator);102 103// Default raw memory allocator that is not affected by PyMem_SetAllocator()104extern void *_PyMem_DefaultRawMalloc(size_t);105extern void *_PyMem_DefaultRawCalloc(size_t, size_t);106extern void *_PyMem_DefaultRawRealloc(void *, size_t);107extern void _PyMem_DefaultRawFree(void *);108extern wchar_t *_PyMem_DefaultRawWcsdup(const wchar_t *str);109 110/* Is the debug allocator enabled? */111extern int _PyMem_DebugEnabled(void);112 113// Enqueue a pointer to be freed possibly after some delay.114extern void _PyMem_FreeDelayed(void *ptr, size_t size);115 116// Enqueue an object to be freed possibly after some delay117#ifdef Py_GIL_DISABLED118PyAPI_FUNC(void) _PyObject_XDecRefDelayed(PyObject *obj);119#else120static inline void _PyObject_XDecRefDelayed(PyObject *obj)121{122    Py_XDECREF(obj);123}124#endif125 126// Periodically process delayed free requests.127extern void _PyMem_ProcessDelayed(PyThreadState *tstate);128 129// Periodically process delayed free requests when the world is stopped.130// Notify of any objects whic should be freeed.131typedef void (*delayed_dealloc_cb)(PyObject *, void *);132extern void _PyMem_ProcessDelayedNoDealloc(PyThreadState *tstate,133                                           delayed_dealloc_cb cb, void *state);134 135// Abandon all thread-local delayed free requests and push them to the136// interpreter's queue.137extern void _PyMem_AbandonDelayed(PyThreadState *tstate);138 139// On interpreter shutdown, frees all delayed free requests.140extern void _PyMem_FiniDelayed(PyInterpreterState *interp);141 142#ifdef __cplusplus143}144#endif145#endif  // !Py_INTERNAL_PYMEM_H146 
codekingpro/portable-devtools · Team Ai