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