This file is indexed.

/usr/include/ucommon/mapped.h is in libucommon-dev 3.2.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Support for memory mapped objects.
 * Memory mapped objects can be used to publish information so that it may be 
 * accessible directly by external programs.  The mapped memory objects will 
 * usually be built as a vector vector or reusable type factory, in the latter 
 * case using the allocated shared memory block itself as a local heap.  A
 * simple template can be used to view the mapped contents that have been
 * published by another process.
 * @file ucommon/mapped.h
 */

#ifndef _UCOMMON_MAPPED_H_
#define	_UCOMMON_MAPPED_H_

#ifndef _UCOMMON_LINKED_H_
#include <ucommon/linked.h>
#endif

#ifndef	_UCOMMON_THREAD_H_
#include <ucommon/thread.h>
#endif

#ifndef	_UCOMMON_STRING_H_
#include <ucommon/string.h>
#endif

#ifndef	_MSWINDOWS_
#include <signal.h>
#endif

NAMESPACE_UCOMMON

/**
 * Construct or access a named section of memory.  A logical name is used
 * which might map to something that is invoked from a call like shm_open
 * or a named w32 mapped swap segment.  This is meant to support mapping a
 * vector onto shared memory and is often used as a supporting class for our 
 * shared memory access templates.
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT MappedMemory
{
private:
	size_t mapsize;
	caddr_t map;
	fd_t fd;	

protected:
	size_t size, used;
	char idname[65];
	bool erase;

	MappedMemory();

	/**
	 * Supporting function to construct a new or access an existing
	 * shared memory segment.  Used by primary constructors.
	 * @param name of segment to create or access.
	 * @param size of segment if creating new.  Use 0 for read-only access.
	 */
	void create(const char *name, size_t size = (size_t)0);

	/**
	 * Handler to invoke in derived class when accessing outside the
	 * shared memory segment boundry.
	 */
	virtual void fault(void) const;

public:
	/**
	 * Construct a read/write access mapped shared segment of memory of a 
     * known size.  This constructs a new memory segment.
	 * @param name of segment.
	 * @param size of segment.
     */
	MappedMemory(const char *name, size_t size);

	/**
	 * Provide read-only mapped access to an existing named shared memory 
     * segment.  The size of the map is found by the size of the already 
     * existing segment.
	 * @param name of existing segment.
	 */
	MappedMemory(const char *name);

	/**
	 * Unmap memory segment.
	 */
	virtual ~MappedMemory();

	/**
	 * Unmap memory segment.
	 */
	void release(void);

	/**
	 * Destroy a previously existing memory segment under the specified name.
	 * This is used both before creating a new one, and after a publishing
	 * process unmaps the segment it created.
	 * @param name of segment to remove.
	 */
	static	void remove(const char *name);

	/**
	 * Test if map active.
	 * @return true if active map.	
	 */
	inline operator bool() const
		{return (size != 0);};

	/**
	 * Test if map is inactive.
	 * @return true if map inactive.
	 */
	inline bool operator!() const
		{return (size == 0);};

	/**
	 * Extend size of managed heap on shared memory segment.  This does not
	 * change the size of the mapped segment in any way, only that of any
	 * heap space that is being allocated and used from the mapped segment.
	 * @return start of space from map.
	 * @param size of space requested.  Will fault if past end of segment.
	 */
	void *sbrk(size_t size);

	/**
	 * Get memory from a specific offset within the mapped memory segment.
	 * @param offset from start of segment.  Will fault if past end.
	 * @return address of offset.
	 */
	void *offset(size_t offset) const;

	/**
	 * Get size of mapped segment.
	 * @return size of mapped segment.
	 */
	inline size_t len(void)
		{return size;};

	/**
	 * Get starting address of mapped segment.
	 * @return starting address of mapped segment.
	 */
	inline caddr_t getStart(void)
		{return map;};

	/**
	 * An API that allows "disabling" of publishing shared memory maps.
	 * This may be useful when an app doesn't want to use shared memory
	 * as a runtime or build option, but does not want to have to be "recoded"
	 * exclicity for non-shared memory either.  Basically it substitutes a 
	 * dummy map running on the local heap.
	 */
	static void disable(void);
};

