view Python/pyarena.c @ 56121:ea697cde6882 3.1

Merged revisions 74881-74885,74888,74891-74895,74900,74905-74906,74931-74932,74934,74941-74942,74944,74956 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r74881 | r.david.murray | 2009-09-17 15:12:49 -0400 (Thu, 17 Sep 2009) | 2 lines Check in first draft of maintainers.rst. ........ r74882 | eric.smith | 2009-09-17 15:22:30 -0400 (Thu, 17 Sep 2009) | 1 line Typo. ........ r74883 | mark.dickinson | 2009-09-17 15:37:28 -0400 (Thu, 17 Sep 2009) | 1 line Add some more module maintainers. ........ r74884 | mark.dickinson | 2009-09-17 15:39:12 -0400 (Thu, 17 Sep 2009) | 1 line Revert accidental changes to Objects/longobject.c ........ r74885 | mark.dickinson | 2009-09-17 16:20:01 -0400 (Thu, 17 Sep 2009) | 1 line Add decimal maintainers ........ r74888 | r.david.murray | 2009-09-17 18:10:48 -0400 (Thu, 17 Sep 2009) | 2 lines Maintainer additions from MAL. ........ r74891 | georg.brandl | 2009-09-17 18:18:01 -0400 (Thu, 17 Sep 2009) | 1 line Some more maintainers. ........ r74892 | r.david.murray | 2009-09-17 19:23:56 -0400 (Thu, 17 Sep 2009) | 2 lines Add a couple interest areas for Nick per his request. ........ r74893 | r.david.murray | 2009-09-17 19:43:20 -0400 (Thu, 17 Sep 2009) | 2 lines Benajmin is also compiler-knowledgeable. ........ r74894 | alexandre.vassalotti | 2009-09-17 20:59:05 -0400 (Thu, 17 Sep 2009) | 2 lines Added myself to my areas of interest. ........ r74895 | brett.cannon | 2009-09-17 21:03:35 -0400 (Thu, 17 Sep 2009) | 1 line Add myself to a couple places as maintainer. ........ r74900 | georg.brandl | 2009-09-18 05:06:37 -0400 (Fri, 18 Sep 2009) | 1 line Add myself in interest areas. ........ r74905 | ezio.melotti | 2009-09-18 05:58:43 -0400 (Fri, 18 Sep 2009) | 1 line Add myself in interest areas and mark effbot as inactive in winsound ........ r74906 | antoine.pitrou | 2009-09-18 09:15:23 -0400 (Fri, 18 Sep 2009) | 3 lines Add myself in a couple of places ........ r74931 | r.david.murray | 2009-09-18 17:25:36 -0400 (Fri, 18 Sep 2009) | 5 lines Add some names gleaned from the "Tracker Archeology" thread on python-dev from back in February. Also consolidate the autoconf and makefiles topics into one, and add tracker and svn/hg topics. ........ r74932 | r.david.murray | 2009-09-18 17:32:16 -0400 (Fri, 18 Sep 2009) | 2 lines Remove non-committer mistakenly added. ........ r74934 | r.david.murray | 2009-09-18 17:40:30 -0400 (Fri, 18 Sep 2009) | 2 lines More descriptive/generic name for the svn/hg entry (version control) ........ r74941 | benjamin.peterson | 2009-09-18 18:45:59 -0400 (Fri, 18 Sep 2009) | 1 line add a new category for myself ........ r74942 | benjamin.peterson | 2009-09-18 18:50:55 -0400 (Fri, 18 Sep 2009) | 1 line add myself for symtable ........ r74944 | georg.brandl | 2009-09-19 03:39:05 -0400 (Sat, 19 Sep 2009) | 1 line I would like to maintain the token module! ........ r74956 | r.david.murray | 2009-09-19 14:40:08 -0400 (Sat, 19 Sep 2009) | 3 lines Add everyone who we know has been a release manager as a resource for the 'release management' topic. ........
author R. David Murray <rdmurray@bitdance.com>
date Sat, 19 Sep 2009 19:04:37 +0000
parents 654c380cf8b9
children cab14be0ada1
line wrap: on
line source

#include "Python.h"
#include "pyarena.h"

/* A simple arena block structure.

   Measurements with standard library modules suggest the average
   allocation is about 20 bytes and that most compiles use a single
   block.

   TODO(jhylton): Think about a realloc API, maybe just for the last
   allocation?
*/

#define DEFAULT_BLOCK_SIZE 8192
#define ALIGNMENT		8
#define ALIGNMENT_MASK		(ALIGNMENT - 1)
#define ROUNDUP(x)		(((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)

