Dawn Framework 1.0
Universal data acquisition framework for embedded systems
memory.cxx
1// dawn/src/porting/nuttx/memory.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/porting/memory.hxx"
7
8#include <cstring>
9#include <errno.h>
10#include <nuttx/mm/mm.h>
11
12// operator new relies on malloc() meeting the C++ default new alignment
13
14static_assert(MM_ALIGN >= __STDCPP_DEFAULT_NEW_ALIGNMENT__,
15 "CONFIG_MM_DEFAULT_ALIGNMENT is below C++ new alignment");
16
17namespace dawn
18{
19namespace porting
20{
21
22int memory_pool_init(memory_pool_s *pool, const char *name, void *buffer, size_t size)
23{
24 if (pool == nullptr || buffer == nullptr || size == 0)
25 {
26 return -EINVAL;
27 }
28
29 if (pool->heap != nullptr)
30 {
31 return OK;
32 }
33
34 pool->heap = mm_initialize(name, buffer, size);
35 return pool->heap == nullptr ? -ENOMEM : OK;
36}
37
38void *memory_pool_zalloc(memory_pool_s *pool, size_t size)
39{
40 void *ptr;
41
42 if (pool == nullptr || pool->heap == nullptr || size == 0)
43 {
44 return nullptr;
45 }
46
47 ptr = mm_malloc(static_cast<mm_heap_s *>(pool->heap), size);
48 if (ptr != nullptr)
49 {
50 std::memset(ptr, 0, size);
51 }
52
53 return ptr;
54}
55
56void memory_pool_free(memory_pool_s *pool, void *ptr)
57{
58 if (pool == nullptr || pool->heap == nullptr || ptr == nullptr)
59 {
60 return;
61 }
62
63 mm_free(static_cast<mm_heap_s *>(pool->heap), ptr);
64}
65
66} // namespace porting
67} // namespace dawn
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Platform-backed memory pool handle.
Definition memory.hxx:19