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
block_init(Block * block,zend_uint block_size)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
block_allocate(Block * block,zend_uint size)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
block_destroy(Block * block)40 inline static void block_destroy(Block *block)
41 {
42 efree(block->bp);
43 }
44
static_allocator_init(StaticAllocator * sa)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
static_allocator_allocate(StaticAllocator * sa,zend_uint size)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
static_allocator_destroy(StaticAllocator * sa)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 */
85