[Python-checkins] CVS: python/dist/src/Objects obmalloc.c,2.10,2.11

Tim Peters tim_one@users.sourceforge.net
Wed, 27 Mar 2002 23:32:13 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv29408/python/Objects

Modified Files:
	obmalloc.c 
Log Message:
PYMALLOC_DEBUG routines:  The "check API family" gimmick was going nowhere
fast, and just cluttered the code.  Get rid of it for now.  If a compelling
case can be made for it, easy to restore it later.


Index: obmalloc.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/obmalloc.c,v
retrieving revision 2.10
retrieving revision 2.11
diff -C2 -d -r2.10 -r2.11
*** obmalloc.c	24 Mar 2002 00:34:21 -0000	2.10
--- obmalloc.c	28 Mar 2002 07:32:11 -0000	2.11
***************
*** 725,748 ****
  }
  
- static void
- check_family(const void *p, int family)
- {
- 	const uchar *q = (const uchar *)p;
- 	int original_family;
- 	char buf[200];
- 
- 	assert(p != NULL);
- 	original_family = (int)*(q-4);
- 	if (family != original_family) {
- 		/* XXX better msg */
- 		PyOS_snprintf(buf, sizeof(buf),
- 			"free or realloc from family #%d called, "
- 			"but block was allocated by family #%d",
- 			family, original_family);
- 		_PyMalloc_DebugDumpAddress(p);
- 		Py_FatalError(buf);
- 	}
- }
- 
  /* The debug malloc asks for 16 extra bytes and fills them with useful stuff,
     here calling the underlying malloc's result p:
--- 725,728 ----
***************
*** 751,757 ****
      Number of bytes originally asked for.  4-byte unsigned integer,
      big-endian (easier to read in a memory dump).
! p[4]
!     The API "family" this malloc call belongs to.  XXX todo XXX
! p[5:8]
      Copies of PYMALLOC_FORBIDDENBYTE.  Used to catch under- writes
      and reads.
--- 731,735 ----
      Number of bytes originally asked for.  4-byte unsigned integer,
      big-endian (easier to read in a memory dump).
! p[4:8]
      Copies of PYMALLOC_FORBIDDENBYTE.  Used to catch under- writes
      and reads.
***************
*** 774,778 ****
  
  void *
! _PyMalloc_DebugMalloc(size_t nbytes, int family)
  {
  	uchar *p;	/* base address of malloc'ed block */
--- 752,756 ----
  
  void *
! _PyMalloc_DebugMalloc(size_t nbytes)
  {
  	uchar *p;	/* base address of malloc'ed block */
***************
*** 780,785 ****
  	size_t total;	/* nbytes + 16 */
  
- 	assert(family == 0);
- 
  	bumpserialno();
  	total = nbytes + 16;
--- 758,761 ----
***************
*** 793,803 ****
  	}
  
! 	p = _PyMalloc_Malloc(total);	/* XXX derive from family */
  	if (p == NULL)
  		return NULL;
  
  	write4(p, nbytes);
! 	p[4] = (uchar)family;
! 	p[5] = p[6] = p[7] = PYMALLOC_FORBIDDENBYTE;
  
  	if (nbytes > 0)
--- 769,778 ----
  	}
  
! 	p = _PyMalloc_Malloc(total);
  	if (p == NULL)
  		return NULL;
  
  	write4(p, nbytes);
! 	p[4] = p[5] = p[6] = p[7] = PYMALLOC_FORBIDDENBYTE;
  
  	if (nbytes > 0)
***************
*** 817,839 ****
  */
  void
! _PyMalloc_DebugFree(void *p, int family)
  {
  	uchar *q = (uchar *)p;
  	size_t nbytes;
  
- 	assert(family == 0);
- 
  	if (p == NULL)
  		return;
- 	check_family(p, family);
  	_PyMalloc_DebugCheckAddress(p);
  	nbytes = read4(q-8);
  	if (nbytes > 0)
  		memset(q, PYMALLOC_DEADBYTE, nbytes);
! 	_PyMalloc_Free(q-8);	/* XXX derive from family */
  }
  
  void *
! _PyMalloc_DebugRealloc(void *p, size_t nbytes, int family)
  {
  	uchar *q = (uchar *)p;
--- 792,811 ----
  */
  void
! _PyMalloc_DebugFree(void *p)
  {
  	uchar *q = (uchar *)p;
  	size_t nbytes;
  
  	if (p == NULL)
  		return;
  	_PyMalloc_DebugCheckAddress(p);
  	nbytes = read4(q-8);
  	if (nbytes > 0)
  		memset(q, PYMALLOC_DEADBYTE, nbytes);
! 	_PyMalloc_Free(q-8);
  }
  
  void *
! _PyMalloc_DebugRealloc(void *p, size_t nbytes)
  {
  	uchar *q = (uchar *)p;
***************
*** 841,852 ****
  	void *fresh;	/* new memory block, if needed */
  
- 	assert(family == 0);
- 
  	if (p == NULL)
! 		return _PyMalloc_DebugMalloc(nbytes, family);
  
- 	check_family(p, family);
  	_PyMalloc_DebugCheckAddress(p);
- 
  	original_nbytes = read4(q-8);
  	if (nbytes == original_nbytes) {
--- 813,820 ----
  	void *fresh;	/* new memory block, if needed */
  
  	if (p == NULL)
! 		return _PyMalloc_DebugMalloc(nbytes);
  
  	_PyMalloc_DebugCheckAddress(p);
  	original_nbytes = read4(q-8);
  	if (nbytes == original_nbytes) {
***************
*** 865,872 ****
  		write4(q-8, nbytes);
  		/* kill the excess bytes plus the trailing 8 pad bytes */
- 		memset(q + nbytes, PYMALLOC_DEADBYTE, excess + 8);
  		q += nbytes;
  		q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE;
  		write4(q+4, serialno);
  		return p;
  	}
--- 833,840 ----
  		write4(q-8, nbytes);
  		/* kill the excess bytes plus the trailing 8 pad bytes */
  		q += nbytes;
  		q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE;
  		write4(q+4, serialno);
+ 		memset(q+8, PYMALLOC_DEADBYTE, excess);
  		return p;
  	}
***************
*** 874,881 ****
  	/* More memory is needed:  get it, copy over the first original_nbytes
  	   of the original data, and free the original memory. */
! 	fresh = _PyMalloc_DebugMalloc(nbytes, family);
  	if (fresh != NULL && original_nbytes > 0)
  		memcpy(fresh, p, original_nbytes);
! 	_PyMalloc_DebugFree(p, family);
  	return fresh;
  }
--- 842,849 ----
  	/* More memory is needed:  get it, copy over the first original_nbytes
  	   of the original data, and free the original memory. */
! 	fresh = _PyMalloc_DebugMalloc(nbytes);
  	if (fresh != NULL && original_nbytes > 0)
  		memcpy(fresh, p, original_nbytes);
! 	_PyMalloc_DebugFree(p);
  	return fresh;
  }
