mmap-cache: modernize mmap_cache_add_fd()

- use hashmap_ensure_put(),
- use _cleanup_free_ attribute,
- use structured initializer.
This commit is contained in:
Yu Watanabe
2023-10-01 06:35:02 +09:00
parent 397caa8122
commit 8926a6a467
5 changed files with 37 additions and 31 deletions
+2 -4
View File
@@ -4030,11 +4030,9 @@ int journal_file_open(
newly_created = f->last_stat.st_size == 0 && journal_file_writable(f);
}
f->cache_fd = mmap_cache_add_fd(mmap_cache, f->fd, mmap_prot_from_open_flags(open_flags));
if (!f->cache_fd) {
r = -ENOMEM;
r = mmap_cache_add_fd(mmap_cache, f->fd, mmap_prot_from_open_flags(open_flags), &f->cache_fd);
if (r < 0)
goto fail;
}
if (newly_created) {
(void) journal_file_warn_btrfs(f);
+9 -9
View File
@@ -875,21 +875,21 @@ int journal_file_verify(
}
m = mmap_cache_fd_cache(f->cache_fd);
cache_data_fd = mmap_cache_add_fd(m, data_fd, PROT_READ|PROT_WRITE);
if (!cache_data_fd) {
r = log_oom();
r = mmap_cache_add_fd(m, data_fd, PROT_READ|PROT_WRITE, &cache_data_fd);
if (r < 0) {
log_error_errno(r, "Failed to cache data file: %m");
goto fail;
}
cache_entry_fd = mmap_cache_add_fd(m, entry_fd, PROT_READ|PROT_WRITE);
if (!cache_entry_fd) {
r = log_oom();
r = mmap_cache_add_fd(m, entry_fd, PROT_READ|PROT_WRITE, &cache_entry_fd);
if (r < 0) {
log_error_errno(r, "Failed to cache entry file: %m");
goto fail;
}
cache_entry_array_fd = mmap_cache_add_fd(m, entry_array_fd, PROT_READ|PROT_WRITE);
if (!cache_entry_array_fd) {
r = log_oom();
r = mmap_cache_add_fd(m, entry_array_fd, PROT_READ|PROT_WRITE, &cache_entry_array_fd);
if (r < 0) {
log_error_errno(r, "Failed to cache entry array file: %m");
goto fail;
}
+24 -16
View File
@@ -541,34 +541,42 @@ bool mmap_cache_fd_got_sigbus(MMapFileDescriptor *f) {
return f->sigbus;
}
MMapFileDescriptor* mmap_cache_add_fd(MMapCache *m, int fd, int prot) {
MMapFileDescriptor *f;
int mmap_cache_add_fd(MMapCache *m, int fd, int prot, MMapFileDescriptor **ret) {
_cleanup_free_ MMapFileDescriptor *f = NULL;
MMapFileDescriptor *existing;
int r;
assert(m);
assert(fd >= 0);
f = hashmap_get(m->fds, FD_TO_PTR(fd));
if (f)
return f;
existing = hashmap_get(m->fds, FD_TO_PTR(fd));
if (existing) {
if (ret)
*ret = existing;
return 0;
}
r = hashmap_ensure_allocated(&m->fds, NULL);
if (r < 0)
return NULL;
f = new0(MMapFileDescriptor, 1);
f = new(MMapFileDescriptor, 1);
if (!f)
return NULL;
return -ENOMEM;
r = hashmap_put(m->fds, FD_TO_PTR(fd), f);
*f = (MMapFileDescriptor) {
.fd = fd,
.prot = prot,
};
r = hashmap_ensure_put(&m->fds, NULL, FD_TO_PTR(fd), f);
if (r < 0)
return mfree(f);
return r;
assert(r > 0);
f->cache = mmap_cache_ref(m);
f->fd = fd;
f->prot = prot;
return f;
if (ret)
*ret = f;
TAKE_PTR(f);
return 1;
}
void mmap_cache_fd_free(MMapFileDescriptor *f) {
+1 -1
View File
@@ -23,7 +23,7 @@ int mmap_cache_fd_get(
size_t size,
struct stat *st,
void **ret);
MMapFileDescriptor* mmap_cache_add_fd(MMapCache *m, int fd, int prot);
int mmap_cache_add_fd(MMapCache *m, int fd, int prot, MMapFileDescriptor **ret);
MMapCache* mmap_cache_fd_cache(MMapFileDescriptor *f);
void mmap_cache_fd_free(MMapFileDescriptor *f);
+1 -1
View File
@@ -23,7 +23,7 @@ int main(int argc, char *argv[]) {
assert_se(x >= 0);
(void) unlink(px);
assert_se(fx = mmap_cache_add_fd(m, x, PROT_READ));
assert_se(mmap_cache_add_fd(m, x, PROT_READ, &fx) > 0);
y = mkostemp_safe(py);
assert_se(y >= 0);