summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'firmware')
-rw-r--r--firmware/buflib.c18
-rw-r--r--firmware/core_alloc.c31
-rw-r--r--firmware/include/buflib.h47
-rw-r--r--firmware/include/core_alloc.h14
-rw-r--r--firmware/kernel/thread.c2
-rw-r--r--firmware/target/hosted/sdl/thread-sdl.c2
6 files changed, 71 insertions, 43 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index be1ff01af7..0f8836b3b7 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -991,8 +991,7 @@ unsigned buflib_pin_count(struct buflib_context *ctx, int handle)
991 return data[idx_PIN].pincount; 991 return data[idx_PIN].pincount;
992} 992}
993 993
994#ifdef DEBUG 994#ifdef BUFLIB_DEBUG_GET_DATA
995
996void *buflib_get_data(struct buflib_context *ctx, int handle) 995void *buflib_get_data(struct buflib_context *ctx, int handle)
997{ 996{
998 if (handle <= 0) 997 if (handle <= 0)
@@ -1000,7 +999,9 @@ void *buflib_get_data(struct buflib_context *ctx, int handle)
1000 999
1001 return (void*)(ctx->handle_table[-handle].alloc); 1000 return (void*)(ctx->handle_table[-handle].alloc);
1002} 1001}
1002#endif
1003 1003
1004#ifdef BUFLIB_DEBUG_CHECK_VALID
1004void buflib_check_valid(struct buflib_context *ctx) 1005void buflib_check_valid(struct buflib_context *ctx)
1005{ 1006{
1006 for(union buflib_data *block = ctx->buf_start; 1007 for(union buflib_data *block = ctx->buf_start;
@@ -1016,10 +1017,11 @@ void buflib_check_valid(struct buflib_context *ctx)
1016} 1017}
1017#endif 1018#endif
1018 1019
1019#ifdef BUFLIB_DEBUG_BLOCK_SINGLE 1020#ifdef BUFLIB_DEBUG_PRINT
1020int buflib_get_num_blocks(struct buflib_context *ctx) 1021int buflib_get_num_blocks(struct buflib_context *ctx)
1021{ 1022{
1022 int i = 0; 1023 int i = 0;
1024
1023 for(union buflib_data *block = ctx->buf_start; 1025 for(union buflib_data *block = ctx->buf_start;
1024 block < ctx->alloc_end; 1026 block < ctx->alloc_end;
1025 block += abs(block->val)) 1027 block += abs(block->val))
@@ -1027,11 +1029,12 @@ int buflib_get_num_blocks(struct buflib_context *ctx)
1027 check_block_length(ctx, block); 1029 check_block_length(ctx, block);
1028 ++i; 1030 ++i;
1029 } 1031 }
1032
1030 return i; 1033 return i;
1031} 1034}
1032 1035
1033void buflib_print_block_at(struct buflib_context *ctx, int block_num, 1036bool buflib_print_block_at(struct buflib_context *ctx, int block_num,
1034 char* buf, size_t bufsize) 1037 char *buf, size_t bufsize)
1035{ 1038{
1036 for(union buflib_data *block = ctx->buf_start; 1039 for(union buflib_data *block = ctx->buf_start;
1037 block < ctx->alloc_end; 1040 block < ctx->alloc_end;
@@ -1044,8 +1047,13 @@ void buflib_print_block_at(struct buflib_context *ctx, int block_num,
1044 snprintf(buf, bufsize, "%8p: val: %4ld (%sallocated)", 1047 snprintf(buf, bufsize, "%8p: val: %4ld (%sallocated)",
1045 block, (long)block->val, 1048 block, (long)block->val,
1046 block->val > 0 ? "" : "un"); 1049 block->val > 0 ? "" : "un");
1050 return true;
1047 } 1051 }
1048 } 1052 }
1053
1054 if (bufsize > 0)
1055 *buf = '\0';
1056 return false;
1049} 1057}
1050#endif 1058#endif
1051 1059
diff --git a/firmware/core_alloc.c b/firmware/core_alloc.c
index f5cc7f3189..948911b973 100644
--- a/firmware/core_alloc.c
+++ b/firmware/core_alloc.c
@@ -35,8 +35,11 @@ unsigned char *audiobufend = audiobuffer + sizeof(audiobuffer);
35extern unsigned char *audiobufend; 35extern unsigned char *audiobufend;
36#endif 36#endif
37 37
38#ifdef BUFLIB_DEBUG_PRINT
38/* debug test alloc */ 39/* debug test alloc */
39static int test_alloc; 40static int test_alloc;
41#endif
42
40void core_allocator_init(void) 43void core_allocator_init(void)
41{ 44{
42 unsigned char *start = ALIGN_UP(audiobuffer, sizeof(intptr_t)); 45 unsigned char *start = ALIGN_UP(audiobuffer, sizeof(intptr_t));
@@ -51,16 +54,9 @@ void core_allocator_init(void)
51 54
52 buflib_init(&core_ctx, start, audiobufend - start); 55 buflib_init(&core_ctx, start, audiobufend - start);
53 56
57#ifdef BUFLIB_DEBUG_PRINT
54 test_alloc = core_alloc(112); 58 test_alloc = core_alloc(112);
55} 59#endif
56
57bool core_test_free(void)
58{
59 bool ret = test_alloc > 0;
60 if (ret)
61 test_alloc = core_free(test_alloc);
62
63 return ret;
64} 60}
65 61
66/* Allocate memory in the "core" context. See documentation 62/* Allocate memory in the "core" context. See documentation
@@ -119,17 +115,28 @@ unsigned core_pin_count(int handle)
119 return buflib_pin_count(&core_ctx, handle); 115 return buflib_pin_count(&core_ctx, handle);
120} 116}
121 117
118#ifdef BUFLIB_DEBUG_PRINT
122int core_get_num_blocks(void) 119int core_get_num_blocks(void)
123{ 120{
124 return buflib_get_num_blocks(&core_ctx); 121 return buflib_get_num_blocks(&core_ctx);
125} 122}
126 123
127void core_print_block_at(int block_num, char* buf, size_t bufsize) 124bool core_print_block_at(int block_num, char* buf, size_t bufsize)
128{ 125{
129 buflib_print_block_at(&core_ctx, block_num, buf, bufsize); 126 return buflib_print_block_at(&core_ctx, block_num, buf, bufsize);
130} 127}
131 128
132#ifdef DEBUG 129bool core_test_free(void)
130{
131 bool ret = test_alloc > 0;
132 if (ret)
133 test_alloc = core_free(test_alloc);
134
135 return ret;
136}
137#endif
138
139#ifdef BUFLIB_DEBUG_CHECK_VALID
133void core_check_valid(void) 140void core_check_valid(void)
134{ 141{
135 buflib_check_valid(&core_ctx); 142 buflib_check_valid(&core_ctx);
diff --git a/firmware/include/buflib.h b/firmware/include/buflib.h
index 349e4a3e7a..49e4db11c0 100644
--- a/firmware/include/buflib.h
+++ b/firmware/include/buflib.h
@@ -23,15 +23,21 @@
23* KIND, either express or implied. 23* KIND, either express or implied.
24* 24*
25****************************************************************************/ 25****************************************************************************/
26
27#ifndef _BUFLIB_H_ 26#ifndef _BUFLIB_H_
28#define _BUFLIB_H_ 27#define _BUFLIB_H_
28
29#include <stdint.h> 29#include <stdint.h>
30#include <stdbool.h> 30#include <stdbool.h>
31#include <string.h> 31#include <string.h>
32 32
33/* enable single block debugging */ 33/* add extra checks to buflib_get_data to catch bad handles */
34#define BUFLIB_DEBUG_BLOCK_SINGLE 34//#define BUFLIB_DEBUG_GET_DATA
35
36/* support integrity check */
37//#define BUFLIB_DEBUG_CHECK_VALID
38
39/* support debug printing of memory blocks */
40//#define BUFLIB_DEBUG_PRINT
35 41
36union buflib_data 42union buflib_data
37{ 43{
@@ -260,12 +266,12 @@ int buflib_alloc_maximum(struct buflib_context* ctx,
260 * 266 *
261 * Returns: The start pointer of the allocation 267 * Returns: The start pointer of the allocation
262 */ 268 */
263#ifdef DEBUG 269#ifdef BUFLIB_DEBUG_GET_DATA
264void* buflib_get_data(struct buflib_context *ctx, int handle); 270void *buflib_get_data(struct buflib_context *ctx, int handle);
265#else 271#else
266static inline void* buflib_get_data(struct buflib_context *ctx, int handle) 272static inline void *buflib_get_data(struct buflib_context *ctx, int handle)
267{ 273{
268 return (void*)(ctx->handle_table[-handle].alloc); 274 return (void *)ctx->handle_table[-handle].alloc;
269} 275}
270#endif 276#endif
271 277
@@ -342,29 +348,34 @@ void* buflib_buffer_out(struct buflib_context *ctx, size_t *size);
342 */ 348 */
343void buflib_buffer_in(struct buflib_context *ctx, int size); 349void buflib_buffer_in(struct buflib_context *ctx, int size);
344 350
345/* debugging */ 351#ifdef BUFLIB_DEBUG_PRINT
346
347/** 352/**
348 * Gets the number of blocks in the entire buffer, allocated or unallocated 353 * Return the number of blocks in the buffer, allocated or unallocated.
349 * 354 *
350 * Only available if BUFLIB_DEBUG_BLOCK_SIGNLE is defined 355 * Only available if BUFLIB_DEBUG_PRINT is defined.
351 */ 356 */
352int buflib_get_num_blocks(struct buflib_context *ctx); 357int buflib_get_num_blocks(struct buflib_context *ctx);
353 358
354/** 359/**
355 * Print information about a single block as indicated by block_num 360 * Write a string describing the block at index block_num to the
356 * into buf 361 * provided buffer. The buffer will always be null terminated and
362 * there is no provision to detect truncation. (A 40-byte buffer
363 * is enough to contain any returned string.)
357 * 364 *
358 * buflib_get_num_blocks() beforehand to get the total number of blocks, 365 * Returns false if the block index is out of bounds, and writes
359 * as passing an block_num higher than that is undefined 366 * an empty string.
360 * 367 *
361 * Only available if BUFLIB_DEBUG_BLOCK_SIGNLE is defined 368 * Only available if BUFLIB_DEBUG_PRINT is defined.
362 */ 369 */
363void buflib_print_block_at(struct buflib_context *ctx, int block_num, 370bool buflib_print_block_at(struct buflib_context *ctx, int block_num,
364 char* buf, size_t bufsize); 371 char *buf, size_t bufsize);
372#endif
365 373
374#ifdef BUFLIB_DEBUG_CHECK_VALID
366/** 375/**
367 * Check integrity of given buflib context 376 * Check integrity of given buflib context
368 */ 377 */
369void buflib_check_valid(struct buflib_context *ctx); 378void buflib_check_valid(struct buflib_context *ctx);
370#endif 379#endif
380
381#endif /* _BUFLIB_H_ */
diff --git a/firmware/include/core_alloc.h b/firmware/include/core_alloc.h
index 8011c227b1..22cc1988da 100644
--- a/firmware/include/core_alloc.h
+++ b/firmware/include/core_alloc.h
@@ -21,21 +21,23 @@ unsigned core_pin_count(int handle);
21int core_free(int handle); 21int core_free(int handle);
22size_t core_available(void); 22size_t core_available(void);
23size_t core_allocatable(void); 23size_t core_allocatable(void);
24#ifdef DEBUG 24
25#ifdef BUFLIB_DEBUG_CHECK_VALID
25void core_check_valid(void); 26void core_check_valid(void);
26#endif 27#endif
27 28
28/* DO NOT ADD wrappers for buflib_buffer_out/in. They do not call 29/* DO NOT ADD wrappers for buflib_buffer_out/in. They do not call
29 * the move callbacks and are therefore unsafe in the core */ 30 * the move callbacks and are therefore unsafe in the core */
30 31
31#ifdef BUFLIB_DEBUG_BLOCK_SINGLE 32#ifdef BUFLIB_DEBUG_PRINT
32int core_get_num_blocks(void); 33int core_get_num_blocks(void);
33void core_print_block_at(int block_num, char* buf, size_t bufsize); 34bool core_print_block_at(int block_num, char* buf, size_t bufsize);
34#endif
35 35
36/* frees the debug test alloc created at initialization, 36/* frees the debug test alloc created at initialization,
37 * since this is the first any further alloc should force a compaction run */ 37 * since this is the first any further alloc should force a compaction run
38 * only used if debug print is active */
38bool core_test_free(void); 39bool core_test_free(void);
40#endif
39 41
40static inline void* core_get_data(int handle) 42static inline void* core_get_data(int handle)
41{ 43{
diff --git a/firmware/kernel/thread.c b/firmware/kernel/thread.c
index a422624df7..25677c79f9 100644
--- a/firmware/kernel/thread.c
+++ b/firmware/kernel/thread.c
@@ -1019,7 +1019,7 @@ void switch_thread(void)
1019#ifdef RB_PROFILE 1019#ifdef RB_PROFILE
1020 profile_thread_stopped(THREAD_ID_SLOT(thread->id)); 1020 profile_thread_stopped(THREAD_ID_SLOT(thread->id));
1021#endif 1021#endif
1022#ifdef DEBUG 1022#ifdef BUFLIB_DEBUG_CHECK_VALID
1023 /* Check core_ctx buflib integrity */ 1023 /* Check core_ctx buflib integrity */
1024 core_check_valid(); 1024 core_check_valid();
1025#endif 1025#endif
diff --git a/firmware/target/hosted/sdl/thread-sdl.c b/firmware/target/hosted/sdl/thread-sdl.c
index a76941f103..17a0f847b5 100644
--- a/firmware/target/hosted/sdl/thread-sdl.c
+++ b/firmware/target/hosted/sdl/thread-sdl.c
@@ -215,7 +215,7 @@ void switch_thread(void)
215 } /* STATE_SLEEPING: */ 215 } /* STATE_SLEEPING: */
216 } 216 }
217 217
218#ifdef DEBUG 218#ifdef BUFLIB_DEBUG_CHECK_VALID
219 core_check_valid(); 219 core_check_valid();
220#endif 220#endif
221 __running_self_entry() = current; 221 __running_self_entry() = current;