@@ -37,27 +37,22 @@ namespace utils {
37
37
38
38
msc_file_handler_t *SharedFiles::find_handler (
39
39
const std::string &fileName) {
40
- msc_file_handler_t *current = m_first;
41
- while (current != NULL ) {
42
- if (current->file_name == fileName) {
43
- return current;
40
+ for (const auto &i: m_handlers) {
41
+ if (i.first == fileName) {
42
+ return i.second ;
44
43
}
45
- current = reinterpret_cast <msc_file_handler_t *>(current->next );
46
44
}
47
-
48
45
return NULL ;
49
46
}
50
47
51
48
52
49
msc_file_handler_t *SharedFiles::add_new_handler (
53
50
const std::string &fileName, std::string *error) {
54
- msc_file_handler_t *current = m_first;
55
51
int shm_id;
56
52
key_t mem_key_structure;
57
- key_t mem_key_file_name;
58
53
msc_file_handler_t *new_debug_log;
59
- char *shm_ptr2;
60
54
FILE *fp;
55
+ bool toBeCreated = true ;
61
56
62
57
fp = fopen (fileName.c_str (), " a" );
63
58
if (fp == 0 ) {
@@ -72,81 +67,44 @@ msc_file_handler_t *SharedFiles::add_new_handler(
72
67
goto err_mem_key;
73
68
}
74
69
75
- mem_key_file_name = ftok (fileName.c_str (), 2 );
76
- if (mem_key_file_name < 0 ) {
77
- error->assign (" Failed to select key for the shared memory (2): " );
78
- error->append (strerror (errno));
79
- goto err_mem_key;
80
- }
81
-
82
- shm_id = shmget (mem_key_structure, sizeof (msc_file_handler_t ),
83
- IPC_CREAT | 0666 );
70
+ shm_id = shmget (mem_key_structure, sizeof (msc_file_handler_t ) + fileName.size () + 1 ,
71
+ IPC_CREAT | IPC_EXCL | 0666 );
84
72
if (shm_id < 0 ) {
85
- error->assign (" Failed to allocate shared memory (1): " );
86
- error->append (strerror (errno));
87
- goto err_shmget1;
73
+ shm_id = shmget (mem_key_structure, sizeof (msc_file_handler_t )
74
+ + fileName.size () + 1 , IPC_CREAT | 0666 );
75
+ toBeCreated = false ;
76
+ if (shm_id < 0 ) {
77
+ error->assign (" Failed to allocate shared memory (1): " );
78
+ error->append (strerror (errno));
79
+ goto err_shmget1;
80
+ }
88
81
}
89
82
90
83
new_debug_log = reinterpret_cast <msc_file_handler_t *>(
91
- shmat (shm_id, NULL , 0 ));
84
+ shmat (shm_id, NULL , 0 ));
92
85
if ((reinterpret_cast <char *>(new_debug_log)[0 ]) == -1 ) {
93
86
error->assign (" Failed to attach shared memory (1): " );
94
87
error->append (strerror (errno));
95
88
goto err_shmat1;
96
89
}
97
- memset (new_debug_log, ' \0 ' , sizeof (msc_file_handler_t ));
98
-
99
- pthread_mutex_init (&new_debug_log->lock , NULL );
100
- new_debug_log->fp = fp;
101
- new_debug_log->file_handler = fileno (new_debug_log->fp );
102
- new_debug_log->next = NULL ;
103
- new_debug_log->previous = NULL ;
104
- new_debug_log->shm_id_structure = shm_id;
105
- shm_id = shmget (mem_key_file_name, (fileName.size () + 1 * sizeof (char )),
106
- IPC_CREAT | 0666 );
107
- if (shm_id < 0 ) {
108
- error->assign (" Failed to allocate shared memory (2): " );
109
- error->append (strerror (errno));
110
- goto err_shmget2;
111
- }
112
- new_debug_log->shm_id_file_name = shm_id;
113
- shm_ptr2 = reinterpret_cast <char *>(shmat (shm_id, NULL , 0 ));
114
- if (shm_ptr2[0 ] == -1 ) {
115
- error->assign (" Failed to attach shared memory (2): " );
116
- error->append (strerror (errno));
117
- goto err_shmat2;
118
- }
119
- memcpy (shm_ptr2, fileName.c_str (), fileName.size ());
120
- shm_ptr2[fileName.size ()] = ' \0 ' ;
121
-
122
- new_debug_log->file_name = shm_ptr2;
123
-
124
- if (m_first == NULL ) {
125
- m_first = new_debug_log;
126
- } else {
127
- current = m_first;
128
- while (current != NULL ) {
129
- if (current->next == NULL ) {
130
- current->next = new_debug_log;
131
- new_debug_log->previous = current;
132
- new_debug_log->next = NULL ;
133
- current = NULL ;
134
- } else {
135
- current = reinterpret_cast <msc_file_handler_t *>(
136
- current->next );
137
- }
138
- }
90
+
91
+ if (toBeCreated) {
92
+ memset (new_debug_log, ' \0 ' , sizeof (msc_file_handler_t ));
93
+ pthread_mutex_init (&new_debug_log->lock , NULL );
94
+ new_debug_log->fp = fp;
95
+ new_debug_log->file_handler = fileno (new_debug_log->fp );
96
+ new_debug_log->shm_id_structure = shm_id;
97
+ memcpy (new_debug_log->file_name , fileName.c_str (), fileName.size ());
98
+ new_debug_log->file_name [fileName.size ()] = ' \0 ' ;
139
99
}
100
+ m_handlers.push_back (std::make_pair (fileName, new_debug_log));
140
101
141
102
return new_debug_log;
142
- err_shmget2:
143
- err_shmat2:
144
- shmdt (shm_ptr2);
145
- fclose (new_debug_log->fp );
146
103
err_shmget1:
147
104
err_shmat1:
148
105
shmdt (new_debug_log);
149
106
err_mem_key:
107
+ fclose (fp);
150
108
err_fh:
151
109
return NULL ;
152
110
}
@@ -173,6 +131,7 @@ bool SharedFiles::open(const std::string& fileName, std::string *error) {
173
131
174
132
void SharedFiles::close (const std::string& fileName) {
175
133
msc_file_handler_t *a;
134
+ int j = 0 ;
176
135
177
136
if (fileName.empty ()) {
178
137
return ;
@@ -186,41 +145,23 @@ void SharedFiles::close(const std::string& fileName) {
186
145
a->using_it --;
187
146
188
147
if (a->using_it == 0 ) {
189
- bool first = false ;
190
148
int shm_id1 = a->shm_id_structure ;
191
- int shm_id2 = a->shm_id_file_name ;
192
149
msc_file_handler_t *p , *n;
193
150
pthread_mutex_lock (&a->lock );
194
151
fclose (a->fp );
195
-
196
- p = reinterpret_cast <msc_file_handler_t *>(a->previous );
197
- n = reinterpret_cast <msc_file_handler_t *>(a->next );
198
- if (p != NULL ) {
199
- p->next = reinterpret_cast <msc_file_handler_t *>(n);
200
- }
201
- if (n != NULL ) {
202
- n->previous = reinterpret_cast <msc_file_handler_t *>(p);
203
- }
204
- a->previous = NULL ;
205
- a->next = NULL ;
206
152
pthread_mutex_unlock (&a->lock );
207
153
pthread_mutex_destroy (&a->lock );
208
-
209
- if (a->file_name == m_first->file_name ) {
210
- first = true ;
211
- }
212
-
213
- shmdt (a->file_name );
214
154
shmdt (a);
215
-
216
155
shmctl (shm_id1, IPC_RMID, NULL );
217
- shmctl (shm_id2, IPC_RMID, NULL );
156
+ }
218
157
219
- if (first) {
220
- m_first = NULL ;
158
+ for (const auto &i: m_handlers) {
159
+ if (i.first == fileName) {
160
+ j++;
221
161
}
222
- a = NULL ;
223
162
}
163
+
164
+ m_handlers.erase (m_handlers.begin () + j, m_handlers.begin () + j + 1 );
224
165
}
225
166
226
167
0 commit comments