[Python-checkins] r50891 - in python/branches/bcannon-sandboxing: Include/Python.h Include/objimpl.h Include/pystate.h Makefile.pre.in Modules/_sre.c Modules/datetimemodule.c Modules/gcmodule.c Modules/parsermodule.c Objects/bufferobject.c Objects/complexobject.c Objects/object.c Objects/obmalloc.c Objects/stringobject.c Objects/typeobject.c Objects/unicodeobject.c Python/pythonrun.c Python/sysmodule.c configure configure.in pyconfig.h.in

brett.cannon python-checkins at python.org
Fri Jul 28 04:38:45 CEST 2006


Author: brett.cannon
Date: Fri Jul 28 04:38:28 2006
New Revision: 50891

Modified:
   python/branches/bcannon-sandboxing/Include/Python.h
   python/branches/bcannon-sandboxing/Include/objimpl.h
   python/branches/bcannon-sandboxing/Include/pystate.h
   python/branches/bcannon-sandboxing/Makefile.pre.in
   python/branches/bcannon-sandboxing/Modules/_sre.c
   python/branches/bcannon-sandboxing/Modules/datetimemodule.c
   python/branches/bcannon-sandboxing/Modules/gcmodule.c
   python/branches/bcannon-sandboxing/Modules/parsermodule.c
   python/branches/bcannon-sandboxing/Objects/bufferobject.c
   python/branches/bcannon-sandboxing/Objects/complexobject.c
   python/branches/bcannon-sandboxing/Objects/object.c
   python/branches/bcannon-sandboxing/Objects/obmalloc.c
   python/branches/bcannon-sandboxing/Objects/stringobject.c
   python/branches/bcannon-sandboxing/Objects/typeobject.c
   python/branches/bcannon-sandboxing/Objects/unicodeobject.c
   python/branches/bcannon-sandboxing/Python/pythonrun.c
   python/branches/bcannon-sandboxing/Python/sysmodule.c
   python/branches/bcannon-sandboxing/configure
   python/branches/bcannon-sandboxing/configure.in
   python/branches/bcannon-sandboxing/pyconfig.h.in
Log:
Redo memory cap by starting from scratch by just trying to track memory usage
for the overall Python process.  Introduce PyObject_T_MALLOC() and friends that
take a const char * specifying what the memory is for.  Also convert
malloc()-like uses of PyObject_MALLOC() over to PyObject_Malloc() (for now).

Still have to add in hooks for several more memory APIs used by Python, but as
of now, no extra memory is magically tracked by hitting Enter at the interpreter
prompt.  Strings also seem to be tracking properly with interned strings and
new ones.


Modified: python/branches/bcannon-sandboxing/Include/Python.h
==============================================================================
--- python/branches/bcannon-sandboxing/Include/Python.h	(original)
+++ python/branches/bcannon-sandboxing/Include/Python.h	Fri Jul 28 04:38:28 2006
@@ -67,7 +67,7 @@
 /* Debug-mode build with pymalloc implies PYMALLOC_DEBUG.
  *  PYMALLOC_DEBUG is in error if pymalloc is not in use.
  */
-#if defined(Py_DEBUG) && defined(WITH_PYMALLOC) && !defined(PYMALLOC_DEBUG) && !defined(Py_MEMORY_CAP)
+#if defined(Py_DEBUG) && defined(WITH_PYMALLOC) && !defined(PYMALLOC_DEBUG)
 #define PYMALLOC_DEBUG
 #endif
 #if defined(PYMALLOC_DEBUG) && !defined(WITH_PYMALLOC)

Modified: python/branches/bcannon-sandboxing/Include/objimpl.h
==============================================================================
--- python/branches/bcannon-sandboxing/Include/objimpl.h	(original)
+++ python/branches/bcannon-sandboxing/Include/objimpl.h	Fri Jul 28 04:38:28 2006
@@ -94,10 +94,17 @@
    the object gets initialized via PyObject_{Init, InitVar} after obtaining
    the raw memory.
 */
+PyAPI_DATA(unsigned long) Py_ProcessMemUsage;
+PyAPI_FUNC(int) PyMalloc_ManagesMemory(void *);
+PyAPI_FUNC(size_t) PyMalloc_AllocatedSize(void *);
 PyAPI_FUNC(void *) PyObject_Malloc(size_t);
 PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t);
 PyAPI_FUNC(void) PyObject_Free(void *);
 
