BRL-CAD
htbl.c
Go to the documentation of this file.
1 /* H T B L . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup librt */
21 /** @{ */
22 /** @file librt/htbl.c
23  *
24  * Support for variable length arrays of "struct hit". Patterned
25  * after the libbu/ptbl.c idea.
26  *
27  */
28 /** @} */
29 
30 #include "common.h"
31 
32 #include <string.h>
33 #include "bio.h"
34 
35 
36 #include "bu/debug.h"
37 #include "vmath.h"
38 #include "raytrace.h"
39 
40 
41 void
42 rt_htbl_init(struct rt_htbl *b, size_t len, const char *str)
43 
44 /* initial len. */
45 
46 {
47  if (bu_debug & BU_DEBUG_PTBL)
48  bu_log("rt_htbl_init(%p, len=%zu, %s)\n", (void *)b, len, str);
49  BU_LIST_INIT(&b->l);
50  b->l.magic = RT_HTBL_MAGIC;
51  if (len == 0) len = 64;
52  b->blen = len;
53  b->hits = (struct hit *)bu_calloc(b->blen, sizeof(struct hit), str);
54  b->end = 0;
55 }
56 
57 
58 void
60 {
61  RT_CK_HTBL(b);
62  b->end = 0;
63  if (bu_debug & BU_DEBUG_PTBL)
64  bu_log("rt_htbl_reset(%p)\n", (void *)b);
65 }
66 
67 
68 void
70 {
71  RT_CK_HTBL(b);
72 
73  bu_free((void *)b->hits, "rt_htbl.hits[]");
74  memset((char *)b, 0, sizeof(struct rt_htbl)); /* sanity */
75 
76  if (bu_debug & BU_DEBUG_PTBL)
77  bu_log("rt_htbl_free(%p)\n", (void *)b);
78 }
79 
80 
81 struct hit *
82 rt_htbl_get(struct rt_htbl *b)
83 {
84  RT_CK_HTBL(b);
85 
86  if (b->end >= b->blen) {
87  /* Increase size of array */
88  b->hits = (struct hit *)bu_realloc((char *)b->hits,
89  sizeof(struct hit) * (b->blen *= 4),
90  "rt_htbl.hits[]");
91  }
92 
93  /* There is sufficient room */
94  return &b->hits[b->end++];
95 }
96 
97 
98 /*
99  * Local Variables:
100  * mode: C
101  * tab-width: 8
102  * indent-tabs-mode: t
103  * c-file-style: "stroustrup"
104  * End:
105  * ex: shiftwidth=4 tabstop=8
106  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Definition: raytrace.h:248
#define RT_HTBL_MAGIC
Definition: magic.h:162
void rt_htbl_init(struct rt_htbl *b, size_t len, const char *str)
Definition: htbl.c:42
Header file for the BRL-CAD common definitions.
void * memset(void *s, int c, size_t n)
struct hit * rt_htbl_get(struct rt_htbl *b)
Definition: htbl.c:82
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
size_t blen
of struct's of storage at *hits
Definition: raytrace.h:1364
void * bu_realloc(void *ptr, size_t siz, const char *str)
struct bu_list l
linked list for caller's use
Definition: raytrace.h:1362
uint32_t magic
Magic # for mem id/check.
Definition: list.h:119
#define BU_DEBUG_PTBL
Definition: debug.h:64
int bu_debug
Definition: globals.c:87
size_t end
index of first available location
Definition: raytrace.h:1363
#define BU_LIST_INIT(_hp)
Definition: list.h:148
void rt_htbl_free(struct rt_htbl *b)
Definition: htbl.c:69
void rt_htbl_reset(struct rt_htbl *b)
Definition: htbl.c:59
struct hit * hits
hits[blen] data storage area
Definition: raytrace.h:1365
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define RT_CK_HTBL(_p)
Definition: raytrace.h:1367