-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathdb_alloc.c
273 lines (230 loc) · 6.61 KB
/
db_alloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* D B _ A L L O C . C
* BRL-CAD
*
* Copyright (c) 1988-2025 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.
*/
/** @addtogroup db4 */
/** @{ */
/** @file librt/db_alloc.c
*
* v4 granule allocation routines
*
*/
#include "common.h"
#include <string.h>
#include "bio.h"
#include "vmath.h"
#include "rt/db4.h"
#include "raytrace.h"
/**
* Find a block of database storage of "count" granules.
*
* Returns:
* 0 OK
* non-0 failure
*/
int
db_alloc(register struct db_i *dbip, register struct directory *dp, size_t count)
{
size_t len;
union record rec;
RT_CK_DBI(dbip);
RT_CK_DIR(dp);
if (RT_G_DEBUG&RT_DEBUG_DB) bu_log("db_alloc(%s) %p, %p, count=%zu\n",
dp->d_namep, (void *)dbip, (void *)dp, count);
if (count <= 0) {
bu_log("db_alloc(0)\n");
return -1;
}
if (dp->d_flags & RT_DIR_INMEM) {
if (dp->d_un.ptr) {
dp->d_un.ptr = bu_realloc(dp->d_un.ptr,
count * sizeof(union record), "db_alloc() d_un.ptr");
} else {
dp->d_un.ptr = bu_malloc(count * sizeof(union record), "db_alloc() d_un.ptr");
}
dp->d_len = count;
return 0;
}
if (dbip->dbi_read_only) {
bu_log("db_alloc on READ-ONLY file\n");
return -1;
}
while (1) {
len = rt_memalloc(&(dbip->dbi_freep), (unsigned)count);
if (len == 0L) {
/* No contiguous free block, append to file */
if ((dp->d_addr = dbip->dbi_eof) == RT_DIR_PHONY_ADDR) {
bu_log("db_alloc: bad EOF\n");
return -1;
}
dp->d_len = count;
dbip->dbi_eof += (b_off_t)(count * sizeof(union record));
dbip->dbi_nrec += count;
break;
}
dp->d_addr = (b_off_t)(len * sizeof(union record));
dp->d_len = count;
if (db_get(dbip, dp, &rec, 0, 1) < 0)
return -1;
if (rec.u_id != ID_FREE) {
bu_log("db_alloc(): len %zu non-FREE (id %d), skipping\n",
len, rec.u_id);
continue;
}
}
/* Clear out ALL the granules, for safety */
return db_zapper(dbip, dp, 0);
}
/**
* Delete a specific record from database entry
* No longer supported.
*/
int
db_delrec(struct db_i *dbip, register struct directory *dp, int recnum)
{
RT_CK_DBI(dbip);
RT_CK_DIR(dp);
if (RT_G_DEBUG&RT_DEBUG_DB) bu_log("db_delrec(%s) %p, %p, recnum=%d\n",
dp->d_namep, (void *)dbip, (void *)dp, recnum);
bu_log("ERROR db_delrec() is no longer supported. Use combination import/export routines.\n");
return -1;
}
/**
* Delete the indicated database record(s).
* Arrange to write "free storage" database markers in its place,
* positively erasing what had been there before.
*
* Returns:
* 0 on success
* non-zero on failure
*/
int
db_delete(struct db_i *dbip, struct directory *dp)
{
int i = 0;
RT_CK_DBI(dbip);
RT_CK_DIR(dp);
if (RT_G_DEBUG&RT_DEBUG_DB) bu_log("db_delete(%s) %p, %p\n",
dp->d_namep, (void *)dbip, (void *)dp);
if (dp->d_flags & RT_DIR_INMEM) {
bu_free(dp->d_un.ptr, "db_delete d_un.ptr");
dp->d_un.ptr = NULL;
dp->d_len = 0;
return 0;
}
if (db_version(dbip) == 4) {
i = db_zapper(dbip, dp, 0);
rt_memfree(&(dbip->dbi_freep), (unsigned)dp->d_len, dp->d_addr/(sizeof(union record)));
} else if (db_version(dbip) == 5) {
i = db5_write_free(dbip, dp, dp->d_len);
rt_memfree(&(dbip->dbi_freep), dp->d_len, dp->d_addr);
} else {
bu_bomb("db_delete() unsupported database version\n");
}
dp->d_len = 0;
dp->d_addr = RT_DIR_PHONY_ADDR;
return i;
}
/**
* Using a single call to db_put(), write multiple zeroed records out,
* all with u_id field set to ID_FREE.
* This will zap all records from "start" to the end of this entry.
*
* Returns:
* 0 on success (from db_put())
* non-zero on failure
*/
int
db_zapper(struct db_i *dbip, struct directory *dp, size_t start)
{
union record *rp;
size_t i;
size_t todo;
int ret;
RT_CK_DBI(dbip);
RT_CK_DIR(dp);
if (RT_G_DEBUG&RT_DEBUG_DB) bu_log("db_zapper(%s) %p, %p, start=%zu\n",
dp->d_namep, (void *)dbip, (void *)dp, start);
if (dp->d_flags & RT_DIR_INMEM) bu_bomb("db_zapper() called on RT_DIR_INMEM object\n");
if (dbip->dbi_read_only)
return -1;
BU_ASSERT(db_version(dbip) == 4);
if (dp->d_len < start)
return -1;
if ((todo = dp->d_len - start) == 0)
return 0; /* OK -- trivial */
rp = (union record *)bu_malloc(todo * sizeof(union record), "db_zapper buf");
memset((char *)rp, 0, todo * sizeof(union record));
for (i=0; i < todo; i++)
rp[i].u_id = ID_FREE;
ret = db_put(dbip, dp, rp, (b_off_t)start, todo);
bu_free((char *)rp, "db_zapper buf");
return ret;
}
void
db_alloc_directory_block(struct resource *resp)
{
struct directory *dp;
size_t bytes;
RT_CK_RESOURCE(resp);
BU_CK_PTBL(&resp->re_directory_blocks);
BU_ASSERT(resp->re_directory_hd == NULL);
/* Get a BIG block */
bytes = (size_t)bu_malloc_len_roundup(1024*sizeof(struct directory));
dp = (struct directory *)bu_calloc(1, bytes, "re_directory_blocks from db_alloc_directory_block() " CPP_FILELINE);
/* Record storage for later */
bu_ptbl_ins(&resp->re_directory_blocks, (long *)dp);
while (bytes >= sizeof(struct directory)) {
dp->d_magic = RT_DIR_MAGIC;
dp->d_forw = resp->re_directory_hd;
resp->re_directory_hd = dp;
dp++;
bytes -= sizeof(struct directory);
}
}
void
rt_alloc_seg_block(register struct resource *res)
{
register struct seg *sp;
size_t bytes;
RT_CK_RESOURCE(res);
if (!BU_LIST_IS_INITIALIZED(&res->re_seg)) {
BU_LIST_INIT(&(res->re_seg));
bu_ptbl_init(&res->re_seg_blocks, 64, "re_seg_blocks ptbl");
}
bytes = bu_malloc_len_roundup(64*sizeof(struct seg));
sp = (struct seg *)bu_malloc(bytes, "rt_alloc_seg_block()");
bu_ptbl_ins(&res->re_seg_blocks, (long *)sp);
while (bytes >= sizeof(struct seg)) {
sp->l.magic = RT_SEG_MAGIC;
BU_LIST_INSERT(&(res->re_seg), &(sp->l));
res->re_seglen++;
sp++;
bytes -= sizeof(struct seg);
}
}
/** @} */
/*
* Local Variables:
* mode: C
* tab-width: 8
* indent-tabs-mode: t
* c-file-style: "stroustrup"
* End:
* ex: shiftwidth=4 tabstop=8
*/