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