/**
 * Map a reusable allocator over a named shared memory segment.  This may be
 * used to form a resource bound fixed size managed heap in shared memory.
 * The request can either be fullfilled from the object reuse pool or from a
 * new section of memory, and if all memory in the segment has been exhausted,
 * it can wait until more objects are returned by another thread to the reuse 
 * pool.
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT MappedReuse : protected ReusableAllocator, protected MappedMemory
{
private:
	unsigned objsize;
	unsigned reading;
	mutex_t mutex;

protected:
	MappedReuse(size_t osize);

	inline void create(const char *fname, unsigned count)
		{MappedMemory::create(fname, count * objsize);};

public:
	/**
	 * Construct a named memory segment for use with managed fixed size 
	 * reusable objects.  The segment is created as writable.  There is no 
     * read-only version of mapped reuse since the mapped segment can be read 
	 * by another process directly as a mapped read-only vector.  The actual
	 * mapped type will be derived from ReusableObject to meet the needs of
	 * the reusable allocator.  The templated version should be used to
	 * assure type correctness rather than using this class directly.
	 * @param name of shared memory segment.
	 * @param size of the object type being mapped.
	 * @param count of the maximum number of active mapped objects.
	 */
	MappedReuse(const char *name, size_t size, unsigned count);
	
	/**
	 * Check whether there are objects available to be allocated.
	 * @return true if objects are available.	
	 */
	bool avail(void);

	/**
	 * Request a reusable object from the free list or mapped space.
	 * @return free object or NULL if pool is exhausted.
	 */
	ReusableObject *request(void);

    /**
     * Request a reusable object from the free list or mapped space.
	 * This method blocks until an object becomes available.
     * @return free object.
     */
	ReusableObject *get(void);

	/**
     * Request a reusable object from the free list or mapped space.
	 * This method blocks until an object becomes available or the
	 * timeout has expired.
	 * @param timeout to wait in milliseconds.
     * @return free object or NULL if timeout.
     */
	ReusableObject *getTimed(timeout_t timeout);

	/**
	 * Used to get an object from the reuse pool when the mutex lock is
	 * already held.
	 * @return object from pool or NULL if exhausted.
	 */
	ReusableObject *getLocked(void);

	/**
	 * Used to return an object to the reuse pool when the mutex lock is
	 * already held.
	 * @param object being returned.
	 */
	void removeLocked(ReusableObject *object);	
};

/**
 * Template class to map typed vector into shared memory.  This is used to
 * construct a typed read/write vector of objects that are held in a named
 * shared memory segment.
 * @author David Sugar <dyfet@gnutelephony.org>
 */
template <class T>
class mapped_array : public MappedMemory
{
protected:
	inline mapped_array() : MappedMemory() {};

	inline void create(const char *fn, unsigned members)
		{MappedMemory::create(fn, members * sizeof(T));};

public:
	/**
	 * Construct mapped vector array of typed objects.  This is constructed
	 * for read/write access.  mapped_view is used in all cases for read-only
	 * access to mapped data.  Member objects are linearly allocated from
	 * the shared memory segment, or may simply be directly accessed by offset.
	 * @param name of mapped segment to construct.
	 * @param number of objects in the mapped vector.
	 */ 
	inline mapped_array(const char *name, unsigned number) : 
		MappedMemory(name, number * sizeof(T)) {};

	/**
	 * Initialize typed data in mapped array.  Assumes default constructor
	 * for type.
	 */
	inline void initialize(void)
		{new((caddr_t)offset(0)) T[size / sizeof(T)];};

	/**
	 * Add mapped space while holding lock for one object.
	 * @return address of object.  
	 */
	inline void *addLock(void)
		{return sbrk(sizeof(T));};
	
	/**
	 * Get typed pointer to member object of vector in mapped segment.
	 * @param member to access.
	 * @return typed pointer or NULL if past end of array.
	 */
	inline T *operator()(unsigned member)
		{return static_cast<T*>(offset(member * sizeof(T)));}

	/**
	 * Allocate mapped space for one object.
	 * @return address of object.  
	 */
	inline T *operator()(void)
		{return static_cast<T*>(sbrk(sizeof(T)));};
	
	/**
	 * Reference typed object of vector in mapped segment.
	 * @param member to access.
	 * @return typed reference.
	 */
	inline T& operator[](unsigned member)
		{return *(operator()(member));};

	/**
	 * Get member size of typed objects that can be held in mapped vector.
	 * @return members mapped in segment.
	 */
	inline unsigned getSize(void)
		{return (unsigned)(size / sizeof(T));};
};

/**
 * Template class to map typed resusble objects into shared memory heap.  
 * This is used to construct a read/write heap of objects that are held in a 
 * named shared memory segment.  Member objects are allocated from a reusable
 * heap but are stored in the shared memory segment as a vector.
 * @author David Sugar <dyfet@gnutelephony.org>
 */
