Fawkes API  Fawkes Development Version
shm_lut.cpp
1 
2 /***************************************************************************
3  * shm_lut.cpp - shared memory lookup table
4  *
5  * Generated: Thu feb 09 17:32:31 2006
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvutils/ipc/shm_lut.h>
25 #include <fvutils/ipc/shm_exceptions.h>
26 #include <utils/system/console_colors.h>
27 
28 #include <iostream>
29 #include <cstring>
30 #include <cstdlib>
31 #include <cstdio>
32 
33 using namespace std;
34 using namespace fawkes;
35 
36 namespace firevision {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 
41 /** @class SharedMemoryLookupTable <fvutils/ipc/shm_lut.h>
42  * Shared memory lookup table.
43  */
44 
45 /** Write Constructor.
46  * Create a new shared memory segment. Will open a shared memory segment that
47  * exactly fits the given information. Will throw an error if image with num
48  * image_num exists it will throw an exception an exception.
49  * I will create a new segment if no matching segment was found.
50  * The segment is accessed in read-write mode.
51  *
52  * @param lut_id LUT ID
53  * @param width LUT width
54  * @param height LUT height
55  * @param depth LUT depth
56  * @param bytes_per_cell LUT bytes per cell
57  */
58 SharedMemoryLookupTable::SharedMemoryLookupTable(const char *lut_id,
59  unsigned int width,
60  unsigned int height,
61  unsigned int depth,
62  unsigned int bytes_per_cell)
63  : SharedMemory(FIREVISION_SHM_LUT_MAGIC_TOKEN, false, true, true)
64 {
65  constructor(lut_id, width, height, depth, bytes_per_cell, false);
66 }
67 
68 /** Read constructor.
69  * This constructor is used to search for an existing shared memory segment.
70  * It will throw an error if it cannot find a segment with the specified data.
71  * The segment is opened read-only by default, but this can be overridden with
72  * the is_read_only argument if needed.
73  *
74  * @param lut_id LUT ID
75  * @param is_read_only true to open read-only
76  */
78  bool is_read_only)
79  : SharedMemory(FIREVISION_SHM_LUT_MAGIC_TOKEN, is_read_only, false, false)
80 {
81  constructor(lut_id, 0, 0, 0, 0, is_read_only);
82 }
83 
84 
85 void
86 SharedMemoryLookupTable::constructor(const char *lut_id,
87  unsigned int width, unsigned int height,
88  unsigned int depth,
89  unsigned int bytes_per_cell,
90  bool is_read_only)
91 {
93  __lut_id = strdup(lut_id);
94  __width = width;
95  __height = height;
96  __depth = depth;
97  __bytes_per_cell = bytes_per_cell;
98 
99  __priv_header = new SharedMemoryLookupTableHeader(__lut_id, __width, __height, __depth, __bytes_per_cell);
100  _header = __priv_header;
101  attach();
102  __raw_header = __priv_header->raw_header();
103 
104  if (_memptr == NULL) {
105  throw Exception("Could not create shared memory segment");
106  }
107 }
108 
109 
110 /** Destructor. */
112 {
113  delete __priv_header;
114  ::free(__lut_id);
115 }
116 
117 
118 /** Get LUT ID.
119  * @return LUT ID
120  */
121 const char *
123 {
124  return __lut_id;
125 }
126 
127 
128 /** Set LUT ID.
129  * @param lut_id LUT ID
130  * @return true on success
131  */
132 bool
134 {
135  free();
136  ::free(__lut_id);
137  __lut_id = strdup(lut_id);
138  __priv_header->set_lut_id(__lut_id);
139  attach();
140  return (_memptr != NULL);
141 }
142 
143 
144 /** Get LUT buffer.
145  * @return LUT buffer
146  */
147 unsigned char *
149 {
150  return (unsigned char *)_memptr;
151 }
152 
153 
154 /** Get LUT width.
155  * @return LUT width
156  */
157 unsigned int
159 {
160  return __raw_header->width;
161 }
162 
163 
164 /** Get LUT height.
165  * @return LUT height
166  */
167 unsigned int
169 {
170  return __raw_header->height;
171 }
172 
173 
174 /** Get LUT depth.
175  * @return LUT depth
176  */
177 unsigned int
179 {
180  return __raw_header->depth;
181 }
182 
183 
184 /** Get bytes per cell.
185  * @return bytes per cell
186  */
187 unsigned int
189 {
190  return __raw_header->bytes_per_cell;
191 }
192 
193 
194 /** List shared memory LUT segments. */
195 void
197 {
200 
201  SharedMemory::list(FIREVISION_SHM_LUT_MAGIC_TOKEN, h, lister);
202 
203  delete lister;
204  delete h;
205 }
206 
207 
208 /** Erase all shared memory segments that contain FireVision LUTs.
209  * @param use_lister if true a lister is used to print the shared memory segments
210  * to stdout while cleaning up.
211  */
212 void
214 {
215  SharedMemoryLookupTableLister *lister = NULL;
217 
218  if ( use_lister ) {
219  lister = new SharedMemoryLookupTableLister();
220  }
221 
222  SharedMemory::erase_orphaned(FIREVISION_SHM_LUT_MAGIC_TOKEN, h, lister);
223 
224  delete lister;
225  delete h;
226 }
227 
228 
229 /** Check LUT availability.
230  * @param lut_id image number to check
231  * @return true if shared memory segment with requested LUT exists
232  */
233 bool
235 {
237  bool ex = SharedMemory::exists(FIREVISION_SHM_LUT_MAGIC_TOKEN, h);
238  delete h;
239  return ex;
240 }
241 
242 
243 /** Erase a specific shared memory segment that contains a LUT.
244  * @param lut_id LUT ID
245  */
246 void
248 {
250  SharedMemory::erase(FIREVISION_SHM_LUT_MAGIC_TOKEN, h, NULL);
251  delete h;
252 }
253 
254 
255 
256 /** @class SharedMemoryLookupTableHeader <fvutils/ipc/shm_lut.h>
257  * Shared memory lookup table header.
258  */
259 
260 /** Constructor. */
262 {
263  __lut_id = NULL;
264  __width = 0;
265  __height = 0;
266  __depth = 0;
267  __bytes_per_cell = 0;
268  __header = NULL;
269 }
270 
271 
272 /** Constructor.
273  * @param lut_id LUT ID
274  * @param width LUT width
275  * @param height LUT height
276  * @param bytes_per_cell bytes per cell
277  */
279  unsigned int width,
280  unsigned int height,
281  unsigned int bytes_per_cell)
282 {
283  __lut_id = strdup(lut_id);
284  __width = width;
285  __height = height;
286  __bytes_per_cell = bytes_per_cell;
287 
288  __header = NULL;
289 }
290 
291 
292 /** Constructor.
293  * @param lut_id LUT ID
294  * @param width LUT width
295  * @param height LUT height
296  * @param depth LUT depth
297  * @param bytes_per_cell bytes per cell
298  */
300  unsigned int width,
301  unsigned int height,
302  unsigned int depth,
303  unsigned int bytes_per_cell)
304 {
305  __lut_id = strdup(lut_id);
306  __width = width;
307  __height = height;
308  __depth = depth;
309  __bytes_per_cell = bytes_per_cell;
310 
311  __header = NULL;
312 }
313 
314 
315 /** Copy constructor.
316  * @param h header to copy data from
317  */
319 {
320  if( h->__lut_id != NULL ) {
321  __lut_id = strdup(h->__lut_id);
322  } else {
323  __lut_id = NULL;
324  }
325  __width = h->__width;
326  __height = h->__height;
327  __depth = h->__depth;
328  __bytes_per_cell = h->__bytes_per_cell;
329 
330  __header = NULL;
331 }
332 
333 
334 /** Destructor. */
336 {
337  __header = NULL;
338  if ( __lut_id != NULL ) {
339  free(__lut_id);
340  __lut_id = NULL;
341  }
342 }
343 
344 
347 {
348  return new SharedMemoryLookupTableHeader(this);
349 }
350 
351 
352 size_t
354 {
355  return sizeof(SharedMemoryLookupTable_header_t);
356 }
357 
358 
359 size_t
361 {
362  if (__header == NULL) {
363  return __width * __height * __depth * __bytes_per_cell;
364  } else {
365  return __header->width * __header->height * __header->depth * __header->bytes_per_cell;
366  }
367 }
368 
369 
370 bool
372 {
374 
375  if (__lut_id == NULL) {
376  return true;
377 
378  } else if (strncmp(h->lut_id, __lut_id, LUT_ID_MAX_LENGTH) == 0) {
379 
380  if ( (__width == 0) ||
381  (__height == 0) ||
382  (__depth == 0) ||
383  (__bytes_per_cell == 0) ||
384  ( (h->width == __width) &&
385  (h->height == __height) &&
386  (h->depth == __depth) &&
387  (h->bytes_per_cell == __bytes_per_cell) )
388  ) {
389  return true;
390  } else {
391  throw InconsistentLUTException("Inconsistent lookup table found in memory (meta)");
392  }
393  } else {
394  return false;
395  }
396 
397 }
398 
399 
400 /** Print Info. */
401 void
403 {
404  if (__header == NULL) {
405  cout << "No image set" << endl;
406  return;
407  }
408  cout << "SharedMemory Lookup Table Info: " << endl
409  << " LUT ID: " << __header->lut_id << endl
410  << " dimensions: " << __header->width << "x" << __header->height << "x"
411  << __header->depth << endl
412  << " bytes per cell: " << __header->bytes_per_cell << endl;
413 }
414 
415 
416 /** Check if buffer should be created.
417  * @return true, if width, height and bytes per cell are all greater than
418  * zero.
419  */
420 bool
422 {
423  return ( (__width > 0) &&
424  (__height > 0) &&
425  (__depth > 0) &&
426  (__bytes_per_cell > 0) );
427 }
428 
429 
430 void
432 {
433  __header = (SharedMemoryLookupTable_header_t *)memptr;
434  memset(memptr, 0, sizeof(SharedMemoryLookupTable_header_t));
435 
436  strncpy(__header->lut_id, __lut_id, LUT_ID_MAX_LENGTH);
437  __header->width = __width;
438  __header->height = __height;
439  __header->depth = __depth;
440  __header->bytes_per_cell = __bytes_per_cell;
441 }
442 
443 
444 void
446 {
447  __header = (SharedMemoryLookupTable_header_t *)memptr;
448 }
449 
450 
451 void
453 {
454  __header = NULL;
455 }
456 
457 
458 /** Check for equality of headers.
459  * First checks if passed SharedMemoryHeader is an instance of
460  * SharedMemoryLookupTableHeader. If not returns false, otherwise it compares
461  * LUT ID, width, height, depth and bytes per cell. If all match returns true,
462  * false if any of them differs.
463  * @param s shared memory header to compare to
464  * @return true if the two instances identify the very same shared memory segments,
465  * false otherwise
466  */
467 bool
469 {
470  const SharedMemoryLookupTableHeader *h = dynamic_cast<const SharedMemoryLookupTableHeader *>(&s);
471  if ( ! h ) {
472  return false;
473  } else {
474  return ( (strncmp(__lut_id, h->__lut_id, LUT_ID_MAX_LENGTH) == 0) &&
475  (__width == h->__width) &&
476  (__height == h->__height) &&
477  (__depth == h->__depth) &&
478  (__bytes_per_cell == h->__bytes_per_cell) );
479  }
480 }
481 
482 
483 /** Get LUT width.
484  * @return LUT width.
485  */
486 unsigned int
488 {
489  if (__header == NULL) return 0;
490  return __header->width;
491 }
492 
493 
494 /** Get LUT height.
495  * @return LUT height.
496  */
497 unsigned int
499 {
500  if (__header == NULL) return 0;
501  return __header->height;
502 }
503 
504 
505 /** Get LUT depth.
506  * @return LUT depth.
507  */
508 unsigned int
510 {
511  if (__header == NULL) return 0;
512  return __header->depth;
513 }
514 
515 
516 /** Get bytes per cell.
517  * @return bytes per cell.
518  */
519 unsigned int
521 {
522  if (__header == NULL) return 0;
523  return __header->bytes_per_cell;
524 }
525 
526 
527 /** Get LUT ID.
528  * @return LUT Id
529  */
530 const char *
532 {
533  if (__header == NULL) return NULL;
534  return __header->lut_id;
535 }
536 
537 
538 /** Set LUT ID.
539  * @param lut_id LUT ID
540  */
541 void
543 {
544  if ( __lut_id ) free(__lut_id);
545  __lut_id = strdup(lut_id);
546 }
547 
548 
549 /** Get raw header.
550  * @return raw header.
551  */
554 {
555  return __header;
556 }
557 
558 /** @class SharedMemoryLookupTableLister <fvutils/ipc/shm_lut.h>
559  * Shared memory lookup table lister.
560  */
561 
562 
563 /** Constructor. */
565 {
566 }
567 
568 
569 /** Destructor. */
571 {
572 }
573 
574 
575 void
577 {
578  cout << endl << cgreen << "FireVision Shared Memory Segments - Lookup Tables"
579  << cnormal << endl
580  << "========================================================================================" << endl
581  << cdarkgray;
582  printf ("%-23s %-10s %-10s %-10s %-9s %-9s %-9s\n",
583  "LUT ID", "ShmID", "Semaphore", "Bytes", "Width", "Height", "State");
584  cout << cnormal
585  << "----------------------------------------------------------------------------------------" << endl;
586 }
587 
588 
589 void
591 {
592 }
593 
594 
595 void
597 {
598  cout << "No FireVision shared memory segments containing lookup tables found" << endl;
599 }
600 
601 
602 
603 
604 void
606 {
607  cout << "No orphaned FireVision shared memory segments containing lookup tables found" << endl;
608 }
609 
610 void
612  int shm_id, int semaphore,
613  unsigned int mem_size,
614  const void *memptr)
615 {
616 
618 
619  printf("%-23s %-10d %-10d %-10u %-9u %-9u %s%s\n",
620  h->lut_id(), shm_id, semaphore, mem_size,
621  h->width(), h->height(),
622  (SharedMemory::is_swapable(shm_id) ? "S" : ""),
623  (SharedMemory::is_destroyed(shm_id) ? "D" : "")
624  );
625 }
626 
627 } // end namespace firevision
uint32_t bytes_per_cell
Bytes per cell.
Definition: shm_lut.h:46
const char * lut_id() const
Get LUT ID.
Definition: shm_lut.cpp:531
const char * lut_id() const
Get LUT ID.
Definition: shm_lut.cpp:122
Fawkes library namespace.
virtual bool matches(void *memptr)
Method to check if the given memptr matches this header.
Definition: shm_lut.cpp:371
virtual void initialize(void *memptr)
Initialize the header.
Definition: shm_lut.cpp:431
unsigned int height() const
Get LUT height.
Definition: shm_lut.cpp:498
Shared memory lookup table header.
Definition: shm_lut.h:50
STL namespace.
virtual bool operator==(const fawkes::SharedMemoryHeader &s) const
Check for equality of headers.
Definition: shm_lut.cpp:468
virtual void print_info(const fawkes::SharedMemoryHeader *header, int shm_id, int semaphore, unsigned int mem_size, const void *memptr)
Print info about segment.
Definition: shm_lut.cpp:611
virtual void print_header()
Print header of the table.
Definition: shm_lut.cpp:576
virtual size_t size()
Size of the header.
Definition: shm_lut.cpp:353
virtual ~SharedMemoryLookupTableHeader()
Destructor.
Definition: shm_lut.cpp:335
static void wipe(const char *lut_id)
Erase a specific shared memory segment that contains a LUT.
Definition: shm_lut.cpp:247
virtual void print_info()
Print Info.
Definition: shm_lut.cpp:402
unsigned int width() const
Get LUT width.
Definition: shm_lut.cpp:158
unsigned int bytes_per_cell() const
Get bytes per cell.
Definition: shm_lut.cpp:188
unsigned int width() const
Get LUT width.
Definition: shm_lut.cpp:487
SharedMemoryLookupTable_header_t * raw_header()
Get raw header.
Definition: shm_lut.cpp:553
char lut_id[LUT_ID_MAX_LENGTH]
LUT ID.
Definition: shm_lut.h:42
bool is_read_only() const
Check for read-only mode.
Definition: shm.cpp:653
bool set_lut_id(const char *lut_id)
Set LUT ID.
Definition: shm_lut.cpp:133
virtual void set(void *memptr)
Set information from memptr.
Definition: shm_lut.cpp:445
static bool exists(const char *lut_id)
Check LUT availability.
Definition: shm_lut.cpp:234
void free()
Detach from and maybe destroy the shared memory segment.
Definition: shm.cpp:423
virtual ~SharedMemoryLookupTableLister()
Destructor.
Definition: shm_lut.cpp:570
Base class for exceptions in Fawkes.
Definition: exception.h:36
void attach()
Attach to the shared memory segment.
Definition: shm.cpp:450
unsigned int bytes_per_cell() const
Get bytes per cell.
Definition: shm_lut.cpp:520
unsigned int height() const
Get LUT height.
Definition: shm_lut.cpp:168
void * memptr() const
Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared me...
Definition: shm.cpp:682
unsigned int depth() const
Get LUT depth.
Definition: shm_lut.cpp:178
void * _memptr
Pointer to the data segment.
Definition: shm.h:177
virtual void print_no_segments()
Print this if no matching segment was found.
Definition: shm_lut.cpp:596
virtual size_t data_size()
Return the size of the data.
Definition: shm_lut.cpp:360
virtual void reset()
Reset information previously set with set().
Definition: shm_lut.cpp:452
Shared memory lookup table header struct.
Definition: shm_lut.h:41
Shared memory segment.
Definition: shm.h:49
unsigned char * buffer() const
Get LUT buffer.
Definition: shm_lut.cpp:148
bool _is_read_only
Read-only.
Definition: shm.h:181
virtual bool create()
Check if buffer should be created.
Definition: shm_lut.cpp:421
void set_lut_id(const char *lut_id)
Set LUT ID.
Definition: shm_lut.cpp:542
SharedMemoryHeader * _header
Data-specific header.
Definition: shm.h:180
static void cleanup(bool use_lister=true)
Erase all shared memory segments that contain FireVision LUTs.
Definition: shm_lut.cpp:213
Shared memory lookup table lister.
Definition: shm_lut.h:97
Throw if an inconsistent LUT was found.
virtual void print_no_orphaned_segments()
Print this if no matching orphaned segment was found.
Definition: shm_lut.cpp:605
virtual fawkes::SharedMemoryHeader * clone() const
Clone this shared memory header.
Definition: shm_lut.cpp:346
Interface for shared memory header.
Definition: shm.h:33
SharedMemoryLookupTable(const char *lut_id, unsigned int width, unsigned int height, unsigned int depth=1, unsigned int bytes_per_cell=1)
Write Constructor.
Definition: shm_lut.cpp:58
unsigned int depth() const
Get LUT depth.
Definition: shm_lut.cpp:509
virtual void print_footer()
Print footer of the table.
Definition: shm_lut.cpp:590
static void list()
List shared memory LUT segments.
Definition: shm_lut.cpp:196