Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00155 inline size_t len(void)
00156 {return size;};
00157
00162 inline caddr_t getStart(void)
00163 {return map;};
00164
00172 static void disable(void);
00173 };
00174
00184 class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
00185 {
00186 private:
00187 unsigned objsize;
00188 unsigned reading;
00189 mutex_t mutex;
00190
00191 protected:
00192 MappedReuse(size_t osize);
00193
00194 inline void create(const char *fname, unsigned count)
00195 {MappedMemory::create(fname, count * objsize);};
00196
00197 public:
00210 MappedReuse(const char *name, size_t size, unsigned count);
00211
00216 bool avail(void);
00217
00222 ReusableObject *request(void);
00223
00229 ReusableObject *get(void);
00230
00238 ReusableObject *getTimed(timeout_t timeout);
00239
00245 ReusableObject *getLocked(void);
00246
00252 void removeLocked(ReusableObject *object);
00253 };
00254
00261 template <class T>
00262 class mapped_array : public MappedMemory
00263 {
00264 protected:
00265 inline mapped_array() : MappedMemory() {};
00266
00267 inline void create(const char *fn, unsigned members)
00268 {MappedMemory::create(fn, members * sizeof(T));};
00269
00270 public:
00279 inline mapped_array(const char *name, unsigned number) :
00280 MappedMemory(name, number * sizeof(T)) {};
00281
00286 inline void initialize(void)
00287 {new((caddr_t)offset(0)) T[size / sizeof(T)];};
00288
00293 inline void *addLock(void)
00294 {return sbrk(sizeof(T));};
00295
00301 inline T *operator()(unsigned member)
00302 {return static_cast<T*>(offset(member * sizeof(T)));}
00303
00308 inline T *operator()(void)
00309 {return static_cast<T*>(sbrk(sizeof(T)));};
00310
00316 inline T& operator[](unsigned member)
00317 {return *(operator()(member));};
00318
00323 inline unsigned getSize(void)
00324 {return (unsigned)(size / sizeof(T));};
00325 };
00326
00334 template <class T>
00335 class mapped_reuse : public MappedReuse
00336 {
00337 protected:
00338 inline mapped_reuse() :
00339 MappedReuse(sizeof(T)) {};
00340
00341 public:
00349 inline mapped_reuse(const char *name, unsigned number) :
00350 MappedReuse(name, sizeof(T), number) {};
00351
00356 inline void initialize(void)
00357 {new((caddr_t)pos(0)) T[size / sizeof(T)];};
00358
00363 inline operator bool() const
00364 {return MappedReuse::avail();};
00365
00370 inline bool operator!() const
00371 {return !MappedReuse::avail();};
00372
00378 inline operator T*()
00379 {return mapped_reuse::get();};
00380
00386 inline T* operator*()
00387 {return mapped_reuse::get();};
00388
00394 inline T *pos(size_t member)
00395 {return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));};
00396
00402 inline T *get(void)
00403 {return static_cast<T*>(MappedReuse::get());};
00404
00412 inline T *getTimed(timeout_t timeout)
00413 {return static_cast<T*>(MappedReuse::getTimed(timeout));};
00414
00420 inline T *request(void)
00421 {return static_cast<T*>(MappedReuse::request());};
00422
00428 inline void removeLocked(T *object)
00429 {MappedReuse::removeLocked(object);};
00430
00436 inline T *getLocked(void)
00437 {return static_cast<T*>(MappedReuse::getLocked());};
00438
00443 inline void release(T *object)
00444 {ReusableAllocator::release(object);};
00445 };
00446
00453 template <class T>
00454 class mapped_view : protected MappedMemory
00455 {
00456 public:
00462 inline mapped_view(const char *name) :
00463 MappedMemory(name) {};
00464
00470 inline volatile const T *operator()(unsigned member)
00471 {return static_cast<const T*>(offset(member * sizeof(T)));}
00472
00478 inline volatile const T &operator[](unsigned member)
00479 {return *(operator()(member));};
00480
00485 inline unsigned getCount(void)
00486 {return (unsigned)(size / sizeof(T));};
00487 };
00488
00489 END_NAMESPACE
00490
00491 #endif