]> granicus.if.org Git - python/commitdiff
Moving pymalloc along.
authorTim Peters <tim.peters@gmail.com>
Mon, 22 Apr 2002 02:33:27 +0000 (02:33 +0000)
committerTim Peters <tim.peters@gmail.com>
Mon, 22 Apr 2002 02:33:27 +0000 (02:33 +0000)
+ 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.

Include/pgenheaders.h
Include/pymem.h
Python/compile.c

index 051173cc5eeb5b4fe07b90b7a1d9b580f21f2604..fecfcc1db56aa8833dd709908093e093679f2478 100644 (file)
@@ -7,23 +7,7 @@ extern "C" {
 
 /* Include files and extern declarations used by most of the parser. */
 
-#include "pyconfig.h"
-
-/* pyconfig.h may or may not define DL_IMPORT */
-#ifndef DL_IMPORT      /* declarations for DLL import/export */
-#define DL_IMPORT(RTYPE) RTYPE
-#endif
-
-#include <stdio.h>
-#include <string.h>
-
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
-
-#include "pymem.h"
-
-#include "pydebug.h"
+#include "Python.h"
 
 DL_IMPORT(void) PySys_WriteStdout(const char *format, ...)
                        __attribute__((format(printf, 1, 2)));
index 3b9c0e2988370dff33a98da84b1deda6f627a852..2330366f483070d3ba00c92611e6ac1a91094444 100644 (file)
@@ -52,13 +52,19 @@ extern DL_IMPORT(void) PyMem_Free(void *);
    no longer supported. They used to call PyErr_NoMemory() on failure. */
 
 /* Macros. */
-#ifndef PyMem_MALLOC
+#ifdef PYMALLOC_DEBUG
+/* Redirect all memory operations to Python's debugging allocator. */
+#define PyMem_MALLOC           PyObject_MALLOC
+#define PyMem_REALLOC          PyObject_REALLOC
+#define PyMem_FREE             PyObject_FREE
+
+#else  /* ! PYMALLOC_DEBUG */
+
 #ifdef MALLOC_ZERO_RETURNS_NULL
 #define PyMem_MALLOC(n)         malloc((n) ? (n) : 1)
 #else
 #define PyMem_MALLOC           malloc
 #endif
-
 /* Caution:  whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to
    do with whether platform realloc(non-NULL, 0) normally frees the memory
    or returns NULL.  Rather than introduce yet another config variation,
@@ -66,7 +72,7 @@ extern DL_IMPORT(void) PyMem_Free(void *);
 #define PyMem_REALLOC(p, n)     realloc((p), (n) ? (n) : 1)
 
 #define PyMem_FREE             free
-#endif /* PyMem_MALLOC */
+#endif /* PYMALLOC_DEBUG */
 
 /*
  * Type-oriented memory interface
@@ -85,12 +91,7 @@ extern DL_IMPORT(void) PyMem_Free(void *);
 /* In order to avoid breaking old code mixing PyObject_{New, NEW} with
    PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter
    have to be redirected to the object allocator. */
-/* XXX The parser module needs rework before this can be enabled. */
-#if 0
 #define PyMem_Del  PyObject_Free
-#else
-#define PyMem_Del  PyMem_Free
-#endif
 
 /* Macros */
 #define PyMem_NEW(type, n) \
@@ -98,12 +99,7 @@ extern DL_IMPORT(void) PyMem_Free(void *);
 #define PyMem_RESIZE(p, type, n) \
        ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
 
-/* XXX The parser module needs rework before this can be enabled. */
-#if 0
 #define PyMem_DEL PyObject_FREE
-#else
-#define PyMem_DEL  PyMem_FREE
-#endif
 
 #ifdef __cplusplus
 }
index 4340522fd881a67defb7bd858abb15e28ddfc2e3..373363fb68deff37608462f650230cec48166a8c 100644 (file)
@@ -1954,7 +1954,7 @@ com_factor(struct compiling *c, node *n)
                        return;
                }
                if (childtype == MINUS) {
-                       char *s = malloc(strlen(STR(pnum)) + 2);
+                       char *s = PyMem_Malloc(strlen(STR(pnum)) + 2);
                        if (s == NULL) {
                                com_error(c, PyExc_MemoryError, "");
                                com_addbyte(c, 255);
@@ -1962,7 +1962,7 @@ com_factor(struct compiling *c, node *n)
                        }
                        s[0] = '-';
                        strcpy(s + 1, STR(pnum));
-                       free(STR(pnum));
+                       PyMem_Free(STR(pnum));
                        STR(pnum) = s;
                }
                com_atom(c, patom);