UCommon
mapped.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015-2020 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
49namespace ucommon {
50
59class __EXPORT MappedMemory
60{
61private:
62 size_t mapsize;
63 caddr_t map;
64 fd_t fd;
65
66 __DELETE_COPY(MappedMemory);
67
68protected:
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
83public:
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
191class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
192{
193private:
194 unsigned objsize;
195 unsigned reading;
196 mutex_t mutex;
197
198 __DELETE_DEFAULTS(MappedReuse);
199
200protected:
201 MappedReuse(size_t osize);
202
203 inline void create(const char *fname, unsigned count)
204 {MappedMemory::create(fname, count * objsize);}
205
206public:
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
270template <class T>
271class mapped_array : public MappedMemory
272{
273private:
274 __DELETE_COPY(mapped_array);
275
276protected:
277 inline mapped_array() : MappedMemory() {}
278
279 inline void create(const char *fn, unsigned members)
280 {MappedMemory::create(fn, members * sizeof(T));}
281
282public:
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
346template <class T>
347class mapped_reuse : public MappedReuse
348{
349private:
350 __DELETE_COPY(mapped_reuse);
351
352protected:
353 inline mapped_reuse() :
354 MappedReuse(sizeof(T)) {}
355
356public:
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
468template <class T>
469class mapped_view : protected MappedMemory
470{
471private:
472 __DELETE_DEFAULTS(mapped_view);
473
474public:
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
Linked objects, lists, templates, and containers.
Common namespace for all ucommon objects.
Definition: access.h:47
Mutex mutex_t
Convenience type for using exclusive mutex locks.
Definition: thread.h:940
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2089
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:400
Map a reusable allocator over a named shared memory segment.
Definition: mapped.h:272
T * operator()(unsigned member)
Get typed pointer to member object of vector in mapped segment.
Definition: mapped.h:313
T * operator()(void)
Allocate mapped space for one object.
Definition: mapped.h:320
T & operator[](unsigned member)
Reference typed object of vector in mapped segment.
Definition: mapped.h:328
mapped_array(const char *name, unsigned number)
Construct mapped vector array of typed objects.
Definition: mapped.h:291
unsigned max(void) const
Get member size of typed objects that can be held in mapped vector.
Definition: mapped.h:335
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:298
void * addLock(void)
Add mapped space while holding lock for one object.
Definition: mapped.h:305
Template class to map typed reusable objects into shared memory heap.
Definition: mapped.h:348
T * get(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:417
bool operator!() const
Check whether there are typed objects available to be allocated.
Definition: mapped.h:385
void release(T *object)
Used to release a typed object back to the reuse typed object pool.
Definition: mapped.h:458
void initialize(void)
Initialize typed data in mapped array.
Definition: mapped.h:371
T * request(void)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:435
T * getLocked(void)
Used to get a typed object from the reuse pool when the mutex lock is already held.
Definition: mapped.h:451
T * pos(size_t member)
Get typed object from a specific member offset within the mapped segment.
Definition: mapped.h:409
mapped_reuse(const char *name, unsigned number)
Construct mapped reuse array of typed objects.
Definition: mapped.h:364
T * getTimed(timeout_t timeout)
Request a typed reusable object from the free list or mapped space.
Definition: mapped.h:427
void removeLocked(T *object)
Used to return a typed object to the reuse pool when the mutex lock is already held.
Definition: mapped.h:443
T * operator*()
Request a typed reusable object from the free list or mapped space by pointer reference.
Definition: mapped.h:401
Class to access a named mapped segment published from another process.
Definition: mapped.h:470
mapped_view(const char *name)
Map existing named memory segment.
Definition: mapped.h:480
volatile const T & operator[](unsigned member)
Reference typed member object in the mapped segment.
Definition: mapped.h:496
volatile const T * operator()(unsigned member)
Access typed member object in the mapped segment.
Definition: mapped.h:488
unsigned count(void) const
Get count of typed member objects held in this map.
Definition: mapped.h:509
A common string class and character string support functions.
Thread classes and sychronization objects.