BRL-CAD
directory.h
Go to the documentation of this file.
1/* D I R E C T O R Y . H
2 * BRL-CAD
3 *
4 * Copyright (c) 1993-2023 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/** @file directory.h
21 *
22 */
23
24#ifndef RT_DIRECTORY_H
25#define RT_DIRECTORY_H
26
27#include "common.h"
28#include "vmath.h"
29#include "bu/list.h"
30#include "rt/anim.h"
31
32__BEGIN_DECLS
33
34/**
35 * One of these structures is allocated in memory to represent each
36 * named object in the database.
37 *
38 * Note that a d_addr of RT_DIR_PHONY_ADDR ((b_off_t)-1) means that
39 * database storage has not been allocated yet.
40 *
41 * Note that there is special handling for RT_DIR_INMEM "in memory"
42 * overrides.
43 *
44 * Construction should be done only by using RT_GET_DIRECTORY()
45 * Destruction should be done only by using db_dirdelete().
46 *
47 * Special note: In order to reduce the overhead of acquiring heap
48 * memory (e.g., via bu_strdup()) to stash the name in d_namep, we
49 * carry along enough storage for small names right in the structure
50 * itself (d_shortname). Thus, d_namep should never be assigned to
51 * directly, it should always be accessed using RT_DIR_SET_NAMEP() and
52 * RT_DIR_FREE_NAMEP().
53 *
54 * The in-memory name of an object should only be changed using
55 * db_rename(), so that it can be requeued on the correct linked list,
56 * based on new hash. This should be followed by rt_db_put_internal()
57 * on the object to modify the on-disk name.
58 */
59struct directory {
60 uint32_t d_magic; /**< @brief Magic number */
61 char * d_namep; /**< @brief pointer to name string */
62 union {
63 b_off_t file_offset; /**< @brief disk address in obj file */
64 void *ptr; /**< @brief ptr to in-memory-only obj */
66 struct directory * d_forw; /**< @brief link to next dir entry */
67 struct animate * d_animate; /**< @brief link to animation */
68 long d_uses; /**< @brief number of uses, from instancing */
69 size_t d_len; /**< @brief number of of db granules used */
70 long d_nref; /**< @brief number of times ref'ed by COMBs */
71 int d_flags; /**< @brief flags */
72 unsigned char d_major_type; /**< @brief object major type */
73 unsigned char d_minor_type; /**< @brief object minor type */
74 struct bu_list d_use_hd; /**< @brief heads list of uses (struct soltab l2) */
75 char d_shortname[16]; /**< @brief Stash short names locally */
76 void *u_data; /**< @brief void pointer hook for user data. user is responsible for freeing. */
77};
78#define RT_DIR_NULL ((struct directory *)0)
79#define RT_CK_DIR(_dp) BU_CKMAG(_dp, RT_DIR_MAGIC, "(librt)directory")
80
81#define d_addr d_un.file_offset
82#define RT_DIR_PHONY_ADDR ((b_off_t)-1) /**< @brief Special marker for d_addr field */
83
84/* flags for db_diradd() and friends */
85#define RT_DIR_SOLID 0x1 /**< @brief this name is a solid */
86#define RT_DIR_COMB 0x2 /**< @brief combination */
87#define RT_DIR_REGION 0x4 /**< @brief region */
88#define RT_DIR_HIDDEN 0x8 /**< @brief object name is hidden */
89#define RT_DIR_NON_GEOM 0x10 /**< @brief object is not geometry (e.g. binary object) */
90#define RT_DIR_USED 0x80 /**< @brief One bit, used similar to d_nref */
91#define RT_DIR_INMEM 0x100 /**< @brief object is in memory (only) */
92
93/**< @brief Args to db_lookup() */
94#define LOOKUP_NOISY 1
95#define LOOKUP_QUIET 0
96
97#define FOR_ALL_DIRECTORY_START(_dp, _dbip) { int _i; \
98 for (_i = RT_DBNHASH-1; _i >= 0; _i--) { \
99 for ((_dp) = (_dbip)->dbi_Head[_i]; (_dp); (_dp) = (_dp)->d_forw) {
100
101#define FOR_ALL_DIRECTORY_END }}}
102
103#define RT_DIR_SET_NAMEP(_dp, _name) { \
104 if (strlen(_name) < sizeof((_dp)->d_shortname)) {\
105 bu_strlcpy((_dp)->d_shortname, (_name), sizeof((_dp)->d_shortname)); \
106 (_dp)->d_namep = (_dp)->d_shortname; \
107 } else { \
108 (_dp)->d_namep = bu_strdup(_name); /* Calls bu_malloc() */ \
109 } }
110
111
112/**
113 * Use this macro to free the d_namep member, which is sometimes not
114 * dynamic.
115 */
116#define RT_DIR_FREE_NAMEP(_dp) { \
117 if ((_dp)->d_namep != (_dp)->d_shortname) \
118 bu_free((_dp)->d_namep, "d_namep"); \
119 (_dp)->d_namep = NULL; }
120
121
122/**
123 * allocate and link in a new directory entry to the resource
124 * structure's freelist
125 */
126#define RT_GET_DIRECTORY(_p, _res) { \
127 while (((_p) = (_res)->re_directory_hd) == NULL) \
128 db_alloc_directory_block(_res); \
129 (_res)->re_directory_hd = (_p)->d_forw; \
130 (_p)->d_forw = NULL; }
131
132
133/**
134 * convert an argv list of names to a directory pointer array.
135 *
136 * If db_lookup fails for any individual argv, an empty directory
137 * structure is created and assigned the name and RT_DIR_PHONY_ADDR
138 *
139 * The returned directory ** structure is NULL terminated.
140 */
141RT_EXPORT extern struct directory **db_argv_to_dpv(const struct db_i *dbip,
142 const char **argv);
143
144
145/**
146 * convert a directory pointer array to an argv char pointer array.
147 */
148RT_EXPORT extern char **db_dpv_to_argv(struct directory **dpv);
149
150
151
152__END_DECLS
153
154#endif /* RT_DIRECTORY_H */
155
156/*
157 * Local Variables:
158 * tab-width: 8
159 * mode: C
160 * indent-tabs-mode: t
161 * c-file-style: "stroustrup"
162 * End:
163 * ex: shiftwidth=4 tabstop=8
164 */
Header file for the BRL-CAD common definitions.
struct directory ** db_argv_to_dpv(const struct db_i *dbip, const char **argv)
char ** db_dpv_to_argv(struct directory **dpv)
#define b_off_t
Definition: common.h:222
Definition: anim.h:76
Definition: list.h:131
union directory::@11 d_un
char * d_namep
pointer to name string
Definition: directory.h:61
int d_flags
flags
Definition: directory.h:71
uint32_t d_magic
Magic number.
Definition: directory.h:60
b_off_t file_offset
disk address in obj file
Definition: directory.h:63
char d_shortname[16]
Stash short names locally.
Definition: directory.h:75
unsigned char d_minor_type
object minor type
Definition: directory.h:73
long d_nref
number of times ref'ed by COMBs
Definition: directory.h:70
size_t d_len
number of of db granules used
Definition: directory.h:69
struct animate * d_animate
link to animation
Definition: directory.h:67
void * u_data
void pointer hook for user data. user is responsible for freeing.
Definition: directory.h:76
struct bu_list d_use_hd
heads list of uses (struct soltab l2)
Definition: directory.h:74
unsigned char d_major_type
object major type
Definition: directory.h:72
void * ptr
ptr to in-memory-only obj
Definition: directory.h:64
struct directory * d_forw
link to next dir entry
Definition: directory.h:66
long d_uses
number of uses, from instancing
Definition: directory.h:68
fundamental vector, matrix, quaternion math macros