+PyAPI_FUNC(void *) PyObject_TrackedMalloc(const char *, size_t);
+PyAPI_FUNC(void *) PyObject_TrackedRealloc(const char *, void *, size_t);
+PyAPI_FUNC(void) PyObject_TrackedFree(const char *, void *);
+
 
 /* Macros */
 #ifdef WITH_PYMALLOC
@@ -128,15 +135,22 @@
 
 #endif	/* WITH_PYMALLOC */
 
-#ifdef Py_MEMORY_CAP
+#ifdef Py_TRACK_MEMORY
 PyAPI_FUNC(void) _PyObject_Del(void *);
 #define PyObject_Del		_PyObject_Del
 #define PyObject_DEL		_PyObject_Del
-#else /* !Py_MEMORY_CAP */
+#define PyObject_T_MALLOC	PyObject_TrackedMalloc
+#define PyObject_T_REALLOC	PyObject_TrackedRealloc
+#define PyObject_T_FREE		PyObject_TrackedFree
+#else /* !Py_TRACK_MEMORY */
 #define PyObject_Del		PyObject_Free
 #define _PyObject_Del		PyObject_Free
 #define PyObject_DEL		PyObject_FREE
-#endif  /* Py_MEMORY_CAP */
+#define PyObject_T_MALLOC(what, size)	PyObject_MALLOC(size)
+#define PyObject_T_REALLOC(what, who, size)	PyObject_REALLOC(who, size)
+#define PyObject_T_FREE(what, who)	PyObject_FREE(who)
+
+#endif  /* Py_TRACK_MEMORY */
 /* for source compatibility with 2.2 */
 
 /*
@@ -187,6 +201,21 @@
 	  ) & ~(SIZEOF_VOID_P - 1)		\
 	)
 
+#ifdef Py_TRACK_MEMORY
+
+#define PyObject_NEW(type, typeobj) \
+( (type *) PyObject_Init( \
+	(PyObject *) PyObject_T_MALLOC((typeobj)->tp_name, \
+				       _PyObject_SIZE(typeobj) ), (typeobj)) )
+
+#define PyObject_NEW_VAR(type, typeobj, n) \
+( (type *) PyObject_InitVar( \
+      (PyVarObject *) PyObject_T_MALLOC((typeobj)->tp_name, \
+					_PyObject_VAR_SIZE((typeobj),(n)) ),\
+      (typeobj), (n)) )
+
+#else
+
 #define PyObject_NEW(type, typeobj) \
 ( (type *) PyObject_Init( \
 	(PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
@@ -195,6 +224,7 @@
 ( (type *) PyObject_InitVar( \
       (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
       (typeobj), (n)) )
+#endif /* Py_TRACK_MEMORY */
 
 /* This example code implements an object constructor with a custom
    allocator, where PyObject_New is inlined, and shows the important

Modified: python/branches/bcannon-sandboxing/Include/pystate.h
==============================================================================
--- python/branches/bcannon-sandboxing/Include/pystate.h	(original)
+++ python/branches/bcannon-sandboxing/Include/pystate.h	Fri Jul 28 04:38:28 2006
@@ -32,11 +32,6 @@
 #ifdef WITH_TSC
     int tscdump;
 #endif
-#ifdef Py_MEMORY_CAP
-    PY_LONG_LONG mem_cap;
-    PY_LONG_LONG mem_usage;
-#endif
-
 } PyInterpreterState;
 
 

Modified: python/branches/bcannon-sandboxing/Makefile.pre.in
==============================================================================
--- python/branches/bcannon-sandboxing/Makefile.pre.in	(original)
+++ python/branches/bcannon-sandboxing/Makefile.pre.in	Fri Jul 28 04:38:28 2006
@@ -308,6 +308,7 @@
 		Objects/sliceobject.o \
 		Objects/stringobject.o \
 		Objects/structseq.o \
+		Objects/trackedmalloc.o \
 		Objects/tupleobject.o \
 		Objects/typeobject.o \
 		Objects/weakrefobject.o \

Modified: python/branches/bcannon-sandboxing/Modules/_sre.c
==============================================================================
--- python/branches/bcannon-sandboxing/Modules/_sre.c	(original)
+++ python/branches/bcannon-sandboxing/Modules/_sre.c	Fri Jul 28 04:38:28 2006
@@ -1165,7 +1165,7 @@
                    ctx->pattern[1], ctx->pattern[2]));
 
             /* install new repeat context */
-            ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
+            ctx->u.rep = (SRE_REPEAT*) PyObject_T_MALLOC("sre.*", sizeof(*ctx->u.rep));
             ctx->u.rep->count = -1;
             ctx->u.rep->pattern = ctx->pattern;
             ctx->u.rep->prev = state->repeat;
