BRL-CAD
db_inmem.c
Go to the documentation of this file.
1 /* D B _ I N M E M . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2006-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 dbio */
21 /** @{ */
22 /** @file librt/db_inmem.c
23  *
24  * Routines in support of maintaining geometry in-memory-only. The
25  * general process for adding geometry to an inmem database is to
26  * either:
27  *
28  * 1) call wdb_export_external() providing an external representation
29  * of the geometry object and a flag marking it as in-memory
30  * (preferred), or
31  *
32  * 2) call db_diradd() and mark the directory entry as in-memory via a
33  * call to db_inmem() providing an external representation.
34  *
35  */
36 
37 #include "common.h"
38 
39 #include "bio.h"
40 
41 #include "vmath.h"
42 #include "db.h"
43 #include "raytrace.h"
44 
45 
46 #define DEFAULT_DB_TITLE "Untitled BRL-CAD Database"
47 
48 struct db_i *
50 {
51  register struct db_i *dbip = DBI_NULL;
52  register int i;
53 
54  BU_ALLOC(dbip, struct db_i);
55  dbip->dbi_eof = (off_t)-1L;
56  dbip->dbi_fp = NULL;
57  dbip->dbi_mf = NULL;
58 
59  /* XXX it "should" be safe and recommended to set this to 1 as it
60  * merely toggles whether the data can be written to _disk_. the
61  * various uses, however, haven't been exhaustively checked so
62  * leave it off for now.
63  */
64  dbip->dbi_read_only = 0;
65 
66  /* Initialize fields */
67  for (i = 0; i < RT_DBNHASH; i++) {
68  dbip->dbi_Head[i] = RT_DIR_NULL;
69  }
70 
71  dbip->dbi_local2base = 1.0; /* mm */
72  dbip->dbi_base2local = 1.0;
74  dbip->dbi_uses = 1;
75  dbip->dbi_filename = NULL;
76  dbip->dbi_filepath = NULL;
77  dbip->dbi_version = 5;
78 
79  /* XXX might want/need to stash an ident record so it's valid.
80  * see db_fwrite_ident();
81  */
82 
83  bu_ptbl_init(&dbip->dbi_clients, 128, "dbi_clients[]");
84  dbip->dbi_magic = DBI_MAGIC; /* Now it's valid */
85 
86  /* mark the wdb structure as in-memory. */
88 
89  return dbip;
90 }
91 
92 struct db_i *
94  struct db_i *dbip;
95  struct bu_external obj;
96  struct bu_attribute_value_set avs;
97  struct bu_vls units = BU_VLS_INIT_ZERO;
98  struct bu_external attr;
99  int flags;
100 
101  dbip = db_open_inmem();
102 
103  RT_CK_DBI(dbip);
104  RT_CK_WDB(dbip->dbi_wdbp);
105 
106  /* Second, create the attribute-only _GLOBAL object */
107  bu_vls_printf(&units, "%.25e", dbip->dbi_local2base);
108 
109  bu_avs_init(&avs, 4, "db_create_inmem");
110  bu_avs_add(&avs, "title", dbip->dbi_title);
111  bu_avs_add(&avs, "units", bu_vls_addr(&units));
112 
113  db5_export_attributes(&attr, &avs);
114  db5_export_object3(&obj, DB5HDR_HFLAGS_DLI_APPLICATION_DATA_OBJECT,
115  DB5_GLOBAL_OBJECT_NAME, DB5HDR_HFLAGS_HIDDEN_OBJECT, &attr, NULL,
116  DB5_MAJORTYPE_ATTRIBUTE_ONLY, 0,
117  DB5_ZZZ_UNCOMPRESSED, DB5_ZZZ_UNCOMPRESSED);
119  wdb_export_external(dbip->dbi_wdbp, &obj, DB5_GLOBAL_OBJECT_NAME, flags, 0);
120 
121  bu_free_external(&obj);
122  bu_free_external(&attr);
123  bu_avs_free(&avs);
124  bu_vls_free(&units);
125 
126  return dbip;
127 }
128 
129 void
130 db_inmem(struct directory *dp, struct bu_external *ext, int flags, struct db_i *dbip)
131 {
132  BU_CK_EXTERNAL(ext);
133  RT_CK_DIR(dp);
134 
135  if (dp->d_flags & RT_DIR_INMEM)
136  bu_free(dp->d_un.ptr, "db_inmem() ext ptr");
137  dp->d_un.ptr = ext->ext_buf;
138  if (db_version(dbip) < 5) {
139  /* DB_MINREC granule size */
140  dp->d_len = ext->ext_nbytes / 128;
141  } else {
142  dp->d_len = ext->ext_nbytes;
143  }
144  dp->d_flags = flags | RT_DIR_INMEM;
145 
146  /* Empty out the external structure, but leave it w/valid magic */
147  ext->ext_buf = (uint8_t *)NULL;
148  ext->ext_nbytes = 0;
149 }
150 
151 
152 /** @} */
153 
154 /*
155  * Local Variables:
156  * mode: C
157  * tab-width: 8
158  * indent-tabs-mode: t
159  * c-file-style: "stroustrup"
160  * End:
161  * ex: shiftwidth=4 tabstop=8
162  */
Definition: raytrace.h:800
#define RT_DBNHASH
hash table is an array of linked lists with this many array pointer elements (Memory use for 32-bit: ...
Definition: raytrace.h:755
int dbi_uses
PRIVATE: # of uses of this struct.
Definition: raytrace.h:818
size_t d_len
of db granules used
Definition: raytrace.h:867
void db5_export_attributes(struct bu_external *ap, const struct bu_attribute_value_set *avs)
Definition: attributes.c:153
#define RT_DIR_INMEM
object is in memory (only)
Definition: raytrace.h:889
#define DBI_MAGIC
Definition: magic.h:202
void bu_ptbl_init(struct bu_ptbl *b, size_t len, const char *str)
Definition: ptbl.c:32
int bu_avs_add(struct bu_attribute_value_set *avp, const char *attribute, const char *value)
Definition: avs.c:78
struct rt_wdb * dbi_wdbp
PRIVATE: ptr back to containing rt_wdb.
Definition: raytrace.h:825
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
char ** dbi_filepath
search path for aux file opens (convenience var)
Definition: raytrace.h:810
void bu_free_external(struct bu_external *ep)
Header file for the BRL-CAD common definitions.
struct db_i * db_create_inmem(void)
Definition: db_inmem.c:93
struct bu_mapped_file * dbi_mf
PRIVATE: Only in read-only mode.
Definition: raytrace.h:822
#define RT_CK_WDB(_p)
Definition: raytrace.h:1292
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
void db_inmem(struct directory *dp, struct bu_external *ext, int flags, struct db_i *dbip)
Definition: db_inmem.c:130
char * dbi_title
title from IDENT rec
Definition: raytrace.h:809
#define RT_DIR_NON_GEOM
object is not geometry (e.g. binary object)
Definition: raytrace.h:887
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define RT_CK_DIR(_dp)
Definition: raytrace.h:876
off_t dbi_eof
PRIVATE: End+1 pos after db_scan()
Definition: raytrace.h:816
int wdb_export_external(struct rt_wdb *wdbp, struct bu_external *ep, const char *name, int flags, unsigned char minor_type)
Definition: wdb.c:105
uint8_t * ext_buf
Definition: parse.h:216
struct bu_ptbl dbi_clients
PRIVATE: List of rtip's using this db_i.
Definition: raytrace.h:823
#define RT_DIR_HIDDEN
object name is hidden
Definition: raytrace.h:886
void bu_avs_init(struct bu_attribute_value_set *avp, size_t len, const char *str)
Definition: avs.c:47
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
FILE * dbi_fp
PRIVATE: object hash table.
Definition: raytrace.h:815
double dbi_base2local
unit conversion factors
Definition: raytrace.h:808
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
int dbi_read_only
!0 => read only file
Definition: raytrace.h:806
union directory::@8 d_un
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
#define DBI_NULL
Definition: raytrace.h:827
struct db_i * db_open_inmem(void)
Definition: db_inmem.c:49
#define DEFAULT_DB_TITLE
Definition: db_inmem.c:46
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
void * ptr
ptr to in-memory-only obj
Definition: raytrace.h:862
int dbi_version
PRIVATE: use db_version()
Definition: raytrace.h:824
#define RT_WDB_TYPE_DB_INMEM
Definition: raytrace.h:1297
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
double dbi_local2base
local2mm
Definition: raytrace.h:807
#define BU_CK_EXTERNAL(_p)
Definition: parse.h:224
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
void db5_export_object3(struct bu_external *out, int dli, const char *name, const unsigned char hidden, const struct bu_external *attrib, const struct bu_external *body, int major, int minor, int a_zzz, int b_zzz)
Definition: db5_io.c:420
size_t ext_nbytes
Definition: parse.h:210
int d_flags
flags
Definition: raytrace.h:869
Definition: vls.h:56
char * dbi_filename
file name
Definition: raytrace.h:805
struct rt_wdb * wdb_dbopen(struct db_i *dbip, int mode)
Definition: wdb.c:64
void bu_avs_free(struct bu_attribute_value_set *avp)
Definition: avs.c:235
uint32_t dbi_magic
magic number
Definition: raytrace.h:801
#define bu_strdup(s)
Definition: str.h:71