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 fault(void) const;
85 
86 public:
93  MappedMemory(const char *name, size_t size);
94 
101  MappedMemory(const char *name);
102 
106  virtual ~MappedMemory();
107 
111  void release(void);
112 
119  static void remove(const char *name);
120 
125  inline operator bool() const
126  {return (size != 0);};
127 
132  inline bool operator!() const
133  {return (size == 0);};
134 
142  void *sbrk(size_t size);
143 
149  void *offset(size_t offset) const;
150 
158  void copy(size_t offset, void *buffer, size_t size) const;
159 
164  inline size_t len(void)
165  {return size;};
166 
171  inline caddr_t getStart(void)
172  {return map;};
173 
181  static void disable(void);
182 };
183 
193 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
194 {
195 private:
196  unsigned objsize;
197  unsigned reading;
198  mutex_t mutex;
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);
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 protected:
274  inline mapped_array() : MappedMemory() {};
275 
276  inline void create(const char *fn, unsigned members)
277  {MappedMemory::create(fn, members * sizeof(T));};
278 
279 public:
288  inline mapped_array(const char *name, unsigned number) :
289  MappedMemory(name, number * sizeof(T)) {};
290 
295  inline void initialize(void)
296  {new((caddr_t)offset(0)) T[size / sizeof(T)];};
297 
302  inline void *addLock(void)
303  {return sbrk(sizeof(T));};
304 
310  inline T *operator()(unsigned member)
311  {return static_cast<T*>(offset(member * sizeof(T)));}
312 
317  inline T *operator()(void)
318  {return static_cast<T*>(sbrk(sizeof(T)));};
319 
325  inline T& operator[](unsigned member)
326  {return *(operator()(member));};
327 
332  inline unsigned getSize(void)
333  {return (unsigned)(size / sizeof(T));};
334 };
335 
343 template <class T>
344 class mapped_reuse : public MappedReuse
345 {
346 protected:
347  inline mapped_reuse() :
348  MappedReuse(sizeof(T)) {};
349 
350 public:
358  inline mapped_reuse(const char *name, unsigned number) :
359  MappedReuse(name, sizeof(T), number) {};
360 
365  inline void initialize(void)
366  {new((caddr_t)pos(0)) T[size / sizeof(T)];};
367 
372  inline operator bool() const
373  {return MappedReuse::avail();};
374 
379  inline bool operator!() const
380  {return !MappedReuse::avail();};
381 
387  inline operator T*()
388  {return mapped_reuse::get();};
389 
395  inline T* operator*()
396  {return mapped_reuse::get();};
397 
403  inline T *pos(size_t member)
404  {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));};
405 
411  inline T *get(void)
412  {return static_cast<T*>(MappedReuse::get());};
413 
421  inline T *getTimed(timeout_t timeout)
422  {return static_cast<T*>(MappedReuse::getTimed(timeout));};
423 
429  inline T *request(void)
430  {return static_cast<T*>(MappedReuse::request());};
431 
437  inline void removeLocked(T *object)
438  {MappedReuse::removeLocked(object);};
439 
445  inline T *getLocked(void)
446  {return static_cast<T*>(MappedReuse::getLocked());};
447 
452  inline void release(T *object)
453  {ReusableAllocator::release(object);};
454 };
455 
462 template <class T>
463 class mapped_view : protected MappedMemory
464 {
465 public:
471  inline mapped_view(const char *name) :
472  MappedMemory(name) {};
473 
479  inline volatile const T *operator()(unsigned member)
480  {return static_cast<const T*>(offset(member * sizeof(T)));}
481 
487  inline volatile const T &operator[](unsigned member)
488  {return *(operator()(member));};
489 
490  inline volatile const T *get(unsigned member)
491  {return static_cast<const T*>(offset(member * sizeof(T)));};
492 
493  inline void copy(unsigned member, T *buffer)
494  {MappedMemory::copy(member * sizeof(T), buffer, sizeof(T));};
495 
500  inline unsigned getCount(void)
501  {return (unsigned)(size / sizeof(T));};
502 };
503 
504 END_NAMESPACE
505 
506 #endif