diff --git a/include/wlib/mem/Construct.h b/include/wlib/mem/Construct.h index c1b4fd5..c6668d0 100644 --- a/include/wlib/mem/Construct.h +++ b/include/wlib/mem/Construct.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace wlp { template diff --git a/include/wlib/mem/New.h b/include/wlib/mem/New.h new file mode 100644 index 0000000..9b2bc2f --- /dev/null +++ b/include/wlib/mem/New.h @@ -0,0 +1,37 @@ +#if defined(WIO_PLATFORM_AVR) && defined(WIO_FRAMEWORK_ARDUINO) + +#include + +void* operator new(decltype(sizeof(0)), void* ptr) noexcept { + return ptr; +} + +#elif defined(WIO_PLATFORM_NATIVE) + +#include + +#else + +#include + +void *operator new(decltype(sizeof(0)) n) noexcept(false) { + return malloc(n); +} + +void *operator new(decltype(sizeof(0)), void *ptr) noexcept { + return ptr; +} + +void operator delete(void *ptr) throw() { + free(ptr); +} + +void *operator new[](decltype(sizeof(0)) n) noexcept(false) { + return malloc(n); +} + +void operator delete[](void *ptr) throw() { + free(ptr); +} + +#endif diff --git a/tests/arduino/debug.cpp b/tests/arduino/debug.cpp new file mode 100644 index 0000000..c1bcdf8 --- /dev/null +++ b/tests/arduino/debug.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +static constexpr int BUFFER_SIZE = 64; + +static char buffer[BUFFER_SIZE]; +static int wrt; + +extern int sprintf(char *str, const char *fmt, ...); +extern int vsprintf(char *str, const char *fmt, va_list); + +void tlsf_printf(const char *fmt, ...) { + va_list args; + va_start(args, fmt); + wrt = vsprintf(buffer, fmt, args); + Serial.write(buffer, wrt); + va_end(args); +} + +void tlsf_assert(bool expr, const char *msg) { + if (!expr) { Serial.write(msg, strlen(msg)); } +} diff --git a/tests/arduino/main.cpp b/tests/arduino/main.cpp new file mode 100644 index 0000000..0fa93ef --- /dev/null +++ b/tests/arduino/main.cpp @@ -0,0 +1,53 @@ +#include + +#include + +#define printf(...) tlsf_printf(__VA_ARGS__) + +static constexpr int BAUD_RATE = 9600; + +extern void tlsf_printf(const char *fmt, ...); + +static char string0[] = "zero"; +static char string1[] = "first"; +static char string2[] = "second"; +static char string3[] = "third"; + +static char *strings[] = {string0, string1, string2, string3}; + +static constexpr int POOL_SIZE = 1024; +static char memory[POOL_SIZE]; + +static int dest = 0; + +using namespace wlp; + +struct item { + int integer; + float floating; + const char *string; + + item() : integer(0), floating(0), string(string0) {} + item(int i, float f, const char *s) : integer(i), floating(f), string(s) {} + ~item() { ++dest; } +}; + +void setup() { + Serial.begin(BAUD_RATE); + printf("Hello %i\n", 55); + + if (mem::init(memory, POOL_SIZE)) + { tlsf_printf("Memory initialized\n"); } + else { tlsf_printf("Failed to initialize memory\n"); } +} + +void loop() { + tlsf_printf("Element test: "); + item *p_item = create(dest, 12.3f, strings[((int) abs(dest)) % 4]); + tlsf_printf("%i, %i, %s\n", p_item->integer, (int) p_item->floating, p_item->string); + destroy(p_item); + + item *p_arr_item = create(16); + destroy(p_arr_item); + delay(50); +} diff --git a/tests/debug.cpp b/tests/cosa/debug.cpp similarity index 100% rename from tests/debug.cpp rename to tests/cosa/debug.cpp diff --git a/tests/main.cpp b/tests/cosa/main.cpp similarity index 100% rename from tests/main.cpp rename to tests/cosa/main.cpp diff --git a/native-tests/main.cpp b/tests/native/main.cpp similarity index 100% rename from native-tests/main.cpp rename to tests/native/main.cpp diff --git a/wio.yml b/wio.yml index 05f38fc..c3ce54d 100755 --- a/wio.yml +++ b/wio.yml @@ -2,7 +2,7 @@ type: pkg project: name: wlib-memory - version: 1.0.4 + version: 1.0.5 organization: Waterloop description: Memory create, destroy, and allocators for WLib license: MIT @@ -14,21 +14,37 @@ project: - waterloop - memory - allocator - definitions: - required: - private: - - WLIB_TLSF_ARCH - - WLIB_TLSF_LOG2_DIV - - WLIB_TLSF_LOG2_ALIGN - - WLIB_TLSF_LOG2_MAX compile_options: - wio_version: 0.4.1 + wio_version: 0.6.0 header_only: true - default_target: tests + default_target: cosa-tests + definitions: + required: + private: + - WLIB_TLSF_ARCH + - WLIB_TLSF_LOG2_DIV + - WLIB_TLSF_LOG2_ALIGN + - WLIB_TLSF_LOG2_MAX + optional: + private: + - WLIB_TLSF_PRINTF + - WLIB_TLSF_ASSERT targets: - tests: - src: tests + arduino-tests: + src: tests/arduino + platform: avr + framework: arduino + board: uno + definitions: + package: + - WLIB_TLSF_ARCH=16 + - WLIB_TLSF_LOG2_DIV=3 + - WLIB_TLSF_LOG2_ALIGN=1 + - WLIB_TLSF_LOG2_MAX=11 + - WLIB_TLSF_PRINTF + cosa-tests: + src: tests/cosa platform: avr framework: cosa board: mega2560 @@ -39,7 +55,7 @@ targets: - WLIB_TLSF_LOG2_ALIGN=1 - WLIB_TLSF_LOG2_MAX=11 native-tests: - src: native-tests + src: tests/native platform: native definitions: package: @@ -50,12 +66,14 @@ targets: dependencies: wlib-malloc: - version: 1.0.4 + version: 1.0.5 definitions: - $(WLIB_TLSF_ARCH) - $(WLIB_TLSF_LOG2_DIV) - $(WLIB_TLSF_LOG2_ALIGN) - $(WLIB_TLSF_LOG2_MAX) + - $optional(WLIB_TLSF_PRINTF) + - $optional(WLIB_TLSF_ASSERT) wlib-tmp: version: 1.0.2