Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/wlib/mem/Construct.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <wlib/utility>
#include <wlib/type_traits>
#include <wlib/mem/Detail.h>
#include <wlib/mem/New.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not include New.h


namespace wlp {
template<typename T, typename... Args>
Expand Down
37 changes: 37 additions & 0 deletions include/wlib/mem/New.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#if defined(WIO_PLATFORM_AVR) && defined(WIO_FRAMEWORK_ARDUINO)

#include <new.h>

void* operator new(decltype(sizeof(0)), void* ptr) noexcept {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is included with wlib-tmp, we don't need it again

return ptr;
}

#elif defined(WIO_PLATFORM_NATIVE)

#include <new>

#else

#include <stdlib.h>

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
23 changes: 23 additions & 0 deletions tests/arduino/debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <Arduino.h>
#include <stdarg.h>
#include <stdio.h>

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)); }
}
53 changes: 53 additions & 0 deletions tests/arduino/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <Arduino.h>

#include <wlib/memory>

#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<item>(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<item>(p_item);

item *p_arr_item = create<item[]>(16);
destroy<item[]>(p_arr_item);
delay(50);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 32 additions & 14 deletions wio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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