ucommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00029 #ifndef _UCOMMON_MAPPED_H_ 00030 #define _UCOMMON_MAPPED_H_ 00031 00032 #ifndef _UCOMMON_LINKED_H_ 00033 #include <ucommon/linked.h> 00034 #endif 00035 00036 #ifndef _UCOMMON_THREAD_H_ 00037 #include <ucommon/thread.h> 00038 #endif 00039 00040 #ifndef _UCOMMON_STRING_H_ 00041 #include <ucommon/string.h> 00042 #endif 00043 00044 #ifndef _MSWINDOWS_ 00045 #include <signal.h> 00046 #endif 00047 00048 namespace ucommon { 00049 00058 class __EXPORT MappedMemory 00059 { 00060 private: 00061 size_t mapsize; 00062 caddr_t map; 00063 fd_t fd; 00064 00065 protected: 00066 size_t size, used; 00067 char idname[65]; 00068 bool erase; 00069 00070 MappedMemory(); 00071 00078 void create(const char *name, size_t size = (size_t)0); 00079 00084 virtual void *invalid(void) const; 00085 00089 virtual void fault(void) const; 00090 00091 public: 00098 MappedMemory(const char *name, size_t size); 00099 00106 MappedMemory(const char *name); 00107 00111 virtual ~MappedMemory(); 00112 00116 void release(void); 00117 00124 static void remove(const char *name); 00125 00130 inline operator bool() const 00131 {return (size != 0);} 00132 00137 inline bool operator!() const 00138 {return (size == 0);} 00139 00147 void *sbrk(size_t size); 00148 00154 void *offset(size_t offset) const; 00155 00164 bool copy(size_t offset, void *buffer, size_t size) const; 00165 00170 inline size_t len(void) 00171 {return size;} 00172 00177 inline caddr_t addr(void) 00178 {return map;} 00179 00187 static void disable(void); 00188 }; 00189 00199 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory 00200 { 00201 private: 00202 unsigned objsize; 00203 unsigned reading; 00204 mutex_t mutex; 00205 00206 protected: 00207 MappedReuse(size_t osize); 00208 00209 inline void create(const char *fname, unsigned count) 00210 {MappedMemory::create(fname, count * objsize);} 00211 00212 public: 00225 MappedReuse(const char *name, size_t size, unsigned count); 00226 00231 bool avail(void); 00232 00237 ReusableObject *request(void); 00238 00244 ReusableObject *get(void); 00245 00253 ReusableObject *getTimed(timeout_t timeout); 00254 00260 ReusableObject *getLocked(void); 00261 00267 void removeLocked(ReusableObject *object); 00268 }; 00269 00276 template <class T> 00277 class mapped_array : public MappedMemory 00278 { 00279 protected: 00280 inline mapped_array() : MappedMemory() {} 00281 00282 inline void create(const char *fn, unsigned members) 00283 {MappedMemory::create(fn, members * sizeof(T));} 00284 00285 public: 00294 inline mapped_array(const char *name, unsigned number) : 00295 MappedMemory(name, number * sizeof(T)) {} 00296 00301 inline void initialize(void) 00302 {new((caddr_t)offset(0)) T[size / sizeof(T)];} 00303 00308 inline void *addLock(void) 00309 {return sbrk(sizeof(T));} 00310 00316 inline T *operator()(unsigned member) 00317 {return static_cast<T*>(offset(member * sizeof(T)));} 00318 00323 inline T *operator()(void) 00324 {return static_cast<T*>(sbrk(sizeof(T)));} 00325 00331 inline T& operator[](unsigned member) 00332 {return *(operator()(member));} 00333 00338 inline unsigned max(void) 00339 {return (unsigned)(size / sizeof(T));} 00340 }; 00341 00349 template <class T> 00350 class mapped_reuse : public MappedReuse 00351 { 00352 protected: 00353 inline mapped_reuse() : 00354 MappedReuse(sizeof(T)) {} 00355 00356 public: 00364 inline mapped_reuse(const char *name, unsigned number) : 00365 MappedReuse(name, sizeof(T), number) {} 00366 00371 inline void initialize(void) 00372 {new((caddr_t)pos(0)) T[size / sizeof(T)];} 00373 00378 inline operator bool() const 00379 {return MappedReuse::avail();} 00380 00385 inline bool operator!() const 00386 {return !MappedReuse::avail();} 00387 00393 inline operator T*() 00394 {return mapped_reuse::get();} 00395 00401 inline T* operator*() 00402 {return mapped_reuse::get();} 00403 00409 inline T *pos(size_t member) 00410 {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));} 00411 00417 inline T *get(void) 00418 {return static_cast<T*>(MappedReuse::get());} 00419 00427 inline T *getTimed(timeout_t timeout) 00428 {return static_cast<T*>(MappedReuse::getTimed(timeout));} 00429 00435 inline T *request(void) 00436 {return static_cast<T*>(MappedReuse::request());} 00437 00443 inline void removeLocked(T *object) 00444 {MappedReuse::removeLocked(object);} 00445 00451 inline T *getLocked(void) 00452 {return static_cast<T*>(MappedReuse::getLocked());} 00453 00458 inline void release(T *object) 00459 {ReusableAllocator::release(object);} 00460 }; 00461 00468 template <class T> 00469 class mapped_view : protected MappedMemory 00470 { 00471 public: 00477 inline mapped_view(const char *name) : 00478 MappedMemory(name) {} 00479 00485 inline volatile const T *operator()(unsigned member) 00486 {return static_cast<const T*>(offset(member * sizeof(T)));} 00487 00493 inline volatile const T &operator[](unsigned member) 00494 {return *(operator()(member));} 00495 00496 inline volatile const T *get(unsigned member) 00497 {return static_cast<const T*>(offset(member * sizeof(T)));} 00498 00499 inline void copy(unsigned member, T& buffer) 00500 {MappedMemory::copy(member * sizeof(T), &buffer, sizeof(T));} 00501 00506 inline unsigned count(void) 00507 {return (unsigned)(size / sizeof(T));} 00508 }; 00509 00510 } // namespace ucommon 00511 00512 #endif