Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
25f3dc21
Commit
25f3dc21
authored
Mar 18, 2002
by
Neil Schemenauer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Drop the PyCore_* memory API.
parent
08de92a2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
71 additions
and
92 deletions
+71
-92
Include/objimpl.h
Include/objimpl.h
+8
-45
Include/pymem.h
Include/pymem.h
+26
-25
Objects/object.c
Objects/object.c
+16
-1
Objects/obmalloc.c
Objects/obmalloc.c
+21
-21
No files found.
Include/objimpl.h
View file @
25f3dc21
...
@@ -56,43 +56,6 @@ form of memory management you're using).
...
@@ -56,43 +56,6 @@ form of memory management you're using).
Unless you have specific memory management requirements, it is
Unless you have specific memory management requirements, it is
recommended to use PyObject_{New, NewVar, Del}. */
recommended to use PyObject_{New, NewVar, Del}. */
/*
* Core object memory allocator
* ============================
*/
/* The purpose of the object allocator is to make the distinction
between "object memory" and the rest within the Python heap.
Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
the one that holds the object's representation defined by its C
type structure, *excluding* any object-specific memory buffers that
might be referenced by the structure (for type structures that have
pointer fields). By default, the object memory allocator is
implemented on top of the raw memory allocator.
The PyCore_* macros can be defined to make the interpreter use a
custom object memory allocator. They are reserved for internal
memory management purposes exclusively. Both the core and extension
modules should use the PyObject_* API. */
#ifdef WITH_PYMALLOC
void
*
_PyCore_ObjectMalloc
(
size_t
nbytes
);
void
*
_PyCore_ObjectRealloc
(
void
*
p
,
size_t
nbytes
);
void
_PyCore_ObjectFree
(
void
*
p
);
#define PyCore_OBJECT_MALLOC _PyCore_ObjectMalloc
#define PyCore_OBJECT_REALLOC _PyCore_ObjectRealloc
#define PyCore_OBJECT_FREE _PyCore_ObjectFree
#endif
/* !WITH_PYMALLOC */
#ifndef PyCore_OBJECT_MALLOC
#undef PyCore_OBJECT_REALLOC
#undef PyCore_OBJECT_FREE
#define PyCore_OBJECT_MALLOC(n) PyCore_MALLOC(n)
#define PyCore_OBJECT_REALLOC(p, n) PyCore_REALLOC((p), (n))
#define PyCore_OBJECT_FREE(p) PyCore_FREE(p)
#endif
/*
/*
* Raw object memory interface
* Raw object memory interface
* ===========================
* ===========================
...
@@ -111,19 +74,19 @@ void _PyCore_ObjectFree(void *p);
...
@@ -111,19 +74,19 @@ void _PyCore_ObjectFree(void *p);
/* Functions */
/* Functions */
/* Wrappers
around PyCore_OBJECT_MALLOC and friends; useful if you
/* Wrappers
that useful if you need to be sure that you are using the
need to be sure that you are using the same object memory allocator
same object memory allocator as Python. These wrappers *do not* make
as Python. These wrappers *do not* make sure that allocating 0
sure that allocating 0 bytes returns a non-NULL pointer. Returned
bytes returns a non-NULL pointer. Returned pointers must be check
ed
pointers must be checked for NULL explicitly; no action is perform
ed
for NULL explicitly; no action is performed
on failure. */
on failure. */
extern
DL_IMPORT
(
void
*
)
PyObject_Malloc
(
size_t
);
extern
DL_IMPORT
(
void
*
)
PyObject_Malloc
(
size_t
);
extern
DL_IMPORT
(
void
*
)
PyObject_Realloc
(
void
*
,
size_t
);
extern
DL_IMPORT
(
void
*
)
PyObject_Realloc
(
void
*
,
size_t
);
extern
DL_IMPORT
(
void
)
PyObject_Free
(
void
*
);
extern
DL_IMPORT
(
void
)
PyObject_Free
(
void
*
);
/* Macros */
/* Macros */
#define PyObject_MALLOC(n)
PyCore_OBJECT
_MALLOC(n)
#define PyObject_MALLOC(n)
_PyMalloc
_MALLOC(n)
#define PyObject_REALLOC(op, n)
PyCore_OBJECT
_REALLOC((void *)(op), (n))
#define PyObject_REALLOC(op, n)
_PyMalloc
_REALLOC((void *)(op), (n))
#define PyObject_FREE(op)
PyCore_OBJECT
_FREE((void *)(op))
#define PyObject_FREE(op)
_PyMalloc
_FREE((void *)(op))
/*
/*
* Generic object allocator interface
* Generic object allocator interface
...
...
Include/pymem.h
View file @
25f3dc21
...
@@ -10,26 +10,6 @@
...
@@ -10,26 +10,6 @@
extern
"C"
{
extern
"C"
{
#endif
#endif
/*
* Core memory allocator
* =====================
*/
/* To make sure the interpreter is user-malloc friendly, all memory
APIs are implemented on top of this one.
The PyCore_* macros can be defined to make the interpreter use a
custom allocator. Note that they are for internal use only. Both
the core and extension modules should use the PyMem_* API. */
#ifndef PyCore_MALLOC
#undef PyCore_REALLOC
#undef PyCore_FREE
#define PyCore_MALLOC(n) malloc(n)
#define PyCore_REALLOC(p, n) realloc((p), (n))
#define PyCore_FREE(p) free(p)
#endif
/* BEWARE:
/* BEWARE:
Each interface exports both functions and macros. Extension modules
Each interface exports both functions and macros. Extension modules
...
@@ -51,9 +31,12 @@ extern "C" {
...
@@ -51,9 +31,12 @@ extern "C" {
* ====================
* ====================
*/
*/
/* To make sure the interpreter is user-malloc friendly, all memory
APIs are implemented on top of this one. */
/* Functions */
/* Functions */
/* Function wrappers around Py
Core
_MALLOC and friends; useful if you
/* Function wrappers around Py
Mem
_MALLOC and friends; useful if you
need to be sure that you are using the same memory allocator as
need to be sure that you are using the same memory allocator as
Python. Note that the wrappers make sure that allocating 0 bytes
Python. Note that the wrappers make sure that allocating 0 bytes
returns a non-NULL pointer, even if the underlying malloc
returns a non-NULL pointer, even if the underlying malloc
...
@@ -66,10 +49,12 @@ extern DL_IMPORT(void) PyMem_Free(void *);
...
@@ -66,10 +49,12 @@ extern DL_IMPORT(void) PyMem_Free(void *);
/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
no longer supported. They used to call PyErr_NoMemory() on failure. */
no longer supported. They used to call PyErr_NoMemory() on failure. */
/* Macros */
/* Macros (override these if you want to a different malloc */
#define PyMem_MALLOC(n) PyCore_MALLOC(n)
#ifndef PyMem_MALLOC
#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
#define PyMem_MALLOC(n) malloc(n)
#define PyMem_FREE(p) PyCore_FREE((void *)(p))
#define PyMem_REALLOC(p, n) realloc((void *)(p), (n))
#define PyMem_FREE(p) free((void *)(p))
#endif
/*
/*
* Type-oriented memory interface
* Type-oriented memory interface
...
@@ -104,6 +89,22 @@ extern DL_IMPORT(void) PyMem_Free(void *);
...
@@ -104,6 +89,22 @@ extern DL_IMPORT(void) PyMem_Free(void *);
it is recommended to write the test explicitly in the code.
it is recommended to write the test explicitly in the code.
Note that according to ANSI C, free(NULL) has no effect. */
Note that according to ANSI C, free(NULL) has no effect. */
/* pymalloc (private to the interpreter) */
#ifdef WITH_PYMALLOC
void
*
_PyMalloc_Malloc
(
size_t
nbytes
);
void
*
_PyMalloc_Realloc
(
void
*
p
,
size_t
nbytes
);
void
_PyMalloc_Free
(
void
*
p
);
#define _PyMalloc_MALLOC _PyMalloc_Malloc
#define _PyMalloc_REALLOC _PyMalloc_Realloc
#define _PyMalloc_FREE _PyMalloc_Free
#else
#define _PyMalloc_MALLOC PyMem_MALLOC
#define _PyMalloc_REALLOC PyMem_REALLOC
#define _PyMalloc_FREE PyMem_FREE
#endif
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
...
...
Objects/object.c
View file @
25f3dc21
...
@@ -2093,4 +2093,19 @@ _PyTrash_destroy_chain(void)
...
@@ -2093,4 +2093,19 @@ _PyTrash_destroy_chain(void)
#ifdef WITH_PYMALLOC
#ifdef WITH_PYMALLOC
#include "obmalloc.c"
#include "obmalloc.c"
#endif
#else
void
*
_PyMalloc_Malloc
(
size_t
n
)
{
return
PyMem_MALLOC
(
n
);
}
void
*
_PyMalloc_Realloc
(
void
*
p
,
size_t
n
)
{
return
PyMem_REALLOC
(
p
,
n
);
}
void
_PyMalloc_Free
(
void
*
p
)
{
PyMem_FREE
(
p
);
}
#endif
/* !WITH_PYMALLOC */
Objects/obmalloc.c
View file @
25f3dc21
...
@@ -349,7 +349,7 @@ static void (*free_hook)(void *) = NULL;
...
@@ -349,7 +349,7 @@ static void (*free_hook)(void *) = NULL;
*/
*/
void
*
void
*
_Py
Core_Object
Malloc
(
size_t
nbytes
)
_Py
Malloc_
Malloc
(
size_t
nbytes
)
{
{
block
*
bp
;
block
*
bp
;
poolp
pool
;
poolp
pool
;
...
@@ -479,7 +479,7 @@ _PyCore_ObjectMalloc(size_t nbytes)
...
@@ -479,7 +479,7 @@ _PyCore_ObjectMalloc(size_t nbytes)
* With malloc, we can't avoid loosing one page address space
* With malloc, we can't avoid loosing one page address space
* per arena due to the required alignment on page boundaries.
* per arena due to the required alignment on page boundaries.
*/
*/
bp
=
(
block
*
)
Py
Core
_MALLOC
(
ARENA_SIZE
+
SYSTEM_PAGE_SIZE
);
bp
=
(
block
*
)
Py
Mem
_MALLOC
(
ARENA_SIZE
+
SYSTEM_PAGE_SIZE
);
if
(
bp
==
NULL
)
{
if
(
bp
==
NULL
)
{
UNLOCK
();
UNLOCK
();
goto
redirect
;
goto
redirect
;
...
@@ -510,13 +510,13 @@ _PyCore_ObjectMalloc(size_t nbytes)
...
@@ -510,13 +510,13 @@ _PyCore_ObjectMalloc(size_t nbytes)
* last chance to serve the request) or when the max memory limit
* last chance to serve the request) or when the max memory limit
* has been reached.
* has been reached.
*/
*/
return
(
void
*
)
Py
Core
_MALLOC
(
nbytes
);
return
(
void
*
)
Py
Mem
_MALLOC
(
nbytes
);
}
}
/* free */
/* free */
void
void
_Py
Core_Object
Free
(
void
*
p
)
_Py
Malloc_
Free
(
void
*
p
)
{
{
poolp
pool
;
poolp
pool
;
poolp
next
,
prev
;
poolp
next
,
prev
;
...
@@ -536,7 +536,7 @@ _PyCore_ObjectFree(void *p)
...
@@ -536,7 +536,7 @@ _PyCore_ObjectFree(void *p)
offset
=
(
off_t
)
p
&
POOL_SIZE_MASK
;
offset
=
(
off_t
)
p
&
POOL_SIZE_MASK
;
pool
=
(
poolp
)((
block
*
)
p
-
offset
);
pool
=
(
poolp
)((
block
*
)
p
-
offset
);
if
(
pool
->
pooladdr
!=
pool
||
pool
->
magic
!=
(
uint
)
POOL_MAGIC
)
{
if
(
pool
->
pooladdr
!=
pool
||
pool
->
magic
!=
(
uint
)
POOL_MAGIC
)
{
Py
Core
_FREE
(
p
);
Py
Mem
_FREE
(
p
);
return
;
return
;
}
}
...
@@ -595,7 +595,7 @@ _PyCore_ObjectFree(void *p)
...
@@ -595,7 +595,7 @@ _PyCore_ObjectFree(void *p)
/* realloc */
/* realloc */
void
*
void
*
_Py
Core_Object
Realloc
(
void
*
p
,
size_t
nbytes
)
_Py
Malloc_
Realloc
(
void
*
p
,
size_t
nbytes
)
{
{
block
*
bp
;
block
*
bp
;
poolp
pool
;
poolp
pool
;
...
@@ -607,7 +607,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
...
@@ -607,7 +607,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
#endif
#endif
if
(
p
==
NULL
)
if
(
p
==
NULL
)
return
_Py
Core_Object
Malloc
(
nbytes
);
return
_Py
Malloc_
Malloc
(
nbytes
);
/* realloc(p, 0) on big blocks is redirected. */
/* realloc(p, 0) on big blocks is redirected. */
pool
=
(
poolp
)((
block
*
)
p
-
((
off_t
)
p
&
POOL_SIZE_MASK
));
pool
=
(
poolp
)((
block
*
)
p
-
((
off_t
)
p
&
POOL_SIZE_MASK
));
...
@@ -618,7 +618,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
...
@@ -618,7 +618,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
size
=
nbytes
;
size
=
nbytes
;
goto
malloc_copy_free
;
goto
malloc_copy_free
;
}
}
bp
=
(
block
*
)
Py
Core
_REALLOC
(
p
,
nbytes
);
bp
=
(
block
*
)
Py
Mem
_REALLOC
(
p
,
nbytes
);
}
}
else
{
else
{
/* We're in charge of this block */
/* We're in charge of this block */
...
@@ -627,7 +627,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
...
@@ -627,7 +627,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
/* Don't bother if a smaller size was requested
/* Don't bother if a smaller size was requested
except for realloc(p, 0) == free(p), ret NULL */
except for realloc(p, 0) == free(p), ret NULL */
if
(
nbytes
==
0
)
{
if
(
nbytes
==
0
)
{
_Py
Core_Object
Free
(
p
);
_Py
Malloc_
Free
(
p
);
bp
=
NULL
;
bp
=
NULL
;
}
}
else
else
...
@@ -637,10 +637,10 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
...
@@ -637,10 +637,10 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
malloc_copy_free:
malloc_copy_free:
bp
=
(
block
*
)
_Py
Core_Object
Malloc
(
nbytes
);
bp
=
(
block
*
)
_Py
Malloc_
Malloc
(
nbytes
);
if
(
bp
!=
NULL
)
{
if
(
bp
!=
NULL
)
{
memcpy
(
bp
,
p
,
size
);
memcpy
(
bp
,
p
,
size
);
_Py
Core_Object
Free
(
p
);
_Py
Malloc_
Free
(
p
);
}
}
}
}
}
}
...
@@ -651,7 +651,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
...
@@ -651,7 +651,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
/* -- unused --
/* -- unused --
void *
void *
_Py
Core_Object
Calloc(size_t nbel, size_t elsz)
_Py
Malloc_
Calloc(size_t nbel, size_t elsz)
{
{
void *p;
void *p;
size_t nbytes;
size_t nbytes;
...
@@ -662,7 +662,7 @@ _PyCore_ObjectCalloc(size_t nbel, size_t elsz)
...
@@ -662,7 +662,7 @@ _PyCore_ObjectCalloc(size_t nbel, size_t elsz)
#endif
#endif
nbytes = nbel * elsz;
nbytes = nbel * elsz;
p = _Py
Core_Object
Malloc(nbytes);
p = _Py
Malloc_
Malloc(nbytes);
if (p != NULL)
if (p != NULL)
memset(p, 0, nbytes);
memset(p, 0, nbytes);
return p;
return p;
...
@@ -678,10 +678,10 @@ _PyCore_ObjectCalloc(size_t nbel, size_t elsz)
...
@@ -678,10 +678,10 @@ _PyCore_ObjectCalloc(size_t nbel, size_t elsz)
#ifdef WITH_MALLOC_HOOKS
#ifdef WITH_MALLOC_HOOKS
void
void
_Py
Core_Object
Malloc_SetHooks
(
void
*
(
*
malloc_func
)(
size_t
),
_PyMalloc_SetHooks
(
void
*
(
*
malloc_func
)(
size_t
),
void
*
(
*
calloc_func
)(
size_t
,
size_t
),
void
*
(
*
calloc_func
)(
size_t
,
size_t
),
void
*
(
*
realloc_func
)(
void
*
,
size_t
),
void
*
(
*
realloc_func
)(
void
*
,
size_t
),
void
(
*
free_func
)(
void
*
)
)
void
(
*
free_func
)(
void
*
)
)
{
{
LOCK
();
LOCK
();
malloc_hook
=
malloc_func
;
malloc_hook
=
malloc_func
;
...
@@ -692,10 +692,10 @@ _PyCore_ObjectMalloc_SetHooks( void *(*malloc_func)(size_t),
...
@@ -692,10 +692,10 @@ _PyCore_ObjectMalloc_SetHooks( void *(*malloc_func)(size_t),
}
}
void
void
_Py
Core_Object
Malloc_FetchHooks
(
void
*
(
**
malloc_funcp
)(
size_t
),
_PyMalloc_FetchHooks
(
void
*
(
**
malloc_funcp
)(
size_t
),
void
*
(
**
calloc_funcp
)(
size_t
,
size_t
),
void
*
(
**
calloc_funcp
)(
size_t
,
size_t
),
void
*
(
**
realloc_funcp
)(
void
*
,
size_t
),
void
*
(
**
realloc_funcp
)(
void
*
,
size_t
),
void
(
**
free_funcp
)(
void
*
)
)
void
(
**
free_funcp
)(
void
*
)
)
{
{
LOCK
();
LOCK
();
*
malloc_funcp
=
malloc_hook
;
*
malloc_funcp
=
malloc_hook
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment