Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2560cd

Browse files
committed
Move PyObject_Malloc and PyObject_Free here from object.c. Remove
PyMalloc_ prefix and use PyObject_ instead. I'm not sure about the debugging functions. Perhaps they should stay as PyMalloc_.
1 parent bdf0eed commit d2560cd

1 file changed

Lines changed: 33 additions & 61 deletions

File tree

Objects/obmalloc.c

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,9 @@ new_arena(void)
565565
* Unless the optimizer reorders everything, being too smart...
566566
*/
567567

568+
#undef PyObject_Malloc
568569
void *
569-
_PyMalloc_Malloc(size_t nbytes)
570+
PyObject_Malloc(size_t nbytes)
570571
{
571572
block *bp;
572573
poolp pool;
@@ -706,8 +707,9 @@ _PyMalloc_Malloc(size_t nbytes)
706707

707708
/* free */
708709

710+
#undef PyObject_Free
709711
void
710-
_PyMalloc_Free(void *p)
712+
PyObject_Free(void *p)
711713
{
712714
poolp pool;
713715
block *lastfree;
@@ -791,15 +793,16 @@ _PyMalloc_Free(void *p)
791793
* return a non-NULL result.
792794
*/
793795

796+
#undef PyObject_Realloc
794797
void *
795-
_PyMalloc_Realloc(void *p, size_t nbytes)
798+
PyObject_Realloc(void *p, size_t nbytes)
796799
{
797800
void *bp;
798801
poolp pool;
799802
uint size;
800803

801804
if (p == NULL)
802-
return _PyMalloc_Malloc(nbytes);
805+
return PyObject_Malloc(nbytes);
803806

804807
pool = POOL_ADDR(p);
805808
if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
@@ -811,18 +814,18 @@ _PyMalloc_Realloc(void *p, size_t nbytes)
811814
return p;
812815
/* We need more memory. */
813816
assert(nbytes != 0);
814-
bp = _PyMalloc_Malloc(nbytes);
817+
bp = PyObject_Malloc(nbytes);
815818
if (bp != NULL) {
816819
memcpy(bp, p, size);
817-
_PyMalloc_Free(p);
820+
PyObject_Free(p);
818821
}
819822
return bp;
820823
}
821824
/* We're not managing this block. */
822825
INCTHEIRS;
823826
if (nbytes <= SMALL_REQUEST_THRESHOLD) {
824827
/* Take over this block. */
825-
bp = _PyMalloc_Malloc(nbytes ? nbytes : 1);
828+
bp = PyObject_Malloc(nbytes ? nbytes : 1);
826829
if (bp != NULL) {
827830
memcpy(bp, p, nbytes);
828831
free(p);
@@ -845,59 +848,28 @@ _PyMalloc_Realloc(void *p, size_t nbytes)
845848
#else /* ! WITH_PYMALLOC */
846849

847850
/*==========================================================================*/
848-
/* pymalloc not enabled: Redirect the entry points to the PyMem family. */
851+
/* pymalloc not enabled: Redirect the entry points to malloc. These will
852+
* only be used by extensions that are compiled with pymalloc enabled. */
849853

850854
void *
851-
_PyMalloc_Malloc(size_t n)
855+
PyObject_Malloc(size_t n)
852856
{
853857
return PyMem_MALLOC(n);
854858
}
855859

856860
void *
857-
_PyMalloc_Realloc(void *p, size_t n)
861+
PyObject_Realloc(void *p, size_t n)
858862
{
859863
return PyMem_REALLOC(p, n);
860864
}
861865

862866
void
863-
_PyMalloc_Free(void *p)
867+
PyObject_Free(void *p)
864868
{
865869
PyMem_FREE(p);
866870
}
867871
#endif /* WITH_PYMALLOC */
868872

869-
/*==========================================================================*/
870-
/* Regardless of whether pymalloc is enabled, export entry points for
871-
* the object-oriented pymalloc functions.
872-
*/
873-
874-
PyObject *
875-
_PyMalloc_New(PyTypeObject *tp)
876-
{
877-
PyObject *op;
878-
op = (PyObject *) _PyMalloc_MALLOC(_PyObject_SIZE(tp));
879-
if (op == NULL)
880-
return PyErr_NoMemory();
881-
return PyObject_INIT(op, tp);
882-
}
883-
884-
PyVarObject *
885-
_PyMalloc_NewVar(PyTypeObject *tp, int nitems)
886-
{
887-
PyVarObject *op;
888-
const size_t size = _PyObject_VAR_SIZE(tp, nitems);
889-
op = (PyVarObject *) _PyMalloc_MALLOC(size);
890-
if (op == NULL)
891-
return (PyVarObject *)PyErr_NoMemory();
892-
return PyObject_INIT_VAR(op, tp, nitems);
893-
}
894-
895-
void
896-
_PyMalloc_Del(PyObject *op)
897-
{
898-
_PyMalloc_FREE(op);
899-
}
900-
901873
#ifdef PYMALLOC_DEBUG
902874
/*==========================================================================*/
903875
/* A x-platform debugging allocator. This doesn't manage memory directly,
@@ -955,22 +927,22 @@ p[4:8]
955927
p[8:8+n]
956928
The requested memory, filled with copies of PYMALLOC_CLEANBYTE.
957929
Used to catch reference to uninitialized memory.
958-
&p[8] is returned. Note that this is 8-byte aligned if PyMalloc
930+
&p[8] is returned. Note that this is 8-byte aligned if pymalloc
959931
handled the request itself.
960932
p[8+n:8+n+4]
961933
Copies of PYMALLOC_FORBIDDENBYTE. Used to catch over- writes
962934
and reads.
963935
p[8+n+4:8+n+8]
964-
A serial number, incremented by 1 on each call to _PyMalloc_DebugMalloc
965-
and _PyMalloc_DebugRealloc.
936+
A serial number, incremented by 1 on each call to _PyObject_DebugMalloc
937+
and _PyObject_DebugRealloc.
966938
4-byte unsigned integer, big-endian.
967939
If "bad memory" is detected later, the serial number gives an
968940
excellent way to set a breakpoint on the next run, to capture the
969941
instant at which this block was passed out.
970942
*/
971943

972944
void *
973-
_PyMalloc_DebugMalloc(size_t nbytes)
945+
_PyObject_DebugMalloc(size_t nbytes)
974946
{
975947
uchar *p; /* base address of malloc'ed block */
976948
uchar *tail; /* p + 8 + nbytes == pointer to tail pad bytes */
@@ -987,7 +959,7 @@ _PyMalloc_DebugMalloc(size_t nbytes)
987959
return NULL;
988960
}
989961

990-
p = _PyMalloc_Malloc(total);
962+
p = PyObject_Malloc(total);
991963
if (p == NULL)
992964
return NULL;
993965

@@ -1010,31 +982,31 @@ _PyMalloc_DebugMalloc(size_t nbytes)
1010982
Then calls the underlying free.
1011983
*/
1012984
void
1013-
_PyMalloc_DebugFree(void *p)
985+
_PyObject_DebugFree(void *p)
1014986
{
1015987
uchar *q = (uchar *)p;
1016988
size_t nbytes;
1017989

1018990
if (p == NULL)
1019991
return;
1020-
_PyMalloc_DebugCheckAddress(p);
992+
_PyObject_DebugCheckAddress(p);
1021993
nbytes = read4(q-8);
1022994
if (nbytes > 0)
1023995
memset(q, PYMALLOC_DEADBYTE, nbytes);
1024-
_PyMalloc_Free(q-8);
996+
PyObject_Free(q-8);
1025997
}
1026998

1027999
void *
1028-
_PyMalloc_DebugRealloc(void *p, size_t nbytes)
1000+
_PyObject_DebugRealloc(void *p, size_t nbytes)
10291001
{
10301002
uchar *q = (uchar *)p;
10311003
size_t original_nbytes;
10321004
void *fresh; /* new memory block, if needed */
10331005

10341006
if (p == NULL)
1035-
return _PyMalloc_DebugMalloc(nbytes);
1007+
return _PyObject_DebugMalloc(nbytes);
10361008

1037-
_PyMalloc_DebugCheckAddress(p);
1009+
_PyObject_DebugCheckAddress(p);
10381010
original_nbytes = read4(q-8);
10391011
if (nbytes == original_nbytes) {
10401012
/* note that this case is likely to be common due to the
@@ -1061,21 +1033,21 @@ _PyMalloc_DebugRealloc(void *p, size_t nbytes)
10611033
assert(nbytes != 0);
10621034
/* More memory is needed: get it, copy over the first original_nbytes
10631035
of the original data, and free the original memory. */
1064-
fresh = _PyMalloc_DebugMalloc(nbytes);
1036+
fresh = _PyObject_DebugMalloc(nbytes);
10651037
if (fresh != NULL) {
10661038
if (original_nbytes > 0)
10671039
memcpy(fresh, p, original_nbytes);
1068-
_PyMalloc_DebugFree(p);
1040+
_PyObject_DebugFree(p);
10691041
}
10701042
return fresh;
10711043
}
10721044

10731045
/* Check the forbidden bytes on both ends of the memory allocated for p.
1074-
* If anything is wrong, print info to stderr via _PyMalloc_DebugDumpAddress,
1046+
* If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress,
10751047
* and call Py_FatalError to kill the program.
10761048
*/
10771049
void
1078-
_PyMalloc_DebugCheckAddress(const void *p)
1050+
_PyObject_DebugCheckAddress(const void *p)
10791051
{
10801052
const uchar *q = (const uchar *)p;
10811053
char *msg;
@@ -1107,13 +1079,13 @@ _PyMalloc_DebugCheckAddress(const void *p)
11071079
return;
11081080

11091081
error:
1110-
_PyMalloc_DebugDumpAddress(p);
1082+
_PyObject_DebugDumpAddress(p);
11111083
Py_FatalError(msg);
11121084
}
11131085

11141086
/* Display info to stderr about the memory block at p. */
11151087
void
1116-
_PyMalloc_DebugDumpAddress(const void *p)
1088+
_PyObject_DebugDumpAddress(const void *p)
11171089
{
11181090
const uchar *q = (const uchar *)p;
11191091
const uchar *tail;
@@ -1236,7 +1208,7 @@ printone(const char* msg, ulong value)
12361208

12371209
/* Print summary info to stderr about the state of pymalloc's structures. */
12381210
void
1239-
_PyMalloc_DebugDumpStats(void)
1211+
_PyObject_DebugDumpStats(void)
12401212
{
12411213
uint i;
12421214
const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;

0 commit comments

Comments
 (0)