Skip to content

Commit 95c72b5

Browse files
Merge pull request #5 from NikolasK-source/main
release 1.0.5
2 parents 0920a8a + 27753fb commit 95c72b5

6 files changed

Lines changed: 51 additions & 33 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "libs/cxxopts"]
22
path = libs/cxxopts
33
url = https://github.com/NikolasK-source/cxxopts.git
4+
[submodule "libs/cxxshm"]
5+
path = libs/cxxshm
6+
url = https://github.com/NikolasK-source/cxxshm.git

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.13.4 FATAL_ERROR)
44
# ======================================================================================================================
55

66
# project
7-
project(shared-mem-random LANGUAGES CXX VERSION 1.0.4)
7+
project(shared-mem-random LANGUAGES CXX VERSION 1.0.5)
88

99
# settings
1010
set(Target "shared-mem-random") # Executable name (without file extension!)

libs/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
# ---------------------------------------- link libraries --------------------------------------------------------------
88
# ======================================================================================================================
9-
target_link_libraries(${Target} PRIVATE rt)
10-
119
add_subdirectory(cxxopts EXCLUDE_FROM_ALL)
1210
target_link_libraries(${Target} PRIVATE cxxopts)
11+
12+
add_subdirectory(cxxshm EXCLUDE_FROM_ALL)
13+
target_link_libraries(${Target} PRIVATE cxxshm)
14+
15+
target_link_libraries(${Target} PRIVATE rt)
16+

libs/cxxshm

Submodule cxxshm added at e4c6953

src/license.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,29 @@ void print_licenses(std::ostream &o) {
5454
o << " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," << std::endl;
5555
o << " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN" << std::endl;
5656
o << " THE SOFTWARE." << std::endl;
57+
o << std::endl;
58+
o << std::endl;
59+
o << "cxxshm Library (https://github.com/NikolasK-source/cxxshm)" << std::endl;
60+
o << std::endl;
61+
o << " MIT License" << std::endl;
62+
o << std::endl;
63+
o << " Copyright (c) 2022 Nikolas Koesling" << std::endl;
64+
o << std::endl;
65+
o << " Permission is hereby granted, free of charge, to any person obtaining a copy" << std::endl;
66+
o << " of this software and associated documentation files (the \"Software\"), to deal" << std::endl;
67+
o << " in the Software without restriction, including without limitation the rights" << std::endl;
68+
o << " to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" << std::endl;
69+
o << " copies of the Software, and to permit persons to whom the Software is" << std::endl;
70+
o << " furnished to do so, subject to the following conditions:" << std::endl;
71+
o << std::endl;
72+
o << " The above copyright notice and this permission notice shall be included in" << std::endl;
73+
o << " all copies or substantial portions of the Software." << std::endl;
74+
o << std::endl;
75+
o << " THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" << std::endl;
76+
o << " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," << std::endl;
77+
o << " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" << std::endl;
78+
o << " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" << std::endl;
79+
o << " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," << std::endl;
80+
o << " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN" << std::endl;
81+
o << " THE SOFTWARE." << std::endl;
5782
}

src/main.cpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
#include "license.hpp"
77

8+
#include "cxxshm.hpp"
89
#include <csignal>
910
#include <cxxopts.hpp>
1011
#include <fcntl.h>
1112
#include <filesystem>
1213
#include <iostream>
14+
#include <memory>
1315
#include <random>
1416
#include <sys/mman.h>
1517
#include <sys/stat.h>
@@ -81,6 +83,7 @@ int main(int argc, char **argv) {
8183
std::cout << std::endl;
8284
std::cout << "This application uses the following libraries:" << std::endl;
8385
std::cout << " - cxxopts by jarro2783 (https://github.com/jarro2783/cxxopts)" << std::endl;
86+
std::cout << " - cxxshm (https://github.com/NikolasK-source/cxxshm)" << std::endl;
8487
exit(EX_OK);
8588
}
8689

