ucommon
|
00001 // Copyright (C) 2006-2010 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 fault(void) const; 00085 00086 public: 00093 MappedMemory(const char *name, size_t size); 00094 00101 MappedMemory(const char *name); 00102 00106 virtual ~MappedMemory(); 00107 00111 void release(void); 00112 00119 static void remove(const char *name); 00120 00125 inline operator bool() const 00126 {return (size != 0);}; 00127 00132 inline bool operator!() const 00133 {return (size == 0);}; 00134 00142 void *sbrk(size_t size); 00143 00149 void *offset(size_t offset) const; 00150 00158 void copy(size_t offset, void *buffer, size_t size) const; 00159 00164 inline size_t len(void) 00165 {return size;}; 00166 00171 inline caddr_t getStart(void) 00172 {return map;}; 00173 00181 static void disable(void); 00182 }; 00183 00193 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory 00194 { 00195 private: 00196 unsigned objsize; 00197 unsigned reading; 00198 mutex_t mutex; 00199 00200 protected: 00201 MappedReuse(size_t osize); 00202 00203 inline void create(const char *fname, unsigned count) 00204 {MappedMemory::create(fname, count * objsize);}; 00205 00206 public: 00219 MappedReuse(const char *name, size_t size, unsigned count); 00220 00225 bool avail(void); 00226 00231 ReusableObject *request(void); 00232 00238 ReusableObject *get(void); 00239 00247 ReusableObject *getTimed(timeout_t timeout); 00248 00254 ReusableObject *getLocked(void); 00255 00261 void removeLocked(ReusableObject *object); 00262 }; 00263 00270 template <class T> 00271 class mapped_array : public MappedMemory 00272 { 00273 protected: 00274 inline mapped_array() : MappedMemory() {}; 00275 00276 inline void create(const char *fn, unsigned members) 00277 {MappedMemory::create(fn, members * sizeof(T));}; 00278 00279 public: 00288 inline mapped_array(const char *name, unsigned number) : 00289 MappedMemory(name, number * sizeof(T)) {}; 00290 00295 inline void initialize(void) 00296 {new((caddr_t)offset(0)) T[size / sizeof(T)];}; 00297 00302 inline void *addLock(void) 00303 {return sbrk(sizeof(T));}; 00304 00310 inline T *operator()(unsigned member) 00311 {return static_cast<T*>(offset(member * sizeof(T)));} 00312 00317 inline T *operator()(void) 00318 {return static_cast<T*>(sbrk(sizeof(T)));}; 00319 00325 inline T& operator[](unsigned member) 00326 {return *(operator()(member));}; 00327 00332 inline unsigned getSize(void) 00333 {return (unsigned)(size / sizeof(T));}; 00334 }; 00335 00343 template <class T> 00344 class mapped_reuse : public MappedReuse 00345 { 00346 protected: 00347 inline mapped_reuse() : 00348 MappedReuse(sizeof(T)) {}; 00349 00350 public: 00358 inline mapped_reuse(const char *name, unsigned number) : 00359 MappedReuse(name, sizeof(T), number) {}; 00360 00365 inline void initialize(void) 00366 {new((caddr_t)pos(0)) T[size / sizeof(T)];}; 00367 00372 inline operator bool() const 00373 {return MappedReuse::avail();}; 00374 00379 inline bool operator!() const 00380 {return !MappedReuse::avail();}; 00381 00387 inline operator T*() 00388 {return mapped_reuse::get();}; 00389 00395 inline T* operator*() 00396 {return mapped_reuse::get();}; 00397 00403 inline T *pos(size_t member) 00404 {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));}; 00405 00411 inline T *get(void) 00412 {return static_cast<T*>(MappedReuse::get());}; 00413 00421 inline T *getTimed(timeout_t timeout) 00422 {return static_cast<T*>(MappedReuse::getTimed(timeout));}; 00423 00429 inline T *request(void) 00430 {return static_cast<T*>(MappedReuse::request());}; 00431 00437 inline void removeLocked(T *object) 00438 {MappedReuse::removeLocked(object);}; 00439 00445 inline T *getLocked(void) 00446 {return static_cast<T*>(MappedReuse::getLocked());}; 00447 00452 inline void release(T *object) 00453 {ReusableAllocator::release(object);}; 00454 }; 00455 00462 template <class T> 00463 class mapped_view : protected MappedMemory 00464 { 00465 public: 00471 inline mapped_view(const char *name) : 00472 MappedMemory(name) {}; 00473 00479 inline volatile const T *operator()(unsigned member) 00480 {return static_cast<const T*>(offset(member * sizeof(T)));} 00481 00487 inline volatile const T &operator[](unsigned member) 00488 {return *(operator()(member));}; 00489 00490 inline volatile const T *get(unsigned member) 00491 {return static_cast<const T*>(offset(member * sizeof(T)));}; 00492 00493 inline void copy(unsigned member, T *buffer) 00494 {MappedMemory::copy(member * sizeof(T), buffer, sizeof(T));}; 00495 00500 inline unsigned getCount(void) 00501 {return (unsigned)(size / sizeof(T));}; 00502 }; 00503 00504 END_NAMESPACE 00505 00506 #endif