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