root/Zend/zend_static_allocator.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. block_init
  2. block_allocate
  3. block_destroy
  4. static_allocator_init
  5. static_allocator_allocate
  6. static_allocator_destroy

   1 /*
   2    +----------------------------------------------------------------------+
   3    | Zend Engine                                                          |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 2.00 of the Zend license,     |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.zend.com/license/2_00.txt.                                |
  11    | If you did not receive a copy of the Zend license and are unable to  |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@zend.com so we can mail you a copy immediately.              |
  14    +----------------------------------------------------------------------+
  15    | Authors: Andi Gutmans <andi@zend.com>                                |
  16    +----------------------------------------------------------------------+
  17 */
  18 
  19 /* $Id$ */
  20 
  21 #include "zend_static_allocator.h"
  22 
  23 /* Not checking emalloc() and erealloc() return values as they are supposed to bailout */
  24 
  25 inline static void block_init(Block *block, zend_uint block_size)
  26 {
  27         block->pos = block->bp = (char *) emalloc(block_size);
  28         block->end = block->bp + block_size;
  29 }
  30 
  31 inline static char *block_allocate(Block *block, zend_uint size)
  32 {
  33         char *retval = block->pos;
  34         if ((block->pos += size) >= block->end) {
  35                 return (char *)NULL;
  36         }
  37         return retval;
  38 }
  39 
  40 inline static void block_destroy(Block *block)
  41 {
  42         efree(block->bp);
  43 }
  44 
  45 void static_allocator_init(StaticAllocator *sa)
  46 {
  47         sa->Blocks = (Block *) emalloc(sizeof(Block));
  48         block_init(sa->Blocks, ALLOCATOR_BLOCK_SIZE);
  49         sa->num_blocks = 1;
  50         sa->current_block = 0;
  51 }
  52 
  53 char *static_allocator_allocate(StaticAllocator *sa, zend_uint size)
  54 {
  55         char *retval;
  56 
  57         retval = block_allocate(&sa->Blocks[sa->current_block], size);
  58         if (retval) {
  59                 return retval;
  60         }
  61         sa->Blocks = (Block *) erealloc(sa->Blocks, ++sa->num_blocks);
  62         sa->current_block++;
  63         block_init(&sa->Blocks[sa->current_block], (size > ALLOCATOR_BLOCK_SIZE) ? size : ALLOCATOR_BLOCK_SIZE);
  64         retval = block_allocate(&sa->Blocks[sa->current_block], size);
  65         return retval;
  66 }
  67 
  68 void static_allocator_destroy(StaticAllocator *sa)
  69 {
  70         zend_uint i;
  71 
  72         for (i=0; i<sa->num_blocks; i++) {
  73                 block_free(&sa->Blocks[i]);
  74         }
  75         efree(sa->Blocks);
  76 }
  77 
  78 /*
  79  * Local variables:
  80  * tab-width: 4
  81  * c-basic-offset: 4
  82  * indent-tabs-mode: t
  83  * End:
  84  */

/* [<][>][^][v][top][bottom][index][help] */