-
Notifications
You must be signed in to change notification settings - Fork 6
/
shared_mem_manager.cpp
116 lines (100 loc) · 4.03 KB
/
shared_mem_manager.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/****************************************************************************
Copyright (c) 2015, ko jung hyun
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "shared_mem_manager.hpp"
#include "common_def.hpp"
///////////////////////////////////////////////////////////////////////////////
SharedMemoryManager::SharedMemoryManager():
sh_mem_id_(-1), sh_mem_size_(0), sh_mem_start_addr_(NULL) , total_attached_(0)
{
}
///////////////////////////////////////////////////////////////////////////////
bool SharedMemoryManager::CreateShMem(key_t key, size_t size, bool* is_first_created)
{
*is_first_created = false;
sh_mem_key_ = key ;
sh_mem_size_ = size;
sh_mem_id_ = shmget((key_t)key, size, IPC_CREAT | IPC_EXCL | 0666 ); //IPC_CREAT | IPC_EXCL
if (sh_mem_id_ < 0) {
if(errno == EINVAL ) {
err_msg_ = std::string("shmget error: key [ ") +
std::to_string(key) + std::string(strerror(errno)) ;
DEBUG_ELOG(err_msg_);
} else if(errno == EEXIST || errno == EACCES ) {
return GetShMem(key,size);
} else {
err_msg_ = std::string("shmget error: key [ ") +
std::to_string(key) + std::string(strerror(errno)) ;
DEBUG_ELOG(err_msg_);
}
return false;
}
*is_first_created = true;
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SharedMemoryManager::GetShMem(key_t key, size_t size)
{
sh_mem_key_ = key ;
sh_mem_size_ = size;
sh_mem_id_ = shmget((key_t)key, size, 0666 );
if (sh_mem_id_ < 0) {
err_msg_ = "shmget error: " + std::string(strerror(errno)) ;
DEBUG_ELOG(err_msg_);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SharedMemoryManager::AttachShMem()
{
sh_mem_start_addr_ = shmat(sh_mem_id_ , NULL, 0);
if (sh_mem_start_addr_ == (void *) -1) {
DEBUG_ELOG("shmat error: "<< strerror(errno) <<",sh_mem_id_="<<sh_mem_id_);
return false;
}
total_attached_ ++ ;
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SharedMemoryManager::DetachShMem()
{
if (sh_mem_start_addr_ == NULL) {
DEBUG_ELOG("Error : shared mem start addr is NULL");
return false;
}
if (shmdt(sh_mem_start_addr_) == -1) {
DEBUG_ELOG("shmdt error: "<< strerror(errno) );
return false;
}
total_attached_ -- ;
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SharedMemoryManager::RemoveShMem()
{
if(total_attached_ > 0 ) {
DEBUG_ELOG("Error : attached exists :" <<total_attached_ );
return false;
}
if (shmctl(sh_mem_id_, IPC_RMID, (struct shmid_ds *)NULL) == -1) {
DEBUG_ELOG("Remove shared memoey error: "<< strerror(errno) );
return false;
}
return true;
}