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 
83 public:
90  MappedMemory(const char *name, size_t size);
91 
98  MappedMemory(const char *name);
99 
103  virtual ~MappedMemory();
104 
108  void release(void);
109 
116  static void remove(const char *name);
117 
122  inline operator bool() const
123  {return (size != 0);}
124 
129  inline bool operator!() const
130  {return (size == 0);}
131 
139  void *sbrk(size_t size);
140 
146  void *offset(size_t offset) const;
147 
156  bool copy(size_t offset, void *buffer, size_t size) const;
157 
162  inline size_t len(void) const
163  {return size;}
164 
169  inline caddr_t addr(void)
170  {return map;}
171 
179  static void disable(void);
180 };
181 
191 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
192 {
193 private:
194  unsigned objsize;
195  unsigned reading;
196  mutex_t mutex;
197 
198  __DELETE_DEFAULTS(MappedReuse);
199 
200 protected:
201  MappedReuse(size_t osize);
202 
203  inline void create(const char *fname, unsigned count)
204  {MappedMemory::create(fname, count * objsize);}
205 
206 public:
219  MappedReuse(const char *name, size_t size, unsigned count);
220 
225  bool avail(void) const;
226 
231  ReusableObject *request(void);
232 
238  ReusableObject *get(void);
239 
247  ReusableObject *getTimed(timeout_t timeout);
248 
254  ReusableObject *getLocked(void);
255 
261  void removeLocked(ReusableObject *object);
262 };
263 
270 template <class T>
272 {
273 private:
274  __DELETE_COPY(mapped_array);
275 
276 protected:
277  inline mapped_array() : MappedMemory() {}
278 
279  inline void create(const char *fn, unsigned members)
280  {MappedMemory::create(fn, members * sizeof(T));}
281 
282 public:
291  inline mapped_array(const char *name, unsigned number) :
292  MappedMemory(name, number * sizeof(T)) {}
293 
298  inline void initialize(void)
299  {new((caddr_t)offset(0)) T[size / sizeof(T)];}
300 
305  inline void *addLock(void)
306  {return sbrk(sizeof(T));}
307 
313  inline T *operator()(unsigned member)
314  {return static_cast<T*>(offset(member * sizeof(T)));}
315 
320  inline T *operator()(void)
321  {return static_cast<T*>(sbrk(sizeof(T)));}
322 
328  inline T& operator[](unsigned member)
329  {return *(operator()(member));}
330 
335  inline unsigned max(void) const
336  {return (unsigned)(size / sizeof(T));}
337 };
338 
346 template <class T>
347 class mapped_reuse : public MappedReuse
348 {
349 private:
350  __DELETE_COPY(mapped_reuse);
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 private:
472  __DELETE_DEFAULTS(mapped_view);
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
Class for resource bound memory pools between threads.
Definition: thread.h:416
Linked objects, lists, templates, and containers.
bool operator!() const
Check whether there are typed objects available to be allocated.
Definition: mapped.h:385
Construct or access a named section of memory.
Definition: mapped.h:59
void removeLocked(ReusableObject *object)
Used to return an object to the reuse pool when the mutex lock is already held.
mapped_view(const char *name)
Map existing named memory segment.
Definition: mapped.h:480
bool operator!() const
Test if map is inactive.
Definition: mapped.h:129
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:371
Template class to map typed vector into shared memory.
Definition: mapped.h:271
T * request(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:435
T * getTimed(timeout_t timeout)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:427
T * pos(size_t member)
Get typed object from a specific member offset within the mapped segment.
Definition: mapped.h:409
void * addLock(void)
Add mapped space while holding lock for one object.
Definition: mapped.h:305
mapped_reuse(const char *name, unsigned number)
Construct mapped reuse array of typed objects.
Definition: mapped.h:364
Class to access a named mapped segment published from another process.
Definition: mapped.h:469
ReusableObject * getLocked(void)
Used to get an object from the reuse pool when the mutex lock is already held.
A common string class and character string support functions.
size_t len(void) const
Get size of mapped segment.
Definition: mapped.h:162
void * offset(size_t offset) const
Get memory from a specific offset within the mapped memory segment.
T * operator()(void)
Allocate mapped space for one object.
Definition: mapped.h:320
ReusableObject * request(void)
Request a reusable object from the free list or mapped space.
mapped_array(const char *name, unsigned number)
Construct mapped vector array of typed objects.
Definition: mapped.h:291
bool avail(void) const
Check whether there are objects available to be allocated.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2089
T * getLocked(void)
Used to get a typed object from the reuse pool when the mutex lock is already held.
Definition: mapped.h:451
Thread classes and sychronization objects.
T * get(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:417
ReusableObject * getTimed(timeout_t timeout)
Request a reusable object from the free list or mapped space.
T * operator *()
Request a typed reusable object from the free list or mapped space by pointer reference.
Definition: mapped.h:401
T & operator[](unsigned member)
Reference typed object of vector in mapped segment.
Definition: mapped.h:328
void release(ReusableObject *object)
Release resuable object.
Reusable objects for forming private heaps.
Definition: linked.h:152
Common namespace for all ucommon objects.
Definition: access.h:47
T * operator()(unsigned member)
Get typed pointer to member object of vector in mapped segment.
Definition: mapped.h:313
void * sbrk(size_t size)
Extend size of managed heap on shared memory segment.
unsigned count(void) const
Get count of typed member objects held in this map.
Definition: mapped.h:509
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:395
bool copy(size_t offset, void *buffer, size_t size) const
Copy memory from specific offset within the mapped memory segment.
Generic non-recursive exclusive lock class.
Definition: thread.h:459
void create(const char *name, size_t size=(size_t) 0)
Supporting function to construct a new or access an existing shared memory segment.
Map a reusable allocator over a named shared memory segment.
Definition: mapped.h:191
volatile const T & operator[](unsigned member)
Reference typed member object in the mapped segment.
Definition: mapped.h:496
void release(T *object)
Used to release a typed object back to the reuse typed object pool.
Definition: mapped.h:458
Template class to map typed reusable objects into shared memory heap.
Definition: mapped.h:347
volatile const T * operator()(unsigned member)
Access typed member object in the mapped segment.
Definition: mapped.h:488
unsigned max(void) const
Get member size of typed objects that can be held in mapped vector.
Definition: mapped.h:335
ReusableObject * get(void)
Request a reusable object from the free list or mapped space.
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:298
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