UCommon
mapped.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
30 #ifndef _UCOMMON_MAPPED_H_
31 #define _UCOMMON_MAPPED_H_
32 
33 #ifndef _UCOMMON_LINKED_H_
34 #include <ucommon/linked.h>
35 #endif
36 
37 #ifndef _UCOMMON_THREAD_H_
38 #include <ucommon/thread.h>
39 #endif
40 
41 #ifndef _UCOMMON_STRING_H_
42 #include <ucommon/string.h>
43 #endif
44 
45 #ifndef _MSWINDOWS_
46 #include <signal.h>
47 #endif
48 
49 namespace ucommon {
50 
59 class __EXPORT MappedMemory
60 {
61 private:
62  size_t mapsize;
63  caddr_t map;
64  fd_t fd;
65 
66  __DELETE_COPY(MappedMemory);
67 
68 protected:
69  size_t size, used;
70  char idname[65];
71  bool erase;
72 
73  MappedMemory();
74 
81  void create(const char *name, size_t size = (size_t)0);
82 
87  virtual void *invalid(void) const;
88 
92  virtual void fault(void) const;
93 
94 public:
101  MappedMemory(const char *name, size_t size);
102 
109  MappedMemory(const char *name);
110 
114  virtual ~MappedMemory();
115 
119  void release(void);
120 
127  static void remove(const char *name);
128 
133  inline operator bool() const
134  {return (size != 0);}
135 
140  inline bool operator!() const
141  {return (size == 0);}
142 
150  void *sbrk(size_t size);
151 
157  void *offset(size_t offset) const;
158 
167  bool copy(size_t offset, void *buffer, size_t size) const;
168 
173  inline size_t len(void) const
174  {return size;}
175 
180  inline caddr_t addr(void)
181  {return map;}
182 
190  static void disable(void);
191 };
192 
202 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
203 {
204 private:
205  unsigned objsize;
206  unsigned reading;
207  mutex_t mutex;
208 
209 protected:
210  MappedReuse(size_t osize);
211 
212  inline void create(const char *fname, unsigned count)
213  {MappedMemory::create(fname, count * objsize);}
214 
215 public:
228  MappedReuse(const char *name, size_t size, unsigned count);
229 
234  bool avail(void) const;
235 
240  ReusableObject *request(void);
241 
247  ReusableObject *get(void);
248 
256  ReusableObject *getTimed(timeout_t timeout);
257 
263  ReusableObject *getLocked(void);
264 
270  void removeLocked(ReusableObject *object);
271 };
272 
279 template <class T>
281 {
282 protected:
283  inline mapped_array() : MappedMemory() {}
284 
285  inline void create(const char *fn, unsigned members)
286  {MappedMemory::create(fn, members * sizeof(T));}
287 
288 public:
297  inline mapped_array(const char *name, unsigned number) :
298  MappedMemory(name, number * sizeof(T)) {}
299 
304  inline void initialize(void)
305  {new((caddr_t)offset(0)) T[size / sizeof(T)];}
306 
311  inline void *addLock(void)
312  {return sbrk(sizeof(T));}
313 
319  inline T *operator()(unsigned member)
320  {return static_cast<T*>(offset(member * sizeof(T)));}
321 
326  inline T *operator()(void)
327  {return static_cast<T*>(sbrk(sizeof(T)));}
328 
334  inline T& operator[](unsigned member)
335  {return *(operator()(member));}
336 
341  inline unsigned max(void) const
342  {return (unsigned)(size / sizeof(T));}
343 };
344 
352 template <class T>
353 class mapped_reuse : public MappedReuse
354 {
355 protected:
356  inline mapped_reuse() :
357  MappedReuse(sizeof(T)) {}
358 
359 public:
367  inline mapped_reuse(const char *name, unsigned number) :
368  MappedReuse(name, sizeof(T), number) {}
369 
374  inline void initialize(void)
375  {new((caddr_t)pos(0)) T[size / sizeof(T)];}
376 
381  inline operator bool() const
382  {return MappedReuse::avail();}
383 
388  inline bool operator!() const
389  {return !MappedReuse::avail();}
390 
396  inline operator T*()
397  {return mapped_reuse::get();}
398 
404  inline T* operator*()
405  {return mapped_reuse::get();}
406 
412  inline T *pos(size_t member)
413  {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));}
414 
420  inline T *get(void)
421  {return static_cast<T*>(MappedReuse::get());}
422 
430  inline T *getTimed(timeout_t timeout)
431  {return static_cast<T*>(MappedReuse::getTimed(timeout));}
432 
438  inline T *request(void)
439  {return static_cast<T*>(MappedReuse::request());}
440 
446  inline void removeLocked(T *object)
447  {MappedReuse::removeLocked(object);}
448 
454  inline T *getLocked(void)
455  {return static_cast<T*>(MappedReuse::getLocked());}
456 
461  inline void release(T *object)
462  {ReusableAllocator::release(object);}
463 };
464 
471 template <class T>
472 class mapped_view : protected MappedMemory
473 {
474 public:
480  inline mapped_view(const char *name) :
481  MappedMemory(name) {}
482 
488  inline volatile const T *operator()(unsigned member)
489  {return static_cast<const T*>(offset(member * sizeof(T)));}
490 
496  inline volatile const T &operator[](unsigned member)
497  {return *(operator()(member));}
498 
499  inline volatile const T *get(unsigned member)
500  {return static_cast<const T*>(offset(member * sizeof(T)));}
501 
502  inline void copy(unsigned member, T& buffer)
503  {MappedMemory::copy(member * sizeof(T), &buffer, sizeof(T));}
504 
509  inline unsigned count(void) const
510  {return (unsigned)(size / sizeof(T));}
511 };
512 
513 } // namespace ucommon
514 
515 #endif
A common string class and character string support functions.
T & operator[](unsigned member)
Reference typed object of vector in mapped segment.
Definition: mapped.h:334
volatile const T * operator()(unsigned member)
Access typed member object in the mapped segment.
Definition: mapped.h:488
T * operator*()
Request a typed reusable object from the free list or mapped space by pointer reference.
Definition: mapped.h:404
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
ReusableObject * get(void)
Request a reusable object from the free list or mapped space.
void release(ReusableObject *object)
Release resuable object.
Thread classes and sychronization objects.
T * request(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:438
caddr_t addr(void)
Get starting address of mapped segment.
Definition: mapped.h:180
mapped_view(const char *name)
Map existing named memory segment.
Definition: mapped.h:480
void * sbrk(size_t size)
Extend size of managed heap on shared memory segment.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
Template class to map typed reusable objects into shared memory heap.
Definition: mapped.h:353
Linked objects, lists, templates, and containers.
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:374
bool avail(void) const
Check whether there are objects available to be allocated.
bool operator!() const
Test if map is inactive.
Definition: mapped.h:140
Reusable objects for forming private heaps.
Definition: linked.h:158
void * offset(size_t offset) const
Get memory from a specific offset within the mapped memory segment.
T * getTimed(timeout_t timeout)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:430
ReusableObject * getLocked(void)
Used to get an object from the reuse pool when the mutex lock is already held.
Generic non-recursive exclusive lock class.
Definition: thread.h:953
mapped_array(const char *name, unsigned number)
Construct mapped vector array of typed objects.
Definition: mapped.h:297
ReusableObject * getTimed(timeout_t timeout)
Request a reusable object from the free list or mapped space.
mapped_reuse(const char *name, unsigned number)
Construct mapped reuse array of typed objects.
Definition: mapped.h:367
void removeLocked(ReusableObject *object)
Used to return an object to the reuse pool when the mutex lock is already held.
Common namespace for all ucommon objects.
Definition: access.h:47
void create(const char *name, size_t size=(size_t) 0)
Supporting function to construct a new or access an existing shared memory segment.
T * pos(size_t member)
Get typed object from a specific member offset within the mapped segment.
Definition: mapped.h:412
void release(T *object)
Used to release a typed object back to the reuse typed object pool.
Definition: mapped.h:461
bool operator!() const
Check whether there are typed objects available to be allocated.
Definition: mapped.h:388
Class for resource bound memory pools between threads.
Definition: thread.h:704
Template class to map typed vector into shared memory.
Definition: mapped.h:280
T * operator()(unsigned member)
Get typed pointer to member object of vector in mapped segment.
Definition: mapped.h:319
unsigned count(void) const
Get count of typed member objects held in this map.
Definition: mapped.h:509
unsigned max(void) const
Get member size of typed objects that can be held in mapped vector.
Definition: mapped.h:341
Construct or access a named section of memory.
Definition: mapped.h:59
T * getLocked(void)
Used to get a typed object from the reuse pool when the mutex lock is already held.
Definition: mapped.h:454
T * get(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:420
bool copy(size_t offset, void *buffer, size_t size) const
Copy memory from specific offset within the mapped memory segment.
void removeLocked(T *object)
Used to return a typed object to the reuse pool when the mutex lock is already held.
Definition: mapped.h:446
void * addLock(void)
Add mapped space while holding lock for one object.
Definition: mapped.h:311
volatile const T & operator[](unsigned member)
Reference typed member object in the mapped segment.
Definition: mapped.h:496
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:304
size_t len(void) const
Get size of mapped segment.
Definition: mapped.h:173
Map a reusable allocator over a named shared memory segment.
Definition: mapped.h:202
ReusableObject * request(void)
Request a reusable object from the free list or mapped space.
T * operator()(void)
Allocate mapped space for one object.
Definition: mapped.h:326
Class to access a named mapped segment published from another process.
Definition: mapped.h:472