FARGOS/VISTA Object Management Environment Core  ..
FARGOS/VISTA Object Management Environment Core Table of Contents
circular_bfr.hpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 - 2019 FARGOS Development, LLC
2  * Author: Geoff Carpenter - http://www.fargos.net/gcc.html
3  */
4 
5 #ifndef _CIRCULAR_BFR_HPP_
6 #define _CIRCULAR_BFR_HPP_ "$Id: circular_bfr.hpp 454 2020-07-23 20:22:23Z geoff $"
8 
10 #include <stdint.h>
11 #include <time.h>
13 #ifndef _WIN32
14 #include <sched.h>
15 #endif
16 
28 #define DEFAULT_CIRCULAR_BFR_MAGIC_NUMBER "CIRCBFR"
29 
31 #define DEFAULT_FIXED_BFR_MAGIC_NUMBER "FIXDBFR"
32 
33 /* NOTE: structures prefixed with SharedBuffer are used in the
34  * context of a shared memory segment or file; they must NOT contain
35  * pointers, as these would have no meaning outside the address space
36  * in which they were created.
37  */
38 
39 typedef uint32_t SharedBuffer_Offset_t; // default
40 
44  char magicNumber[8];
45  uint8_t headerLength;
46  char componentId[47];
47  uint32_t byteOrderTag; /* 0x11223344; last nybble indicates word length */
48  uint32_t _pad;
49  // should be new cache line
50  uint32_t blockDimensions;
52  uint32_t updatingThread;
55 
56 
57  void initialize(const char *component, const char *magic,
58  size_t regionLen);
59 
60  constexpr bool isNativeByteOrder() const {
61  return (byteOrderTag == 0x11223344);
62  }
63 };
64 
68  char magicNumber[8];
69  uint8_t headerLength;
70  char componentId[47];
71  uint32_t byteOrderTag; /* 0x18283848; last nybble indicates word length */
72  uint32_t _pad;
73  // should be new cache line
74  uint64_t blockDimensions;
75  uint32_t offset_startFreeList; // should be 64 bits
76  uint32_t updatingThread;
79 
80  void initialize(const char *component, const char *magic,
81  size_t regionLen);
82 
83  constexpr bool isNativeByteOrder() const {
84  return (byteOrderTag == 0x11223344);
85  }
86 };
87 
90 inline bool sharedBufferSegmentIsInNativeByteOrder(const void *segment)
91 {
92  return (reinterpret_cast<const SharedBufferRegionHeader_32 *>(segment)->isNativeByteOrder());
93 }
94 
97 inline bool sharedBufferSegmentSizeIs64bit(const void *segment)
98 {
99  return (reinterpret_cast<const SharedBufferRegionHeader_32 *>(segment)->headerLength != sizeof(SharedBufferRegionHeader_32));
100 }
101 
105  uint32_t offset_block;
106  uint32_t blockLen;
107  uint32_t usedLen;
109 };
110 
114  uint64_t offset_block;
115  uint64_t blockLen;
116  uint64_t usedLen;
118 };
119 
121 #if __SIZEOF_POINTER__ == 8
123 #define SHARED_BUFFER_OFFSET_SIZE 4 /* should be 8 */
124 #else
126 #define SHARED_BUFFER_OFFSET_SIZE 4
127 #endif
128 
130 #if __SIZEOF_POINTER__ == 8
132 #else
134 #endif
135 
136 
149 
150 #ifndef _WIN32
151 // This apparently orphaned inline is intentional
152 inline
154 #endif
155 
156 
157 /* Layout in shared memory segment:
158  * SharedBufferRegionHeader
159  * SharedBufferAllocRecord[maxPackets]
160  * ...
161  * data block
162  * data block
163  * ....
164  */
165 
166 #pragma GCC diagnostic push
167 #pragma GCC diagnostic ignored "-Wsuggest-final-types"
168 
178 public:
180  NOT_OWNER = 0,
181  OWN_BLOCK = 1,
182  OWN_MAP = 2,
184  };
185  typedef uint_fast8_t BufferRegion_Overhead_t;
186 protected:
187  unsigned char *region;
188  size_t regionLength;
189  size_t blockSize;
192 public:
193  /* Create a buffer region at the specified address.
194  * \param ptr points at the base of the region of memory to be used
195  * \param len indicate the number of bytes available in the region
196  * \param blk_size indicate the maximum individual allocation unit.
197  * \param ownerBy indicates the administrative ownershp state
198  * of the region.
199  *
200  * If the administrative ownership state is set to OWN_BLOCK,
201  * the region block will be deleted by the BufferRegion destructor.
202  * If set to OWN_MAP, the region will be unmapped.
203  */
204  BufferRegion(unsigned char *ptr, size_t len, size_t blk_size,
205  OwnershipState ownedBy=NOT_OWNER);
206 
212  virtual ~BufferRegion();
213 
225  void replaceRegion(unsigned char *ptr, size_t len, size_t blk_size,
226  OwnershipState ownedBy=NOT_OWNER);
227 
232  void setOwnership(OwnershipState newOwnership) {
233  ownership = newOwnership;
234  }
235 
239  return (ownership);
240  }
241 
243  inline size_t getRegionLength() const OME_ALWAYS_INLINE {
244  return (regionLength);
245  }
246 
248  inline size_t getBlockSize() const OME_ALWAYS_INLINE {
249  return (blockSize);
250  }
251 
253  inline unsigned char *getBufferBase() const OME_ALWAYS_INLINE {
254  return (region);
255  }
256 
257 #pragma GCC diagnostic push
258 #pragma GCC diagnostic ignored "-Wsuggest-final-methods"
259 
262 
265 
275 
282  virtual unsigned char *blockAddress(const SharedBufferAllocRecord *record) {
283  return (region + record->offset_block);
284  }
285 
286 #pragma GCC diagnostic pop
287 
294  virtual SharedBufferAllocRecord *allocateBlock(size_t len) = 0;
295 
305  virtual void returnBlock(SharedBufferAllocRecord *record) = 0;
306 
310  virtual BufferRegion_Overhead_t getBlockOverhead() const = 0;
311 
324  int checkLists(int displayFlag);
325 
326 }; // end class BufferRegion
327 
328 
331 public:
333  static int32_t TOTAL_return_wait_spins;
334 
335  CircularBufferManager(unsigned char *ptr, size_t len, size_t blk_size,
336  const char *component="",
337  const char *magicNumber=DEFAULT_CIRCULAR_BFR_MAGIC_NUMBER);
338 
339  CircularBufferManager(unsigned char *ptr, size_t len); // existing segment
340 
341 #pragma GCC diagnostic push
342 #pragma GCC diagnostic ignored "-Wsuggest-final-methods"
344 #pragma GCC diagnostic pop
345 
347 
349 
351  return (0);
352  }
353 }; // end class CircularBufferManager
354 
357 public:
359 
360  FixedBufferManager(unsigned char *ptr, size_t len, size_t blk_size,
361  const char *component="",
362  const char *magicNumber=DEFAULT_FIXED_BFR_MAGIC_NUMBER);
363 
364  FixedBufferManager(unsigned char *ptr, size_t len); // existing segment
365 
367 
369 
371 
373  return (sizeof(SharedBufferAllocRecord));
374  }
375 }; // end class FixedBufferManager
376 
377 #pragma GCC diagnostic pop
378 
380 #endif
381 
382 /* vim: set expandtab shiftwidth=4 tabstop=4: */
FixedBufferManager
Impose a fixed-size buffer on a BufferRegion.
Definition: circular_bfr.hpp:356
SharedBufferRegionHeader_64::headerLength
uint8_t headerLength
Definition: circular_bfr.hpp:69
CircularBufferManager::allocateBlock
virtual SharedBufferAllocRecord * allocateBlock(size_t len) VIRTUAL_OVERRIDE
Definition: circular_bfr.cpp:324
CircularBufferManager::getBlockOverhead
virtual BufferRegion_Overhead_t getBlockOverhead() const VIRTUAL_OVERRIDE
Interface to return the per-block overhead associated with an allocator.
Definition: circular_bfr.hpp:350
sharedBufferSegmentIsInNativeByteOrder
bool sharedBufferSegmentIsInNativeByteOrder(const void *segment)
Indicates if segment uses native byte order.
Definition: circular_bfr.hpp:90
GET_VAL
#define GET_VAL(fieldName, ptr32, ptr64, is64, isNative)
Definition: circular_bfr.cpp:32
BufferRegion::getFreeListHead
virtual SharedBufferAllocRecord * getFreeListHead()
Return first free allocation record.
Definition: circular_bfr.cpp:213
CircularBufferManager::returnBlock
virtual void returnBlock(SharedBufferAllocRecord *record) VIRTUAL_OVERRIDE
Definition: circular_bfr.cpp:513
SharedBufferAllocRecord_32::offset_block
uint32_t offset_block
Definition: circular_bfr.hpp:105
BufferRegion::NOT_OWNER
@ NOT_OWNER
indicates region is not owned by this BufferRegion
Definition: circular_bfr.hpp:180
SharedBufferRegionHeader_32::isNativeByteOrder
constexpr bool isNativeByteOrder() const
Definition: circular_bfr.hpp:60
BufferRegion::getBlockOverhead
virtual BufferRegion_Overhead_t getBlockOverhead() const =0
Interface to return the per-block overhead associated with an allocator.
stdout
#define stdout
Definition: tmp.o.cpp:3117
SharedBufferRegionHeader_32::magicNumber
char magicNumber[8]
Definition: circular_bfr.hpp:44
SharedBufferRegionHeader_32::blockDimensions
uint32_t blockDimensions
Definition: circular_bfr.hpp:50
SharedBufferAllocRecord_32
Allocation record for chains in a 32-bit shared memory buffer.
Definition: circular_bfr.hpp:103
BufferRegion::blockAddress
virtual unsigned char * blockAddress(const SharedBufferAllocRecord *record)
Definition: circular_bfr.hpp:282
FixedBufferManager::getBlockOverhead
virtual BufferRegion_Overhead_t getBlockOverhead() const VIRTUAL_OVERRIDE
Interface to return the per-block overhead associated with an allocator.
Definition: circular_bfr.hpp:372
BufferRegion::getActiveListHead
virtual SharedBufferAllocRecord * getActiveListHead()
Return first active allocation record.
Definition: circular_bfr.cpp:196
SharedBufferAllocRecord_64::usedLen
uint64_t usedLen
Definition: circular_bfr.hpp:116
BufferRegion::ownership
OwnershipState ownership
Definition: circular_bfr.hpp:191
stderr
#define stderr
Definition: tmp.o.cpp:3115
BufferRegion::OWN_BLOCK
@ OWN_BLOCK
indicates region is owned by the BufferRegion and should be recovered when deleted.
Definition: circular_bfr.hpp:181
FixedBufferManager::TOTAL_allocation_wait_spins
static int32_t TOTAL_allocation_wait_spins
Definition: circular_bfr.hpp:358
SharedBufferRegionHeader_64::initialize
void initialize(const char *component, const char *magic, size_t regionLen)
Definition: circular_bfr.cpp:617
SharedBufferRegionHeader_32::byteOrderTag
uint32_t byteOrderTag
Definition: circular_bfr.hpp:47
SharedBufferAllocRecord_32::offset_allocRecord
uint32_t offset_allocRecord
Definition: circular_bfr.hpp:104
SharedBufferRegionHeader
SharedBufferRegionHeader_32 SharedBufferRegionHeader
Default SharedBufferRegionHeader.
Definition: circular_bfr.hpp:125
FixedBufferManager::allocateBlock
virtual SharedBufferAllocRecord * allocateBlock(size_t len) VIRTUAL_OVERRIDE
Definition: circular_bfr.cpp:681
CircularBufferManager::TOTAL_return_wait_spins
static int32_t TOTAL_return_wait_spins
Definition: circular_bfr.hpp:333
VIRTUAL_OVERRIDE
#define VIRTUAL_OVERRIDE
Generates override if the compiler supports it.
Definition: compiler_hints.h:435
SharedBufferAllocRecord_64::offset_allocRecord
uint64_t offset_allocRecord
Definition: circular_bfr.hpp:113
SharedBufferAllocRecord_64::blockLen
uint64_t blockLen
Definition: circular_bfr.hpp:115
BufferRegion::availableBlocks
size_t availableBlocks
Definition: circular_bfr.hpp:190
BufferRegion::getBufferBase
unsigned char * getBufferBase() const OME_ALWAYS_INLINE
Return the address of the buffer region.
Definition: circular_bfr.hpp:253
BufferRegion::OWN_MAP
@ OWN_MAP
indicates region is a memory map owned by the BufferRegion and should be unmapped when deleted.
Definition: circular_bfr.hpp:182
SharedBufferAllocRecord_64::offset_block
uint64_t offset_block
Definition: circular_bfr.hpp:114
OME_YIELD_THREAD
#define OME_YIELD_THREAD()
Macro for platform-specific yield of thread's time slice.
Definition: compiler_hints.h:547
BufferRegion::getRegionLength
size_t getRegionLength() const OME_ALWAYS_INLINE
Return the number of bytes in the region.
Definition: circular_bfr.hpp:243
getFileType
const char * getFileType(const char *fileName)
Definition: catSym.c:161
BufferRegion::traverseNextBlock
virtual SharedBufferAllocRecord * traverseNextBlock(SharedBufferAllocRecord *record)
Traverse to next allocation record on current chain.
Definition: circular_bfr.cpp:233
BufferRegion::region
unsigned char * region
Definition: circular_bfr.hpp:187
MAX_MODULES
#define MAX_MODULES
Definition: catSym.c:19
BufferRegion::OwnershipState
OwnershipState
Definition: circular_bfr.hpp:179
OME_PREFETCH
#define OME_PREFETCH(addr, rw, locality)
Macro to request prefetch.
Definition: compiler_hints.h:362
SharedBufferAllocRecord_64::offset_nextInChain
uint64_t offset_nextInChain
Definition: circular_bfr.hpp:117
CircularBufferManager::~CircularBufferManager
~CircularBufferManager()
Definition: circular_bfr.hpp:343
SharedBufferRegionHeader_32
Region header for a 32-bit shared memory segment.
Definition: circular_bfr.hpp:43
CircularBufferManager::CircularBufferManager
CircularBufferManager(unsigned char *ptr, size_t len, size_t blk_size, const char *component="", const char *magicNumber=DEFAULT_CIRCULAR_BFR_MAGIC_NUMBER)
Definition: circular_bfr.cpp:159
BufferRegion::getOwnership
OwnershipState getOwnership() const
Return the administrative ownership state of the region.
Definition: circular_bfr.hpp:238
EOF
#define EOF
Definition: tmp.o.cpp:135
CircularBufferManager::TOTAL_allocation_wait_spins
static int32_t TOTAL_allocation_wait_spins
Definition: circular_bfr.hpp:332
srcID
const char srcID[]
Definition: catSym.c:17
BufferRegion::regionLength
size_t regionLength
Definition: circular_bfr.hpp:188
waitForBufferAllocRecordToBeReady
void waitForBufferAllocRecordToBeReady(SharedBufferAllocRecord *rec)
Verify record is prepared and, if needed, wait until it is prepared.
Definition: circular_wait.hpp:16
MAX_LINE_LEN
#define MAX_LINE_LEN
Definition: catSym.c:15
SharedBufferRegionHeader_64::offset_endActiveList
uint64_t offset_endActiveList
Definition: circular_bfr.hpp:78
circular_bfr.hpp
DEFINE_DID_COMPARE_AND_SWAP_TYPE
DEFINE_DID_COMPARE_AND_SWAP_TYPE(size_t, size_t)
BufferRegion::~BufferRegion
virtual ~BufferRegion()
Destructor for a BufferRegion.
Definition: circular_bfr.cpp:121
FixedBufferManager::FixedBufferManager
FixedBufferManager(unsigned char *ptr, size_t len, size_t blk_size, const char *component="", const char *magicNumber=DEFAULT_FIXED_BFR_MAGIC_NUMBER)
Definition: circular_bfr.cpp:654
OFFSET_T
uint32_t OFFSET_T
Definition: circular_bfr.cpp:21
SharedBufferRegionHeader_64::blockDimensions
uint64_t blockDimensions
Definition: circular_bfr.hpp:74
SharedBufferRegionHeader_32::headerLength
uint8_t headerLength
Definition: circular_bfr.hpp:45
OME_EXPECT_TRUE
#define OME_EXPECT_TRUE(expr)
Annotation macro for conditional expression expected to be true.
Definition: compiler_hints.h:541
BufferRegion::allocateBlock
virtual SharedBufferAllocRecord * allocateBlock(size_t len)=0
BufferRegion::returnBlock
virtual void returnBlock(SharedBufferAllocRecord *record)=0
SharedBufferRegionHeader_32::offset_startFreeList
uint32_t offset_startFreeList
Definition: circular_bfr.hpp:51
SharedBufferRegionHeader_32::initialize
void initialize(const char *component, const char *magic, size_t regionLen)
Definition: circular_bfr.cpp:595
SharedBufferAllocRecord_32::offset_nextInChain
uint32_t offset_nextInChain
Definition: circular_bfr.hpp:108
circular_wait.hpp
DEFINE_COMPARE_AND_SWAP_TYPE
DEFINE_COMPARE_AND_SWAP_TYPE(size_t, size_t)
BufferRegion::getBlockSize
size_t getBlockSize() const OME_ALWAYS_INLINE
Return the block size set for the region.
Definition: circular_bfr.hpp:248
SharedBufferRegionHeader_32::offset_startActiveList
uint32_t offset_startActiveList
Definition: circular_bfr.hpp:53
BufferRegion::BufferRegion
BufferRegion(unsigned char *ptr, size_t len, size_t blk_size, OwnershipState ownedBy=NOT_OWNER)
Definition: circular_bfr.cpp:106
display
int display(OMEthread *thread, OMEtype &result, const OMEtype &argSet)
Definition: OILdebug.cpp:112
sharedBufferSegmentSizeIs64bit
bool sharedBufferSegmentSizeIs64bit(const void *segment)
Indicates if segment uses 64-bit offsets.
Definition: circular_bfr.hpp:97
BufferRegion::OWN_RECORD
@ OWN_RECORD
Definition: circular_bfr.hpp:183
BufferRegion::blockSize
size_t blockSize
Definition: circular_bfr.hpp:189
SharedBufferRegionHeader_64::offset_startActiveList
uint64_t offset_startActiveList
Definition: circular_bfr.hpp:77
SharedBufferRegionHeader_32::_pad
uint32_t _pad
Definition: circular_bfr.hpp:48
OME_USED
const char srcID[] OME_USED
Definition: tick_time.cpp:24
SharedBufferRegionHeader_64
Region header for a 64-bit shared memory segment.
Definition: circular_bfr.hpp:67
FixedBufferManager::~FixedBufferManager
~FixedBufferManager()
Definition: circular_bfr.cpp:676
BufferRegion::BufferRegion_Overhead_t
uint_fast8_t BufferRegion_Overhead_t
Definition: circular_bfr.hpp:185
SharedBufferRegionHeader_32::updatingThread
uint32_t updatingThread
Definition: circular_bfr.hpp:52
SharedBufferRegionHeader_64::componentId
char componentId[47]
Definition: circular_bfr.hpp:70
SharedBufferRegionHeader_32::offset_endActiveList
uint32_t offset_endActiveList
Definition: circular_bfr.hpp:54
atomic_values.h
Atomic operations.
CircularBufferManager
Impose a circular buffer on a BufferRegion.
Definition: circular_bfr.hpp:330
compiler_hints.h
Compiler-specific macros to provide performance-related hints.
OME_EXPECT_FALSE
#define OME_EXPECT_FALSE(expr)
Annotation macro for conditional expression expected to be false.
Definition: compiler_hints.h:540
SharedBufferAllocRecord_32::usedLen
uint32_t usedLen
Definition: circular_bfr.hpp:107
OME_ALWAYS_INLINE
#define OME_ALWAYS_INLINE
Tell the compiler to alway inline a function, regardless of optimization level.
Definition: compiler_hints.h:364
DEFAULT_CIRCULAR_BFR_MAGIC_NUMBER
#define DEFAULT_CIRCULAR_BFR_MAGIC_NUMBER
Magic number to identify a shared memory circular buffer.
Definition: circular_bfr.hpp:28
SharedBufferAllocRecord_32::blockLen
uint32_t blockLen
Definition: circular_bfr.hpp:106
SharedBufferRegionHeader_64::byteOrderTag
uint32_t byteOrderTag
Definition: circular_bfr.hpp:71
SharedBufferRegionHeader_64::updatingThread
uint32_t updatingThread
Definition: circular_bfr.hpp:76
malloc
char * malloc(unsigned int bytes)
Definition: test_malloc.c:441
BufferRegion::setOwnership
void setOwnership(OwnershipState newOwnership)
Change administrative ownership of region.
Definition: circular_bfr.hpp:232
DID_COMPARE_SWAP_OFFSET
#define DID_COMPARE_SWAP_OFFSET(ptr, v1, v2)
Definition: circular_bfr.cpp:22
BufferRegion::replaceRegion
void replaceRegion(unsigned char *ptr, size_t len, size_t blk_size, OwnershipState ownedBy=NOT_OWNER)
Reconstruct the buffer using a new region.
Definition: circular_bfr.cpp:142
BufferRegion::checkLists
int checkLists(int displayFlag)
Debug routine used to verify integrity of block lists.
Definition: circular_bfr.cpp:249
FixedBufferManager::returnBlock
virtual void returnBlock(SharedBufferAllocRecord *record) VIRTUAL_OVERRIDE
Definition: circular_bfr.cpp:811
SharedBufferAllocRecord
SharedBufferAllocRecord_32 SharedBufferAllocRecord
Default SharedBufferAllocRecord.
Definition: circular_bfr.hpp:133
BufferRegion
Interface to a buffer region. This is an abstract class.
Definition: circular_bfr.hpp:177
SharedBuffer_Offset_t
uint32_t SharedBuffer_Offset_t
Definition: circular_bfr.hpp:39
SharedBufferRegionHeader_64::_pad
uint32_t _pad
Definition: circular_bfr.hpp:72
GET_VAL32
#define GET_VAL32(fieldName, ptr32, ptr64, is64, isNative)
Definition: circular_bfr.cpp:30
SharedBufferRegionHeader_64::magicNumber
char magicNumber[8]
Definition: circular_bfr.hpp:68
DEFAULT_FIXED_BFR_MAGIC_NUMBER
#define DEFAULT_FIXED_BFR_MAGIC_NUMBER
Magic number to identify a fixed-size memory buffer.
Definition: circular_bfr.hpp:31
SharedBufferRegionHeader_32::componentId
char componentId[47]
Definition: circular_bfr.hpp:46
main
int main(int argc, const char *argv[])
Definition: catSym.c:209
SharedBufferAllocRecord_64
Allocation record for chains in a 64-bit shared memory buffer.
Definition: circular_bfr.hpp:112
SharedBufferRegionHeader_64::isNativeByteOrder
constexpr bool isNativeByteOrder() const
Definition: circular_bfr.hpp:83
SharedBufferRegionHeader_64::offset_startFreeList
uint32_t offset_startFreeList
Definition: circular_bfr.hpp:75
Generated: Tue Jul 28 2020 16:03:24
Support Information