summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--firmware/buflib.c27
-rw-r--r--firmware/core_alloc.c5
-rw-r--r--firmware/include/buflib.h15
-rw-r--r--firmware/include/core_alloc.h1
4 files changed, 41 insertions, 7 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index 2ceb7fcb16..1aa7404ece 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -647,9 +647,9 @@ free_space_at_end(struct buflib_context* ctx)
647 return 0; 647 return 0;
648} 648}
649 649
650/* Return the maximum allocatable memory in bytes */ 650/* Return the maximum allocatable contiguous memory in bytes */
651size_t 651size_t
652buflib_available(struct buflib_context* ctx) 652buflib_allocatable(struct buflib_context* ctx)
653{ 653{
654 union buflib_data *this; 654 union buflib_data *this;
655 size_t free_space = 0, max_free_space = 0; 655 size_t free_space = 0, max_free_space = 0;
@@ -687,6 +687,29 @@ buflib_available(struct buflib_context* ctx)
687 return 0; 687 return 0;
688} 688}
689 689
690/* Return the amount of unallocated memory in bytes (even if not contiguous) */
691size_t
692buflib_available(struct buflib_context* ctx)
693{
694 union buflib_data *this;
695 size_t free_space = 0;
696
697 /* now look if there's free in holes */
698 for(this = find_first_free(ctx); this < ctx->alloc_end; this += abs(this->val))
699 {
700 if (this->val < 0)
701 {
702 free_space += -this->val;
703 continue;
704 }
705 }
706
707 free_space *= sizeof(union buflib_data); /* make it bytes */
708 free_space += free_space_at_end(ctx);
709
710 return free_space;
711}
712
690/* 713/*
691 * Allocate all available (as returned by buflib_available()) memory and return 714 * Allocate all available (as returned by buflib_available()) memory and return
692 * a handle to it 715 * a handle to it
diff --git a/firmware/core_alloc.c b/firmware/core_alloc.c
index 47faed6e95..aa662fbee5 100644
--- a/firmware/core_alloc.c
+++ b/firmware/core_alloc.c
@@ -67,6 +67,11 @@ size_t core_available(void)
67 return buflib_available(&core_ctx); 67 return buflib_available(&core_ctx);
68} 68}
69 69
70size_t core_allocatable(void)
71{
72 return buflib_allocatable(&core_ctx);
73}
74
70int core_free(int handle) 75int core_free(int handle)
71{ 76{
72 return buflib_free(&core_ctx, handle); 77 return buflib_free(&core_ctx, handle);
diff --git a/firmware/include/buflib.h b/firmware/include/buflib.h
index 6c9ccf7402..7183951c6c 100644
--- a/firmware/include/buflib.h
+++ b/firmware/include/buflib.h
@@ -143,15 +143,20 @@ void buflib_init(struct buflib_context *context, void *buf, size_t size);
143 143
144 144
145/** 145/**
146 * Returns how many bytes left the buflib has to satisfy allocations. 146 * Returns the amount of unallocated bytes. It does not mean this amount
147 * can be actually allocated because they might not be contiguous.
147 * 148 *
148 * This function does not yet consider possible compaction so there might 149 * Returns: The number of unallocated bytes in the memory pool.
149 * be more space left. This may change in the future.
150 *
151 * Returns: The number of bytes left in the memory pool.
152 */ 150 */
153size_t buflib_available(struct buflib_context *ctx); 151size_t buflib_available(struct buflib_context *ctx);
154 152
153/**
154 * Returns the biggest possible allocation that can be determined to succeed.
155 *
156 * Returns: The amount of bytes of the biggest unallocated, contiguous region.
157 */
158size_t buflib_allocatable(struct buflib_context *ctx);
159
155 160
156/** 161/**
157 * Allocates memory from buflib's memory pool 162 * Allocates memory from buflib's memory pool
diff --git a/firmware/include/core_alloc.h b/firmware/include/core_alloc.h
index d234947db1..a100b7cc6c 100644
--- a/firmware/include/core_alloc.h
+++ b/firmware/include/core_alloc.h
@@ -16,6 +16,7 @@ int core_alloc_maximum(const char* name, size_t *size, struct buflib_callbacks *
16bool core_shrink(int handle, void* new_start, size_t new_size); 16bool core_shrink(int handle, void* new_start, size_t new_size);
17int core_free(int handle); 17int core_free(int handle);
18size_t core_available(void); 18size_t core_available(void);
19size_t core_allocatable(void);
19 20
20/* DO NOT ADD wrappers for buflib_buffer_out/in. They do not call 21/* DO NOT ADD wrappers for buflib_buffer_out/in. They do not call
21 * the move callbacks and are therefore unsafe in the core */ 22 * the move callbacks and are therefore unsafe in the core */