Clean up maintenance of config table sentinels

Xundump had special hackery to maintain configuration tables'
sentinels: xubody() and getobj() added a sentinel element when
initializing or growing a table, which xubody() stripped off again
before returning.  The latter was an unclean hack.

Replace this by building knowledge of sentinels into struct empfile:
new flag EFF_SENTINEL, set for the appropriate members of empfile[],
obeyed by ef_extend() and ef_truncate().
This commit is contained in:
Markus Armbruster 2008-09-01 09:20:08 -04:00
parent 02254398e8
commit 1492845c12
4 changed files with 20 additions and 23 deletions

View file

@ -99,6 +99,8 @@ struct emptypedstr {
#define EFF_GROUP bit(3) #define EFF_GROUP bit(3)
/* Table is allocated statically */ /* Table is allocated statically */
#define EFF_STATIC bit(4) #define EFF_STATIC bit(4)
/* Table has a sentinel (all zero, not counted as elt), implies EFF_MEM */
#define EFF_SENTINEL bit(5)
/* All the immutable flags */ /* All the immutable flags */
#define EFF_IMMUTABLE \ #define EFF_IMMUTABLE \
(EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC) (EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)

View file

@ -554,7 +554,7 @@ ef_extend(int type, int count)
{ {
struct empfile *ep; struct empfile *ep;
char *p; char *p;
int i, id; int need_sentinel, i, id;
if (ef_check(type) < 0) if (ef_check(type) < 0)
return 0; return 0;
@ -564,13 +564,14 @@ ef_extend(int type, int count)
id = ep->fids; id = ep->fids;
if (ep->flags & EFF_MEM) { if (ep->flags & EFF_MEM) {
if (id + count > ep->csize) { need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
if (id + count + need_sentinel > ep->csize) {
if (ep->flags & EFF_STATIC) { if (ep->flags & EFF_STATIC) {
logerror("Can't extend %s beyond %d elements", logerror("Can't extend %s beyond %d elements",
ep->name, ep->csize); ep->name, ep->csize - need_sentinel);
return 0; return 0;
} }
if (!ef_realloc_cache(ep, id + count)) { if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
logerror("Can't extend %s to %d elements (%s)", logerror("Can't extend %s to %d elements (%s)",
ep->name, id + count, strerror(errno)); ep->name, id + count, strerror(errno));
return 0; return 0;
@ -582,7 +583,9 @@ ef_extend(int type, int count)
if (do_write(ep, p, id, count) < 0) if (do_write(ep, p, id, count) < 0)
return 0; return 0;
} }
ep->cids += count; if (need_sentinel)
memset(ep->cache + (id + count) * ep->size, 0, ep->size);
ep->cids = id + count;
} else { } else {
/* need a buffer, steal last cache slot */ /* need a buffer, steal last cache slot */
if (ep->cids == ep->csize) if (ep->cids == ep->csize)
@ -594,7 +597,7 @@ ef_extend(int type, int count)
return 0; return 0;
} }
} }
ep->fids += count; ep->fids = id + count;
return 1; return 1;
} }
@ -646,6 +649,7 @@ int
ef_truncate(int type, int count) ef_truncate(int type, int count)
{ {
struct empfile *ep; struct empfile *ep;
int need_sentinel;
if (ef_check(type) < 0) if (ef_check(type) < 0)
return 0; return 0;
@ -663,13 +667,16 @@ ef_truncate(int type, int count)
ep->fids = count; ep->fids = count;
if (ep->flags & EFF_MEM) { if (ep->flags & EFF_MEM) {
need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
if (!(ep->flags & EFF_STATIC)) { if (!(ep->flags & EFF_STATIC)) {
if (!ef_realloc_cache(ep, count)) { if (!ef_realloc_cache(ep, count + need_sentinel)) {
logerror("Can't shrink %s cache after truncate (%s)", logerror("Can't shrink %s cache after truncate (%s)",
ep->name, strerror(errno)); ep->name, strerror(errno));
/* continue with unshrunk cache */ /* continue with unshrunk cache */
} }
} }
if (need_sentinel)
memset(ep->cache + count * ep->size, 0, ep->size);
ep->cids = count; ep->cids = count;
} else { } else {
if (ep->baseid >= count) if (ep->baseid >= count)

View file

@ -482,17 +482,16 @@ static void *
getobj(void) getobj(void)
{ {
struct empfile *ep = &empfile[cur_type]; struct empfile *ep = &empfile[cur_type];
int need_sentinel = !EF_IS_GAME_STATE(cur_type);
if (!cur_obj) { if (!cur_obj) {
cur_obj_is_blank = cur_id >= ep->fids - !!need_sentinel; cur_obj_is_blank = cur_id >= ep->fids;
if (cur_obj_is_blank) { if (cur_obj_is_blank) {
if (ef_ensure_space(cur_type, cur_id + !!need_sentinel, 1)) if (ef_ensure_space(cur_type, cur_id, 1))
cur_obj = ef_ptr(cur_type, cur_id); cur_obj = ef_ptr(cur_type, cur_id);
/* FIXME diagnose out of dynamic memory vs. static table full */ /* FIXME diagnose out of dynamic memory vs. static table full */
if (!cur_obj) if (!cur_obj)
gripe("Can't put ID %d into table %s, it holds only 0..%d.", gripe("Can't put ID %d into table %s, it holds only 0..%d.",
cur_id, ep->name, ep->fids - !!need_sentinel - 1); cur_id, ep->name, ep->fids - 1);
} else } else
cur_obj = ef_ptr(cur_type, cur_id); cur_obj = ef_ptr(cur_type, cur_id);
} }
@ -962,13 +961,8 @@ static int
xubody(FILE *fp) xubody(FILE *fp)
{ {
struct empfile *ep = &empfile[cur_type]; struct empfile *ep = &empfile[cur_type];
int need_sentinel = !EF_IS_GAME_STATE(cur_type);
int old_maxid = ep->fids;
int i, maxid, ch; int i, maxid, ch;
if (old_maxid == 0 && need_sentinel)
ef_ensure_space(cur_type, 0, 1);
maxid = 0; maxid = 0;
for (i = 0;; ++i) { for (i = 0;; ++i) {
while ((ch = skipfs(fp)) == '\n') while ((ch = skipfs(fp)) == '\n')
@ -983,12 +977,6 @@ xubody(FILE *fp)
maxid = MAX(maxid, cur_id + 1); maxid = MAX(maxid, cur_id + 1);
} }
if (maxid >= old_maxid && need_sentinel) {
/* appended a sentinel, strip it off */
ep->fids--;
ep->cids--;
}
if (CANT_HAPPEN(maxid > ep->fids)) if (CANT_HAPPEN(maxid > ep->fids))
maxid = ep->fids; maxid = ep->fids;
if (maxid < ep->fids) { if (maxid < ep->fids) {

View file

@ -86,7 +86,7 @@
SZ((array)), 0, SZ((array)) - 1, SZ((array)) - 1, -1, NULL, NULL SZ((array)), 0, SZ((array)) - 1, SZ((array)) - 1, -1, NULL, NULL
/* Common configuration table flags */ /* Common configuration table flags */
#define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC) #define EFF_CFG (EFF_PRIVATE | EFF_MEM | EFF_STATIC | EFF_SENTINEL)
struct empfile empfile[] = { struct empfile empfile[] = {
/* /*