Back to index

php5  5.3.10
zend_static_allocator.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: zend_static_allocator.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "zend_static_allocator.h"
00022 
00023 /* Not checking emalloc() and erealloc() return values as they are supposed to bailout */
00024 
00025 inline static void block_init(Block *block, zend_uint block_size)
00026 {
00027        block->pos = block->bp = (char *) emalloc(block_size);
00028        block->end = block->bp + block_size;
00029 }
00030 
00031 inline static char *block_allocate(Block *block, zend_uint size)
00032 {
00033        char *retval = block->pos;
00034        if ((block->pos += size) >= block->end) {
00035               return (char *)NULL;
00036        }
00037        return retval;
00038 }
00039 
00040 inline static void block_destroy(Block *block)
00041 {
00042        efree(block->bp);
00043 }
00044 
00045 void static_allocator_init(StaticAllocator *sa)
00046 {
00047        sa->Blocks = (Block *) emalloc(sizeof(Block));
00048        block_init(sa->Blocks, ALLOCATOR_BLOCK_SIZE);
00049        sa->num_blocks = 1;
00050        sa->current_block = 0;
00051 }
00052 
00053 char *static_allocator_allocate(StaticAllocator *sa, zend_uint size)
00054 {
00055        char *retval;
00056 
00057        retval = block_allocate(&sa->Blocks[sa->current_block], size);
00058        if (retval) {
00059               return retval;
00060        }
00061        sa->Blocks = (Block *) erealloc(sa->Blocks, ++sa->num_blocks);
00062        sa->current_block++;
00063        block_init(&sa->Blocks[sa->current_block], (size > ALLOCATOR_BLOCK_SIZE) ? size : ALLOCATOR_BLOCK_SIZE);
00064        retval = block_allocate(&sa->Blocks[sa->current_block], size);
00065        return retval;
00066 }
00067 
00068 void static_allocator_destroy(StaticAllocator *sa)
00069 {
00070        zend_uint i;
00071 
00072        for (i=0; i<sa->num_blocks; i++) {
00073               block_free(&sa->Blocks[i]);
00074        }
00075        efree(sa->Blocks);
00076 }
00077 
00078 /*
00079  * Local variables:
00080  * tab-width: 4
00081  * c-basic-offset: 4
00082  * indent-tabs-mode: t
00083  * End:
00084  */