diff --git a/firmware/core_alloc.c b/firmware/core_alloc.c index 2250f5c..85c52a1 100644 --- a/firmware/core_alloc.c +++ b/firmware/core_alloc.c @@ -1,5 +1,6 @@ #include +#include #include "core_alloc.h" #include "buflib.h" #include "buffer.h" @@ -15,14 +16,34 @@ void core_allocator_init(void) buffer_release_buffer(size); } -int core_alloc(const char* name, size_t size) +int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks *ops) { - return buflib_alloc_ex(&core_ctx, size, name, NULL); +#ifdef APPLICATION + int handle = buflib_alloc_ex(&core_ctx, sizeof(void*), name, ops); + char* buffer = (char*)malloc(size); + if (handle >= 0 && buffer) + { + char** data_ptr = core_get_data(handle); + *data_ptr = buffer; + printf("alloc: %d %x\n", handle, buffer); + return CORE_TO_APP(handle); + } + else if (buffer) + { + free(buffer); + handle = -1; + } + else + buflib_free(&core_ctx, handle); +#else + int handle = buflib_alloc_ex(&core_ctx, size, name, ops); +#endif + return handle; } -int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks *ops) +int core_alloc(const char* name, size_t size) { - return buflib_alloc_ex(&core_ctx, size, name, ops); + return core_alloc_ex(name, size, NULL); } size_t core_available(void) @@ -32,6 +53,14 @@ size_t core_available(void) int core_free(int handle) { +#ifdef APPLICATION + if (IS_APP_HANDLE(handle)) + { + handle = APP_TO_CORE(handle); + char** buf = core_get_data(handle); + free(*buf); + } +#endif return buflib_free(&core_ctx, handle); } @@ -42,6 +71,12 @@ int core_alloc_maximum(const char* name, size_t *size, struct buflib_callbacks * bool core_shrink(int handle, void* new_start, size_t new_size) { +#ifdef APPLICATION + if (IS_APP_HANDLE(handle)) + { + return false; + } +#endif return buflib_shrink(&core_ctx, handle, new_start, new_size); } diff --git a/firmware/include/core_alloc.h b/firmware/include/core_alloc.h index f5206c9..b352526 100644 --- a/firmware/include/core_alloc.h +++ b/firmware/include/core_alloc.h @@ -3,6 +3,7 @@ #define __CORE_ALLOC_H__ #include #include +#include #include "buflib.h" /* All functions below are wrappers for functions in buflib.h, except @@ -28,9 +29,26 @@ int core_get_num_blocks(void); void core_print_block_at(int block_num, char* buf, size_t bufsize); #endif +/* macros to help convert between application handles and core handles. + * 1<<31 would be a negative number so an error value, so use bit 30 to + * identify application handles. App handles use malloc() as the actual data + * store + */ +#define APP_TO_CORE(h) ((h)&(~(1<<30))) +#define CORE_TO_APP(h) ((h)|(1<<30)) +#define IS_APP_HANDLE(h) ((h)&(1<<30)) + static inline void* core_get_data(int handle) { extern struct buflib_context core_ctx; +#ifdef APPLICATION + if (IS_APP_HANDLE(handle)) + { + char **buf = buflib_get_data(&core_ctx, APP_TO_CORE(handle)); + printf("get %d %x\n", APP_TO_CORE(handle), *buf); + return *buf; + } +#endif return buflib_get_data(&core_ctx, handle); } #endif /* __CORE_ALLOC_H__ */