summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-03-22 14:16:58 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-03-22 14:59:12 -0400
commit9827b2bae1d3864dffb06b50e2f59af594154693 (patch)
treeca95e69f3ee583b4216b2691648f3cb61bee4b9e /src
parentc643e1dd27e633c67589b622a06582bbb6fb5cf6 (diff)
downloadbfs-9827b2bae1d3864dffb06b50e2f59af594154693.tar.xz
bftw: Use a signed integer for dir_limit
Diffstat (limited to 'src')
-rw-r--r--src/bftw.c33
1 files changed, 8 insertions, 25 deletions
diff --git a/src/bftw.c b/src/bftw.c
index 50b8b02..6130c44 100644
--- a/src/bftw.c
+++ b/src/bftw.c
@@ -558,9 +558,7 @@ struct bftw_cache {
/** bfs_dir arena. */
struct arena dirs;
/** Remaining bfs_dir capacity. */
- size_t dir_limit;
- /** Excess force-allocated bfs_dirs. */
- size_t dir_excess;
+ int dir_limit;
/** bfs_stat arena. */
struct arena stat_bufs;
@@ -576,47 +574,32 @@ static void bftw_cache_init(struct bftw_cache *cache, size_t capacity) {
bfs_dir_arena(&cache->dirs);
- cache->dir_limit = capacity - 1;
- if (cache->dir_limit > 1024) {
+ if (cache->capacity > 1024) {
cache->dir_limit = 1024;
+ } else {
+ cache->dir_limit = capacity - 1;
}
- cache->dir_excess = 0;
-
ARENA_INIT(&cache->stat_bufs, struct bfs_stat);
}
/** Allocate a directory. */
static struct bfs_dir *bftw_allocdir(struct bftw_cache *cache, bool force) {
- size_t limit = cache->dir_limit;
- size_t excess = cache->dir_excess;
-
- if (cache->dir_limit > 0) {
- --cache->dir_limit;
- } else if (force) {
- ++cache->dir_excess;
- } else {
+ if (!force && cache->dir_limit <= 0) {
errno = ENOMEM;
return NULL;
}
struct bfs_dir *dir = arena_alloc(&cache->dirs);
- if (!dir) {
- cache->dir_limit = limit;
- cache->dir_excess = excess;
+ if (dir) {
+ --cache->dir_limit;
}
-
return dir;
}
/** Free a directory. */
static void bftw_freedir(struct bftw_cache *cache, struct bfs_dir *dir) {
- if (cache->dir_excess > 0) {
- --cache->dir_excess;
- } else {
- ++cache->dir_limit;
- }
-
+ ++cache->dir_limit;
arena_free(&cache->dirs, dir);
}