Minor code cleanup -- no semantic changes.

This commit is contained in:
Tim Peters 2002-03-23 22:28:18 +00:00
parent 194152845c
commit 62c06ba6a9

View file

@ -627,6 +627,7 @@ _PyMalloc_Realloc(void *p, size_t nbytes)
/*==========================================================================*/ /*==========================================================================*/
/* pymalloc not enabled: Redirect the entry points to the PyMem family. */ /* pymalloc not enabled: Redirect the entry points to the PyMem family. */
void * void *
_PyMalloc_Malloc(size_t n) _PyMalloc_Malloc(size_t n)
{ {
@ -646,6 +647,11 @@ _PyMalloc_Free(void *p)
} }
#endif /* WITH_PYMALLOC */ #endif /* WITH_PYMALLOC */
/*==========================================================================*/
/* Regardless of whether pymalloc is enabled, export entry points for
* the object-oriented pymalloc functions.
*/
PyObject * PyObject *
_PyMalloc_New(PyTypeObject *tp) _PyMalloc_New(PyTypeObject *tp)
{ {
@ -675,7 +681,9 @@ _PyMalloc_Del(PyObject *op)
#ifdef PYMALLOC_DEBUG #ifdef PYMALLOC_DEBUG
/*==========================================================================*/ /*==========================================================================*/
/* A x-platform debugging allocator. */ /* A x-platform debugging allocator. This doesn't manage memory directly,
* it wraps a real allocator, adding extra debugging info to the memory blocks.
*/
#define PYMALLOC_CLEANBYTE 0xCB /* uninitialized memory */ #define PYMALLOC_CLEANBYTE 0xCB /* uninitialized memory */
#define PYMALLOC_DEADBYTE 0xDB /* free()ed memory */ #define PYMALLOC_DEADBYTE 0xDB /* free()ed memory */
@ -687,7 +695,7 @@ static ulong serialno = 0; /* incremented on each debug {m,re}alloc */
static ulong static ulong
read4(const void *p) read4(const void *p)
{ {
const unsigned char *q = (unsigned char *)p; const uchar *q = (const uchar *)p;
return ((ulong)q[0] << 24) | return ((ulong)q[0] << 24) |
((ulong)q[1] << 16) | ((ulong)q[1] << 16) |
((ulong)q[2] << 8) | ((ulong)q[2] << 8) |
@ -699,11 +707,11 @@ read4(const void *p)
static void static void
write4(void *p, ulong n) write4(void *p, ulong n)
{ {
unsigned char *q = (unsigned char *)p; uchar *q = (uchar *)p;
q[0] = (unsigned char)((n >> 24) & 0xff); q[0] = (uchar)((n >> 24) & 0xff);
q[1] = (unsigned char)((n >> 16) & 0xff); q[1] = (uchar)((n >> 16) & 0xff);
q[2] = (unsigned char)((n >> 8) & 0xff); q[2] = (uchar)((n >> 8) & 0xff);
q[3] = (unsigned char)( n & 0xff); q[3] = (uchar)( n & 0xff);
} }
static void static void
@ -758,7 +766,7 @@ void *
_PyMalloc_DebugMalloc(size_t nbytes, int family) _PyMalloc_DebugMalloc(size_t nbytes, int family)
{ {
uchar *p; /* base address of malloc'ed block */ uchar *p; /* base address of malloc'ed block */
uchar *q; /* p + 8 + nbytes + */ uchar *tail; /* p + 8 + nbytes == pointer to tail pad bytes */
size_t total; /* nbytes + 16 */ size_t total; /* nbytes + 16 */
assert(family == 0); assert(family == 0);
@ -785,17 +793,15 @@ _PyMalloc_DebugMalloc(size_t nbytes, int family)
if (nbytes > 0) if (nbytes > 0)
memset(p+8, PYMALLOC_CLEANBYTE, nbytes); memset(p+8, PYMALLOC_CLEANBYTE, nbytes);
q = p + 8 + nbytes; tail = p + 8 + nbytes;
q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE; tail[0] = tail[1] = tail[2] = tail[3] = PYMALLOC_FORBIDDENBYTE;
write4(q+4, serialno); write4(tail + 4, serialno);
return p+8; return p+8;
} }
/* The debug free first uses the address to find the number of bytes /* The debug free first checks the 8 bytes on each end for sanity (in
originally asked for, then checks the 8 bytes on each end for particular, that the PYMALLOC_FORBIDDENBYTEs are still intact).
sanity (in particular, that the PYMALLOC_FORBIDDENBYTEs are still
intact).
Then fills the original bytes with PYMALLOC_DEADBYTE. Then fills the original bytes with PYMALLOC_DEADBYTE.
Then calls the underlying free. Then calls the underlying free.
*/ */
@ -914,11 +920,11 @@ _PyMalloc_DebugDumpAddress(const void *p)
/* In case this is nuts, check the pad bytes before trying to read up /* In case this is nuts, check the pad bytes before trying to read up
the serial number (the address deref could blow up). */ the serial number (the address deref could blow up). */
fprintf(stderr, " the 3 pad bytes at p-3 are "); fputs(" the 3 pad bytes at p-3 are ", stderr);
if (*(q-3) == PYMALLOC_FORBIDDENBYTE && if (*(q-3) == PYMALLOC_FORBIDDENBYTE &&
*(q-2) == PYMALLOC_FORBIDDENBYTE && *(q-2) == PYMALLOC_FORBIDDENBYTE &&
*(q-1) == PYMALLOC_FORBIDDENBYTE) { *(q-1) == PYMALLOC_FORBIDDENBYTE) {
fprintf(stderr, "PYMALLOC_FORBIDDENBYTE, as expected\n"); fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
} }
else { else {
int i; int i;
@ -939,7 +945,7 @@ _PyMalloc_DebugDumpAddress(const void *p)
tail[1] == PYMALLOC_FORBIDDENBYTE && tail[1] == PYMALLOC_FORBIDDENBYTE &&
tail[2] == PYMALLOC_FORBIDDENBYTE && tail[2] == PYMALLOC_FORBIDDENBYTE &&
tail[3] == PYMALLOC_FORBIDDENBYTE) { tail[3] == PYMALLOC_FORBIDDENBYTE) {
fprintf(stderr, "PYMALLOC_FORBIDDENBYTE, as expected\n"); fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
} }
else { else {
int i; int i;
@ -961,7 +967,7 @@ _PyMalloc_DebugDumpAddress(const void *p)
if (nbytes > 0) { if (nbytes > 0) {
int i = 0; int i = 0;
fprintf(stderr, " data at p:"); fputs(" data at p:", stderr);
/* print up to 8 bytes at the start */ /* print up to 8 bytes at the start */
while (q < tail && i < 8) { while (q < tail && i < 8) {
fprintf(stderr, " %02x", *q); fprintf(stderr, " %02x", *q);
@ -971,7 +977,7 @@ _PyMalloc_DebugDumpAddress(const void *p)
/* and up to 8 at the end */ /* and up to 8 at the end */
if (q < tail) { if (q < tail) {
if (tail - q > 8) { if (tail - q > 8) {
fprintf(stderr, " ..."); fputs(" ...", stderr);
q = tail - 8; q = tail - 8;
} }
while (q < tail) { while (q < tail) {
@ -979,7 +985,7 @@ _PyMalloc_DebugDumpAddress(const void *p)
++q; ++q;
} }
} }
fprintf(stderr, "\n"); fputc('\n', stderr);
} }
} }