***************
*** 885,914 ****
  {
  	const uchar *q = (const uchar *)p;
! 	char *msg = NULL;
  
! 	if (p == NULL)
  		msg = "didn't expect a NULL pointer";
  
! 	else if (*(q-3) != PYMALLOC_FORBIDDENBYTE ||
! 	    	 *(q-2) != PYMALLOC_FORBIDDENBYTE ||
! 	    	 *(q-1) != PYMALLOC_FORBIDDENBYTE)
! 	    	msg = "bad leading pad byte";
  
! 	else {
  		const ulong nbytes = read4(q-8);
  		const uchar *tail = q + nbytes;
- 		int i;
  		for (i = 0; i < 4; ++i) {
  			if (tail[i] != PYMALLOC_FORBIDDENBYTE) {
  				msg = "bad trailing pad byte";
! 				break;
  			}
  		}
  	}
  
! 	if (msg != NULL) {
! 		_PyMalloc_DebugDumpAddress(p);
! 		Py_FatalError(msg);
! 	}
  }
  
--- 853,887 ----
  {
  	const uchar *q = (const uchar *)p;
! 	char *msg;
! 	int i;
  
! 	if (p == NULL) {
  		msg = "didn't expect a NULL pointer";
+ 		goto error;
+ 	}
  
! 	for (i = 4; i >= 1; --i) {
! 		if (*(q-i) != PYMALLOC_FORBIDDENBYTE) {
! 			msg = "bad leading pad byte";
! 			goto error;
! 		}
! 	}
  
! 	{
  		const ulong nbytes = read4(q-8);
  		const uchar *tail = q + nbytes;
  		for (i = 0; i < 4; ++i) {
  			if (tail[i] != PYMALLOC_FORBIDDENBYTE) {
  				msg = "bad trailing pad byte";
! 				goto error;
  			}
  		}
  	}
  
! 	return;
! 
! error:
! 	_PyMalloc_DebugDumpAddress(p);
! 	Py_FatalError(msg);
  }
  
***************
*** 919,922 ****
--- 892,896 ----
  	const uchar *tail;
  	ulong nbytes, serial;
+ 	int i;
  
  	fprintf(stderr, "Debug memory block at address p=%p:\n", p);
***************
*** 926,936 ****
  	nbytes = read4(q-8);
  	fprintf(stderr, "    %lu bytes originally allocated\n", nbytes);
- 	fprintf(stderr, "    from API family #%d\n", *(q-4));
  
  	/* In case this is nuts, check the pad bytes before trying to read up
  	   the serial number (the address deref could blow up). */
  
! 	fputs("    the 3 pad bytes at p-3 are ", stderr);
! 	if (*(q-3) == PYMALLOC_FORBIDDENBYTE &&
  	    *(q-2) == PYMALLOC_FORBIDDENBYTE &&
  	    *(q-1) == PYMALLOC_FORBIDDENBYTE) {
--- 900,910 ----
  	nbytes = read4(q-8);
  	fprintf(stderr, "    %lu bytes originally allocated\n", nbytes);
  
  	/* In case this is nuts, check the pad bytes before trying to read up
  	   the serial number (the address deref could blow up). */
  
! 	fputs("    the 4 pad bytes at p-4 are ", stderr);
! 	if (*(q-4) == PYMALLOC_FORBIDDENBYTE &&
! 	    *(q-3) == PYMALLOC_FORBIDDENBYTE &&
  	    *(q-2) == PYMALLOC_FORBIDDENBYTE &&
  	    *(q-1) == PYMALLOC_FORBIDDENBYTE) {
***************
*** 938,945 ****
  	}
  	else {
- 		int i;
  		fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
  			PYMALLOC_FORBIDDENBYTE);
! 		for (i = 3; i >= 1; --i) {
  			const uchar byte = *(q-i);
  			fprintf(stderr, "        at p-%d: 0x%02x", i, byte);
--- 912,918 ----
  	}
  	else {
  		fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
  			PYMALLOC_FORBIDDENBYTE);
! 		for (i = 4; i >= 1; --i) {
  			const uchar byte = *(q-i);
  			fprintf(stderr, "        at p-%d: 0x%02x", i, byte);
***************
*** 959,963 ****
  	}
  	else {
- 		int i;
  		fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
  			PYMALLOC_FORBIDDENBYTE);
--- 932,935 ----