2 * Copyright (c) 2003 Nara Institute of Science and Technology
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name Nara Institute of Science and Technology may not be used to
15 * endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE Nara Institute
22 * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $Id: block.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
45 cha_block_new(size_t size, int nitem)
49 block = cha_malloc(sizeof(cha_block_t));
51 block->item_size = size;
52 block->allocated_num = nitem;
53 block->blocks = cha_malloc(size * nitem);
59 void cha_block_delete(cha_block_t *block)
61 cha_free(block->blocks);
66 cha_block_new_item(cha_block_t *block)
68 if (++block->num > block->allocated_num) {
69 block->allocated_num *= 2;
70 block->blocks = cha_realloc(block->blocks,
71 block->item_size * block->allocated_num);
73 return block->blocks + block->item_size * (block->num - 1);
77 cha_block_get_item(cha_block_t *block, int i)
79 return block->blocks + block->item_size * i;
83 cha_block_pop(cha_block_t *block)
85 return block->blocks + block->item_size * --block->num;
89 cha_block_num(cha_block_t *block)
95 cha_block_clear(cha_block_t *block)