• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libpocketsphinx/blkarray_list.c

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 /* SphinxBase headers. */
00045 #include <prim_type.h>
00046 #include <err.h>
00047 #include <ckd_alloc.h>
00048 #include <assert.h>
00049 
00050 /* Local headers. */
00051 #include "blkarray_list.h"
00052 
00053 
00054 #define BLKARRAY_DEFAULT_MAXBLKS        16380
00055 #define BLKARRAY_DEFAULT_BLKSIZE        16380
00056 
00057 
00058 blkarray_list_t *
00059 _blkarray_list_init(int32 maxblks, int32 blksize)
00060 {
00061     blkarray_list_t *bl;
00062 
00063     if ((maxblks <= 0) || (blksize <= 0)) {
00064         E_ERROR("Cannot allocate %dx%d blkarray\n", maxblks, blksize);
00065         return NULL;
00066     }
00067 
00068     bl = (blkarray_list_t *) ckd_calloc(1, sizeof(blkarray_list_t));
00069     bl->ptr = (void ***) ckd_calloc(maxblks, sizeof(void **));
00070     bl->maxblks = maxblks;
00071     bl->blksize = blksize;
00072     bl->n_valid = 0;
00073     bl->cur_row = -1;           /* No row is allocated (dummy) */
00074     bl->cur_row_free = blksize; /* The dummy row is full */
00075 
00076     return bl;
00077 }
00078 
00079 
00080 blkarray_list_t *
00081 blkarray_list_init(void)
00082 {
00083     return _blkarray_list_init(BLKARRAY_DEFAULT_MAXBLKS,
00084                                BLKARRAY_DEFAULT_BLKSIZE);
00085 }
00086 
00087 void
00088 blkarray_list_free(blkarray_list_t *bl)
00089 {
00090     blkarray_list_reset(bl);
00091     ckd_free(bl->ptr);
00092     ckd_free(bl);
00093 }
00094 
00095 
00096 int32
00097 blkarray_list_append(blkarray_list_t * bl, void *data)
00098 {
00099     int32 id;
00100 
00101     assert(bl);
00102 
00103     if (bl->cur_row_free >= bl->blksize) {
00104         /* Previous row is filled; need to allocate a new row */
00105         bl->cur_row++;
00106 
00107         if (bl->cur_row >= bl->maxblks) {
00108             E_ERROR("Block array (%dx%d) exhausted\n",
00109                     bl->maxblks, bl->blksize);
00110             bl->cur_row--;
00111             return -1;
00112         }
00113 
00114         /* Allocate the new row */
00115         assert(bl->ptr[bl->cur_row] == NULL);
00116         bl->ptr[bl->cur_row] = (void **) ckd_calloc(bl->blksize,
00117                                                     sizeof(void *));
00118 
00119         bl->cur_row_free = 0;
00120     }
00121 
00122     bl->ptr[bl->cur_row][bl->cur_row_free] = data;
00123     (bl->cur_row_free)++;
00124 
00125     id = (bl->n_valid)++;
00126     assert(id >= 0);
00127 
00128     return id;
00129 }
00130 
00131 
00132 void
00133 blkarray_list_reset(blkarray_list_t * bl)
00134 {
00135     int32 i, j;
00136 
00137     /* Free all the allocated elements as well as the blocks */
00138     for (i = 0; i < bl->cur_row; i++) {
00139         for (j = 0; j < bl->blksize; j++)
00140             ckd_free(bl->ptr[i][j]);
00141 
00142         ckd_free(bl->ptr[i]);
00143         bl->ptr[i] = NULL;
00144     }
00145     if (i == bl->cur_row) {     /* NEED THIS! (in case cur_row < 0) */
00146         for (j = 0; j < bl->cur_row_free; j++)
00147             ckd_free(bl->ptr[i][j]);
00148 
00149         ckd_free(bl->ptr[i]);
00150         bl->ptr[i] = NULL;
00151     }
00152 
00153     bl->n_valid = 0;
00154     bl->cur_row = -1;
00155     bl->cur_row_free = bl->blksize;
00156 }

Generated on Thu Jan 27 2011 for PocketSphinx by  doxygen 1.7.1