18 #ifndef __SHAREDIM_STACK_H__
19 #define __SHAREDIM_STACK_H__
21 #define IM0_OFFSET (sizeof(sharedim_stack_header) + header->n_images*sizeof(int))
22 #define IMD_OFFSET (2*sizeof(int)+sizeof(int *))
60 #ifndef ERROR_REPORTSIM
61 #define ERROR_REPORTSIM(er) if(global_error_report) (*global_error_report)(er,__FILE__,__LINE__);
65 #define LOG_INFOSIM(li) if(global_log_info) (*global_log_info)(li);
122 void * get_shmemptr(){
return shmemptr;}
124 int get_shmemid(){
return shmemid;}
125 size_t get_shmemsz(){
return shmemsz;}
134 int get_last_image_abs(){
return header->last_image_abs;}
160 next_last_image = -1;
167 return header->n_images;
177 header->max_n_images = mni;
191 return header->max_n_images;
200 return header->last_image;
209 if(imno < header->n_images)
228 if(imno > header->n_images)
230 ERROR_REPORTSIM(
"Can't calculate image offset without previous image.");
254 std::ostringstream oss;
257 if((shmemid = shmget(mkey, sz, IPC_CREAT | 0666))<0)
263 oss <<
"shmget returned EINVAL. Check kernel SHMMAX paramter in /etc/sysctl.conf.";
264 oss <<
" sz = " << sz;
265 ERROR_REPORTSIM(oss.str().c_str());
269 shmemid = shmget(mkey, 1, 0666);
273 if(shmctl(shmemid, IPC_RMID, 0) < 0)
276 oss <<
"Could not remove shared memory with key " << mkey <<
" and ID " << shmemid <<
".";
277 ERROR_REPORTSIM(oss.str().c_str());
280 oss <<
"Removed shared memory with key " << mkey <<
".";
281 LOG_INFOSIM(oss.str().c_str());
284 if((shmemid = shmget(mkey, sz, IPC_CREAT | 0666))<0)
286 oss <<
"Could not create shared memory with key " << mkey <<
".";
287 ERROR_REPORTSIM(oss.str().c_str());
292 oss <<
"Shared memory created with key " << mkey <<
".";
293 LOG_INFOSIM(oss.str().c_str());
307 set_max_n_images(nims);
308 header->last_image = -1;
309 header->last_image_abs = -1;
310 header->n_images = 0;
317 std::ostringstream oss;
318 static int reported = 0;
321 if((shmemid = shmget(mkey, 0, 0666))<0)
325 oss <<
"Could not get shared memory with key " << mkey;
326 ERROR_REPORTSIM(oss.str().c_str());
334 if ((shmemptr = shmat(shmemid, 0, 0)) == (
char *) -1)
338 oss <<
"Could not attach shared memory with key " << mkey;
339 ERROR_REPORTSIM(oss.str().c_str());
346 if (shmctl(shmemid, IPC_STAT, &shmstats) < 0)
350 oss <<
"Could not get shared memory stats with key " << mkey;
351 ERROR_REPORTSIM(oss.str().c_str());
358 shmemsz = shmstats.shm_segsz;
360 oss <<
"Attached to shared memory with key " << mkey <<
" of size: " << shmemsz;
361 LOG_INFOSIM(oss.str().c_str());
376 if(!shmemptr)
return 0;
379 if(header->last_image + 1 < header->max_n_images)
381 nextim = calc_im_pos((header->last_image) + 1);
384 std::cerr <<
"Error in calc_im_pos. Last image " <<
" " <<header->last_image<< std::endl;
393 if( (intptr_t)nextnextim - (intptr_t) shmemptr > (intptr_t)(shmemsz-1))
395 std::cerr <<
"Wrapping " << (header->last_image) + 1 <<
" " << (intptr_t)nextnextim - (intptr_t) shmemptr << "\n";
397 nextim = (
sharedim<IMDATA_TYPE>*)((intptr_t)header + image_list[0]);
402 next_last_image = header->last_image + 1;
403 image_list[next_last_image] = (intptr_t)nextim-(intptr_t)header;
415 nextim->frameNo = -1;
423 header->last_image = next_last_image;
424 if(header->n_images <= next_last_image) header->n_images++;
425 header->last_image_abs++;
439 if(imno < header->n_images)
445 sim.depth = simraw->depth;
446 sim.frameNo = simraw->frameNo;
447 sim.frame_time = simraw->frame_time;
448 sim.frame_time_dma = simraw->frame_time_dma;
449 sim.saved = simraw->saved;
461 #endif //__SHAREDIM_STACK_H__
key_t shmemkey
The key used to lookup the shared memory.
int enable_next_image()
Increments last_image.
size_t shmemsz
Size of the shared memory block.
int set_max_n_images(int mni)
Sets max_n_images in the header.
int set_saved(int imno, int sv)
Update the saved field of this image.
Class to manage a stack of images in shared memory.
struct shmid_ds shmstats
Used to retrieve shared memory block size.
sharedim_stack_header * header
Convenient pointer to the stack header, has same value as shmemptr.
sharedim< IMDATA_TYPE > * calc_im_pos(int imno)
Calculates the offset of the image given by imno, takes into account max_n_images and available space...
sharedim< IMDATA_TYPE > * set_next_image(int nx, int ny)
Sets the next image, and returns a pointer to it, but does not increment last_image.
VisAO software utilitites, declarations.
int attach_shm(key_t mkey)
Attachess the shared memory. A Reader process should start here.
void * attach_shm(size_t *sz, key_t mkey, int shmemid)
Attach to a shared memory buffer and get its size.
int shmemid
The shared memory id.
sharedim< IMDATA_TYPE > get_image(int imno)
Gets an image, as a sharedim structure, with the imdata pointer properly set for the calling process...
void * shmemptr
The pointer to the shared memory block.
Convenience structure for an image.
int create_shm(key_t mkey, size_t sz)
For the writer process, creates then attaches the shared memory.
intptr_t * image_list
Convenient pointer to the list of image offsets.
sharedim_stack()
Constructor.
int get_n_images()
Returns the value of n_images currently in the header.
int get_last_image()
Returns the value of last_image currently in the header.
int get_max_n_images()
Returns the value of max_n_images currently in the header.