Skip to content

Commit c3687c4

Browse files
committed
add a get for user defined object
1 parent a42e338 commit c3687c4

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

lua_shm.cc

+56-28
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ inline key_t _create(unsigned int count, ShmBufferType type, size_t type_size, i
211211
}
212212

213213
//
214-
key_t create_object_regions(unsigned int count, size_t type_size, int key) {
214+
key_t create_object_region(unsigned int count, size_t type_size, int key) {
215215
if ( key < 0 ) return(-1);
216216
else if ( key > 0 ){
217217
if ( !( (key >= keyMin) && (key <= keyMax) ) ) {
@@ -222,7 +222,7 @@ key_t create_object_regions(unsigned int count, size_t type_size, int key) {
222222
if ( !(count >= lengthMin && count <= lengthMax) ) {
223223
return -1;
224224
}
225-
if ( !(type_size >= userSizeMin && count <= userSizeMax) ) {
225+
if ( !(type_size >= userSizeMin && type_size <= userSizeMax) ) {
226226
return -1;
227227
}
228228
//
@@ -248,46 +248,74 @@ key_t create(unsigned int count, ShmBufferType type, int key) {
248248
}
249249

250250

251-
key_t get(key_t key, ShmBufferType type) {
251+
252+
inline key_t _get_and_set_memres(size_t type_size, int key, ShmBufferType type) {
253+
unsigned int count = 0;
254+
int resId = 0;
255+
void *ref = NULL;
256+
int status = _get(key,count,0,0,type_size,&ref,&resId);
257+
//
258+
if ( status == 0 ) {
259+
//
260+
Mem_resource *ms = new Mem_resource();
261+
g_all_regions[key] = ms;
262+
//
263+
//
264+
ms->count = count; // set by memsize (has to be found)
265+
ms->key = key;
266+
ms->type = type;
267+
ms->el_sz = type_size;
268+
ms->_shared_resource = ref;
269+
ms->resid = resId;
270+
//
271+
return(key);
272+
}
273+
//
274+
return(-1);
275+
}
276+
277+
278+
279+
280+
key_t get_object_region(key_t key,size_t type_size) {
252281
if ( key < 0 ) return(-1);
253282
else if ( key > 0 ){
254283
if ( !( (key >= keyMin) && (key <= keyMax) ) ) {
255284
return -1;
256285
}
257286
}
258-
if ( type < 0 || type > SHMBT_FLOAT64 ) type = SHMBT_BUFFER;
287+
if ( !(type_size >= userSizeMin && type_size <= userSizeMax) ) {
288+
return -1;
289+
}
290+
//
291+
map<key_t,Mem_resource *>::iterator fnd = g_all_regions.find(key);
292+
if ( fnd != g_all_regions.end() ) {
293+
return(key);
294+
} else {
295+
return(_get_and_set_memres(type_size, key, SHMBT_USER));
296+
}
297+
return(-1);
298+
}
259299

300+
301+
key_t get(key_t key, ShmBufferType type) {
302+
if ( key < 0 ) return(-1);
303+
else if ( key > 0 ){
304+
if ( !( (key >= keyMin) && (key <= keyMax) ) ) {
305+
return -1;
306+
}
307+
}
308+
if ( type < 0 || type >= SHMBT_USER ) type = SHMBT_BUFFER;
260309
//
261310
map<key_t,Mem_resource *>::iterator fnd = g_all_regions.find(key);
262311
if ( fnd != g_all_regions.end() ) {
263312
return(key);
264313
} else {
265-
314+
//
266315
size_t type_size = getSize1ForShmBufferType(type);
267-
unsigned int count = 0;
268-
int resId = 0;
269-
void *ref = NULL;
270-
int status = _get(key,count,0,0,type_size,&ref,&resId);
271-
272-
if ( status == 0 ) {
273-
//
274-
key_t k = key;
275-
Mem_resource *ms = new Mem_resource();
276-
g_all_regions[key] = ms;
277-
//
278-
//
279-
ms->count = count; // set by memsize (has to be found)
280-
ms->key = k;
281-
ms->type = type;
282-
ms->el_sz = type_size;
283-
ms->_shared_resource = ref;
284-
ms->resid = resId;
285-
//
286-
return(k);
287-
}
288-
316+
//
317+
return(_get_and_set_memres(type_size, key, type));
289318
}
290-
291319
return(-1);
292320
}
293321

lua_shm.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <sys/shm.h>
1010
#include <cmath>
1111
#include <complex>
12-
12+
//
1313
#include <stdio.h> /* printf, scanf, puts, NULL */
1414
#include <stdlib.h> /* srand, rand */
1515
#include <time.h> /* time */
@@ -154,8 +154,9 @@ extern "C" {
154154
*/
155155
key_t create(unsigned int count, ShmBufferType type, int key);
156156

157-
key_t create_object_regions(unsigned int count, size_t type_size, int key);
158-
157+
key_t create_object_region(unsigned int count, size_t type_size, int key);
158+
key_t get_object_region(key_t key,size_t type_size);
159+
159160
/**
160161
* Get shared memory segment
161162
* @param {int} key - integer key of shared memory segment

shm_typed_array.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ffi.cdef[[
2727
void detachAll(void);
2828
unsigned long int getTotalSize(void);
2929
key_t create(unsigned int count, ShmBufferType type, int key);
30-
key_t create_object_regions(unsigned int count, size_t type_size, int key);
30+
key_t get_object_region(key_t key,size_t type_size);
3131
key_t get(key_t key, ShmBufferType type);
3232
//
3333
Mem_resource *MemResrouceFromKey(key_t key);

0 commit comments

Comments
 (0)