From 7052a4987f151ebb6e70e3d901edd29d95d14500 Mon Sep 17 00:00:00 2001 From: koon Date: Wed, 8 May 2019 22:44:22 +0200 Subject: [PATCH 01/11] WIP to change array's to ringbuffers --- code/headers/nfc_mem.hpp | 75 ++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/code/headers/nfc_mem.hpp b/code/headers/nfc_mem.hpp index 009daf3..8ad5e87 100644 --- a/code/headers/nfc_mem.hpp +++ b/code/headers/nfc_mem.hpp @@ -1,8 +1,10 @@ #pragma once +#include #include #include #include +#include #include "priority.hpp" #include "can.hpp" @@ -18,10 +20,11 @@ namespace r2d2 { constexpr uint16_t _large_buffer_size = 256; constexpr uint8_t _large_buffer_count = 4; + #pragma pack(1) struct _uid_index { - uint8_t uid; uint8_t frame_type; uint8_t *data; + uint8_t uid:5; }; /** @@ -33,22 +36,44 @@ namespace r2d2 { struct _nfc_memory_area_s { using queue_type = queue_c; - using extra_small_buffer = uint8_t[_extra_small_buffer_size]; - using small_buffer = uint8_t[_small_buffer_size]; - using large_buffer = uint8_t[_large_buffer_size]; - queue_type tx_queues[4]; // 912 bytes - extra_small_buffer extra_small_buffers[_extra_small_buffer_count]; // 512 bytes - size_t extra_small_buffer_counters[_extra_small_buffer_count]; // 256 bytes - - small_buffer small_buffers[_small_buffer_count]; // 1024 bytes - size_t small_buffer_counters[_small_buffer_count]; // 32 bytes - - large_buffer large_buffers[_large_buffer_count]; // 1024 bytes - size_t large_buffer_counters[_large_buffer_count]; // 16 bytes + template + using buffer_type = ringbuffer_c< + std::array, + Amount + >; + + constexpr static size_t p1_buffers_count = 96; + constexpr static size_t p4_buffers_count = 18; + constexpr static size_t p16_buffers_count = 4; + constexpr static size_t p32_buffers_count = 2; + + buffer_type<1, p1_buffers_count> p1_buffers; + buffer_type<4, p4_buffers_count> p4_buffers; + buffer_type<16, p16_buffers_count> p16_buffers; + buffer_type<32, p32_buffers_count> p32_buffers; + + _uid_index uid_indices[ + p1_buffers_count + + p4_buffers_count + + p16_buffers_count + + p32_buffers_count + ]; - _uid_index uid_indices[_small_buffer_count + _large_buffer_count]; + uint8_t *allocate(uint8_t size) { + if (size <= 1 * 8) { + return &(p1_buffers.emplace()[0]); + } else if (size <= 4 * 8) { + return &(p4_buffers.emplace()[0]); + } else if (size <= 16 * 8) { + return &(p16_buffers.emplace()[0]); + } else if (size <= 32 * 8) { + return &(p32_buffers.emplace()[0]); + } else { + return nullptr; + } + } }; /** @@ -246,21 +271,21 @@ namespace r2d2 { return nullptr; } - static void print_memory_statistics() { - hwlib::cout << "Counters: \r\n\tSmall buffers:\r\n\t\t"; + // static void print_memory_statistics() { + // hwlib::cout << "Counters: \r\n\tSmall buffers:\r\n\t\t"; - for (size_t i = 0; i < _small_buffer_count; i++) { - hwlib::cout << _nfc_mem->small_buffer_counters[i] << ' '; - } + // for (size_t i = 0; i < _small_buffer_count; i++) { + // hwlib::cout << _nfc_mem->p1_buffers.size() << ' '; + // } - hwlib::cout << "\r\n\r\n\tLarge buffers:\r\n\t\t"; + // hwlib::cout << "\r\n\r\n\tLarge buffers:\r\n\t\t"; - for (size_t i = 0; i < _large_buffer_count; i++) { - hwlib::cout << _nfc_mem->large_buffer_counters[i] << ' '; - } + // for (size_t i = 0; i < _large_buffer_count; i++) { + // hwlib::cout << _nfc_mem->large_buffer_counters[i] << ' '; + // } - hwlib::cout << "\r\n"; - } + // hwlib::cout << "\r\n"; + // } }; } From 1766c2be07b3b4f1c29930379d91b6111a9406da Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Wed, 8 May 2019 23:01:51 +0200 Subject: [PATCH 02/11] removed the allocator from the memory_manager --- code/headers/nfc_mem.hpp | 177 --------------------------------------- 1 file changed, 177 deletions(-) diff --git a/code/headers/nfc_mem.hpp b/code/headers/nfc_mem.hpp index 8ad5e87..0f158a8 100644 --- a/code/headers/nfc_mem.hpp +++ b/code/headers/nfc_mem.hpp @@ -135,58 +135,6 @@ namespace r2d2 { * of the NFC memory area. */ struct _memory_manager_s { - static size_t *get_counter(const uint8_t *ptr) { - const size_t offset = reinterpret_cast(ptr) - reinterpret_cast(_nfc_mem); - - if (offset < offsetof(_nfc_memory_area_s, small_buffers)){ - // extra small buffers - const size_t array_offset = - (offset - offsetof(_nfc_memory_area_s, extra_small_buffers)) / _extra_small_buffer_size; - return &_nfc_mem->extra_small_buffer_counters[array_offset]; - } else if (offset < offsetof(_nfc_memory_area_s, large_buffers)) { - // Small buffers - const size_t array_offset = - (offset - offsetof(_nfc_memory_area_s, small_buffers)) / _small_buffer_size; - return &_nfc_mem->small_buffer_counters[array_offset]; - } else { - // Large buffers - const size_t array_offset = - (offset - offsetof(_nfc_memory_area_s, large_buffers)) / _large_buffer_size; - return &_nfc_mem->large_buffer_counters[array_offset]; - } - } - - /** - * Deallocate the block, starting at the given pointer. - * This won't zero the memory, but simply mark it as free. - * - * @param ptr - */ - static void dealloc(const uint8_t *ptr) { - if (!ptr) { - return; - } - - size_t offset = reinterpret_cast(ptr) - reinterpret_cast(_nfc_mem); - - if (offset < offsetof(_nfc_memory_area_s, small_buffers)) { - // Small buffers - const size_t array_offset = - (offset - offsetof(_nfc_memory_area_s, extra_small_buffers)) / _extra_small_buffer_size; - _nfc_mem->extra_small_buffer_counters[array_offset] = 0; - } else if (offset < offsetof(_nfc_memory_area_s, large_buffers)) { - // Small buffers - const size_t array_offset = - (offset - offsetof(_nfc_memory_area_s, small_buffers)) / _small_buffer_size; - _nfc_mem->small_buffer_counters[array_offset] = 0; - } else { - // Large buffers - const size_t array_offset = - (offset - offsetof(_nfc_memory_area_s, large_buffers)) / _large_buffer_size; - _nfc_mem->large_buffer_counters[array_offset] = 0; - } - } - /** * @brief gets the pointer for the data for the current type and uid * @@ -239,131 +187,6 @@ namespace r2d2 { } } } - - /** - * Allocate a memory block for the given size. - * If no memory could be allocated, a nullptr is returned. - * - * @param size - * @return uint8_t* - */ - static uint8_t *alloc(const size_t size) { - if (size <= _extra_small_buffer_size) { - for (size_t i = 0; i < _extra_small_buffer_count; i++) { - if (!(_nfc_mem->extra_small_buffer_counters[i])) { - return reinterpret_cast(&_nfc_mem->extra_small_buffers[i]); - } - } - } else if (size <= _small_buffer_size) { - for (size_t i = 0; i < _small_buffer_count; i++) { - if (!(_nfc_mem->small_buffer_counters[i])) { - return reinterpret_cast(&_nfc_mem->small_buffers[i]); - } - } - } else { - for (size_t i = 0; i < _large_buffer_count; i++) { - if (!(_nfc_mem->large_buffer_counters[i])) { - return reinterpret_cast(&_nfc_mem->large_buffers[i]); - } - } - } - - return nullptr; - } - - // static void print_memory_statistics() { - // hwlib::cout << "Counters: \r\n\tSmall buffers:\r\n\t\t"; - - // for (size_t i = 0; i < _small_buffer_count; i++) { - // hwlib::cout << _nfc_mem->p1_buffers.size() << ' '; - // } - - // hwlib::cout << "\r\n\r\n\tLarge buffers:\r\n\t\t"; - - // for (size_t i = 0; i < _large_buffer_count; i++) { - // hwlib::cout << _nfc_mem->large_buffer_counters[i] << ' '; - // } - - // hwlib::cout << "\r\n"; - // } }; } - -#ifdef HWLIB_TARGET_native -#include - - using shared_nfc_ptr_c = std::shared_ptr; -#else - - class shared_nfc_ptr_c { - protected: - uint8_t *ptr = nullptr; - size_t *counter = nullptr; - - void increment() { - (*counter) += 1; - } - - void decrement() { - (*counter) -= 1; - } - - public: - shared_nfc_ptr_c() = default; - - explicit shared_nfc_ptr_c(uint8_t *ptr) : ptr(ptr) { - counter = can_bus::detail::_memory_manager_s::get_counter(ptr); - (*counter) = 1; - } - - ~shared_nfc_ptr_c() { - decrement(); - - if ((*counter) == 0) { - can_bus::detail::_memory_manager_s::dealloc(ptr); - } - } - - shared_nfc_ptr_c(const shared_nfc_ptr_c &other) - : ptr(other.ptr), counter(other.counter) { - increment(); - } - - shared_nfc_ptr_c(shared_nfc_ptr_c &&other) - : ptr(other.ptr), counter(other.counter) {} - - shared_nfc_ptr_c &operator=(const shared_nfc_ptr_c &other) { - shared_nfc_ptr_c tmp(other); - swap(tmp); - - return *this; - } - - uint8_t const *get() const { - return ptr; - } - - uint8_t *get() { - return ptr; - } - - size_t get_counter() const { - return *counter; - } - - uint8_t *operator*() { - return ptr; - } - - uint8_t const *operator*() const { - return ptr; - } - - void swap(shared_nfc_ptr_c &other) { - std::swap(ptr, other.ptr); - std::swap(counter, other.counter); - } - }; - -#endif } \ No newline at end of file From 868b9ee92950d89c5a4e660dee566d99147f9e8d Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Wed, 8 May 2019 23:05:15 +0200 Subject: [PATCH 03/11] changed the frame to have a data pointer instead of a shared_nfc_ptr_c --- code/headers/base_comm.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/headers/base_comm.hpp b/code/headers/base_comm.hpp index 4c5ab8c..2d3c0c3 100644 --- a/code/headers/base_comm.hpp +++ b/code/headers/base_comm.hpp @@ -8,7 +8,7 @@ namespace r2d2 { struct frame_s { - shared_nfc_ptr_c data; + uint8_t* data; size_t length; frame_type type; From e5301e81d8e840799246109e32544e1e78af8811 Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Wed, 8 May 2019 23:05:58 +0200 Subject: [PATCH 04/11] removed the memory statistics for now --- code/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/code/main.cpp b/code/main.cpp index 64f3779..4c156d9 100644 --- a/code/main.cpp +++ b/code/main.cpp @@ -31,7 +31,6 @@ int main() { const auto data = t.as_frame_type< frame_type::DISPLAY_FILLED_RECTANGLE>(); - can_bus::detail::_memory_manager_s::print_memory_statistics(); hwlib::cout << "Got frame: " << int(data.x) << '\n'; } } From 200e24dfaec9cef398742c414ed0516087eb7ca7 Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Wed, 8 May 2019 23:06:58 +0200 Subject: [PATCH 05/11] changed the frame data to the array format --- code/headers/channel.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/code/headers/channel.hpp b/code/headers/channel.hpp index 7c31bdf..936107a 100644 --- a/code/headers/channel.hpp +++ b/code/headers/channel.hpp @@ -377,7 +377,7 @@ namespace r2d2::can_bus { if (can_frame.sequence_id == 0) { // Allocate memory for the frame - ptr = detail::_memory_manager_s::alloc((can_frame.sequence_total + 1) * 8); + ptr = detail::_nfc_mem->allocate((can_frame.sequence_total + 1) * 8); if (!ptr) { // Return because we don't have enough memory available for the current sequence @@ -397,11 +397,11 @@ namespace r2d2::can_bus { } } - frame.data = shared_nfc_ptr_c(ptr); + frame.data = ptr; // Copy CAN frame to frame.data for(uint_fast8_t i = 0; i < can_frame.length; i++) { - (*frame.data)[i + (can_frame.sequence_id * 8)] = can_frame.data.bytes[i]; + frame.data[i + (can_frame.sequence_id * 8)] = can_frame.data.bytes[i]; } // Check if the frame is complete. Otherwise return because we don't want @@ -415,16 +415,16 @@ namespace r2d2::can_bus { } else { // copy can frame to frame.data - uint8_t *ptr = detail::_memory_manager_s::alloc(can_frame.length);; + uint8_t *ptr = detail::_nfc_mem->allocate(can_frame.length);; if(!ptr){ return; } - frame.data = shared_nfc_ptr_c(ptr); + frame.data = ptr; for(uint_fast8_t i = 0; i < can_frame.length; i++) { - (*frame.data)[i] = can_frame.data.bytes[i]; + frame.data[i] = can_frame.data.bytes[i]; } frame.length = can_frame.length; From a6fb0bdae56bdb6da0a7d8c669f99f64584c13f3 Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Thu, 9 May 2019 00:31:17 +0200 Subject: [PATCH 06/11] removed the dereference from the reinterpret_cast --- code/headers/base_comm.hpp | 2 +- code/headers/channel.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/code/headers/base_comm.hpp b/code/headers/base_comm.hpp index 2d3c0c3..5c99d1c 100644 --- a/code/headers/base_comm.hpp +++ b/code/headers/base_comm.hpp @@ -47,7 +47,7 @@ namespace r2d2 { template auto as_frame_type() const -> frame_data_t

{ return *( - reinterpret_cast *>(*data) + reinterpret_cast *>(data) ); } }; diff --git a/code/headers/channel.hpp b/code/headers/channel.hpp index 936107a..9aa76fa 100644 --- a/code/headers/channel.hpp +++ b/code/headers/channel.hpp @@ -427,7 +427,7 @@ namespace r2d2::can_bus { frame.data[i] = can_frame.data.bytes[i]; } - frame.length = can_frame.length; + frame.length = can_frame.length; } // Distribute the frame to all registered modules From 3075c4ec9b47176ea22ec1a01f2e6695dab6444b Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Thu, 9 May 2019 00:33:26 +0200 Subject: [PATCH 07/11] changed the main to test the new nfc memory ringbuffer --- code/main.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/code/main.cpp b/code/main.cpp index 4c156d9..2521f66 100644 --- a/code/main.cpp +++ b/code/main.cpp @@ -18,12 +18,17 @@ int main() { frame_display_filled_rectangle_s state; state.x = 0xAA; state.y = 0xBA; + state.width = 10; + state.height = 20; state.red = 0xFF; + state.green = 0xEE; + state.blue = 0xCC; - //comm.send(state); - comm.send_external({0xAA, 0xFA}, state); - //hwlib::wait_ms(50); + comm.send(state); + // comm.send_external({0xAA, 0xFA}, state); + + hwlib::wait_ms(50); while(comm.has_data()){ auto t = comm.get_data(); From 0aea67333b90aaa78c1fa6f381940226e0016ff3 Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Thu, 9 May 2019 10:27:17 +0200 Subject: [PATCH 08/11] removed count's and sizes that we dont use --- code/headers/nfc_mem.hpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/code/headers/nfc_mem.hpp b/code/headers/nfc_mem.hpp index 0f158a8..0b7e367 100644 --- a/code/headers/nfc_mem.hpp +++ b/code/headers/nfc_mem.hpp @@ -11,15 +11,6 @@ namespace r2d2 { namespace can_bus::detail { - constexpr uint16_t _extra_small_buffer_size = 8; - constexpr uint8_t _extra_small_buffer_count = 64; - - constexpr uint16_t _small_buffer_size = 64; - constexpr uint8_t _small_buffer_count = 16; - - constexpr uint16_t _large_buffer_size = 256; - constexpr uint8_t _large_buffer_count = 4; - #pragma pack(1) struct _uid_index { uint8_t frame_type; From a4550d07a2023ad443f8dfc49d8b09131d9ab0ee Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Thu, 9 May 2019 16:41:39 +0200 Subject: [PATCH 09/11] made the mock_bus compile... Running the test give a error. --- code/headers/mock_bus.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/headers/mock_bus.hpp b/code/headers/mock_bus.hpp index 5162a28..8adaad6 100644 --- a/code/headers/mock_bus.hpp +++ b/code/headers/mock_bus.hpp @@ -23,7 +23,7 @@ namespace r2d2 { frame.type = type; memcpy( - (void *) frame.bytes, + (void *) frame.data, (const void *) &data, 8 ); @@ -61,7 +61,7 @@ namespace r2d2 { frame.type = P; memcpy( - (void *) &frame.bytes, + (void *) &frame.data, (const void *) &data, sizeof(frame_data_t

) ); From 9fd04a90046d75dd5feebfbacf8972b7af6ab9fb Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Thu, 9 May 2019 16:45:05 +0200 Subject: [PATCH 10/11] removed comment about a ringbuffer that moved to its own repo --- test/main.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/main.cpp b/test/main.cpp index dea7c32..740c394 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -8,8 +8,6 @@ using namespace r2d2; -/** RINGBUFFER **/ - TEST_CASE("Mock bus accepts packet", "[mock_bus]") { mock_comm_c comm; From cf7da0deea87a9808cd924872e021e47342eb8a5 Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Thu, 9 May 2019 21:43:13 +0200 Subject: [PATCH 11/11] removed nullptr return from allocate --- code/headers/nfc_mem.hpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/code/headers/nfc_mem.hpp b/code/headers/nfc_mem.hpp index 0b7e367..03e8bd7 100644 --- a/code/headers/nfc_mem.hpp +++ b/code/headers/nfc_mem.hpp @@ -59,10 +59,8 @@ namespace r2d2 { return &(p4_buffers.emplace()[0]); } else if (size <= 16 * 8) { return &(p16_buffers.emplace()[0]); - } else if (size <= 32 * 8) { - return &(p32_buffers.emplace()[0]); } else { - return nullptr; + return &(p32_buffers.emplace()[0]); } } };