@@ -1175,7 +1175,7 @@
             state->ptr = ctx->ptr;
             DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
             state->repeat = ctx->u.rep->prev;
-            PyObject_FREE(ctx->u.rep);
+            PyObject_T_FREE("sre.*", ctx->u.rep);
 
             if (ret) {
                 RETURN_ON_ERROR(ret);

Modified: python/branches/bcannon-sandboxing/Modules/datetimemodule.c
==============================================================================
--- python/branches/bcannon-sandboxing/Modules/datetimemodule.c	(original)
+++ python/branches/bcannon-sandboxing/Modules/datetimemodule.c	Fri Jul 28 04:38:28 2006
@@ -601,7 +601,7 @@
 	PyObject *self;
 
 	self = (PyObject *)
-		PyObject_MALLOC(aware ?
+		PyObject_T_MALLOC("datetime.time", aware ?
 				sizeof(PyDateTime_Time) :
 				sizeof(_PyDateTime_BaseTime));
 	if (self == NULL)
@@ -616,7 +616,7 @@
 	PyObject *self;
 
 	self = (PyObject *)
-		PyObject_MALLOC(aware ?
+		PyObject_T_MALLOC("datetime.datetime", aware ?
 				sizeof(PyDateTime_DateTime) :
 				sizeof(_PyDateTime_BaseDateTime));
 	if (self == NULL)

Modified: python/branches/bcannon-sandboxing/Modules/gcmodule.c
==============================================================================
--- python/branches/bcannon-sandboxing/Modules/gcmodule.c	(original)
+++ python/branches/bcannon-sandboxing/Modules/gcmodule.c	Fri Jul 28 04:38:28 2006
@@ -1341,12 +1341,9 @@
 _PyObject_GC_New(PyTypeObject *tp)
 {
 	PyObject *op = NULL;
+	size_t obj_size  = _PyObject_SIZE(tp);
 
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddObjectMem(tp))
-	    return NULL;
-#endif
-	op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
+	op = _PyObject_GC_Malloc(obj_size);
 	if (op != NULL)
 		op = PyObject_INIT(op, tp);
 	return op;
@@ -1358,10 +1355,6 @@
 	const size_t size = _PyObject_VAR_SIZE(tp, nitems);
 	PyVarObject *op = NULL;
 
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddVarObjectMem(tp, nitems))
-	    return NULL;
-#endif
 	op = (PyVarObject *) _PyObject_GC_Malloc(size);
 	if (op != NULL)
 		op = PyObject_INIT_VAR(op, tp, nitems);
@@ -1390,9 +1383,6 @@
 	if (generations[0].count > 0) {
 		generations[0].count--;
 	}
-#ifdef Py_MEMORY_CAP
-	PyInterpreterState_RemoveObjectMem((PyObject *)op);
-#endif
 	PyObject_FREE(g);
 }
 

Modified: python/branches/bcannon-sandboxing/Modules/parsermodule.c
==============================================================================
--- python/branches/bcannon-sandboxing/Modules/parsermodule.c	(original)
+++ python/branches/bcannon-sandboxing/Modules/parsermodule.c	Fri Jul 28 04:38:28 2006
@@ -701,7 +701,7 @@
                 }
             }
             len = PyString_GET_SIZE(temp) + 1;
-            strn = (char *)PyObject_MALLOC(len);
+            strn = (char *)PyObject_Malloc(len);
             if (strn != NULL)
                 (void) memcpy(strn, PyString_AS_STRING(temp), len);
             Py_DECREF(temp);
@@ -719,11 +719,11 @@
         }
         err = PyNode_AddChild(root, type, strn, *line_num, 0);
         if (err == E_NOMEM) {
-            PyObject_FREE(strn);
+            PyObject_T_FREE("str", strn);
             return (node *) PyErr_NoMemory();
         }
         if (err == E_OVERFLOW) {
-            PyObject_FREE(strn);
+            PyObject_Free(strn);
             PyErr_SetString(PyExc_ValueError,
                             "unsupported number of child nodes");
             return NULL;
@@ -787,7 +787,7 @@
             if (res && encoding) {
                 Py_ssize_t len;
                 len = PyString_GET_SIZE(encoding) + 1;
-                res->n_str = (char *)PyObject_MALLOC(len);
+                res->n_str = (char *)PyObject_Malloc(len);
                 if (res->n_str != NULL)
                     (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
                 Py_DECREF(encoding);

Modified: python/branches/bcannon-sandboxing/Objects/bufferobject.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/bufferobject.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/bufferobject.c	Fri Jul 28 04:38:28 2006
@@ -209,12 +209,7 @@
 	}
 	/* XXX: check for overflow in multiply */
 	/* Inline PyObject_New */
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddRawMem("buffer",
-					    sizeof(*b) + size))
-	    return PyErr_NoMemory();
-#endif
-	o = (PyObject *)PyObject_MALLOC(sizeof(*b) + size);
+	o = (PyObject *)PyObject_T_MALLOC("buffer", sizeof(*b) + size);
 	if ( o == NULL )
 		return PyErr_NoMemory();
 	b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type);

