PocketSphinx
0.6
|
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00020 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00022 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00023 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00026 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00027 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00029 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 * 00031 * ==================================================================== 00032 * 00033 */ 00034 00035 /* 00036 * blkarray_list.c -- block array-based list structure. 00037 * 00038 * HISTORY 00039 * 00040 * 18-Feb-2004 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon 00041 * Started. 00042 */ 00043 00044 /* System headers. */ 00045 #include <assert.h> 00046 00047 /* SphinxBase headers. */ 00048 #include <sphinxbase/prim_type.h> 00049 #include <sphinxbase/err.h> 00050 #include <sphinxbase/ckd_alloc.h> 00051 00052 /* Local headers. */ 00053 #include "blkarray_list.h" 00054 00055 00056 #define BLKARRAY_DEFAULT_MAXBLKS 16380 00057 #define BLKARRAY_DEFAULT_BLKSIZE 16380 00058 00059 00060 blkarray_list_t * 00061 _blkarray_list_init(int32 maxblks, int32 blksize) 00062 { 00063 blkarray_list_t *bl; 00064 00065 if ((maxblks <= 0) || (blksize <= 0)) { 00066 E_ERROR("Cannot allocate %dx%d blkarray\n", maxblks, blksize); 00067 return NULL; 00068 } 00069 00070 bl = (blkarray_list_t *) ckd_calloc(1, sizeof(blkarray_list_t)); 00071 bl->ptr = (void ***) ckd_calloc(maxblks, sizeof(void **)); 00072 bl->maxblks = maxblks; 00073 bl->blksize = blksize; 00074 bl->n_valid = 0; 00075 bl->cur_row = -1; /* No row is allocated (dummy) */ 00076 bl->cur_row_free = blksize; /* The dummy row is full */ 00077 00078 return bl; 00079 } 00080 00081 00082 blkarray_list_t * 00083 blkarray_list_init(void) 00084 { 00085 return _blkarray_list_init(BLKARRAY_DEFAULT_MAXBLKS, 00086 BLKARRAY_DEFAULT_BLKSIZE); 00087 } 00088 00089 void 00090 blkarray_list_free(blkarray_list_t *bl) 00091 { 00092 blkarray_list_reset(bl); 00093 ckd_free(bl->ptr); 00094 ckd_free(bl); 00095 } 00096 00097 00098 int32 00099 blkarray_list_append(blkarray_list_t * bl, void *data) 00100 { 00101 int32 id; 00102 00103 assert(bl); 00104 00105 if (bl->cur_row_free >= bl->blksize) { 00106 /* Previous row is filled; need to allocate a new row */ 00107 bl->cur_row++; 00108 00109 if (bl->cur_row >= bl->maxblks) { 00110 E_ERROR("Block array (%dx%d) exhausted\n", 00111 bl->maxblks, bl->blksize); 00112 bl->cur_row--; 00113 return -1; 00114 } 00115 00116 /* Allocate the new row */ 00117 assert(bl->ptr[bl->cur_row] == NULL); 00118 bl->ptr[bl->cur_row] = (void **) ckd_calloc(bl->blksize, 00119 sizeof(void *)); 00120 00121 bl->cur_row_free = 0; 00122 } 00123 00124 bl->ptr[bl->cur_row][bl->cur_row_free] = data; 00125 (bl->cur_row_free)++; 00126 00127 id = (bl->n_valid)++; 00128 assert(id >= 0); 00129 00130 return id; 00131 } 00132 00133 00134 void 00135 blkarray_list_reset(blkarray_list_t * bl) 00136 { 00137 int32 i, j; 00138 00139 /* Free all the allocated elements as well as the blocks */ 00140 for (i = 0; i < bl->cur_row; i++) { 00141 for (j = 0; j < bl->blksize; j++) 00142 ckd_free(bl->ptr[i][j]); 00143 00144 ckd_free(bl->ptr[i]); 00145 bl->ptr[i] = NULL; 00146 } 00147 if (i == bl->cur_row) { /* NEED THIS! (in case cur_row < 0) */ 00148 for (j = 0; j < bl->cur_row_free; j++) 00149 ckd_free(bl->ptr[i][j]); 00150 00151 ckd_free(bl->ptr[i]); 00152 bl->ptr[i] = NULL; 00153 } 00154 00155 bl->n_valid = 0; 00156 bl->cur_row = -1; 00157 bl->cur_row_free = bl->blksize; 00158 }