summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/buflib.c13
-rw-r--r--firmware/include/buflib.h8
2 files changed, 17 insertions, 4 deletions
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,
724 /* limit name to 16 since that's what buflib_available() accounts for it */ 724 /* limit name to 16 since that's what buflib_available() accounts for it */
725 char buf[16]; 725 char buf[16];
726 726
727 *size = buflib_available(ctx); 727 /* ignore ctx->compact because it's true if all movable blocks are contiguous
728 * even if the buffer has holes due to unmovable allocations */
729 unsigned hints;
730 size_t bufsize = ctx->handle_table - ctx->buf_start;
731 bufsize = MIN(BUFLIB_SHRINK_SIZE_MASK, bufsize*sizeof(union buflib_data)); /* make it bytes */
732 /* try as hard as possible to free up space. allocations are
733 * welcome to give up some or all of their memory */
734 hints = BUFLIB_SHRINK_POS_BACK | BUFLIB_SHRINK_POS_FRONT | bufsize;
735 /* compact until no space can be gained anymore */
736 while (buflib_compact_and_shrink(ctx, hints));
737
738 *size = buflib_allocatable(ctx);
728 if (*size <= 0) /* OOM */ 739 if (*size <= 0) /* OOM */
729 return -1; 740 return -1;
730 741
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 {
111 void (*sync_callback)(int handle, bool sync_on); 111 void (*sync_callback)(int handle, bool sync_on);
112}; 112};
113 113
114#define BUFLIB_SHRINK_POS_MASK ((1<<0|1<<1)<<30)
115#define BUFLIB_SHRINK_SIZE_MASK (~BUFLIB_SHRINK_POS_MASK) 114#define BUFLIB_SHRINK_SIZE_MASK (~BUFLIB_SHRINK_POS_MASK)
116#define BUFLIB_SHRINK_POS_FRONT (1u<<31) 115#define BUFLIB_SHRINK_POS_FRONT (1u<<31)
117#define BUFLIB_SHRINK_POS_BACK (1u<<30) 116#define BUFLIB_SHRINK_POS_BACK (1u<<30)
117#define BUFLIB_SHRINK_POS_MASK (BUFLIB_SHRINK_POS_FRONT|BUFLIB_SHRINK_POS_BACK)
118 118
119/** 119/**
120 * Possible return values for the callbacks, some of them can cause 120 * 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,
193 * will allow buflib to permit allocations by shrinking the buffer returned by 193 * will allow buflib to permit allocations by shrinking the buffer returned by
194 * this function. 194 * this function.
195 * 195 *
196 * Note that this currently gives whatever buflib_available() returns. However, 196 * Note that this might return many more bytes than buflib_available() or
197 * do not depend on this behavior, it may change. 197 * buflib_allocatable() return, because it agressively compacts the pool
198 * and even shrinks other allocations. However, do not depend on this behavior,
199 * it may change.
198 * 200 *
199 * name: A string identifier giving this allocation a name 201 * name: A string identifier giving this allocation a name
200 * size: The actual size will be returned into size 202 * size: The actual size will be returned into size