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

Skip to content

Commit 51e7f5c

Browse files
committed
Moving pymalloc along.
+ Redirect PyMem_{Del, DEL} to the object allocator's free() when pymalloc is enabled. Needed so old extensions can continue to mix PyObject_New with PyMem_DEL. + This implies that pgen needs to be able to see the PyObject_XYZ declarations too. pgenheaders.h now includes Python.h. An implication is that I expect obmalloc.o needs to get linked into pgen on non-Windows boxes. + When PYMALLOC_DEBUG is defined, *all* Py memory API functions now funnel through the debug allocator wrapper around pymalloc. This is the default in a debug build. + That caused compile.c to fail: it indirectly mixed PyMem_Malloc with raw platform free() in one place. This is verbotten.
1 parent a2bc259 commit 51e7f5c

3 files changed

Lines changed: 12 additions & 32 deletions

File tree

Include/pgenheaders.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,7 @@ extern "C" {
77

88
/* Include files and extern declarations used by most of the parser. */
99

10-
#include "pyconfig.h"
11-
12-
/* pyconfig.h may or may not define DL_IMPORT */
13-
#ifndef DL_IMPORT /* declarations for DLL import/export */
14-
#define DL_IMPORT(RTYPE) RTYPE
15-
#endif
16-
17-
#include <stdio.h>
18-
#include <string.h>
19-
20-
#ifdef HAVE_STDLIB_H
21-
#include <stdlib.h>
22-
#endif
23-
24-
#include "pymem.h"
25-
26-
#include "pydebug.h"
10+
#include "Python.h"
2711

2812
DL_IMPORT(void) PySys_WriteStdout(const char *format, ...)
2913
__attribute__((format(printf, 1, 2)));

Include/pymem.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,27 @@ extern DL_IMPORT(void) PyMem_Free(void *);
5252
no longer supported. They used to call PyErr_NoMemory() on failure. */
5353

5454
/* Macros. */
55-
#ifndef PyMem_MALLOC
55+
#ifdef PYMALLOC_DEBUG
56+
/* Redirect all memory operations to Python's debugging allocator. */
57+
#define PyMem_MALLOC PyObject_MALLOC
58+
#define PyMem_REALLOC PyObject_REALLOC
59+
#define PyMem_FREE PyObject_FREE
60+
61+
#else /* ! PYMALLOC_DEBUG */
62+
5663
#ifdef MALLOC_ZERO_RETURNS_NULL
5764
#define PyMem_MALLOC(n) malloc((n) ? (n) : 1)
5865
#else
5966
#define PyMem_MALLOC malloc
6067
#endif
61-
6268
/* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to
6369
do with whether platform realloc(non-NULL, 0) normally frees the memory
6470
or returns NULL. Rather than introduce yet another config variation,
6571
just make a realloc to 0 bytes act as if to 1 instead. */
6672
#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
6773

6874
#define PyMem_FREE free
69-
#endif /* PyMem_MALLOC */
75+
#endif /* PYMALLOC_DEBUG */
7076

7177
/*
7278
* Type-oriented memory interface
@@ -85,25 +91,15 @@ extern DL_IMPORT(void) PyMem_Free(void *);
8591
/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
8692
PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter
8793
have to be redirected to the object allocator. */
88-
/* XXX The parser module needs rework before this can be enabled. */
89-
#if 0
9094
#define PyMem_Del PyObject_Free
91-
#else
92-
#define PyMem_Del PyMem_Free
93-
#endif
9495

9596
/* Macros */
9697
#define PyMem_NEW(type, n) \
9798
( (type *) PyMem_MALLOC((n) * sizeof(type)) )
9899
#define PyMem_RESIZE(p, type, n) \
99100
( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
100101

101-
/* XXX The parser module needs rework before this can be enabled. */
102-
#if 0
103102
#define PyMem_DEL PyObject_FREE
104-
#else
105-
#define PyMem_DEL PyMem_FREE
106-
#endif
107103

108104
#ifdef __cplusplus
109105
}

Python/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,15 +1954,15 @@ com_factor(struct compiling *c, node *n)
19541954
return;
19551955
}
19561956
if (childtype == MINUS) {
1957-
char *s = malloc(strlen(STR(pnum)) + 2);
1957+
char *s = PyMem_Malloc(strlen(STR(pnum)) + 2);
19581958
if (s == NULL) {
19591959
com_error(c, PyExc_MemoryError, "");
19601960
com_addbyte(c, 255);
19611961
return;
19621962
}
19631963
s[0] = '-';
19641964
strcpy(s + 1, STR(pnum));
1965-
free(STR(pnum));
1965+
PyMem_Free(STR(pnum));
19661966
STR(pnum) = s;
19671967
}
19681968
com_atom(c, patom);

0 commit comments

Comments
 (0)