#define EFF_GROUP bit(3)
/* Table is allocated statically */
#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 */
#define EFF_IMMUTABLE \
(EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
{
struct empfile *ep;
char *p;
- int i, id;
+ int need_sentinel, i, id;
if (ef_check(type) < 0)
return 0;
id = ep->fids;
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) {
logerror("Can't extend %s beyond %d elements",
- ep->name, ep->csize);
+ ep->name, ep->csize - need_sentinel);
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)",
ep->name, id + count, strerror(errno));
return 0;
if (do_write(ep, p, id, count) < 0)
return 0;
}
- ep->cids += count;
+ if (need_sentinel)
+ memset(ep->cache + (id + count) * ep->size, 0, ep->size);
+ ep->cids = id + count;
} else {
/* need a buffer, steal last cache slot */
if (ep->cids == ep->csize)
return 0;
}
}
- ep->fids += count;
+ ep->fids = id + count;
return 1;
}
ef_truncate(int type, int count)
{
struct empfile *ep;
+ int need_sentinel;
if (ef_check(type) < 0)
return 0;
ep->fids = count;
if (ep->flags & EFF_MEM) {
+ need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
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)",
ep->name, strerror(errno));
/* continue with unshrunk cache */
}
}
+ if (need_sentinel)
+ memset(ep->cache + count * ep->size, 0, ep->size);
ep->cids = count;
} else {
if (ep->baseid >= count)
getobj(void)
{
struct empfile *ep = &empfile[cur_type];
- int need_sentinel = !EF_IS_GAME_STATE(cur_type);
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 (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);
/* FIXME diagnose out of dynamic memory vs. static table full */
if (!cur_obj)
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
cur_obj = ef_ptr(cur_type, cur_id);
}
xubody(FILE *fp)
{
struct empfile *ep = &empfile[cur_type];
- int need_sentinel = !EF_IS_GAME_STATE(cur_type);
- int old_maxid = ep->fids;
int i, maxid, ch;
- if (old_maxid == 0 && need_sentinel)
- ef_ensure_space(cur_type, 0, 1);
-
maxid = 0;
for (i = 0;; ++i) {
while ((ch = skipfs(fp)) == '\n')
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))
maxid = ep->fids;
if (maxid < ep->fids) {
SZ((array)), 0, SZ((array)) - 1, SZ((array)) - 1, -1, NULL, NULL
/* 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[] = {
/*