template <class T>
class mapped_reuse : public MappedReuse
{
protected:
	inline mapped_reuse() :
		MappedReuse(sizeof(T)) {};

public:
	/**
	 * Construct mapped reuse array of typed objects.  This is constructed
	 * for read/write access.  mapped_view is used in all cases for read-only
	 * access to mapped data.
	 * @param name of mapped segment to construct.
	 * @param number of objects in the mapped vector.
	 */ 
	inline mapped_reuse(const char *name, unsigned number) :
		MappedReuse(name, sizeof(T), number) {};

	/**
	 * Initialize typed data in mapped array.  Assumes default constructor
	 * for type.
	 */
	inline void initialize(void)
		{new((caddr_t)pos(0)) T[size / sizeof(T)];};

	/**
	 * Check whether there are typed objects available to be allocated.
	 * @return true if objects are available.	
	 */
	inline operator bool() const
		{return MappedReuse::avail();};

	/**
	 * Check whether there are typed objects available to be allocated.
	 * @return true if no more typed objects are available.	
	 */
	inline bool operator!() const
		{return !MappedReuse::avail();};

	/**
     * Request a typed reusable object from the free list or mapped space.
	 * This method blocks until an object becomes available.
     * @return free object.
     */
	inline operator T*()
		{return mapped_reuse::get();};

	/**
     * Request a typed reusable object from the free list or mapped space by
	 * pointer reference.  This method blocks until an object becomes available.
     * @return free object.
     */
	inline T* operator*()
		{return mapped_reuse::get();};

	/**
	 * Get typed object from a specific member offset within the mapped segment.
	 * @param member offset from start of segment.  Will fault if past end.
	 * @return typed object pointer.
	 */
	inline T *pos(size_t member)
		{return static_cast<T*>(MappedReuse::offset(member * sizeof(T)));};

	/**
     * Request a typed reusable object from the free list or mapped space.
	 * This method blocks until an object becomes available.
     * @return free typed object.
     */
	inline T *get(void)
		{return static_cast<T*>(MappedReuse::get());};

	/**
     * Request a typed reusable object from the free list or mapped space.
	 * This method blocks until an object becomes available from another
	 * thread or the timeout expires.
	 * @param timeout in milliseconds.
     * @return free typed object.
     */
    inline T *getTimed(timeout_t timeout)
        {return static_cast<T*>(MappedReuse::getTimed(timeout));};

	/**
     * Request a typed reusable object from the free list or mapped space.
	 * This method does not block or wait.
     * @return free typed object if available or NULL.
     */
	inline T *request(void)
		{return static_cast<T*>(MappedReuse::request());};

	/**
	 * Used to return a typed object to the reuse pool when the mutex lock is
	 * already held.
	 * @param object being returned.
	 */
	inline void removeLocked(T *object)
		{MappedReuse::removeLocked(object);};

	/**
	 * Used to get a typed object from the reuse pool when the mutex lock is
	 * already held.
	 * @return typed object from pool or NULL if exhausted.
	 */
	inline T *getLocked(void)
		{return static_cast<T*>(MappedReuse::getLocked());};

	/**
	 * Used to release a typed object back to the reuse typed object pool.
	 * @param object being released.
	 */
	inline void release(T *object)
		{ReusableAllocator::release(object);};
};

/**
 * Class to access a named mapped segment published from another process.  
 * This offers a simple typed vector interface to access the shared memory
 * segment in read-only mode.
 * @author David Sugar <dyfet@gnutelephony.org>
 */ 	
template <class T>
class mapped_view : protected MappedMemory
{
public:
	/**
	 * Map existing named memory segment.  The size of the map is derived
	 * from the existing map alone.
	 * @param name of memory segment to map.
	 */
	inline mapped_view(const char *name) : 
		MappedMemory(name) {};
	
	/**
	 * Access typed member object in the mapped segment.
	 * @param member to access.
	 * @return typed object pointer.
	 */
	inline volatile const T *operator()(unsigned member)
		{return static_cast<const T*>(offset(member * sizeof(T)));}
	
	/**
	 * Reference typed member object in the mapped segment.
	 * @param member to access.
	 * @return typed object reference.
	 */
	inline volatile const T &operator[](unsigned member)
		{return *(operator()(member));};

	/**
	 * Get count of typed member objects held in this map.
	 * @return count of typed member objects.
	 */
	inline unsigned getCount(void)
		{return (unsigned)(size / sizeof(T));};
};

END_NAMESPACE

#endif