OSDN Git Service

Bumped version 2.4.4.
[chasen-legacy/chasen.git] / lib / block.c
1 /*
2  * Copyright (c) 2003 Nara Institute of Science and Technology
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
17  *
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.
29  *
30  * $Id: block.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
31  */
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include "chalib.h"
36
37 struct _cha_block_t {
38     void *blocks;
39     size_t item_size;
40     int allocated_num;
41     int num;
42 };
43
44 cha_block_t *
45 cha_block_new(size_t size, int nitem)
46 {
47     cha_block_t *block;
48
49     block = cha_malloc(sizeof(cha_block_t));
50
51     block->item_size = size;
52     block->allocated_num = nitem;
53     block->blocks = cha_malloc(size * nitem);
54     block->num = 0;
55
56     return block;
57 }
58
59 void cha_block_delete(cha_block_t *block)
60 {
61     cha_free(block->blocks);
62     cha_free(block);
63 }
64
65 void *
66 cha_block_new_item(cha_block_t *block)
67 {
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);
72     }
73     return block->blocks + block->item_size * (block->num - 1);
74 }
75
76 void *
77 cha_block_get_item(cha_block_t *block, int i)
78 {
79     return block->blocks + block->item_size * i;
80 }
81
82 void *
83 cha_block_pop(cha_block_t *block)
84 {
85     return block->blocks + block->item_size * --block->num;
86 }
87
88 int
89 cha_block_num(cha_block_t *block)
90 {
91     return block->num;
92 }
93
94 void
95 cha_block_clear(cha_block_t *block)
96 {
97     block->num = 0;
98 }