summaryrefslogtreecommitdiff
path: root/firmware/buflib.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/buflib.c')
-rw-r--r--firmware/buflib.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index 294d2926d3..27a6965ee0 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -105,6 +105,8 @@ void
105buflib_init(struct buflib_context *ctx, void *buf, size_t size) 105buflib_init(struct buflib_context *ctx, void *buf, size_t size)
106{ 106{
107 union buflib_data *bd_buf = buf; 107 union buflib_data *bd_buf = buf;
108 BDEBUGF("buflib initialized with %lu.%02lu kiB",
109 (unsigned long)size / 1024, ((unsigned long)size%1000)/10);
108 110
109 /* Align on sizeof(buflib_data), to prevent unaligned access */ 111 /* Align on sizeof(buflib_data), to prevent unaligned access */
110 ALIGN_BUFFER(bd_buf, size, sizeof(union buflib_data)); 112 ALIGN_BUFFER(bd_buf, size, sizeof(union buflib_data));
@@ -119,9 +121,6 @@ buflib_init(struct buflib_context *ctx, void *buf, size_t size)
119 */ 121 */
120 ctx->alloc_end = bd_buf; 122 ctx->alloc_end = bd_buf;
121 ctx->compact = true; 123 ctx->compact = true;
122
123 BDEBUGF("buflib initialized with %lu.%2lu kiB",
124 (unsigned long)size / 1024, ((unsigned long)size%1000)/10);
125} 124}
126 125
127bool buflib_context_relocate(struct buflib_context *ctx, void *buf) 126bool buflib_context_relocate(struct buflib_context *ctx, void *buf)
@@ -206,12 +205,16 @@ void handle_free(struct buflib_context *ctx, union buflib_data *handle)
206} 205}
207 206
208/* Get the start block of an allocation */ 207/* Get the start block of an allocation */
209static union buflib_data* handle_to_block(struct buflib_context* ctx, int handle) 208static inline
209union buflib_data* handle_to_block(struct buflib_context* ctx, int handle)
210{ 210{
211 union buflib_data* name_field = 211 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data));
212 (union buflib_data*)buflib_get_name(ctx, handle); 212 /* this is a valid case, e.g. during buflib_alloc_ex() when the handle
213 213 * has already been allocated but not the data */
214 return name_field ? name_field - 3 : NULL; 214 if (!data)
215 return NULL;
216 volatile size_t len = data[-2].val;
217 return data - (len + 4);
215} 218}
216 219
217/* Shrink the handle table, returning true if its size was reduced, false if 220/* Shrink the handle table, returning true if its size was reduced, false if
@@ -480,7 +483,7 @@ buflib_buffer_in(struct buflib_context *ctx, int size)
480int 483int
481buflib_alloc(struct buflib_context *ctx, size_t size) 484buflib_alloc(struct buflib_context *ctx, size_t size)
482{ 485{
483 return buflib_alloc_ex(ctx, size, "<anonymous>", NULL); 486 return buflib_alloc_ex(ctx, size, NULL, NULL);
484} 487}
485 488
486/* Allocate a buffer of size bytes, returning a handle for it. 489/* Allocate a buffer of size bytes, returning a handle for it.
@@ -588,7 +591,8 @@ buffer_alloc:
588 block->val = size; 591 block->val = size;
589 block[1].handle = handle; 592 block[1].handle = handle;
590 block[2].ops = ops; 593 block[2].ops = ops;
591 strcpy(block[3].name, name); 594 if (name_len > 0)
595 strcpy(block[3].name, name);
592 name_len_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len); 596 name_len_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len);
593 name_len_slot->val = 1 + name_len/sizeof(union buflib_data); 597 name_len_slot->val = 1 + name_len/sizeof(union buflib_data);
594 crc_slot = (union buflib_data*)(name_len_slot + 1); 598 crc_slot = (union buflib_data*)(name_len_slot + 1);
@@ -599,7 +603,7 @@ buffer_alloc:
599 603
600 BDEBUGF("buflib_alloc_ex: size=%d handle=%p clb=%p crc=0x%0x name=\"%s\"\n", 604 BDEBUGF("buflib_alloc_ex: size=%d handle=%p clb=%p crc=0x%0x name=\"%s\"\n",
601 (unsigned int)size, (void *)handle, (void *)ops, 605 (unsigned int)size, (void *)handle, (void *)ops,
602 (unsigned int)crc_slot->crc, block[3].name); 606 (unsigned int)crc_slot->crc, name ? block[3].name:"");
603 607
604 block += size; 608 block += size;
605 /* alloc_end must be kept current if we're taking the last block. */ 609 /* alloc_end must be kept current if we're taking the last block. */
@@ -889,8 +893,6 @@ buflib_shrink(struct buflib_context* ctx, int handle, void* new_start, size_t ne
889const char* buflib_get_name(struct buflib_context *ctx, int handle) 893const char* buflib_get_name(struct buflib_context *ctx, int handle)
890{ 894{
891 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data)); 895 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data));
892 if (!data)
893 return NULL;
894 size_t len = data[-2].val; 896 size_t len = data[-2].val;
895 if (len <= 1) 897 if (len <= 1)
896 return NULL; 898 return NULL;