summaryrefslogtreecommitdiffstats
path: root/firmware/buflib.c
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-04-03 10:45:11 +0100
committerAidan MacDonald <amachronic@protonmail.com>2022-09-19 15:09:51 -0400
commitecfec3e9bf9178299cb0fe64bd530a81e10b1142 (patch)
tree96f2edfb52e24a8f718b7a679970ba80eb495c08 /firmware/buflib.c
parent88b21731fca0ef969e7759c03653421e2ca3aa22 (diff)
downloadrockbox-ecfec3e9bf.tar.gz
rockbox-ecfec3e9bf.zip
buflib: add handle_to_block_end
Change-Id: I598cf32bc8cd08ab4bca3827645e0a38b284468f
Diffstat (limited to 'firmware/buflib.c')
-rw-r--r--firmware/buflib.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index 52d52459a4..2a4b4b4f14 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -317,6 +317,20 @@ void handle_free(struct buflib_context *ctx, union buflib_data *handle)
ctx->compact = false;
}
+static inline
+union buflib_data* handle_to_block_end(struct buflib_context *ctx, int handle)
+{
+ void *ptr = buflib_get_data(ctx, handle);
+
+ /* this is a valid case for shrinking if handle
+ * was freed by the shrink callback */
+ if (!ptr)
+ return NULL;
+
+ union buflib_data *data = ALIGN_DOWN(ptr, sizeof(*data));
+ return data;
+}
+
/* Get the start block of an allocation */
static inline
union buflib_data* handle_to_block(struct buflib_context* ctx, int handle)
@@ -1038,7 +1052,7 @@ buflib_shrink(struct buflib_context* ctx, int handle, void* new_start, size_t ne
const char* buflib_get_name(struct buflib_context *ctx, int handle)
{
- union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data));
+ union buflib_data *data = handle_to_block_end(ctx, handle);
size_t len = data[-bidx_BSIZE].val;
if (len <= BUFLIB_NUM_FIELDS)
return NULL;