[Python-checkins] python/dist/src/Python newcompile.c,1.1.2.15,1.1.2.16
jhylton@users.sourceforge.net
jhylton@users.sourceforge.net
2002年10月21日 15:36:19 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv27827/Python
Modified Files:
Tag: ast-branch
newcompile.c
Log Message:
Add sane reallocation strategy for u_blocks.
Add compiler_unit_check() that looks for bogus u_blocks arrays. Hint:
realloc puts 0xfbfbfbfb at the end of the realloc block.
Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.15
retrieving revision 1.1.2.16
diff -C2 -d -r1.1.2.15 -r1.1.2.16
*** newcompile.c 21 Oct 2002 21:43:39 -0000 1.1.2.15
--- newcompile.c 21 Oct 2002 22:36:16 -0000 1.1.2.16
***************
*** 36,39 ****
--- 36,40 ----
int u_argcount;
int u_nblocks;
+ int u_nalloc;
int u_curblock;
struct basicblock u_entry;
***************
*** 209,212 ****
--- 210,214 ----
Py_INCREF(u->u_varnames);
u->u_nblocks = 0;
+ u->u_nalloc = DEFAULT_BLOCKS;
u->u_blocks = (struct basicblock **)PyObject_Malloc(
sizeof(struct basicblock *) * DEFAULT_BLOCKS);
***************
*** 238,249 ****
}
! static int
! compiler_exit_scope(struct compiler *c)
{
! struct compiler_unit *u = c->u;
! int i, n;
! PyObject *wrapper;
! for (i = 0; i < u->u_nblocks; i++)
PyObject_Free((void *)u->u_blocks[i]);
if (u->u_blocks)
--- 240,249 ----
}
! static void
! compiler_unit_free(struct compiler_unit *u)
{
! int i;
! for (i = 0; i < u->u_nblocks; i++)
PyObject_Free((void *)u->u_blocks[i]);
if (u->u_blocks)
***************
*** 253,259 ****
Py_XDECREF(u->u_names);
Py_XDECREF(u->u_varnames);
-
PyObject_Free(u);
/* Restore c->u to the parent unit. */
n = PyList_GET_SIZE(c->c_stack) - 1;
--- 253,279 ----
Py_XDECREF(u->u_names);
Py_XDECREF(u->u_varnames);
PyObject_Free(u);
+ }
+
+ static void
+ compiler_unit_check(struct compiler_unit *u)
+ {
+ int i;
+ assert(u->u_curblock < u->u_nblocks);
+ assert(u->u_nblocks <= u->u_nalloc);
+ for (i = 0; i < u->u_nblocks; i++) {
+ assert(u->u_blocks[i]);
+ assert(u->u_blocks[i] != 0xcbcbcbcb);
+ assert(u->u_blocks[i] != 0xfbfbfbfb);
+ }
+ }
+ static int
+ compiler_exit_scope(struct compiler *c)
+ {
+ int n;
+ PyObject *wrapper;
+
+ compiler_unit_free(c->u);
/* Restore c->u to the parent unit. */
n = PyList_GET_SIZE(c->c_stack) - 1;
***************
*** 263,266 ****
--- 283,287 ----
if (PySequence_DelItem(c->c_stack, n) < 0)
return 0;
+ compiler_unit_check(c->u);
}
else
***************
*** 282,293 ****
u = c->u;
! if (u->u_nblocks && u->u_nblocks % DEFAULT_BLOCKS == 0) {
! /* XXX should double */
! int newsize = u->u_nblocks + DEFAULT_BLOCKS;
u->u_blocks = (struct basicblock **)PyObject_Realloc(
u->u_blocks, newsize);
if (u->u_blocks == NULL)
return -1;
}
b = (struct basicblock *)PyObject_Malloc(sizeof(struct basicblock));
if (b == NULL)
--- 303,318 ----
u = c->u;
! if (u->u_nblocks == u->u_nalloc) {
! int newsize = ((u->u_nalloc + u->u_nalloc)
! * sizeof(struct basicblock *));
u->u_blocks = (struct basicblock **)PyObject_Realloc(
u->u_blocks, newsize);
if (u->u_blocks == NULL)
return -1;
+ memset(u->u_blocks + u->u_nalloc, 0,
+ sizeof(struct basicblock *) * u->u_nalloc);
+ u->u_nalloc += u->u_nalloc;
}
+ compiler_unit_check(u);
b = (struct basicblock *)PyObject_Malloc(sizeof(struct basicblock));
if (b == NULL)
***************
*** 296,299 ****
--- 321,325 ----
b->b_ialloc = DEFAULT_BLOCK_SIZE;
block = u->u_nblocks++;
+ assert(u->u_blocks[block] == NULL);
u->u_blocks[block] = b;
return block;
***************
*** 361,366 ****
if (ptr == NULL)
return -1;
! if (ptr != (void *)b)
c->u->u_blocks[block] = (struct basicblock *)ptr;
}
return b->b_iused++;
--- 387,394 ----
if (ptr == NULL)
return -1;
! if (ptr != (void *)b) {
! fprintf(stderr, "resize block %d\n", block);
c->u->u_blocks[block] = (struct basicblock *)ptr;
+ }
}
return b->b_iused++;