Dawn Framework 1.0
Universal data acquisition framework for embedded systems
platform.cxx
1// dawn/src/proto/wakaama/platform.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "internal.hxx"
7
8#include "dawn/porting/memory.hxx"
9
10#include <cstddef>
11#include <cstring>
12#include <ctime>
13#include <mutex>
14#include <strings.h>
15
16using namespace dawn::wakaama_internal;
17
18namespace
19{
20alignas(std::max_align_t) uint8_t g_pool[CONFIG_DAWN_PROTO_WAKAAMA_MEMORY_POOL_SIZE];
21dawn::porting::memory_pool_s g_allocator = {};
22std::mutex g_lock;
23
24bool initAllocator()
25{
26 return dawn::porting::memory_pool_init(&g_allocator, "wakaama", g_pool, sizeof(g_pool)) == OK;
27}
28} // namespace
29
30extern "C"
31{
32 void *lwm2m_malloc(size_t size)
33 {
34 std::lock_guard<std::mutex> lock(g_lock);
35 size_t allocSize;
36 void *ptr;
37
38 if (!initAllocator())
39 {
40 return nullptr;
41 }
42
43 allocSize = size == 0 ? 1 : size;
44 ptr = dawn::porting::memory_pool_zalloc(&g_allocator, allocSize);
45 if (ptr == nullptr)
46 {
47 DAWNERR("Wakaama allocator exhausted: size=%zu pool=%zu\n",
48 size,
49 static_cast<size_t>(sizeof(g_pool)));
50 }
51
52 return ptr;
53 }
54
55 void lwm2m_free(void *ptr)
56 {
57 if (ptr == nullptr)
58 {
59 return;
60 }
61
62 std::lock_guard<std::mutex> lock(g_lock);
63 dawn::porting::memory_pool_free(&g_allocator, ptr);
64 }
65
66 char *lwm2m_strdup(const char *str)
67 {
68 char *dup;
69 size_t len;
70
71 if (str == nullptr)
72 {
73 return nullptr;
74 }
75
76 len = std::strlen(str) + 1;
77 dup = static_cast<char *>(lwm2m_malloc(len));
78 if (dup != nullptr)
79 {
80 std::memcpy(dup, str, len);
81 }
82
83 return dup;
84 }
85
86 int lwm2m_strncmp(const char *str1, const char *str2, size_t len)
87 {
88 return std::strncmp(str1, str2, len);
89 }
90
91 int lwm2m_strcasecmp(const char *str1, const char *str2)
92 {
93 return strcasecmp(str1, str2);
94 }
95
96 time_t lwm2m_gettime(void)
97 {
98 return time(nullptr);
99 }
100}
Platform-backed memory pool handle.
Definition memory.hxx:19