POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
custom_table_sizes.cpp
Go to the documentation of this file.
1#include <iostream>
2#include "posix_shm.h"
3#include "shm_array.h"
4#include "shm_queue.h"
5
6// Example showing how to use custom table sizes for different use cases
7
9 std::cout << "=== Minimal Overhead Configuration ===\n";
10
11 // Use small table: 16-char names, 16 max entries
12 // Good for embedded systems or when you know you'll have few structures
13 posix_shm_small shm("minimal_shm", 64 * 1024); // 64KB
14
15 std::cout << "Table overhead: " << sizeof(shm_table_small) << " bytes\n";
16 std::cout << "Max name length: " << shm_table_small::MAX_NAME_SIZE << "\n";
17 std::cout << "Max entries: " << shm_table_small::MAX_ENTRIES << "\n\n";
18
19 // Arrays and queues automatically use the same table type
20 shm_array<int, shm_table_small> small_array(shm, "data", 100);
21 small_array[0] = 42;
22
23 std::cout << "Created array '" << small_array.name() << "' with "
24 << small_array.size() << " elements\n\n";
25}
26
28 std::cout << "=== Large Simulation Configuration ===\n";
29
30 // Use large table: 64-char names, 256 max entries
31 // Good for complex simulations with many data structures
32 posix_shm_large shm("simulation_shm", 100 * 1024 * 1024); // 100MB
33
34 std::cout << "Table overhead: " << sizeof(shm_table_large) << " bytes\n";
35 std::cout << "Max name length: " << shm_table_large::MAX_NAME_SIZE << "\n";
36 std::cout << "Max entries: " << shm_table_large::MAX_ENTRIES << "\n\n";
37
38 // Can have descriptive names and many structures
40 shm, "sensor_data_from_camera_01_preprocessed", 1000);
41
43 shm, "high_priority_event_queue_for_controller", 500);
44
45 std::cout << "Created structures with long descriptive names:\n";
46 std::cout << " - " << sensor_data.name() << "\n";
47 std::cout << " - " << event_queue.name() << "\n\n";
48}
49
51 std::cout << "=== Custom Configuration ===\n";
52
53 // Define your own configuration
54 using my_custom_table = shm_table_impl<48, 128>;
55 using my_custom_shm = posix_shm_impl<my_custom_table>;
56
57 my_custom_shm shm("custom_shm", 10 * 1024 * 1024); // 10MB
58
59 std::cout << "Custom table overhead: " << sizeof(my_custom_table) << " bytes\n";
60 std::cout << "Max name length: " << my_custom_table::MAX_NAME_SIZE << "\n";
61 std::cout << "Max entries: " << my_custom_table::MAX_ENTRIES << "\n\n";
62
63 shm_array<uint64_t, my_custom_table> timestamps(shm, "timestamps", 1000);
64 timestamps[0] = 123456789;
65
66 std::cout << "Custom configuration working with " << timestamps.size()
67 << " timestamps\n\n";
68}
69
70int main() {
71 try {
75
76 std::cout << "All custom table configurations work successfully!\n";
77 } catch (const std::exception& e) {
78 std::cerr << "Error: " << e.what() << std::endl;
79 return 1;
80 }
81
82 return 0;
83}
POSIX shared memory wrapper with RAII and reference counting.
Definition posix_shm.h:62
Fixed-size array in shared memory with zero-overhead access.
Definition shm_array.h:63
std::string_view name() const noexcept
Get array name from metadata table.
Definition shm_array.h:353
size_t size() const noexcept
Get number of elements.
Definition shm_array.h:221
Lock-free circular queue for shared memory IPC.
Definition shm_queue.h:24
std::string_view name() const noexcept
Definition shm_queue.h:191
Metadata table for managing shared memory data structures.
Definition shm_table.h:21
static constexpr size_t MAX_NAME_SIZE
Definition shm_table.h:23
static constexpr size_t MAX_ENTRIES
Definition shm_table.h:24
void example_custom_config()
void example_minimal_overhead()
void example_large_simulation()
int main()
Core POSIX shared memory management with automatic reference counting.
Fixed-size shared memory array with STL compatibility.
shm_table_impl< 16, 16 > shm_table_small
Definition shm_table.h:207
shm_table_impl< 64, 256 > shm_table_large
Definition shm_table.h:208