diff options
-rw-r--r-- | firmware/buflib.c | 28 | ||||
-rw-r--r-- | firmware/core_alloc.c | 6 | ||||
-rw-r--r-- | firmware/include/buflib.h | 8 | ||||
-rw-r--r-- | firmware/include/core_alloc.h | 7 |
4 files changed, 27 insertions, 22 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 | |||
105 | buflib_init(struct buflib_context *ctx, void *buf, size_t size) | 105 | buflib_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 | ||
127 | bool buflib_context_relocate(struct buflib_context *ctx, void *buf) | 126 | bool 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 */ |
209 | static union buflib_data* handle_to_block(struct buflib_context* ctx, int handle) | 208 | static inline |
209 | union 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) | |||
480 | int | 483 | int |
481 | buflib_alloc(struct buflib_context *ctx, size_t size) | 484 | buflib_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 | |||
889 | const char* buflib_get_name(struct buflib_context *ctx, int handle) | 893 | const 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; |
diff --git a/firmware/core_alloc.c b/firmware/core_alloc.c index 68c400f669..58e12141e1 100644 --- a/firmware/core_alloc.c +++ b/firmware/core_alloc.c | |||
@@ -87,6 +87,12 @@ bool core_shrink(int handle, void* new_start, size_t new_size) | |||
87 | return buflib_shrink(&core_ctx, handle, new_start, new_size); | 87 | return buflib_shrink(&core_ctx, handle, new_start, new_size); |
88 | } | 88 | } |
89 | 89 | ||
90 | const char* core_get_name(int handle) | ||
91 | { | ||
92 | const char *name = buflib_get_name(&core_ctx, handle); | ||
93 | return name ?: "<anonymous>"; | ||
94 | } | ||
95 | |||
90 | int core_get_num_blocks(void) | 96 | int core_get_num_blocks(void) |
91 | { | 97 | { |
92 | return buflib_get_num_blocks(&core_ctx); | 98 | return buflib_get_num_blocks(&core_ctx); |
diff --git a/firmware/include/buflib.h b/firmware/include/buflib.h index 30484431f8..e912429b1f 100644 --- a/firmware/include/buflib.h +++ b/firmware/include/buflib.h | |||
@@ -303,12 +303,14 @@ void buflib_buffer_in(struct buflib_context *ctx, int size); | |||
303 | /* debugging */ | 303 | /* debugging */ |
304 | 304 | ||
305 | /** | 305 | /** |
306 | * Returns the name, as given to core_alloc() and core_allloc_ex(), of the | 306 | * Returns the name, as given to buflib_alloc() and buflib_allloc_ex(), of the |
307 | * allocation associated with the given handle | 307 | * allocation associated with the given handle. As naming allocations |
308 | * is optional, there might be no name associated. | ||
308 | * | 309 | * |
309 | * handle: The handle indicating the allocation | 310 | * handle: The handle indicating the allocation |
310 | * | 311 | * |
311 | * Returns: A pointer to the string identifier of the allocation | 312 | * Returns: A pointer to the string identifier of the allocation, or NULL |
313 | * if none was specified with buflib_alloc_ex/(. | ||
312 | */ | 314 | */ |
313 | const char* buflib_get_name(struct buflib_context *ctx, int handle); | 315 | const char* buflib_get_name(struct buflib_context *ctx, int handle); |
314 | 316 | ||
diff --git a/firmware/include/core_alloc.h b/firmware/include/core_alloc.h index 095cb5da11..67fe99dfdc 100644 --- a/firmware/include/core_alloc.h +++ b/firmware/include/core_alloc.h | |||
@@ -17,6 +17,7 @@ bool core_shrink(int handle, void* new_start, size_t new_size); | |||
17 | int core_free(int handle); | 17 | int core_free(int handle); |
18 | size_t core_available(void); | 18 | size_t core_available(void); |
19 | size_t core_allocatable(void); | 19 | size_t core_allocatable(void); |
20 | const char* core_get_name(int handle); | ||
20 | #ifdef DEBUG | 21 | #ifdef DEBUG |
21 | void core_check_valid(void); | 22 | void core_check_valid(void); |
22 | #endif | 23 | #endif |
@@ -43,10 +44,4 @@ static inline void* core_get_data(int handle) | |||
43 | return buflib_get_data(&core_ctx, handle); | 44 | return buflib_get_data(&core_ctx, handle); |
44 | } | 45 | } |
45 | 46 | ||
46 | static inline const char* core_get_name(int handle) | ||
47 | { | ||
48 | extern struct buflib_context core_ctx; | ||
49 | return buflib_get_name(&core_ctx, handle); | ||
50 | } | ||
51 | |||
52 | #endif /* __CORE_ALLOC_H__ */ | 47 | #endif /* __CORE_ALLOC_H__ */ |