BRL-CAD
mapped_file.h
Go to the documentation of this file.
1/* M A P P E D _ F I L E . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-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
21#ifndef BU_MAPPED_FILE_H
22#define BU_MAPPED_FILE_H
23
24#include "common.h"
25
26#include <sys/types.h> /* for time_t */
27#include <stddef.h> /* for size_t */
28
29#include "bu/defines.h"
30#include "bu/list.h"
31
32
33__BEGIN_DECLS
34
35/** @addtogroup bu_mf
36 *
37 * @brief
38 * Routines for sharing large read-only data files.
39 *
40 * Typical use cases include height fields,
41 * bit map solids, texture maps, etc. Uses memory mapped files where
42 * available.
43 *
44 * Each instance of the file has the raw data available as element
45 * "buf". If a particular application needs to transform the raw data
46 * in a manner that is identical across all uses of that application
47 * (e.g. height fields, EBMs, etc.), then the application should
48 * provide a non-null "appl" string, to tag the format of the "apbuf".
49 * This will keep different applications from sharing that instance of
50 * the file.
51 *
52 * Thus, if the same filename is opened for interpretation as both an
53 * EBM and a height field, they will be assigned different mapped file
54 * structures, so that the "apbuf" pointers are distinct.
55 *
56 */
57
58/** @{ */
59/** @file bu/mapped_file.h */
60
61/**
62 * @struct bu_mapped_file bu/mapped_file.h
63 *
64 * Structure for opening a mapped file.
65 *
66 * Each file is opened and mapped only once (per application, as
67 * tagged by the string in "appl" field). Subsequent opens require an
68 * exact match on both strings.
69 *
70 * Before allocating apbuf and performing data conversion into it,
71 * openers should check to see if the file has already been opened and
72 * converted previously.
73 *
74 * When used in RT, the mapped files are not closed at the end of a
75 * frame, so that subsequent frames may take advantage of the large
76 * data files having already been read and converted. Examples
77 * include EBMs, texture maps, and height fields.
78 *
79 * For appl == "db_i", file is a ".g" database & apbuf is (struct db_i *).
80 */
82 char *name; /**< bu_strdup() of file name */
83 void *buf; /**< In-memory copy of file (may be mmapped) */
84 size_t buflen; /**< # bytes in 'buf' */
85 int is_mapped; /**< 1=mmap() used, 0=bu_malloc/fread */
86 char *appl; /**< bu_strdup() of tag for application using 'apbuf' */
87 void *apbuf; /**< opt: application-specific buffer */
88 size_t apbuflen; /**< opt: application-specific buflen */
89 time_t modtime; /**< date stamp, in case file is modified */
90 int uses; /**< # ptrs to this struct handed out */
91 void *handle; /**< PRIVATE - for internal file-specific implementation data */
92};
94#define BU_MAPPED_FILE_NULL ((struct bu_mapped_file *)0)
95
96/**
97 * macro suitable for declaration statement initialization of a
98 * bu_mapped_file struct. does not allocate memory.
99 */
100#define BU_MAPPED_FILE_INIT_ZERO { NULL, NULL, 0, 0, NULL, NULL, 0, 0, 0, NULL }
101
102/**
103 * Provides a standardized interface for acquiring the entire contents
104 * of an existing file mapped into the current address space, using
105 * the virtual memory capabilities of the operating system (such as
106 * mmap()) where available, or by allocating sufficient dynamic memory
107 * and reading the entire file.
108 *
109 * If the file can not be opened, as descriptive an error message as
110 * possible will be printed, to simplify code handling in the caller.
111 *
112 * Mapped files are always opened read-only.
113 *
114 * If the system does not support mapped files, the data is read into
115 * memory.
116 */
117BU_EXPORT extern struct bu_mapped_file *bu_open_mapped_file(const char *name,
118 const char *appl);
119
120/**
121 * Release a use of a mapped file. Because it may be re-used shortly,
122 * e.g. by the next frame of an animation, don't release the memory
123 * even on final close, so that it's available when next needed.
124 *
125 * Call bu_free_mapped_files() after final close to reclaim space.
126 * But only do that if you're SURE that ALL these files will never
127 * again need to be mapped by this process. Such as when running
128 * multi-frame animations.
129 */
130BU_EXPORT extern void bu_close_mapped_file(struct bu_mapped_file *mp);
131
132BU_EXPORT extern void bu_pr_mapped_file(const char *title,
133 const struct bu_mapped_file *mp);
134
135/**
136 * Release storage being used by mapped files with no remaining users. This
137 * will slow subsequent re-opening of those files (since files with no users
138 * will be unmapped as part of the freeing process, they will have to be
139 * re-mapped on a subsequent reopen.) Use cases where there is a possibility of
140 * reopening such files in the future will generally want to postpone calling
141 * this routine unless they need to free up memory.
142 *
143 * This entire routine runs inside a critical section, for parallel protection.
144 */
145BU_EXPORT extern void bu_free_mapped_files(int verbose);
146
147/**
148 * A wrapper for bu_open_mapped_file() which uses a search path to
149 * locate the file.
150 *
151 * The search path is specified as a normal C argv array, terminated
152 * by a null string pointer. If the file name begins with a slash
153 * ('/') the path is not used.
154 */
155BU_EXPORT extern struct bu_mapped_file *bu_open_mapped_file_with_path(char * const *path,
156 const char *name,
157 const char *appl);
158
159/** @} */
160
161__END_DECLS
162
163#endif /* BU_MAPPED_FILE_H */
164
165/*
166 * Local Variables:
167 * mode: C
168 * tab-width: 8
169 * indent-tabs-mode: t
170 * c-file-style: "stroustrup"
171 * End:
172 * ex: shiftwidth=4 tabstop=8
173 */
Header file for the BRL-CAD common definitions.
void bu_free_mapped_files(int verbose)
void bu_close_mapped_file(struct bu_mapped_file *mp)
struct bu_mapped_file * bu_open_mapped_file(const char *name, const char *appl)
struct bu_mapped_file * bu_open_mapped_file_with_path(char *const *path, const char *name, const char *appl)
void bu_pr_mapped_file(const char *title, const struct bu_mapped_file *mp)
time_t modtime
Definition: mapped_file.h:89
size_t apbuflen
Definition: mapped_file.h:88