From d25a512caf4adf2aac3ce6ac8e1f2c556c0c6c37 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Wed, 29 May 2013 07:12:01 +0200 Subject: buflib: Try harder in buflib_alloc_maximum(). This function will now ask shrinkable allocations to give up all of their memory. With future support of playback.c this can be used as a safe replacement for audio_get_buffer(). Change-Id: I290a51d2c75254e66baf5698c41dc444dea6247a --- firmware/buflib.c | 13 ++++++++++++- firmware/include/buflib.h | 8 +++++--- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'firmware') diff --git a/firmware/buflib.c b/firmware/buflib.c index 1aa7404ece..4233bf9a61 100644 --- a/firmware/buflib.c +++ b/firmware/buflib.c @@ -724,7 +724,18 @@ buflib_alloc_maximum(struct buflib_context* ctx, const char* name, size_t *size, /* limit name to 16 since that's what buflib_available() accounts for it */ char buf[16]; - *size = buflib_available(ctx); + /* ignore ctx->compact because it's true if all movable blocks are contiguous + * even if the buffer has holes due to unmovable allocations */ + unsigned hints; + size_t bufsize = ctx->handle_table - ctx->buf_start; + bufsize = MIN(BUFLIB_SHRINK_SIZE_MASK, bufsize*sizeof(union buflib_data)); /* make it bytes */ + /* try as hard as possible to free up space. allocations are + * welcome to give up some or all of their memory */ + hints = BUFLIB_SHRINK_POS_BACK | BUFLIB_SHRINK_POS_FRONT | bufsize; + /* compact until no space can be gained anymore */ + while (buflib_compact_and_shrink(ctx, hints)); + + *size = buflib_allocatable(ctx); if (*size <= 0) /* OOM */ return -1; diff --git a/firmware/include/buflib.h b/firmware/include/buflib.h index 7183951c6c..aa6c232f72 100644 --- a/firmware/include/buflib.h +++ b/firmware/include/buflib.h @@ -111,10 +111,10 @@ struct buflib_callbacks { void (*sync_callback)(int handle, bool sync_on); }; -#define BUFLIB_SHRINK_POS_MASK ((1<<0|1<<1)<<30) #define BUFLIB_SHRINK_SIZE_MASK (~BUFLIB_SHRINK_POS_MASK) #define BUFLIB_SHRINK_POS_FRONT (1u<<31) #define BUFLIB_SHRINK_POS_BACK (1u<<30) +#define BUFLIB_SHRINK_POS_MASK (BUFLIB_SHRINK_POS_FRONT|BUFLIB_SHRINK_POS_BACK) /** * Possible return values for the callbacks, some of them can cause @@ -193,8 +193,10 @@ int buflib_alloc_ex(struct buflib_context *ctx, size_t size, const char *name, * will allow buflib to permit allocations by shrinking the buffer returned by * this function. * - * Note that this currently gives whatever buflib_available() returns. However, - * do not depend on this behavior, it may change. + * Note that this might return many more bytes than buflib_available() or + * buflib_allocatable() return, because it agressively compacts the pool + * and even shrinks other allocations. However, do not depend on this behavior, + * it may change. * * name: A string identifier giving this allocation a name * size: The actual size will be returned into size -- cgit v1.2.3