This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmemory.cpp
More file actions
102 lines (87 loc) · 2.62 KB
/
memory.cpp
File metadata and controls
102 lines (87 loc) · 2.62 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "./memory.hpp"
#include <string.h>
#include <rmw/allocators.h>
static bool has_memory(
rmw_ertps_mempool_t * mem)
{
bool rv = mem->freeitems != NULL ? true : false;
return rv;
}
rmw_ertps_mempool_item_t * get_memory(
rmw_ertps_mempool_t * mem)
{
rtps::Lock lock{mem->memory_mutex};
rmw_ertps_mempool_item_t * item = NULL;
if (has_memory(mem)) {
// Gets item from free pool
item = mem->freeitems;
mem->freeitems = item->next;
if (mem->freeitems) {
mem->freeitems->prev = NULL;
}
// Puts item in allocated pool
item->next = mem->allocateditems;
if (item->next) {
item->next->prev = item;
}
item->prev = NULL;
mem->allocateditems = item;
} else if (mem->is_dynamic_allowed) {
#ifdef RMW_ERTPS_ALLOW_DYNAMIC_ALLOCATIONS
item =
reinterpret_cast<rmw_ertps_mempool_item_t *>(rmw_allocate(sizeof(rmw_ertps_mempool_item_t)));
item->prev = NULL;
item->next = NULL;
item->data = reinterpret_cast<void *>(rmw_allocate(mem->element_size));
memset(item->data, 0, mem->element_size);
item->is_dynamic_memory = false; // Allow to put element in free pool the first time
put_memory(mem, item);
item->is_dynamic_memory = true;
item = get_memory(mem);
#endif /* ifdef RMW_ERTPS_ALLOW_DYNAMIC_ALLOCATIONS */
}
return item;
}
void put_memory(
rmw_ertps_mempool_t * mem,
rmw_ertps_mempool_item_t * item)
{
rtps::Lock lock{mem->memory_mutex};
// Gets item from allocated pool
if (item->prev) {
item->prev->next = item->next;
}
if (item->next) {
item->next->prev = item->prev;
}
if (mem->allocateditems == item) {
mem->allocateditems = item->next;
}
#ifdef RMW_ERTPS_ALLOW_DYNAMIC_ALLOCATIONS
if (item->is_dynamic_memory) {
rmw_free(item->data);
rmw_free(item);
return;
}
#endif /* ifdef RMW_ERTPS_ALLOW_DYNAMIC_ALLOCATIONS */
// Puts item in free pool
item->next = mem->freeitems;
if (item->next) {
item->next->prev = item;
}
item->prev = NULL;
mem->freeitems = item;
}