Modified: python/branches/bcannon-sandboxing/Objects/complexobject.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/complexobject.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/complexobject.c	Fri Jul 28 04:38:28 2006
@@ -200,11 +200,8 @@
 	register PyComplexObject *op;
 
 	/* Inline PyObject_New */
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddRawMem("complex number", sizeof(PyComplexObject)))
-		return PyErr_NoMemory();
-#endif
-	op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
+	op = (PyComplexObject *) PyObject_T_MALLOC("complex",
+						    sizeof(PyComplexObject));
 	if (op == NULL)
 		return PyErr_NoMemory();
 	PyObject_INIT(op, &PyComplex_Type);

Modified: python/branches/bcannon-sandboxing/Objects/object.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/object.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/object.c	Fri Jul 28 04:38:28 2006
@@ -236,12 +236,7 @@
 	PyObject *op;
 	size_t tp_size = _PyObject_SIZE(tp);
 
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddObjectMem(tp))
-	    return PyErr_NoMemory();
-#endif
-
-	op = (PyObject *) PyObject_MALLOC(tp_size);
+	op = (PyObject *) PyObject_T_MALLOC(tp->tp_name, tp_size);
 	if (op == NULL)
 		return PyErr_NoMemory();
 	return PyObject_INIT(op, tp);
@@ -253,12 +248,7 @@
 	PyVarObject *op;
 	const size_t size = _PyObject_VAR_SIZE(tp, nitems);
 
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddVarObjectMem(tp, nitems))
-	    return (PyVarObject *)PyErr_NoMemory();
-#endif
-
-	op = (PyVarObject *) PyObject_MALLOC(size);
+	op = (PyVarObject *) PyObject_T_MALLOC(tp->tp_name, size);
 	if (op == NULL)
 		return (PyVarObject *)PyErr_NoMemory();
 	return PyObject_INIT_VAR(op, tp, nitems);
@@ -266,13 +256,15 @@
 
 /* for binary compatibility with 2.2. */
 #undef _PyObject_Del
+/*
+   Assume that argument is PyObject *!!!
+*/
 void
 _PyObject_Del(void *op)
 {
-#ifdef Py_MEMORY_CAP
-    PyInterpreterState_RemoveObjectMem((PyObject *)op);
-#endif
-	PyObject_FREE(op);
+    PyObject *obj_ptr = (PyObject *)op;
+
+    PyObject_T_FREE(obj_ptr->ob_type->tp_name, op);
 }
 
 /* Implementation of PyObject_Print with recursion checking */

Modified: python/branches/bcannon-sandboxing/Objects/obmalloc.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/obmalloc.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/obmalloc.c	Fri Jul 28 04:38:28 2006
@@ -702,6 +702,35 @@
 #undef Py_NO_INLINE
 #endif
 
+/*
+   Return the amount of memory allocated for pointer if created by pymalloc,
+   else return 0.
+*/
+int
+PyMalloc_ManagesMemory(void *ptr)
+{
+    poolp pool = NULL;
+
+    if (!ptr)
+	return 0;
+    
+    pool = POOL_ADDR(ptr);
+
+    if (Py_ADDRESS_IN_RANGE(ptr, pool))
+	return 1;
+    else
+	return 0;
+}
+
+size_t
+PyMalloc_AllocatedSize(void *ptr)
+{
+    poolp pool = POOL_ADDR(ptr);
+
+    return INDEX2SIZE(pool->szidx);
+}
+
+
 /*==========================================================================*/
 
 /* malloc.  Note that nbytes==0 tries to return a non-NULL pointer, distinct

Modified: python/branches/bcannon-sandboxing/Objects/stringobject.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/stringobject.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/stringobject.c	Fri Jul 28 04:38:28 2006
@@ -72,11 +72,7 @@
 	}
 
 	/* Inline PyObject_NewVar */
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddRawMem("str", sizeof(PyStringObject) + size))
-	    return PyErr_NoMemory();
-#endif
-	op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
+	op = (PyStringObject *)PyObject_T_MALLOC("str", sizeof(PyStringObject) + size);
 	if (op == NULL)
 		return PyErr_NoMemory();
 	PyObject_INIT_VAR(op, &PyString_Type, size);
