Relaxed assertions on lfs3_handle_close

Though note most high-level calls (lfs3_file_close, lfs3_dir_close,
etc), still include an assertion at a higher-level.

Why make the internal APIs harder to use than they need to be? As a
plus this drops the need for a separate bool-returning
lfs3_handle_close_.

Saves a bit of code:

                 code          stack          ctx
  before:       35924           2280          660
  after:        35912 (-0.0%)   2280 (+0.0%)  660 (+0.0%)

                 code          stack          ctx
  gbmap before: 38812           2296          772
  gbmap after:  38800 (-0.0%)   2296 (+0.0%)  772 (+0.0%)
This commit is contained in:
Christopher Haster
2025-11-16 01:00:48 -06:00
parent c16c4a00d3
commit d9adbc9ca1

22
lfs3.c
View File

@ -7342,7 +7342,7 @@ static void lfs3_handle_open(lfs3_t *lfs3, lfs3_handle_t *h) {
lfs3->handles = h;
}
static bool lfs3_handle_close_(lfs3_t *lfs3, lfs3_handle_t *h) {
static bool lfs3_handle_close(lfs3_t *lfs3, lfs3_handle_t *h) {
// remove from opened list
for (lfs3_handle_t **h_ = &lfs3->handles; *h_; h_ = &(*h_)->next) {
if (*h_ == h) {
@ -7354,11 +7354,6 @@ static bool lfs3_handle_close_(lfs3_t *lfs3, lfs3_handle_t *h) {
return false;
}
static void lfs3_handle_close(lfs3_t *lfs3, lfs3_handle_t *h) {
LFS3_ASSERT(lfs3_handle_isopen(lfs3, h));
lfs3_handle_close_(lfs3, h);
}
// check if a given mid is open
static bool lfs3_mid_isopen(const lfs3_t *lfs3,
lfs3_smid_t mid, uint32_t mask) {
@ -7383,7 +7378,7 @@ static bool lfs3_mid_isopen(const lfs3_t *lfs3,
// iterations through the handle list
static void lfs3_handle_rewind(lfs3_t *lfs3, lfs3_handle_t *h) {
bool entangled = lfs3_handle_close_(lfs3, h);
bool entangled = lfs3_handle_close(lfs3, h);
h->next = lfs3->handles;
if (entangled) {
lfs3->handles = h;
@ -7393,7 +7388,7 @@ static void lfs3_handle_rewind(lfs3_t *lfs3, lfs3_handle_t *h) {
// seek _after_ h_
static void lfs3_handle_seek(lfs3_t *lfs3, lfs3_handle_t *h,
lfs3_handle_t **h_) {
bool entangled = lfs3_handle_close_(lfs3, h);
bool entangled = lfs3_handle_close(lfs3, h);
h->next = *h_;
if (entangled) {
*h_ = h;
@ -12235,7 +12230,7 @@ static int lfs3_file_fetch(lfs3_t *lfs3, lfs3_file_t *file, uint32_t flags) {
}
// needed in lfs3_file_opencfg
static void lfs3_file_close_(lfs3_t *lfs3, const lfs3_file_t *file);
static void lfs3_file_close_(lfs3_t *lfs3, lfs3_file_t *file);
#ifndef LFS3_RDONLY
static int lfs3_file_sync_(lfs3_t *lfs3, lfs3_file_t *file,
const lfs3_name_t *name);
@ -12410,7 +12405,6 @@ int lfs3_file_opencfg_(lfs3_t *lfs3, lfs3_file_t *file,
LFS3_CK_CKMETA
| LFS3_CK_CKDATA));
if (err) {
lfs3_handle_close(lfs3, &file->b.h);
goto failed;
}
}
@ -12472,8 +12466,11 @@ int lfs3_file_open(lfs3_t *lfs3, lfs3_file_t *file,
}
// clean up resources
static void lfs3_file_close_(lfs3_t *lfs3, const lfs3_file_t *file) {
static void lfs3_file_close_(lfs3_t *lfs3, lfs3_file_t *file) {
(void)lfs3;
// remove from tracked mdirs
lfs3_handle_close(lfs3, &file->b.h);
// clean up memory
if (!file->cfg->fcache_buffer) {
lfs3_free(file->cache.buffer);
@ -12517,9 +12514,6 @@ int lfs3_file_close(lfs3_t *lfs3, lfs3_file_t *file) {
err = lfs3_file_sync(lfs3, file);
}
// remove from tracked mdirs
lfs3_handle_close(lfs3, &file->b.h);
// clean up resources
lfs3_file_close_(lfs3, file);