aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-16 13:28:00 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-16 13:28:00 +0200
commita3fba8221cb26ce43958f42dd1f58cc049c3bba6 (patch)
tree2afca47358d1e7d4f425c3944285d36231f8711d /lib
parent30171b5c84dd90ee9de9f3efc395766497a86c85 (diff)
WIP fixing memory handling
Diffstat (limited to 'lib')
-rw-r--r--lib/pbdrv/CMakeLists.txt14
-rw-r--r--lib/pbdrv/drv/arduino/mod.cpp11
-rw-r--r--lib/pbdrv/mpack-config.h8
-rw-r--r--lib/pbdrv/pb-buf.c5
-rw-r--r--lib/pbdrv/pb-buf.h2
-rw-r--r--lib/pbdrv/pb-mem.h16
-rw-r--r--lib/pbdrv/pb-mod.h4
-rw-r--r--lib/pbdrv/pb-msg.c5
-rw-r--r--lib/pbdrv/pb-route.c7
-rw-r--r--lib/pbdrv/pb-route.h2
-rw-r--r--lib/pbdrv/pb-serial.c12
11 files changed, 54 insertions, 32 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt
index 59a1f25..0c09e67 100644
--- a/lib/pbdrv/CMakeLists.txt
+++ b/lib/pbdrv/CMakeLists.txt
@@ -11,6 +11,8 @@ add_compile_definitions(DEBUG)
project(pbdrv C CXX)
add_subdirectory(lib/mpack)
+add_compile_definitions(MPACK_HAS_CONFIG=1)
+target_include_directories(mpack INTERFACE .) # mpack-config.h
# generic puzzle bus message handling library functions
add_library(pbdrv STATIC
@@ -18,25 +20,29 @@ add_library(pbdrv STATIC
pb-serial.c
pb-buf.c
)
-target_link_libraries(pbdrv mpack)
target_include_directories(pbdrv SYSTEM INTERFACE .)
+target_compile_definitions(pbdrv PRIVATE PB_TARGET_STDLIB)
+target_link_libraries(pbdrv mpack)
# puzzle bus *module* specific code
add_library(pbdrv-mod STATIC
+ pb-msg.c
+ pb-serial.c
+ pb-buf.c
pb-mod.c
pb-send.c
pb-route.c
)
+target_include_directories(pbdrv-mod SYSTEM INTERFACE .)
+target_compile_definitions(pbdrv-mod PRIVATE PB_TARGET_FREERTOS)
target_link_libraries(pbdrv-mod
+ mpack
# freertos is used to defer the handling of i2c messages outside the receive
# interrupt service routine
freertos_kernel
freertos_kernel_include
freertos_config
- # pbdrv-mod is a superset of pbdrv
- pbdrv
)
-target_include_directories(pbdrv-mod SYSTEM INTERFACE .)
# supported puzzle bus drivers
include(drv/arduino/cfg.cmake)
diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp
index 8766444..9130334 100644
--- a/lib/pbdrv/drv/arduino/mod.cpp
+++ b/lib/pbdrv/drv/arduino/mod.cpp
@@ -10,23 +10,22 @@
#include <timers.h>
#include <task.h>
-#include <stdlib.h>
-#include <stdint.h>
-
#include "../../pb.h"
#include "../../pb-mod.h"
+#include "../../pb-types.h"
#include "../../pb-buf.h"
+#include "../../pb-mem.h"
static void async_pb_i2c_recv(void * _msg, uint32_t _) {
pb_buf_t * msg = (pb_buf_t *) _msg;
pb_i2c_recv((uint8_t *) msg->data, msg->size);
pb_buf_free(msg);
- free(msg);
+ pb_free(msg);
}
static void recv_event(int bytes) {
- pb_buf_t * msg = (pb_buf_t *) malloc(sizeof(pb_buf_t));
- msg->data = (char *) malloc(bytes);
+ pb_buf_t * msg = (pb_buf_t *) pb_malloc(sizeof(pb_buf_t));
+ msg->data = (char *) pb_malloc(bytes);
msg->size = 0;
while (Wire.available())
msg->data[msg->size++] = Wire.read();
diff --git a/lib/pbdrv/mpack-config.h b/lib/pbdrv/mpack-config.h
new file mode 100644
index 0000000..b8e806c
--- /dev/null
+++ b/lib/pbdrv/mpack-config.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include "pb-mem.h"
+
+#define MPACK_FREE(ptr) pb_free(ptr)
+#define MPACK_MALLOC(sz) pb_malloc(sz)
+#define MPACK_REALLOC(ptr, sz) pb_realloc(ptr, sz)
+
diff --git a/lib/pbdrv/pb-buf.c b/lib/pbdrv/pb-buf.c
index 5dfaa66..3d6cb8a 100644
--- a/lib/pbdrv/pb-buf.c
+++ b/lib/pbdrv/pb-buf.c
@@ -1,10 +1,9 @@
#include "pb-buf.h"
-
-#include <stdlib.h>
+#include "pb-mem.h"
void pb_buf_free(pb_buf_t * buf) {
if (buf->data == NULL) return;
- free(buf->data);
+ pb_free(buf->data);
buf->data = NULL;
}
diff --git a/lib/pbdrv/pb-buf.h b/lib/pbdrv/pb-buf.h
index 671f344..78ee380 100644
--- a/lib/pbdrv/pb-buf.h
+++ b/lib/pbdrv/pb-buf.h
@@ -1,6 +1,6 @@
#pragma once
-#include <stddef.h>
+#include "pb-types.h"
#ifdef __cplusplus
extern "C" {
diff --git a/lib/pbdrv/pb-mem.h b/lib/pbdrv/pb-mem.h
new file mode 100644
index 0000000..9bf98cb
--- /dev/null
+++ b/lib/pbdrv/pb-mem.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#ifdef PB_TARGET_FREERTOS
+#include <FreeRTOS.h>
+#define pb_free(ptr) vPortFree(ptr)
+#define pb_malloc(sz) pvPortMalloc(sz)
+#define pb_realloc(ptr, sz) pvPortRealloc(ptr, sz)
+#endif
+
+#ifdef PB_TARGET_STDLIB
+#include <stdlib.h>
+#define pb_free(ptr) free(ptr)
+#define pb_malloc(sz) malloc(sz)
+#define pb_realloc(ptr, sz) realloc(ptr, sz)
+#endif
+
diff --git a/lib/pbdrv/pb-mod.h b/lib/pbdrv/pb-mod.h
index a2da421..ae36d22 100644
--- a/lib/pbdrv/pb-mod.h
+++ b/lib/pbdrv/pb-mod.h
@@ -10,10 +10,6 @@
* use the puzzle bus driver library.
*/
-#include <stdint.h>
-#include <stddef.h>
-#include <stdbool.h>
-
#include "pb-types.h"
#ifdef __cplusplus
diff --git a/lib/pbdrv/pb-msg.c b/lib/pbdrv/pb-msg.c
index 3f6fa95..ab3f5b2 100644
--- a/lib/pbdrv/pb-msg.c
+++ b/lib/pbdrv/pb-msg.c
@@ -2,6 +2,7 @@
#include "pb-msg.h"
#include "pb-serial.h"
+#include "pb-mem.h"
pb_buf_t pb_msg_write(const pb_msg_t * msg) {
pb_buf_t buf = { 0 };
@@ -20,7 +21,7 @@ pb_msg_t * pb_msg_read(const pb_buf_t * buf) {
mpack_reader_t reader;
mpack_reader_init_data(&reader, buf->data, buf->size);
- pb_msg_t * msg = malloc(sizeof(pb_msg_t));
+ pb_msg_t * msg = pb_malloc(sizeof(pb_msg_t));
pb_ser_r(&reader, msg);
@@ -33,6 +34,6 @@ void pb_msg_free(pb_msg_t * msg) {
pb_ser_free(msg);
// free message container that was created in \p pb_msg_read
- free(msg);
+ pb_free(msg);
}
diff --git a/lib/pbdrv/pb-route.c b/lib/pbdrv/pb-route.c
index 279b430..84d0fa0 100644
--- a/lib/pbdrv/pb-route.c
+++ b/lib/pbdrv/pb-route.c
@@ -1,3 +1,6 @@
+#include <FreeRTOS.h>
+#include <task.h>
+
#include "pb-route.h"
#include "pb-mod.h"
#include "pb-send.h"
@@ -5,10 +8,6 @@
#include <string.h>
-// FIXME: this should be removed (see handover: RP2040 I2C limitations)
-#include <FreeRTOS.h>
-#include <task.h>
-
__weak bool pb_hook_route_msg(pb_msg_t * msg) { return false; }
__weak void pb_route_msg(pb_msg_t * msg) {
if (pb_hook_route_msg(msg)) return;
diff --git a/lib/pbdrv/pb-route.h b/lib/pbdrv/pb-route.h
index aa65057..41c009a 100644
--- a/lib/pbdrv/pb-route.h
+++ b/lib/pbdrv/pb-route.h
@@ -1,7 +1,5 @@
#pragma once
-#include <stdbool.h>
-
#include "pb-types.h"
#ifdef __cplusplus
diff --git a/lib/pbdrv/pb-serial.c b/lib/pbdrv/pb-serial.c
index 29a307c..689db0c 100644
--- a/lib/pbdrv/pb-serial.c
+++ b/lib/pbdrv/pb-serial.c
@@ -48,7 +48,7 @@ void pb_ser_w_cmd_prop(mpack_writer_t * writer, const pb_msg_t * _msg) {
mpack_write_bin(writer, (char *) cmd->value, cmd->_value_size);
}
void pb_ser_r_cmd_prop(mpack_reader_t * reader, pb_msg_t * _msg) {
- pb_cmd_prop_t * cmd = _msg->cmd = malloc(sizeof(pb_cmd_prop_t));
+ pb_cmd_prop_t * cmd = _msg->cmd = pb_malloc(sizeof(pb_cmd_prop_t));
cmd->propid = mpack_expect_u8(reader);
cmd->_value_size = mpack_expect_bin(reader);
@@ -62,7 +62,7 @@ void pb_ser_free_cmd_prop(pb_msg_t * _msg) {
MPACK_FREE(cmd->value);
cmd->value = NULL;
}
- free(_msg->cmd);
+ pb_free(_msg->cmd);
_msg->cmd = NULL;
}
}
@@ -73,13 +73,13 @@ void pb_ser_w_cmd_state(mpack_writer_t * writer, const pb_msg_t * _msg) {
mpack_write_u8(writer, cmd->state);
}
void pb_ser_r_cmd_state(mpack_reader_t * reader, pb_msg_t * _msg) {
- pb_cmd_state_t * cmd = _msg->cmd = malloc(sizeof(pb_cmd_state_t));
+ pb_cmd_state_t * cmd = _msg->cmd = pb_malloc(sizeof(pb_cmd_state_t));
cmd->state = mpack_expect_u8(reader);
}
void pb_ser_free_cmd_state(pb_msg_t * _msg) {
if (_msg->cmd != NULL) {
- free(_msg->cmd);
+ pb_free(_msg->cmd);
_msg->cmd = NULL;
}
}
@@ -90,7 +90,7 @@ void pb_ser_w_cmd_magic(mpack_writer_t * writer, const pb_msg_t * _msg) {
mpack_write_bin(writer, (char *) cmd->magic, cmd->_magic_size);
}
void pb_ser_r_cmd_magic(mpack_reader_t * reader, pb_msg_t * _msg) {
- pb_cmd_magic_t * cmd = _msg->cmd = malloc(sizeof(pb_cmd_magic_t));
+ pb_cmd_magic_t * cmd = _msg->cmd = pb_malloc(sizeof(pb_cmd_magic_t));
cmd->_magic_size = mpack_expect_bin(reader);
cmd->magic = mpack_read_bytes_alloc(reader, cmd->_magic_size);
@@ -103,7 +103,7 @@ void pb_ser_free_cmd_magic(pb_msg_t * _msg) {
MPACK_FREE(cmd->magic);
cmd->magic = NULL;
}
- free(_msg->cmd);
+ pb_free(_msg->cmd);
_msg->cmd = NULL;
}
}