UCommon
/usr/src/RPM/BUILD/ucommon-6.3.3/inc/ucommon/mapped.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
00002 // Copyright (C) 2015 Cherokees of Idaho.
00003 //
00004 // This file is part of GNU uCommon C++.
00005 //
00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License as published
00008 // by the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // GNU uCommon C++ is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00018 
00030 #ifndef _UCOMMON_MAPPED_H_
00031 #define _UCOMMON_MAPPED_H_
00032 
00033 #ifndef _UCOMMON_LINKED_H_
00034 #include <ucommon/linked.h>
00035 #endif
00036 
00037 #ifndef _UCOMMON_THREAD_H_
00038 #include <ucommon/thread.h>
00039 #endif
00040 
00041 #ifndef _UCOMMON_STRING_H_
00042 #include <ucommon/string.h>
00043 #endif
00044 
00045 #ifndef _MSWINDOWS_
00046 #include <signal.h>
00047 #endif
00048 
00049 namespace ucommon {
00050 
00059 class __EXPORT MappedMemory
00060 {
00061 private:
00062     size_t mapsize;
00063     caddr_t map;
00064     fd_t fd;
00065 
00066 protected:
00067     size_t size, used;
00068     char idname[65];
00069     bool erase;
00070 
00071     MappedMemory();
00072 
00079     void create(const char *name, size_t size = (size_t)0);
00080 
00085     virtual void *invalid(void) const;
00086 
00090     virtual void fault(void) const;
00091 
00092 public:
00099     MappedMemory(const char *name, size_t size);
00100 
00107     MappedMemory(const char *name);
00108 
00112     virtual ~MappedMemory();
00113 
00117     void release(void);
00118 
00125     static  void remove(const char *name);
00126 
00131     inline operator bool() const
00132         {return (size != 0);}
00133 
00138     inline bool operator!() const
00139         {return (size == 0);}
00140 
00148     void *sbrk(size_t size);
00149 
00155     void *offset(size_t offset) const;
00156 
00165     bool copy(size_t offset, void *buffer, size_t size) const;
00166 
00171     inline size_t len(void) const
00172         {return size;}
00173 
00178     inline caddr_t addr(void)
00179         {return map;}
00180 
00188     static void disable(void);
00189 };
00190 
00200 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
00201 {
00202 private:
00203     unsigned objsize;
00204     unsigned reading;
00205     mutex_t mutex;
00206 
00207 protected:
00208     MappedReuse(size_t osize);
00209 
00210     inline void create(const char *fname, unsigned count)
00211         {MappedMemory::create(fname, count * objsize);}
00212 
00213 public:
00226     MappedReuse(const char *name, size_t size, unsigned count);
00227 
00232     bool avail(void) const;
00233 
00238     ReusableObject *request(void);
00239 
00245     ReusableObject *get(void);
00246 
00254     ReusableObject *getTimed(timeout_t timeout);
00255 
00261     ReusableObject *getLocked(void);
00262 
00268     void removeLocked(ReusableObject *object);
00269 };
00270 
00277 template <class T>
00278 class mapped_array : public MappedMemory
00279 {
00280 protected:
00281     inline mapped_array() : MappedMemory() {}
00282 
00283     inline void create(const char *fn, unsigned members)
00284         {MappedMemory::create(fn, members * sizeof(T));}
00285 
00286 public:
00295     inline mapped_array(const char *name, unsigned number) :
00296         MappedMemory(name, number * sizeof(T)) {}
00297 
00302     inline void initialize(void)
00303         {new((caddr_t)offset(0)) T[size / sizeof(T)];}
00304 
00309     inline void *addLock(void)
00310         {return sbrk(sizeof(T));}
00311 
00317     inline T *operator()(unsigned member)
00318         {return static_cast<T*>(offset(member * sizeof(T)));}
00319 
00324     inline T *operator()(void)
00325         {return static_cast<T*>(sbrk(sizeof(T)));}
00326 
00332     inline T& operator[](unsigned member)
00333         {return *(operator()(member));}
00334 
00339     inline unsigned max(void) const
00340         {return (unsigned)(size / sizeof(T));}
00341 };
00342 
00350 template <class T>
00351 class mapped_reuse : public MappedReuse
00352 {
00353 protected:
00354     inline mapped_reuse() :
00355         MappedReuse(sizeof(T)) {}
00356 
00357 public:
00365     inline mapped_reuse(const char *name, unsigned number) :
00366         MappedReuse(name, sizeof(T), number) {}
00367 
00372     inline void initialize(void)
00373         {new((caddr_t)pos(0)) T[size / sizeof(T)];}
00374 
00379     inline operator bool() const
00380         {return MappedReuse::avail();}
00381 
00386     inline bool operator!() const
00387         {return !MappedReuse::avail();}
00388 
00394     inline operator T*()
00395         {return mapped_reuse::get();}
00396 
00402     inline T* operator*()
00403         {return mapped_reuse::get();}
00404 
00410     inline T *pos(size_t member)
00411         {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));}
00412 
00418     inline T *get(void)
00419         {return static_cast<T*>(MappedReuse::get());}
00420 
00428     inline T *getTimed(timeout_t timeout)
00429         {return static_cast<T*>(MappedReuse::getTimed(timeout));}
00430 
00436     inline T *request(void)
00437         {return static_cast<T*>(MappedReuse::request());}
00438 
00444     inline void removeLocked(T *object)
00445         {MappedReuse::removeLocked(object);}
00446 
00452     inline T *getLocked(void)
00453         {return static_cast<T*>(MappedReuse::getLocked());}
00454 
00459     inline void release(T *object)
00460         {ReusableAllocator::release(object);}
00461 };
00462 
00469 template <class T>
00470 class mapped_view : protected MappedMemory
00471 {
00472 public:
00478     inline mapped_view(const char *name) :
00479         MappedMemory(name) {}
00480 
00486     inline volatile const T *operator()(unsigned member)
00487         {return static_cast<const T*>(offset(member * sizeof(T)));}
00488 
00494     inline volatile const T &operator[](unsigned member)
00495         {return *(operator()(member));}
00496 
00497     inline volatile const T *get(unsigned member)
00498         {return static_cast<const T*>(offset(member * sizeof(T)));}
00499 
00500     inline void copy(unsigned member, T& buffer)
00501         {MappedMemory::copy(member * sizeof(T), &buffer, sizeof(T));}
00502 
00507     inline unsigned count(void) const
00508         {return (unsigned)(size / sizeof(T));}
00509 };
00510 
00511 } // namespace ucommon
00512 
00513 #endif