ucommon
mapped.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
29 #ifndef _UCOMMON_MAPPED_H_
30 #define _UCOMMON_MAPPED_H_
31 
32 #ifndef _UCOMMON_LINKED_H_
33 #include <ucommon/linked.h>
34 #endif
35 
36 #ifndef _UCOMMON_THREAD_H_
37 #include <ucommon/thread.h>
38 #endif
39 
40 #ifndef _UCOMMON_STRING_H_
41 #include <ucommon/string.h>
42 #endif
43 
44 #ifndef _MSWINDOWS_
45 #include <signal.h>
46 #endif
47 
48 NAMESPACE_UCOMMON
49 
58 class __EXPORT MappedMemory
59 {
60 private:
61  size_t mapsize;
62  caddr_t map;
63  fd_t fd;
64 
65 protected:
66  size_t size, used;
67  char idname[65];
68  bool erase;
69 
70  MappedMemory();
71 
78  void create(const char *name, size_t size = (size_t)0);
79 
84  virtual void *invalid(void) const;
85 
89  virtual void fault(void) const;
90 
91 public:
98  MappedMemory(const char *name, size_t size);
99 
106  MappedMemory(const char *name);
107 
111  virtual ~MappedMemory();
112 
116  void release(void);
117 
124  static void remove(const char *name);
125 
130  inline operator bool() const
131  {return (size != 0);};
132 
137  inline bool operator!() const
138  {return (size == 0);};
139 
147  void *sbrk(size_t size);
148 
154  void *offset(size_t offset) const;
155 
164  bool copy(size_t offset, void *buffer, size_t size) const;
165 
170  inline size_t len(void)
171  {return size;};
172 
177  inline caddr_t addr(void)
178  {return map;};
179 
187  static void disable(void);
188 };
189 
199 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
200 {
201 private:
202  unsigned objsize;
203  unsigned reading;
204  mutex_t mutex;
205 
206 protected:
207  MappedReuse(size_t osize);
208 
209  inline void create(const char *fname, unsigned count)
210  {MappedMemory::create(fname, count * objsize);};
211 
212 public:
225  MappedReuse(const char *name, size_t size, unsigned count);
226 
231  bool avail(void);
232 
237  ReusableObject *request(void);
238 
244  ReusableObject *get(void);
245 
253  ReusableObject *getTimed(timeout_t timeout);
254 
260  ReusableObject *getLocked(void);
261 
267  void removeLocked(ReusableObject *object);
268 };
269 
276 template <class T>
278 {
279 protected:
280  inline mapped_array() : MappedMemory() {};
281 
282  inline void create(const char *fn, unsigned members)
283  {MappedMemory::create(fn, members * sizeof(T));};
284 
285 public:
294  inline mapped_array(const char *name, unsigned number) :
295  MappedMemory(name, number * sizeof(T)) {};
296 
301  inline void initialize(void)
302  {new((caddr_t)offset(0)) T[size / sizeof(T)];};
303 
308  inline void *addLock(void)
309  {return sbrk(sizeof(T));};
310 
316  inline T *operator()(unsigned member)
317  {return static_cast<T*>(offset(member * sizeof(T)));}
318 
323  inline T *operator()(void)
324  {return static_cast<T*>(sbrk(sizeof(T)));};
325 
331  inline T& operator[](unsigned member)
332  {return *(operator()(member));};
333 
338  inline unsigned max(void)
339  {return (unsigned)(size / sizeof(T));};
340 };
341 
349 template <class T>
350 class mapped_reuse : public MappedReuse
351 {
352 protected:
353  inline mapped_reuse() :
354  MappedReuse(sizeof(T)) {};
355 
356 public:
364  inline mapped_reuse(const char *name, unsigned number) :
365  MappedReuse(name, sizeof(T), number) {};
366 
371  inline void initialize(void)
372  {new((caddr_t)pos(0)) T[size / sizeof(T)];};
373 
378  inline operator bool() const
379  {return MappedReuse::avail();};
380 
385  inline bool operator!() const
386  {return !MappedReuse::avail();};
387 
393  inline operator T*()
394  {return mapped_reuse::get();};
395 
401  inline T* operator*()
402  {return mapped_reuse::get();};
403 
409  inline T *pos(size_t member)
410  {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));};
411 
417  inline T *get(void)
418  {return static_cast<T*>(MappedReuse::get());};
419 
427  inline T *getTimed(timeout_t timeout)
428  {return static_cast<T*>(MappedReuse::getTimed(timeout));};
429 
435  inline T *request(void)
436  {return static_cast<T*>(MappedReuse::request());};
437 
443  inline void removeLocked(T *object)
444  {MappedReuse::removeLocked(object);};
445 
451  inline T *getLocked(void)
452  {return static_cast<T*>(MappedReuse::getLocked());};
453 
458  inline void release(T *object)
459  {ReusableAllocator::release(object);};
460 };
461 
468 template <class T>
469 class mapped_view : protected MappedMemory
470 {
471 public:
477  inline mapped_view(const char *name) :
478  MappedMemory(name) {};
479 
485  inline volatile const T *operator()(unsigned member)
486  {return static_cast<const T*>(offset(member * sizeof(T)));}
487 
493  inline volatile const T &operator[](unsigned member)
494  {return *(operator()(member));};
495 
496  inline volatile const T *get(unsigned member)
497  {return static_cast<const T*>(offset(member * sizeof(T)));};
498 
499  inline void copy(unsigned member, T& buffer)
500  {MappedMemory::copy(member * sizeof(T), &buffer, sizeof(T));};
501 
506  inline unsigned count(void)
507  {return (unsigned)(size / sizeof(T));};
508 };
509 
510 END_NAMESPACE
511 
512 #endif
mapped_view(const char *name)
Map existing named memory segment.
Definition: mapped.h:477
mapped_reuse(const char *name, unsigned number)
Construct mapped reuse array of typed objects.
Definition: mapped.h:364
size_t len(void)
Get size of mapped segment.
Definition: mapped.h:170
Class for resource bound memory pools between threads.
Definition: thread.h:678
bool operator!() const
Test if map is inactive.
Definition: mapped.h:137
T & operator[](unsigned member)
Reference typed object of vector in mapped segment.
Definition: mapped.h:331
Linked objects, lists, templates, and containers.
Template class to map typed vector into shared memory.
Definition: mapped.h:277
void release(T *object)
Used to release a typed object back to the reuse typed object pool.
Definition: mapped.h:458
T * getLocked(void)
Used to get a typed object from the reuse pool when the mutex lock is already held.
Definition: mapped.h:451
void * addLock(void)
Add mapped space while holding lock for one object.
Definition: mapped.h:308
void removeLocked(T *object)
Used to return a typed object to the reuse pool when the mutex lock is already held.
Definition: mapped.h:443
T * getTimed(timeout_t timeout)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:427
bool operator!() const
Check whether there are typed objects available to be allocated.
Definition: mapped.h:385
mapped_array(const char *name, unsigned number)
Construct mapped vector array of typed objects.
Definition: mapped.h:294
Class to access a named mapped segment published from another process.
Definition: mapped.h:469
caddr_t addr(void)
Get starting address of mapped segment.
Definition: mapped.h:177
T * pos(size_t member)
Get typed object from a specific member offset within the mapped segment.
Definition: mapped.h:409
void create(const char *name, size_t size=(size_t) 0)
Supporting function to construct a new or access an existing shared memory segment.
A common string class and character string support functions.
T * operator*()
Request a typed reusable object from the free list or mapped space by pointer reference.
Definition: mapped.h:401
unsigned count(void)
Get count of typed member objects held in this map.
Definition: mapped.h:506
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
void release(SharedAccess &object)
Convenience function to unlock shared object through it&#39;s protocol.
Definition: access.h:260
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:371
T * operator()(unsigned member)
Get typed pointer to member object of vector in mapped segment.
Definition: mapped.h:316
Thread classes and sychronization objects.
Reusable objects for forming private heaps.
Definition: linked.h:152
volatile const T & operator[](unsigned member)
Reference typed member object in the mapped segment.
Definition: mapped.h:493
Template class to map typed reusable objects into shared memory heap.
Definition: mapped.h:350
Map a reusable allocator over a named shared memory segment.
Definition: mapped.h:199
volatile const T * operator()(unsigned member)
Access typed member object in the mapped segment.
Definition: mapped.h:485
Generic non-recursive exclusive lock class.
Definition: thread.h:919
T * operator()(void)
Allocate mapped space for one object.
Definition: mapped.h:323
unsigned max(void)
Get member size of typed objects that can be held in mapped vector.
Definition: mapped.h:338
T * request(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:435
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:301
Construct or access a named section of memory.
Definition: mapped.h:58