2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2013 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 +----------------------------------------------------------------------+
21 #include "zend_static_allocator.h"
23 /* Not checking emalloc() and erealloc() return values as they are supposed to bailout */
25 inline static void block_init(Block
*block
, zend_uint block_size
)
27 block
->pos
= block
->bp
= (char *) emalloc(block_size
);
28 block
->end
= block
->bp
+ block_size
;
31 inline static char *block_allocate(Block
*block
, zend_uint size
)
33 char *retval
= block
->pos
;
34 if ((block
->pos
+= size
) >= block
->end
) {
40 inline static void block_destroy(Block
*block
)
45 void static_allocator_init(StaticAllocator
*sa
)
47 sa
->Blocks
= (Block
*) emalloc(sizeof(Block
));
48 block_init(sa
->Blocks
, ALLOCATOR_BLOCK_SIZE
);
50 sa
->current_block
= 0;
53 char *static_allocator_allocate(StaticAllocator
*sa
, zend_uint size
)
57 retval
= block_allocate(&sa
->Blocks
[sa
->current_block
], size
);
61 sa
->Blocks
= (Block
*) erealloc(sa
->Blocks
, ++sa
->num_blocks
);
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
);
68 void static_allocator_destroy(StaticAllocator
*sa
)
72 for (i
=0; i
<sa
->num_blocks
; i
++) {
73 block_free(&sa
->Blocks
[i
]);