@@ -131,11 +127,7 @@
 	}
 
 	/* Inline PyObject_NewVar */
-#ifdef Py_MEMORY_CAP
-	 if (!PyInterpreterState_AddRawMem("str", sizeof(PyStringObject) + size))
-	     return PyErr_NoMemory();
-#endif
-	op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
+	op = (PyStringObject *)PyObject_T_MALLOC("str", sizeof(PyStringObject) + size);
 	if (op == NULL)
 		return PyErr_NoMemory();
 	PyObject_INIT_VAR(op, &PyString_Type, size);
@@ -968,11 +960,7 @@
 	}
 	  
 	/* Inline PyObject_NewVar */
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddRawMem("str", sizeof(PyStringObject) + size))
-	    return PyErr_NoMemory();
-#endif
-	op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
+	op = (PyStringObject *)PyObject_T_MALLOC("str", sizeof(PyStringObject) + size);
 	if (op == NULL)
 		return PyErr_NoMemory();
 	PyObject_INIT_VAR(op, &PyString_Type, size);
@@ -1014,12 +1002,8 @@
 			"repeated string is too long");
 		return NULL;
 	}
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddRawMem("str", sizeof(PyStringObject) + nbytes))
-	    return PyErr_NoMemory();
-#endif
 	op = (PyStringObject *)
-		PyObject_MALLOC(sizeof(PyStringObject) + nbytes);
+		PyObject_T_MALLOC("str", sizeof(PyStringObject) + nbytes);
 	if (op == NULL)
 		return PyErr_NoMemory();
 	PyObject_INIT_VAR(op, &PyString_Type, size);
@@ -4123,7 +4107,8 @@
 	_Py_DEC_REFTOTAL;
 	_Py_ForgetReference(v);
 	*pv = (PyObject *)
