summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2006-02-28 18:52:28 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2006-02-28 18:52:28 (GMT)
commit99b4ee637322a9e3705a316008f9fc68eefceb02 (patch)
tree21b0489d278430f105f303b71d503878651d22bf /Python
parent400cbc3a4490d2d8358fcec394fed43affb3ee9c (diff)
downloadcpython-99b4ee637322a9e3705a316008f9fc68eefceb02.zip
cpython-99b4ee637322a9e3705a316008f9fc68eefceb02.tar.gz
cpython-99b4ee637322a9e3705a316008f9fc68eefceb02.tar.bz2
Use simple PyList to implement list of PyObject pointers
Diffstat (limited to 'Python')
-rw-r--r--Python/pyarena.c64
1 files changed, 20 insertions, 44 deletions
diff --git a/Python/pyarena.c b/Python/pyarena.c
index 0b865db..c6d0139 100644
--- a/Python/pyarena.c
+++ b/Python/pyarena.c
@@ -1,13 +1,6 @@
#include "Python.h"
#include "pyarena.h"
-/* An arena list is a linked list that can store PyObjects. */
-
-typedef struct _arena_list {
- struct _arena_list *al_next;
- void *al_pointer;
-} PyArenaList;
-
/* A simple arena block structure */
/* TODO(jhylton): Measurement to justify block size. */
@@ -19,38 +12,17 @@ typedef struct _block {
void *ab_mem;
} block;
+/* The arena manages two kinds of memory, blocks of raw memory
+ and a list of PyObject* pointers. PyObjects are decrefed
+ when the arena is freed.
+*/
+
struct _arena {
block *a_head;
block *a_cur;
- PyArenaList *a_object_head;
- PyArenaList *a_object_tail;
+ PyObject *a_objects;
};
-static PyArenaList*
-PyArenaList_New(void)
-{
- PyArenaList *alist = (PyArenaList *)malloc(sizeof(PyArenaList));
- if (!alist)
- return NULL;
-
- alist->al_next = NULL;
- alist->al_pointer = NULL;
- return alist;
-}
-
-static void
-PyArenaList_FreeObject(PyArenaList *alist)
-{
- while (alist) {
- PyArenaList *prev;
- Py_XDECREF((PyObject *)alist->al_pointer);
- alist->al_pointer = NULL;
- prev = alist;
- alist = alist->al_next;
- free(prev);
- }
-}
-
static block *
block_new(size_t size)
{
@@ -110,8 +82,16 @@ PyArena_New()
arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
arena->a_cur = arena->a_head;
- arena->a_object_head = PyArenaList_New();
- arena->a_object_tail = arena->a_object_head;
+ if (!arena->a_head) {
+ free((void *)arena);
+ return NULL;
+ }
+ arena->a_objects = PyList_New(16);
+ if (!arena->a_objects) {
+ block_free(arena->a_head);
+ free((void *)arena);
+ return NULL;
+ }
return arena;
}
@@ -120,7 +100,8 @@ PyArena_Free(PyArena *arena)
{
assert(arena);
block_free(arena->a_head);
- PyArenaList_FreeObject(arena->a_object_head);
+ assert(arena->a_objects->ob_refcnt == 1);
+ Py_DECREF(arena->a_objects);
free(arena);
}
@@ -138,12 +119,7 @@ PyArena_Malloc(PyArena *arena, size_t size)
}
int
-PyArena_AddPyObject(PyArena *arena, PyObject *pointer)
+PyArena_AddPyObject(PyArena *arena, PyObject *obj)
{
- PyArenaList *tail = arena->a_object_tail;
- assert(pointer);
- tail->al_next = PyArenaList_New();
- tail->al_pointer = pointer;
- arena->a_object_tail = tail->al_next;
- return 1;
+ return PyList_Append(arena->a_objects, obj) >= 0;
}