POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
shm_table.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <string>
4#include <array>
5#include <cstring>
6#include <optional>
7#include <algorithm>
8#include <string_view>
9
19template<size_t MaxNameSize = 32, size_t MaxEntries = 64>
21{
22public:
23 static constexpr size_t MAX_NAME_SIZE = MaxNameSize;
24 static constexpr size_t MAX_ENTRIES = MaxEntries;
25
26 struct entry
27 {
28 std::array<char, MAX_NAME_SIZE> name{};
29 size_t offset{0};
30 size_t size{0};
31 size_t elem_size{0};
32 size_t num_elem{0};
33 bool active{false};
34 };
35
36private:
37 entry entries[MAX_ENTRIES]{};
38 size_t total_allocated{0};
39
40public:
41 shm_table_impl() = default;
42
53 bool add(const char* name, size_t offset, size_t size,
54 size_t elem_size = 0, size_t num_elem = 0)
55 {
56 if (find(name))
57 return false;
58
59 for (size_t i = 0; i < MAX_ENTRIES; ++i)
60 {
61 if (!entries[i].active)
62 {
63 auto &e = entries[i];
64 std::strncpy(e.name.data(), name, MAX_NAME_SIZE - 1);
65 e.name[MAX_NAME_SIZE - 1] = '\0';
66 e.offset = offset;
67 e.size = size;
68 e.elem_size = elem_size;
69 e.num_elem = num_elem;
70 e.active = true;
71
72 // Update total allocated
73 size_t end_offset = offset + size - sizeof(*this);
74 total_allocated = std::max(total_allocated, end_offset);
75
76 return true;
77 }
78 }
79 return false;
80 }
81
82 bool add(std::string_view name, size_t offset, size_t size,
83 size_t elem_size = 0, size_t num_elem = 0)
84 {
85 char name_buf[MAX_NAME_SIZE]{};
86 size_t copy_len = std::min(name.size(), sizeof(name_buf) - 1);
87 std::copy_n(name.begin(), copy_len, name_buf);
88 return add(name_buf, offset, size, elem_size, num_elem);
89 }
90
91 bool erase(const char* name)
92 {
93 for (size_t i = 0; i < MAX_ENTRIES; ++i)
94 {
95 if (entries[i].active &&
96 std::strcmp(name, entries[i].name.data()) == 0)
97 {
98 entries[i].active = false;
99 // Note: doesn't reclaim space, just marks as inactive
100 return true;
101 }
102 }
103 return false;
104 }
105
106 bool erase(std::string_view name)
107 {
108 char name_buf[MAX_NAME_SIZE]{};
109 size_t copy_len = std::min(name.size(), sizeof(name_buf) - 1);
110 std::copy_n(name.begin(), copy_len, name_buf);
111 return erase(name_buf);
112 }
113
118 entry* find(const char* name)
119 {
120 for (size_t i = 0; i < MAX_ENTRIES; ++i)
121 {
122 if (entries[i].active &&
123 std::strcmp(name, entries[i].name.data()) == 0)
124 {
125 return &entries[i];
126 }
127 }
128 return nullptr;
129 }
130
131 const entry* find(const char* name) const
132 {
133 for (size_t i = 0; i < MAX_ENTRIES; ++i)
134 {
135 if (entries[i].active &&
136 std::strcmp(name, entries[i].name.data()) == 0)
137 {
138 return &entries[i];
139 }
140 }
141 return nullptr;
142 }
143
144 entry* find(std::string_view name)
145 {
146 char name_buf[MAX_NAME_SIZE]{};
147 size_t copy_len = std::min(name.size(), sizeof(name_buf) - 1);
148 std::copy_n(name.begin(), copy_len, name_buf);
149 return find(name_buf);
150 }
151
152 const entry* find(std::string_view name) const
153 {
154 char name_buf[MAX_NAME_SIZE]{};
155 size_t copy_len = std::min(name.size(), sizeof(name_buf) - 1);
156 std::copy_n(name.begin(), copy_len, name_buf);
157 return find(name_buf);
158 }
159
164 {
165 return total_allocated;
166 }
167
171 size_t get_entry_count() const
172 {
173 size_t count = 0;
174 for (size_t i = 0; i < MAX_ENTRIES; ++i)
175 {
176 if (entries[i].active)
177 ++count;
178 }
179 return count;
180 }
181
185 void clear()
186 {
187 for (size_t i = 0; i < MAX_ENTRIES; ++i)
188 {
189 entries[i].active = false;
190 }
191 total_allocated = 0;
192 }
193
197 static constexpr size_t size_bytes()
198 {
200 }
201};
202
203// Default table type for backward compatibility
205
206// Common alternative configurations
207using shm_table_small = shm_table_impl<16, 16>; // Minimal overhead
208using shm_table_large = shm_table_impl<64, 256>; // More entries, longer names
209using shm_table_huge = shm_table_impl<256, 1024>; // Maximum flexibility
Metadata table for managing shared memory data structures.
Definition shm_table.h:21
entry * find(std::string_view name)
Definition shm_table.h:144
bool add(std::string_view name, size_t offset, size_t size, size_t elem_size=0, size_t num_elem=0)
Definition shm_table.h:82
static constexpr size_t MAX_NAME_SIZE
Definition shm_table.h:23
entry * find(const char *name)
Find an entry by name.
Definition shm_table.h:118
static constexpr size_t MAX_ENTRIES
Definition shm_table.h:24
shm_table_impl()=default
const entry * find(std::string_view name) const
Definition shm_table.h:152
bool erase(std::string_view name)
Definition shm_table.h:106
bool add(const char *name, size_t offset, size_t size, size_t elem_size=0, size_t num_elem=0)
Add a new entry to the table.
Definition shm_table.h:53
bool erase(const char *name)
Definition shm_table.h:91
static constexpr size_t size_bytes()
Get the actual size of this table in bytes.
Definition shm_table.h:197
size_t get_total_allocated_size() const
Get total allocated size (excluding the table itself)
Definition shm_table.h:163
size_t get_entry_count() const
Get number of active entries.
Definition shm_table.h:171
const entry * find(const char *name) const
Definition shm_table.h:131
void clear()
Clear all entries (for initialization)
Definition shm_table.h:185
std::array< char, MAX_NAME_SIZE > name
Definition shm_table.h:28