[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <175573712970.20753.1034438294852882216.stgit@frogsfrogsfrogs>
Date: Wed, 20 Aug 2025 18:10:13 -0700
From: "Darrick J. Wong" <djwong@...nel.org>
To: tytso@....edu
Cc: John@...ves.net, bernd@...ernd.com, linux-fsdevel@...r.kernel.org,
linux-ext4@...r.kernel.org, miklos@...redi.hu, amir73il@...il.com,
joannelkoong@...il.com, neal@...pa.dev
Subject: [PATCH 09/20] cache: embed struct cache in the owner
From: Darrick J. Wong <djwong@...nel.org>
It'll be easier to embed a struct cache into the object that owns the
cache rather than passing pointers around. This is the prelude to the
next patch, which will enable cache functions to walk back to the owning
struct.
Signed-off-by: "Darrick J. Wong" <djwong@...nel.org>
---
lib/support/cache.h | 10 ++++++++--
lib/support/cache.c | 38 ++++++++++++++++++++------------------
2 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/lib/support/cache.h b/lib/support/cache.h
index 16b17a9b7a1a51..993f1385dedcee 100644
--- a/lib/support/cache.h
+++ b/lib/support/cache.h
@@ -122,8 +122,14 @@ struct cache {
unsigned int c_max; /* max nodes ever used */
};
-struct cache *cache_init(int, unsigned int, const struct cache_operations *);
-void cache_destroy(struct cache *);
+static inline bool cache_initialized(const struct cache *cache)
+{
+ return cache->hash != NULL;
+}
+
+int cache_init(int flags, unsigned int size,
+ const struct cache_operations *ops, struct cache *cache);
+void cache_destroy(struct cache *cache);
void cache_walk(struct cache *, cache_walk_t);
void cache_purge(struct cache *);
void cache_flush(struct cache *);
diff --git a/lib/support/cache.c b/lib/support/cache.c
index d8f8231ac36d28..8b4f9f03c3899b 100644
--- a/lib/support/cache.c
+++ b/lib/support/cache.c
@@ -12,6 +12,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
+#include <errno.h>
#include "list.h"
#include "cache.h"
@@ -33,23 +34,18 @@
static unsigned int cache_generic_bulkrelse(struct cache *, struct list_head *);
-struct cache *
+int
cache_init(
int flags,
unsigned int hashsize,
- const struct cache_operations *cache_operations)
+ const struct cache_operations *cache_operations,
+ struct cache *cache)
{
- struct cache * cache;
unsigned int i, maxcount;
maxcount = hashsize * HASH_CACHE_RATIO;
- if (!(cache = malloc(sizeof(struct cache))))
- return NULL;
- if (!(cache->c_hash = calloc(hashsize, sizeof(struct cache_hash)))) {
- free(cache);
- return NULL;
- }
+ memset(cache, 0, sizeof(*cache));
cache->c_flags = flags;
cache->c_count = 0;
@@ -57,8 +53,6 @@ cache_init(
cache->c_hits = 0;
cache->c_misses = 0;
cache->c_maxcount = maxcount;
- cache->c_hashsize = hashsize;
- cache->c_hashshift = fls(hashsize) - 1;
cache->hash = cache_operations->hash;
cache->alloc = cache_operations->alloc;
cache->flush = cache_operations->flush;
@@ -70,18 +64,26 @@ cache_init(
cache->put = cache_operations->put;
pthread_mutex_init(&cache->c_mutex, NULL);
+ for (i = 0; i <= CACHE_DIRTY_PRIORITY; i++) {
+ list_head_init(&cache->c_mrus[i].cm_list);
+ cache->c_mrus[i].cm_count = 0;
+ pthread_mutex_init(&cache->c_mrus[i].cm_mutex, NULL);
+ }
+
+ cache->c_hash = calloc(hashsize, sizeof(struct cache_hash));
+ if (!cache->c_hash)
+ return ENOMEM;
+
+ cache->c_hashsize = hashsize;
+ cache->c_hashshift = fls(hashsize) - 1;
+
for (i = 0; i < hashsize; i++) {
list_head_init(&cache->c_hash[i].ch_list);
cache->c_hash[i].ch_count = 0;
pthread_mutex_init(&cache->c_hash[i].ch_mutex, NULL);
}
- for (i = 0; i <= CACHE_DIRTY_PRIORITY; i++) {
- list_head_init(&cache->c_mrus[i].cm_list);
- cache->c_mrus[i].cm_count = 0;
- pthread_mutex_init(&cache->c_mrus[i].cm_mutex, NULL);
- }
- return cache;
+ return 0;
}
static void
@@ -153,7 +155,7 @@ cache_destroy(
}
pthread_mutex_destroy(&cache->c_mutex);
free(cache->c_hash);
- free(cache);
+ memset(cache, 0, sizeof(*cache));
}
static unsigned int
Powered by blists - more mailing lists