-		PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize);
+		PyObject_T_REALLOC("str", (char *)v,
+				    sizeof(PyStringObject) + newsize);
 	if (*pv == NULL) {
 		PyObject_Del(v);
 		PyErr_NoMemory();

Modified: python/branches/bcannon-sandboxing/Objects/typeobject.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/typeobject.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/typeobject.c	Fri Jul 28 04:38:28 2006
@@ -450,14 +450,10 @@
 	const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
 	/* note that we need to add one, for the sentinel */
 
-#ifdef Py_MEMORY_CAP
-	if (!PyInterpreterState_AddVarObjectMem(type, nitems))
-	    return PyErr_NoMemory();
-#endif
 	if (PyType_IS_GC(type))
 		obj = _PyObject_GC_Malloc(size);
 	else
-		obj = (PyObject *)PyObject_MALLOC(size);
+		obj = (PyObject *)PyObject_Malloc(size);
 
 	if (obj == NULL)
 		return PyErr_NoMemory();
@@ -1896,13 +1892,7 @@
 		PyObject *doc = PyDict_GetItemString(dict, "__doc__");
 		if (doc != NULL && PyString_Check(doc)) {
 			const size_t n = (size_t)PyString_GET_SIZE(doc);
-#ifdef Py_MEMORY_CAP
-			if (!PyInterpreterState_AddRawMem("str", n)) {
-			    Py_DECREF(type);
-			    return NULL;
-			}
-#endif
-                        char *tp_doc = (char *)PyObject_MALLOC(n+1);
+                        char *tp_doc = (char *)PyObject_Malloc(n+1);
 			if (tp_doc == NULL) {
 				Py_DECREF(type);
 				return NULL;
@@ -2155,7 +2145,7 @@
         /* A type's tp_doc is heap allocated, unlike the tp_doc slots
          * of most other objects.  It's okay to cast it to char *.
          */
-	PyObject_FREE((char *)type->tp_doc);
+	PyObject_Free((char *)type->tp_doc);
 	Py_XDECREF(et->ht_name);
 	Py_XDECREF(et->ht_slots);
 	type->ob_type->tp_free((PyObject *)type);

Modified: python/branches/bcannon-sandboxing/Objects/unicodeobject.c
==============================================================================
--- python/branches/bcannon-sandboxing/Objects/unicodeobject.c	(original)
+++ python/branches/bcannon-sandboxing/Objects/unicodeobject.c	Fri Jul 28 04:38:28 2006
@@ -3219,7 +3219,7 @@
 static void
 encoding_map_dealloc(PyObject* o)
 {
-	PyObject_FREE(o);
+	PyObject_T_FREE("<internal unicode>", o);
 }
 
 static PyTypeObject EncodingMapType = {
@@ -3342,7 +3342,7 @@
     }
 
     /* Create a three-level trie */
-    result = PyObject_MALLOC(sizeof(struct encoding_map) +
+    result = PyObject_T_MALLOC("<internal unicode>", sizeof(struct encoding_map) +
                              16*count2 + 128*count3 - 1);
     if (!result)
         return PyErr_NoMemory();

Modified: python/branches/bcannon-sandboxing/Python/pythonrun.c
==============================================================================
--- python/branches/bcannon-sandboxing/Python/pythonrun.c	(original)
+++ python/branches/bcannon-sandboxing/Python/pythonrun.c	Fri Jul 28 04:38:28 2006
@@ -35,10 +35,20 @@
 #define PRINT_TOTAL_REFS()
 #else /* Py_REF_DEBUG */
 #define PRINT_TOTAL_REFS() fprintf(stderr,				\
-				   "[%" PY_FORMAT_SIZE_T "d refs, %lld memory]\n",	\
-				   _Py_GetRefTotal(), PyThreadState_Get()->interp->mem_usage)
+				   "[%" PY_FORMAT_SIZE_T "d refs]\n",	\
+				   _Py_GetRefTotal())
 #endif
 
+#ifdef Py_TRACK_MEMORY
+#define PRINT_TOTAL_MEM() fprintf(stderr, \
+				  "[%lu bytes used]\n", \
+				  Py_ProcessMemUsage)
+#else
+#define PRINT_TOTAL_MEM()
+#endif /* Py_TRACK_MEMORY */
+
+#define PRINT_STATE_DATA() PRINT_TOTAL_REFS(); PRINT_TOTAL_MEM()
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -413,7 +423,7 @@
 	dump_counts(stdout);
 #endif
 
-	PRINT_TOTAL_REFS();
+	PRINT_STATE_DATA();
 
 #ifdef Py_TRACE_REFS
 	/* Display all objects still alive -- this can invoke arbitrary
@@ -703,7 +713,7 @@
 	}
 	for (;;) {
 		ret = PyRun_InteractiveOneFlags(fp, filename, flags);
-		PRINT_TOTAL_REFS();
+		PRINT_STATE_DATA();
 		if (ret == E_EOF)
 			return 0;
 		/*
@@ -1483,7 +1493,7 @@
 	v = Py_BuildValue("(ziiz)", err->filename,
 			  err->lineno, err->offset, err->text);
 	if (err->text != NULL) {
-		PyObject_FREE(err->text);
+		PyObject_Free(err->text);
 		err->text = NULL;
 	}
 	w = NULL;

Modified: python/branches/bcannon-sandboxing/Python/sysmodule.c
==============================================================================
--- python/branches/bcannon-sandboxing/Python/sysmodule.c	(original)
+++ python/branches/bcannon-sandboxing/Python/sysmodule.c	Fri Jul 28 04:38:28 2006
@@ -700,64 +700,6 @@
 10. Number of stack pops performed by call_function()"
 );
 
-#ifdef Py_MEMORY_CAP
-static PyObject *
-sys_setmemorycap(PyObject *self, PyObject *arg)
-{
-    PyInterpreterState *interp = PyInterpreterState_SafeGet();
-    PY_LONG_LONG new_memory_cap;
-    PyObject *arg_as_long = PyNumber_Long(arg);
-
-    if (!arg_as_long)
-	return NULL;
-
-    new_memory_cap = PyLong_AsLongLong(arg_as_long);
-    Py_DECREF(arg_as_long); /* DEAD: arg_as_long */
-
-    if (!interp)
-	Py_FatalError("interpreter not available");
-
-    if (!PyInterpreterState_SetMemoryCap(interp, new_memory_cap))
-	return NULL;
-
-    Py_RETURN_NONE;
-}
-
-PyDoc_STRVAR(setmemorycap_doc,
-"XXX"
-);
-
-static PyObject *
-sys_getmemorycap(PyObject *self, PyObject *ignore)
-{
-    PyInterpreterState *interp = PyInterpreterState_SafeGet();
-
-    if (!interp)
-	Py_FatalError("interpreter not available");
-
-    return PyLong_FromLongLong(interp->mem_cap);
-}
-
-PyDoc_STRVAR(getmemorycap_doc,
-"XXX"
-);
-
-static PyObject *
-sys_getmemoryused(PyObject *self, PyObject *ignore)
-{
-    PyInterpreterState *interp = PyInterpreterState_SafeGet();
-
-    if (!interp)
-	Py_FatalError("interpreter not available");
-
-    return PyLong_FromLongLong(interp->mem_usage);
-}
-
-PyDoc_STRVAR(getmemoryused_doc,
-"XXX"
-);
-#endif /* Py_MEMORY_CAP */
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -840,11 +782,6 @@
 #endif
 	{"settrace",	sys_settrace, METH_O, settrace_doc},
 	{"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
-#ifdef Py_MEMORY_CAP
-	{"setmemorycap", sys_setmemorycap, METH_O, setmemorycap_doc},
-	{"getmemorycap", sys_getmemorycap, METH_NOARGS, getmemorycap_doc},
-	{"getmemoryused", sys_getmemoryused, METH_NOARGS, getmemoryused_doc},
-#endif 
 	{NULL,		NULL}		/* sentinel */
 };
 

