htbl.c

Go to the documentation of this file.
00001 /*                          H T B L . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** @addtogroup librt */
00023 
00024 /*@{*/
00025 /** @file htbl.c
00026  *  Support for variable length arrays of "struct hit".
00027  *  Patterned after the libbu/ptbl.c idea.
00028  *
00029  *  Author -
00030  *      Michael John Muuss
00031  *
00032  *  Source -
00033  *      The U. S. Army Research Laboratory
00034  *      Aberdeen Proving Ground, Maryland  21005-5068  USA
00035  *
00036  */
00037 /*@}*/
00038 
00039 #ifndef lint
00040 static const char librt_htbl_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/htbl.c,v 14.10 2006/09/16 02:04:25 lbutler Exp $ (ARL)";
00041 #endif
00042 
00043 #include "common.h"
00044 
00045 
00046 #include <stdio.h>
00047 #include <string.h>
00048 #include "machine.h"
00049 #include "bu.h"
00050 #include "vmath.h"
00051 #include "raytrace.h"
00052 
00053 /*
00054  *                      R T _ H T B L _ I N I T
00055  */
00056 void
00057 rt_htbl_init(struct rt_htbl *b, int len, const char *str)
00058 
00059                                 /* initial len. */
00060 
00061 {
00062         if (bu_debug & BU_DEBUG_PTBL)
00063                 bu_log("rt_htbl_init(%8x, len=%d, %s)\n", b, len, str);
00064         BU_LIST_INIT(&b->l);
00065         b->l.magic = RT_HTBL_MAGIC;
00066         if( len <= 0 )  len = 64;
00067         b->blen = len;
00068         b->hits = (struct hit *)bu_calloc(b->blen, sizeof(struct hit), str);
00069         b->end = 0;
00070 }
00071 
00072 /*
00073  *                      R T _ H T B L _ R E S E T
00074  *
00075  *  Reset the table to have no elements, but retain any existing storage.
00076  */
00077 void
00078 rt_htbl_reset(struct rt_htbl *b)
00079 {
00080         RT_CK_HTBL(b);
00081         b->end = 0;
00082         if (bu_debug & BU_DEBUG_PTBL)
00083                 bu_log("rt_htbl_reset(%8x)\n", b);
00084 }
00085 
00086 /*
00087  *                      R T _ H T B L _ F R E E
00088  *
00089  *  Deallocate dynamic hit buffer
00090  *  and render unusable without a subsequent rt_htbl_init().
00091  */
00092 void
00093 rt_htbl_free(struct rt_htbl *b)
00094 {
00095         RT_CK_HTBL(b);
00096 
00097         bu_free((genptr_t)b->hits, "rt_htbl.hits[]");
00098         bzero((char *)b, sizeof(struct rt_htbl));       /* sanity */
00099 
00100         if (bu_debug & BU_DEBUG_PTBL)
00101                 bu_log("rt_htbl_free(%8x)\n", b);
00102 }
00103 
00104 /*
00105  *                      R T _ H T B L _ G E T
00106  *
00107  *  Allocate another hit structure, extending the array if necessary.
00108  */
00109 struct hit *
00110 rt_htbl_get(struct rt_htbl *b)
00111 {
00112         RT_CK_HTBL(b);
00113 
00114         if( b->end >= b->blen )  {
00115                 /* Increase size of array */
00116                 b->hits = (struct hit *)bu_realloc( (char *)b->hits,
00117                         sizeof(struct hit) * (b->blen *= 4),
00118                         "rt_htbl.hits[]" );
00119         }
00120 
00121         /* There is sufficient room */
00122         return &b->hits[b->end++];
00123 }
00124 
00125 /*
00126  * Local Variables:
00127  * mode: C
00128  * tab-width: 8
00129  * c-basic-offset: 4
00130  * indent-tabs-mode: t
00131  * End:
00132  * ex: shiftwidth=4 tabstop=8
00133  */

Generated on Mon Sep 18 01:24:52 2006 for BRL-CAD by  doxygen 1.4.6