PocketSphinx 5prealpha
blkarray_list.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 *
19 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * ====================================================================
32 *
33 */
34
35/*
36 * blkarray_list.c -- block array-based list structure.
37 *
38 * HISTORY
39 *
40 * 18-Feb-2004 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon
41 * Started.
42 */
43
44/* System headers. */
45#include <assert.h>
46
47/* SphinxBase headers. */
48#include <sphinxbase/prim_type.h>
49#include <sphinxbase/err.h>
50#include <sphinxbase/ckd_alloc.h>
51
52/* Local headers. */
53#include "blkarray_list.h"
54
55
56#define BLKARRAY_DEFAULT_MAXBLKS 16380
57#define BLKARRAY_DEFAULT_BLKSIZE 16380
58
59
61_blkarray_list_init(int32 maxblks, int32 blksize)
62{
64
65 if ((maxblks <= 0) || (blksize <= 0)) {
66 E_ERROR("Cannot allocate %dx%d blkarray\n", maxblks, blksize);
67 return NULL;
68 }
69
70 bl = (blkarray_list_t *) ckd_calloc(1, sizeof(blkarray_list_t));
71 bl->ptr = (void ***) ckd_calloc(maxblks, sizeof(void **));
72 bl->maxblks = maxblks;
73 bl->blksize = blksize;
74 bl->n_valid = 0;
75 bl->cur_row = -1; /* No row is allocated (dummy) */
76 bl->cur_row_free = blksize; /* The dummy row is full */
77
78 return bl;
79}
80
81
83blkarray_list_init(void)
84{
85 return _blkarray_list_init(BLKARRAY_DEFAULT_MAXBLKS,
86 BLKARRAY_DEFAULT_BLKSIZE);
87}
88
89void
90blkarray_list_free(blkarray_list_t *bl)
91{
92 blkarray_list_reset(bl);
93 ckd_free(bl->ptr);
94 ckd_free(bl);
95}
96
97
98int32
99blkarray_list_append(blkarray_list_t * bl, void *data)
100{
101 int32 id;
102
103 assert(bl);
104
105 if (bl->cur_row_free >= bl->blksize) {
106 /* Previous row is filled; need to allocate a new row */
107 bl->cur_row++;
108
109 if (bl->cur_row >= bl->maxblks) {
110 E_ERROR("Block array (%dx%d) exhausted\n",
111 bl->maxblks, bl->blksize);
112 bl->cur_row--;
113 return -1;
114 }
115
116 /* Allocate the new row */
117 assert(bl->ptr[bl->cur_row] == NULL);
118 bl->ptr[bl->cur_row] = (void **) ckd_malloc(bl->blksize *
119 sizeof(void *));
120
121 bl->cur_row_free = 0;
122 }
123
124 bl->ptr[bl->cur_row][bl->cur_row_free] = data;
125 (bl->cur_row_free)++;
126
127 id = (bl->n_valid)++;
128 assert(id >= 0);
129
130 return id;
131}
132
133
134void
135blkarray_list_reset(blkarray_list_t * bl)
136{
137 int32 i, j;
138
139 /* Free all the allocated elements as well as the blocks */
140 for (i = 0; i < bl->cur_row; i++) {
141 for (j = 0; j < bl->blksize; j++)
142 ckd_free(bl->ptr[i][j]);
143
144 ckd_free(bl->ptr[i]);
145 bl->ptr[i] = NULL;
146 }
147 if (i == bl->cur_row) { /* NEED THIS! (in case cur_row < 0) */
148 for (j = 0; j < bl->cur_row_free; j++)
149 ckd_free(bl->ptr[i][j]);
150
151 ckd_free(bl->ptr[i]);
152 bl->ptr[i] = NULL;
153 }
154
155 bl->n_valid = 0;
156 bl->cur_row = -1;
157 bl->cur_row_free = bl->blksize;
158}
159
160void *
161blkarray_list_get(blkarray_list_t *list, int32 n)
162{
163 int32 r, c;
164
165 if (n >= blkarray_list_n_valid(list))
166 return NULL;
167
168 r = n / blkarray_list_blksize(list);
169 c = n - (r * blkarray_list_blksize(list));
170
171 return blkarray_list_ptr(list, r, c);
172}