Modified: python/branches/bcannon-sandboxing/configure
==============================================================================
--- python/branches/bcannon-sandboxing/configure	(original)
+++ python/branches/bcannon-sandboxing/configure	Fri Jul 28 04:38:28 2006
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.in Revision: 50540 .
+# From configure.in Revision: 50730 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.59 for python 2.5.
 #
@@ -866,7 +866,7 @@
                           compiler
   --with-suffix=.exe      set executable suffix
   --with-pydebug          build with Py_DEBUG defined
-  --with-memory-cap       build with Py_MEMORY_CAP defined
+  --with-memory-tracking  build with Py_TRACK_MEMORY defined
   --with-libs='lib1 ...'  link against additional libs
   --with-system-ffi       build _ctypes module using an installed ffi library
   --with-signal-module    disable/enable signal module
@@ -3765,26 +3765,26 @@
 echo "${ECHO_T}no" >&6
 fi;
 
-# Check for --with-memory-cap
-echo "$as_me:$LINENO: checking for --with-memory-cap" >&5
-echo $ECHO_N "checking for --with-memory-cap... $ECHO_C" >&6
-
-# Check whether --with-memory-cap or --without-memory-cap was given.
-if test "${with_memory_cap+set}" = set; then
-  withval="$with_memory_cap"
+# Check for --with-memory-tracking
+echo "$as_me:$LINENO: checking for --with-memory-tracking" >&5
+echo $ECHO_N "checking for --with-memory-tracking... $ECHO_C" >&6
+
+# Check whether --with-memory-tracking or --without-memory-tracking was given.
+if test "${with_memory_tracking+set}" = set; then
+  withval="$with_memory_tracking"
 
 if test "$withval" != no
 then
 
 cat >>confdefs.h <<\_ACEOF
-#define Py_MEMORY_CAP 1
+#define Py_TRACK_MEMORY 1
 _ACEOF
 
   echo "$as_me:$LINENO: result: yes" >&5
 echo "${ECHO_T}yes" >&6;
-  Py_MEMORY_CAP='true'
+  Py_TRACK_MEMORY='true'
 else echo "$as_me:$LINENO: result: no" >&5
-echo "${ECHO_T}no" >&6; Py_MEMORY_CAP='false'
+echo "${ECHO_T}no" >&6; Py_TRACK_MEMORY='false'
 fi
 else
   echo "$as_me:$LINENO: result: no" >&5
@@ -20455,6 +20455,70 @@
 
 fi
 