typedef struct _block {
	/* Total number of bytes owned by this block available to pass out.
	 * Read-only after initialization.  The first such byte starts at
	 * ab_mem.
	 */
	size_t ab_size;

	/* Total number of bytes already passed out.  The next byte available
	 * to pass out starts at ab_mem + ab_offset.
	 */
	size_t ab_offset;

	/* An arena maintains a singly-linked, NULL-terminated list of
	 * all blocks owned by the arena.  These are linked via the
	 * ab_next member.
	 */
	struct _block *ab_next;

	/* Pointer to the first allocatable byte owned by this block.  Read-
	 * only after initialization.
	 */
	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 {
        /* Pointer to the first block allocated for the arena, never NULL.
           It is used only to find the first block when the arena is
           being freed.
         */
	block *a_head;

        /* Pointer to the block currently used for allocation.  It's
           ab_next field should be NULL.  If it is not-null after a
           call to block_alloc(), it means a new block has been allocated
           and a_cur should be reset to point it.
         */
	block *a_cur;

        /* A Python list object containing references to all the PyObject
           pointers associated with this area.  They will be DECREFed
           when the arena is freed.
        */
        PyObject *a_objects;

#if defined(Py_DEBUG)
        /* Debug output */
        size_t total_allocs;
        size_t total_size;
        size_t total_blocks;
        size_t total_block_size;
        size_t total_big_blocks;
#endif
};

static block *
block_new(size_t size)
{
	/* Allocate header and block as one unit.
	   ab_mem points just past header. */
	block *b = (block *)malloc(sizeof(block) + size);
	if (!b)
		return NULL;
	b->ab_size = size;
	b->ab_mem = (void *)(b + 1);
	b->ab_next = NULL;
	b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) - 
	  (Py_uintptr_t)(b->ab_mem);
	return b;
}

static void
block_free(block *b) {
	while (b) {
		block *next = b->ab_next;
		free(b);
		b = next;
	}
}

static void *
block_alloc(block *b, size_t size)
{
	void *p;
	assert(b);
	size = ROUNDUP(size);
	if (b->ab_offset + size > b->ab_size) {
		/* If we need to allocate more memory than will fit in
		   the default block, allocate a one-off block that is
		   exactly the right size. */
		/* TODO(jhylton): Think about space waste at end of block */
		block *newbl = block_new(
				size < DEFAULT_BLOCK_SIZE ?
				DEFAULT_BLOCK_SIZE : size);
		if (!newbl)
			return NULL;
		assert(!b->ab_next);
		b->ab_next = newbl;
		b = newbl;
	}

	assert(b->ab_offset + size <= b->ab_size);
	p = (void *)(((char *)b->ab_mem) + b->ab_offset);
	b->ab_offset += size;
	return p;
}

PyArena *
PyArena_New()
{
	PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
	if (!arena)
		return (PyArena*)PyErr_NoMemory();

	arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
	arena->a_cur = arena->a_head;
        if (!arena->a_head) {
                free((void *)arena);
                return (PyArena*)PyErr_NoMemory();
        }
        arena->a_objects = PyList_New(0);
        if (!arena->a_objects) {
                block_free(arena->a_head);
                free((void *)arena);
                return (PyArena*)PyErr_NoMemory();
        }
#if defined(Py_DEBUG)
        arena->total_allocs = 0;
        arena->total_size = 0;
        arena->total_blocks = 1;
        arena->total_block_size = DEFAULT_BLOCK_SIZE;
        arena->total_big_blocks = 0;
#endif
	return arena;
}

void
PyArena_Free(PyArena *arena)
{
        int r;
	assert(arena);
#if defined(Py_DEBUG)
        /*
        fprintf(stderr,
                "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n",
                arena->total_allocs, arena->total_size, arena->total_blocks,
                arena->total_block_size, arena->total_big_blocks,
                PyList_Size(arena->a_objects));
        */
#endif
	block_free(arena->a_head);
	/* This property normally holds, except when the code being compiled
	   is sys.getobjects(0), in which case there will be two references.
        assert(arena->a_objects->ob_refcnt == 1);
	*/

        /* Clear all the elements from the list.  This is necessary
           to guarantee that they will be DECREFed. */
        r = PyList_SetSlice(arena->a_objects,
                            0, PyList_GET_SIZE(arena->a_objects), NULL);
        assert(r == 0);
        assert(PyList_GET_SIZE(arena->a_objects) == 0);
        Py_DECREF(arena->a_objects);
	free(arena);
}

void *
PyArena_Malloc(PyArena *arena, size_t size)
{
	void *p = block_alloc(arena->a_cur, size);
	if (!p)
		return PyErr_NoMemory();
#if defined(Py_DEBUG)
        arena->total_allocs++;
        arena->total_size += size;
#endif
	/* Reset cur if we allocated a new block. */
	if (arena->a_cur->ab_next) {
		arena->a_cur = arena->a_cur->ab_next;
#if defined(Py_DEBUG)
                arena->total_blocks++;
                arena->total_block_size += arena->a_cur->ab_size;
                if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
                        ++arena->total_big_blocks;
#endif
	}
	return p;
}

int
PyArena_AddPyObject(PyArena *arena, PyObject *obj)
{
        int r = PyList_Append(arena->a_objects, obj);
        if (r >= 0) {
                Py_DECREF(obj);
        }
        return r;
}