This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
NodePool.cpp
86 lines (71 loc) · 2.07 KB
/
NodePool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* a static memory pool for JSON and AvlNode objects */
#include <Arduino.h>
#include "NodePool.h"
#include "AvlNode.h"
#include "Names.h"
#include "JSON.h"
#include "WebThings.h"
WotNodePool::WotNodePool()
{
used = 0; // index for allocation next node
last_allocated = WOT_NODE_POOL_SIZE - 1;
}
unsigned int WotNodePool::size()
{
return WOT_NODE_POOL_SIZE * sizeof(wot_node_pool_t);
}
float WotNodePool::percent_used()
{
// return percentage of allocated nodes
return 100.0 * used / (1.0 * WOT_NODE_POOL_SIZE);
}
// dumb allocator - I should form free nodes into a linked list
void *WotNodePool::allocate_node()
{
if (used >= WOT_NODE_POOL_SIZE)
WebThings::collect_garbage();
if (used < WOT_NODE_POOL_SIZE) {
for (unsigned int i = last_allocated + 1; i < WOT_NODE_POOL_SIZE; ++i) {
if (wot_pool[i].byte[0] == 0) {
used++;
last_allocated = i;
return (void *)(wot_pool + i);
}
}
}
if (used < WOT_NODE_POOL_SIZE) {
for (unsigned int i = 0; i < last_allocated + 1; ++i) {
if (wot_pool[i].byte[0] == 0) {
used++;
last_allocated = i;
return (void *)(wot_pool + i);
}
}
}
// should record this in EEPROM then restart server
Serial.println(F("Error: exhausted node pool"));
return 0;
}
void *WotNodePool::get_node(unsigned int index)
{
if (index < WOT_NODE_POOL_SIZE)
return (void *)(wot_pool + index);
return 0;
}
wot_node_pool_t *WotNodePool::get_pool()
{
return wot_pool;
}
void WotNodePool::free(void *node)
{
// check for valid pointer to node pool and
// take care to avoid freeing a node twice
// since we would then mess up the used count
if ((wot_node_pool_t *)node >= wot_pool &&
(wot_node_pool_t *)node < wot_pool + WOT_NODE_POOL_SIZE &&
((char *)node)[0]) {
memset((char *)node, 0, sizeof(wot_node_pool_t));
if (used)
--used;
}
}