+echo "$as_me:$LINENO: checking for good malloc_usable_size()" >&5
+echo $ECHO_N "checking for good malloc_usable_size()... $ECHO_C" >&6
+if test "${ac_cv_good_malloc_usable_size+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+
+if test "$cross_compiling" = yes; then
+  ac_cv_good_malloc_usable_size=no
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+#include <stdlib.h>
+#include <malloc.h>
+int
+main()
+{
+	void *p = malloc(8);
+	if (malloc_usable_size(p) <= 16)
+		exit(0);
+	exit(1);
+}
+
+_ACEOF
+rm -f conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && { ac_try='./conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_good_malloc_usable_size=yes
+else
+  echo "$as_me: program exited with status $ac_status" >&5
+echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+( exit $ac_status )
+ac_cv_good_malloc_usable_size=no
+fi
+rm -f core *.core gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
+fi
+fi
+
+echo "$as_me:$LINENO: result: $ac_cv_good_malloc_usable_size" >&5
+echo "${ECHO_T}$ac_cv_good_malloc_usable_size" >&6
+if test "$ac_cv_good_malloc_usable_size" = yes
+then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_MALLOC_USABLE_SIZE 1
+_ACEOF
+
+fi
+
+
 # check where readline lives
 # save the value of LIBS so we don't actually link Python with readline
 LIBS_no_readline=$LIBS

Modified: python/branches/bcannon-sandboxing/configure.in
==============================================================================
--- python/branches/bcannon-sandboxing/configure.in	(original)
+++ python/branches/bcannon-sandboxing/configure.in	Fri Jul 28 04:38:28 2006
@@ -725,18 +725,18 @@
 fi],
 [AC_MSG_RESULT(no)])
 
-# Check for --with-memory-cap
-AC_MSG_CHECKING(for --with-memory-cap)
-AC_ARG_WITH(memory-cap, 
-            AC_HELP_STRING(--with-memory-cap, build with Py_MEMORY_CAP defined),
+# Check for --with-memory-tracking
+AC_MSG_CHECKING(for --with-memory-tracking)
+AC_ARG_WITH(memory-tracking, 
+            AC_HELP_STRING(--with-memory-tracking, build with Py_TRACK_MEMORY defined),
 [
 if test "$withval" != no
 then 
-  AC_DEFINE(Py_MEMORY_CAP, 1, 
-  [Define if you want to build an interpreter that can cap memory usage.]) 
+  AC_DEFINE(Py_TRACK_MEMORY, 1, 
+  [Define if you want to build an interpreter that tracks memory usage.]) 
   AC_MSG_RESULT(yes); 
-  Py_MEMORY_CAP='true'
-else AC_MSG_RESULT(no); Py_MEMORY_CAP='false'
+  Py_TRACK_MEMORY='true'
+else AC_MSG_RESULT(no); Py_TRACK_MEMORY='false'
 fi],
 [AC_MSG_RESULT(no)])
 
@@ -3055,6 +3055,31 @@
   [Define this if you have flockfile(), getc_unlocked(), and funlockfile()])
 fi
 
+AC_MSG_CHECKING(for good malloc_usable_size())
+AC_CACHE_VAL(ac_cv_good_malloc_usable_size, [
+AC_TRY_RUN([
+#include <stdlib.h>
+#include <malloc.h>
+int
+main()
+{
+	void *p = malloc(8);
+	if (malloc_usable_size(p) <= 16)
+		exit(0);
+	exit(1);
+}
+],
+ac_cv_good_malloc_usable_size=yes,
+ac_cv_good_malloc_usable_size=no,
+ac_cv_good_malloc_usable_size=no)])
+AC_MSG_RESULT($ac_cv_good_malloc_usable_size)
+if test "$ac_cv_good_malloc_usable_size" = yes
+then
+  AC_DEFINE(HAVE_MALLOC_USABLE_SIZE, 1,
+  [Define if malloc_usable_size() exists and works.])
+fi
+
+
 # check where readline lives
 # save the value of LIBS so we don't actually link Python with readline
 LIBS_no_readline=$LIBS

Modified: python/branches/bcannon-sandboxing/pyconfig.h.in
==============================================================================
--- python/branches/bcannon-sandboxing/pyconfig.h.in	(original)
+++ python/branches/bcannon-sandboxing/pyconfig.h.in	Fri Jul 28 04:38:28 2006
@@ -326,6 +326,9 @@
 /* Define this if you have the makedev macro. */
 #undef HAVE_MAKEDEV
 
+/* Define if malloc_usable_size() exists and works. */
+#undef HAVE_MALLOC_USABLE_SIZE
+
 /* Define to 1 if you have the `memmove' function. */
 #undef HAVE_MEMMOVE
 
@@ -772,8 +775,8 @@
 /* Defined if Python is built as a shared library. */
 #undef Py_ENABLE_SHARED
 
-/* Define if you want to build an interpreter that can cap memory usage. */
-#undef Py_MEMORY_CAP
+/* Define if you want to build an interpreter that tracks memory usage. */
+#undef Py_TRACK_MEMORY
 
 /* Define as the size of the unicode type. */
 #undef Py_UNICODE_SIZE


More information about the Python-checkins mailing list