@@ -152,34 +155,20 @@ int main(int argc, char **argv) {
152155
}
153156
}
154157

155-
if (signal(SIGINT, sig_term_handler) || signal(SIGTERM, sig_term_handler)) {
158+
if (signal(SIGINT, sig_term_handler) == SIG_ERR || signal(SIGTERM, sig_term_handler) == SIG_ERR) {
156159
perror("signal");
157160
exit(EX_OSERR);
158161
}
159162

160-
// open shared memory
161-
int fd = shm_open(shm_name.c_str(), O_RDWR, 0660);
162-
if (fd < 0) {
163-
perror(("failed to open shared memory '" + shm_name + '\'').c_str());
164-
exit(EX_OSERR);
165-
}
166-
167-
// determine size of shared memory
168-
struct stat shm_stats {};
169-
if (fstat(fd, &shm_stats)) {
170-
perror("fstat");
171-
close(fd);
172-
exit(EX_OSERR);
163+
std::unique_ptr<cxxshm::SharedMemory> shm;
164+
try {
165+
shm = std::make_unique<cxxshm::SharedMemory>(shm_name);
166+
} catch (std::exception &e) {
167+
std::cerr << e.what() << std::endl;
168+
return EX_OSERR;
173169
}
174-
const auto SIZE = static_cast<std::size_t>(shm_stats.st_size);
175170

176-
// map shared memory
177-
void *data = mmap(nullptr, SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
178-
if (data == MAP_FAILED || data == nullptr) {
179-
perror("mmap");
180-
if (close(fd)) { perror("close"); }
181-
exit(EX_OSERR);
182-
}
171+
const auto SIZE = shm->get_size();
183172

184173
std::cerr << "Opened shared memory '" << shm_name << "'. Size: " << SIZE << (SIZE != 1 ? " bytes" : " byte") << '.'
185174
<< std::endl;
@@ -196,7 +185,7 @@ int main(int argc, char **argv) {
196185
switch (alignment) {
197186
case BYTE:
198187
while (!terminate) {
199-
random_data<uint8_t>(data, shm_elements, bitmask);
188+
random_data<uint8_t>(shm->get_addr(), shm_elements, bitmask);
200189
nanosleep(&sleep_time, nullptr);
201190

202191
if (interval_counter) {
@@ -206,7 +195,7 @@ int main(int argc, char **argv) {
206195
break;
207196
case WORD:
208197
while (!terminate) {
209-
random_data<uint16_t>(data, shm_elements, bitmask);
198+
random_data<uint16_t>(shm->get_addr(), shm_elements, bitmask);
210199
nanosleep(&sleep_time, nullptr);
211200

212201
if (interval_counter) {
@@ -216,7 +205,7 @@ int main(int argc, char **argv) {
216205
break;
217206
case DWORD:
218207
while (!terminate) {
219-
random_data<uint32_t>(data, shm_elements, bitmask);
208+
random_data<uint32_t>(shm->get_addr(), shm_elements, bitmask);
220209
nanosleep(&sleep_time, nullptr);
221210

222211
if (interval_counter) {
@@ -226,7 +215,7 @@ int main(int argc, char **argv) {
226215
break;
227216
case QWORD:
228217
while (!terminate) {
229-
random_data<uint64_t>(data, shm_elements, bitmask);
218+
random_data<uint64_t>(shm->get_addr(), shm_elements, bitmask);
230219
nanosleep(&sleep_time, nullptr);
231220

232221
if (interval_counter) {
@@ -236,9 +225,5 @@ int main(int argc, char **argv) {
236225
break;
237226
}
238227

239-
// clean
240-
if (munmap(data, SIZE)) { perror("munmap"); }
241-
if (close(fd)) { perror("close"); }
242-
243228
std::cerr << "Terminating..." << std::endl;
244229
}

0 commit comments

Comments
 (0)