Add load-time initialization to LIBRT

Like libbu, libged, etc. use an rt_init.cpp file to definite load time
initializations for librt.  Historically, applications have needed to
initialize the RTG.rtg_vlfree list themselves before being able to
successfully use draw routines.  Rather than keep requiring that, we
instead initialize it ourselves at library load time.

Ironically, if we do eventually succeed in eliminating the global
completely applications will be back to having to initialize their own
vlfree lists, but at least in that scenario they will be managing the
list themselves and will need to know about it - unlike the current
situation, where the only awareness of rtg_vlfree in application code
comes from them needing to know to initialize it a-priori.

In principle this should also remove the need for initializing
rt_uniresource, but deliberately focusing this commit on the rtg_vlfree
initializations to limit how much changes at once for testing.
This commit is contained in:
Clifford Yapp committed 2024-12-18 11:35:25 -05:00
1 parent 1a7a14ac2a
commit 3a68e6180a
45 files changed
+81 -88

No files matched your search

-1
View File
@@ -271,7 +271,6 @@ load_g(struct tie_s *tie, const char *db, int argc, const char **argv, struct ad
/* make empty NMG model */
the_model = nmg_mm();
BU_LIST_INIT(&RTG.rtg_vlfree); /* for vlist macros */
/*
* these should probably encode so the result can be passed back to client
-2
View File
@@ -409,8 +409,6 @@ main(int argc, char *argv[])
tol.perp = 1e-6;
tol.para = 1 - tol.perp;
BU_LIST_INIT(&RTG.rtg_vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:n:o:pr:vx:D:P:X:ih?")) != -1) {
switch (c) {
+1 -2
View File
@@ -512,7 +512,6 @@ main(int argc, char **argv)
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:n:o:r:vx:D:P:X:e:ih?")) != -1) {
@@ -639,7 +638,7 @@ main(int argc, char **argv)
0, /* take all regions */
do_region_end,
rt_booltree_leaf_tess,
(void *)NULL); /* in librt/nmg_bool.c */
(void *)vlfree); /* in librt/nmg_bool.c */
if (regions_tried>0) {
percent = ((double)regions_converted * 100) / regions_tried;
-1
View File
@@ -225,7 +225,6 @@ main(int argc, char *argv[])
/* make empty NMG model */
the_model = nmg_mm();
conv_data.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(conv_data.vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((i = bu_getopt(argc, argv, "a:89n:o:r:vx:D:P:X:h?")) != -1) {
-1
View File
@@ -390,7 +390,6 @@ main(int argc, char *argv[])
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:n:o:r:vx:D:X:e:ih?")) != -1) {
-1
View File
@@ -215,7 +215,6 @@ main(int argc, const char **argv)
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
++argv; --argc;
-1
View File
@@ -1638,7 +1638,6 @@ main(int argc, char **argv)
fastf_t x_start, y_start, z_start;
fastf_t bb_area[3];
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
bu_setprogname(argv[0]);
+2 -4
View File
@@ -629,9 +629,7 @@ main(int argc, char **argv)
nmg_eue_dist = 2.0;
}
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
pm.vlfree = vlfree;
pm.vlfree = &RTG.rtg_vlfree;
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:bd:en:o:r:vx:X:u:h?")) != -1) {
@@ -754,7 +752,7 @@ main(int argc, char **argv)
0,
nmg_region_end,
rt_booltree_leaf_tess,
(void *)vlfree); /* in librt/nmg_bool.c */
(void *)pm.vlfree); /* in librt/nmg_bool.c */
goto out;
}
-1
View File
@@ -380,7 +380,6 @@ main(int argc, char **argv)
}
pm.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(pm.vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "d:a:n:o:r:vx:P:X:u:h?")) != -1) {
-1
View File
@@ -119,7 +119,6 @@ main(int argc, char **argv)
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "r:a:n:o:vx:D:X:h?")) != -1) {
-1
View File
@@ -411,7 +411,6 @@ Draw_entities(struct model *m, int de_list[], size_t no_of_des, fastf_t x, fastf
s = BU_LIST_FIRST(shell, &r->s_hd);
BU_LIST_INIT(&vhead);
BU_LIST_INIT(vlfree);
sina = sin(ang);
cosa = cos(ang);
-1
View File
@@ -224,7 +224,6 @@ main(int argc, char *argv[])
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
prog_name = argv[0];
-1
View File
@@ -153,7 +153,6 @@ main(int argc, char *argv [])
int c;
int file_count = 0;
char *output_file = (char *)NULL;
BU_LIST_INIT(&RTG.rtg_vlfree);
struct bu_list *vlfree = &RTG.rtg_vlfree;
bu_setprogname(argv[0]);
-1
View File
@@ -1113,7 +1113,6 @@ main(int argc, char **argv)
char *nastran_file = "Converted from NASTRAN file (stdin)";
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
bu_setprogname(argv[0]);
fpin = stdin;
-1
View File
@@ -60,7 +60,6 @@ main(int argc, char **argv)
FILE *fpin;
struct rt_wdb *fpout;
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
if ( BU_STR_EQUAL(argv[1],"-h") || BU_STR_EQUAL(argv[1],"-?")) {
usage();
-1
View File
@@ -3194,7 +3194,6 @@ main(int argc, char **argv)
char bot_orientation = RT_BOT_UNORIENTED;
int fuse_vertices = 0;
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
bu_setprogname(argv[0]);
-1
View File
@@ -104,7 +104,6 @@ main(int argc, char **argv)
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:dn:p:r:vx:P:X:h?")) != -1) {
-1
View File
@@ -228,7 +228,6 @@ int main(int argc, char **argv)
bu_setprogname(argv[0]);
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
tol.magic = BN_TOL_MAGIC; /* Copied from proc-db/nmgmodel.c */
tol.dist = 0.01;
-1
View File
@@ -3379,7 +3379,6 @@ main(int argc, char **argv)
int stop, num;
char name[NAMESIZE+1];
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* This size is dictated by the specific format output by the
* 'rpatch' tool and expected material file. They are
-2
View File
@@ -248,8 +248,6 @@ main(int argc, char *argv[])
/* make empty NMG model */
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:b8m:n:o:r:vx:D:X:i")) != -1) {
-2
View File
@@ -341,8 +341,6 @@ main(int argc, char *argv[])
/* make empty NMG model */
the_model = nmg_mm();
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
/* Get command line arguments. */
while ((c = bu_getopt(argc, argv, "a:b8m:n:o:r:vx:D:X:ih?")) != -1) {
-1
View File
@@ -158,7 +158,6 @@ main(int argc, char **argv)
int group_len[100];
int all_len=0;
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree); /* for vlist macros */
bu_setprogname(argv[0]);
-3
View File
@@ -754,9 +754,6 @@ main(int argc, const char **argv)
fprintf(stderr, "%s", ged_init_str);
}
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
// Use a C++ class to manage info we will need
std::shared_ptr<GshState> gs = std::make_shared<GshState>();
-1
View File
@@ -268,7 +268,6 @@ GFile::load_g(const char *filename, int argc, const char *argv[])
/* make empty NMG model */
the_model = nmg_mm();
d.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(d.vlfree); /* for vlist macros */
/*
* these should probably encode so the result can be passed back to client
@@ -414,7 +414,6 @@ assetimport_write(struct gcv_context *context, const struct gcv_opts *gcv_option
/* make empty NMG model */
state.the_model = nmg_mm();
state.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(state.vlfree); /* for vlist macros */
/* Walk indicated tree(s). Each region will be output separately */
if (state.assetimport_write_options->model_workaround) {
@@ -2462,7 +2462,6 @@ do_conversion(db_i &db, const struct gcv_opts &gcv_options,
facetize_regions);
data.vlfree = &RTG.rtg_vlfree;
//BU_LIST_INIT(data.vlfree); /* for vlist macros */
const int ret = db_walk_tree(&db,
static_cast<int>(gcv_options.num_objects),
-1
View File
@@ -528,7 +528,6 @@ obj_write(struct gcv_context *context, const struct gcv_opts *gcv_options, const
the_model = nmg_mm();
state.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(state.vlfree); /* for vlist macros */
/* Write out header */
if (NEAR_EQUAL(state.gcv_options->scale_factor, 1.0 / 25.4, RT_LEN_TOL))
-1
View File
@@ -650,7 +650,6 @@ ply_write_gcv(struct gcv_context* context, const struct gcv_opts* gcv_options, c
state.the_model = nmg_mm();
state.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(state.vlfree); /* for vlist macros */
if (!state.ply_write_options->separate) {
/* open outputfile using specified type */
-1
View File
@@ -418,7 +418,6 @@ stl_write(struct gcv_context *context, const struct gcv_opts *gcv_options, const
/* make empty NMG model */
state.the_model = nmg_mm();
state.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(state.vlfree); /* for vlist macros */
/* Walk indicated tree(s). Each region will be output separately */
(void) db_walk_tree(state.dbip, gcv_options->num_objects, (const char **)gcv_options->object_names,
-1
View File
@@ -1258,7 +1258,6 @@ vrml_write(struct gcv_context *context, const struct gcv_opts *gcv_options, cons
}
region_end_data.vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(region_end_data.vlfree); /* for vlist macros */
fprintf(state.fp_out, "#VRML V2.0 utf8\n");
fprintf(state.fp_out, "#Units are %s\n", vrml_write_make_units_str(gcv_options->scale_factor));
-3
View File
@@ -166,9 +166,6 @@ main(int ac, char *av[]) {
return 2;
}
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
/* make a temporary copy of moss */
bu_vls_sprintf(&fname, "%s/moss.g", av[1]);
std::ifstream orig(bu_vls_cstr(&fname), std::ios::binary);
-3
View File
@@ -233,9 +233,6 @@ main(int ac, char *av[]) {
bu_mkdir(lcache);
bu_setenv("BU_DIR_CACHE", lcache, 1);
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
/* We are going to generate geometry from the basic moss data,
* so we make a temporary copy */
bu_vls_sprintf(&fname, "%s/moss.g", av[1]);
-3
View File
@@ -77,9 +77,6 @@ main(int ac, char *av[]) {
return 2;
}
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
/* Open the temp file, then dbconcat argv[1] into it */
bu_vls_sprintf(&fname, "%s/moss.g", av[1]);
dbp = ged_open("db", bu_vls_cstr(&fname), 1);
-3
View File
@@ -84,9 +84,6 @@ main(int ac, char *av[]) {
bu_mkdir(lcache);
bu_setenv("BU_DIR_CACHE", lcache, 1);
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
/* We are going to generate geometry from the basic moss data,
* so we make a temporary copy */
bu_vls_sprintf(&fname, "%s/moss.g", av[1]);
-3
View File
@@ -417,9 +417,6 @@ main(int ac, char *av[]) {
bu_mkdir(lcache);
bu_setenv("BU_DIR_CACHE", lcache, 1);
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
/* We are going to generate geometry from the basic moss data,
* so we make a temporary copy */
bu_vls_sprintf(&fname, "%s/moss.g", av[1]);
-3
View File
@@ -84,9 +84,6 @@ main(int ac, char *av[]) {
bu_mkdir(lcache);
bu_setenv("BU_DIR_CACHE", lcache, 1);
/* FIXME: To draw, we need to init this LIBRT global */
BU_LIST_INIT(&RTG.rtg_vlfree);
/* We are going to generate geometry from the basic moss data,
* so we make a temporary copy */
bu_vls_sprintf(&fname, "%s/moss.g", av[1]);
+1
View File
@@ -214,6 +214,7 @@ set(
reduce_db.cpp
regionfix.c
roots.c
rt_init.cpp
search.cpp
search_old.cpp
shoot.c
-16
View File
@@ -74,25 +74,9 @@ rt_new_rti(struct db_i *dbip)
{
struct rt_i *rtip;
int i;
struct bu_list *vlfree = &RTG.rtg_vlfree;
RT_CK_DBI(dbip);
/* XXX Move to rt_global_init() ? */
if (BU_LIST_FIRST(bu_list, vlfree) == 0) {
char *debug_flags;
debug_flags = getenv("LIBRT_DEBUG");
if (debug_flags) {
if (rt_debug) {
bu_log("WARNING: discarding LIBRT_DEBUG value in favor of application-specified flags\n");
} else {
rt_debug = strtol(debug_flags, NULL, 0x10);
}
}
BU_LIST_INIT(vlfree);
}
BU_ALLOC(rtip, struct rt_i);
rtip->rti_magic = RTI_MAGIC;
for (i=0; i < RT_DBNHASH; i++) {
-2
View File
@@ -2216,8 +2216,6 @@ rt_extrude_tess(struct nmgregion **r, struct model *m, struct rt_db_internal *ip
}
BU_LIST_INIT(&vhead);
if (!BU_LIST_IS_INITIALIZED(vlfree))
BU_LIST_INIT(vlfree);
for (i = 0; outer_loop && i<(size_t)BU_PTBL_LEN(outer_loop); i++) {
void *seg;
+77
View File
@@ -0,0 +1,77 @@
/* R T _ I N I T . C P P
* BRL-CAD
*
* Copyright (c) 2019-2024 United States Government as represented by
* the U.S. Army Research Laboratory.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; see the file named COPYING for more
* information.
*/
/** @file rt_init.cpp
*
* NOTE: as this init is global to ALL applications before main(),
* care must be taken to not write to STDOUT or STDERR or app output
* may be corrupted, signals can be raised, or worse.
*
*/
#include "common.h"
#include "rt/defines.h"
#include "rt/debug.h"
#include "rt/global.h"
#include "rt/resource.h"
#include "rt/rt_instance.h"
static void
librt_init(void)
{
BU_LIST_INIT(&RTG.rtg_vlfree);
rt_init_resource(&rt_uniresource, 0, NULL);
// NOTE - rt_new_rti used to do this, checking if the rtg_vlfree list was
// initialized. Since we're doing that initialization in this routine,
// handle reading LIBRT_DEBUG here as well.
const char *debug_flags = getenv("LIBRT_DEBUG");
if (debug_flags)
rt_debug = strtol(debug_flags, NULL, 0x10);
}
static void
librt_clear(void)
{
}
struct librt_initializer {
/* constructor */
librt_initializer() {
librt_init();
}
/* destructor */
~librt_initializer() {
librt_clear();
}
};
static librt_initializer LIBRT;
// Local Variables:
// tab-width: 8
// mode: C++
// c-basic-offset: 4
// indent-tabs-mode: t
// c-file-style: "stroustrup"
// End:
// ex: shiftwidth=4 tabstop=8
-9
View File
@@ -699,15 +699,6 @@ tclcad_rt_import_from_path(Tcl_Interp *interp, struct rt_db_internal *ip, const
int
Rt_Init(Tcl_Interp *interp)
{
/*XXX how much will this break? */
if (!BU_LIST_IS_INITIALIZED(&RTG.rtg_vlfree)) {
/* initialize RT's global state */
BU_LIST_INIT(&RTG.rtg_vlfree);
if (rt_uniresource.re_magic != RESOURCE_MAGIC) {
rt_init_resource(&rt_uniresource, 0, NULL);
}
}
Tcl_PkgProvide(interp, "Rt", brlcad_version());
return TCL_OK;
-1
View File
@@ -1838,7 +1838,6 @@ main(int argc, char *argv[])
/* Set up linked lists */
s->vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(s->vlfree);
BU_LIST_INIT(&rtg_headwdb.l);
// Start out in an initialization state
-1
View File
@@ -519,7 +519,6 @@ main(int ac, char *av[])
struct bn_tol tol;
FILE *fdplot;
struct rt_wdb *fdmodel;
BU_LIST_INIT(&RTG.rtg_vlfree);
struct bu_list *vlfree = &RTG.rtg_vlfree;
bu_setprogname(av[0]);
-1
View File
@@ -189,7 +189,6 @@ main(int argc, char **argv)
bu_log("Writing out geometry to file [tea_nmg.g] ...");
struct bu_list *vlfree = &RTG.rtg_vlfree;
BU_LIST_INIT(vlfree);
outfp = wdb_fopen("tea_nmg.g");
-1
View File
@@ -167,7 +167,6 @@ QgEdApp::QgEdApp(int &argc, char *argv[], int swrast_mode, int quad_mode) :QAppl
bu_setenv("GED_TEST_NEW_CMD_FORMS", "1", 1);
mdl = new QgModel();
BU_LIST_INIT(&RTG.rtg_vlfree);
// Install a filter to handle the top level key bindings and other
// interactive event management requiring global application knowledge.