ATLAS Offline Software
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
SG::ArenaBlock Class Reference

A large memory block that gets carved into smaller uniform elements. More...

#include <ArenaBlock.h>

Collaboration diagram for SG::ArenaBlock:

Public Types

typedef char * pointer
 Type for a pointer to an element. More...
 
typedef const char * const_pointer
 
typedef void func_t(pointer)
 Function that operates on an element. More...
 

Public Member Functions

size_t size () const
 Return the number of elements in the block. More...
 
size_t eltSize () const
 Return the size of the elements in the block. More...
 
ArenaBlock *& link ()
 Return the link pointer of the block. More...
 
const ArenaBlocklink () const
 Return the link pointer of the block. More...
 
pointer index (size_t i)
 Return a pointer to element i in the block. More...
 
const_pointer index (size_t i) const
 Return a pointer to element i in the block. More...
 
pointer index (size_t i, size_t elt_size)
 Return a pointer to element i in the block. More...
 
const_pointer index (size_t i, size_t elt_size) const
 Return a pointer to element i in the block. More...
 
void protect ()
 Write-protect this block. More...
 
void unprotect ()
 Write-enable this block. More...
 

Static Public Member Functions

static ArenaBlocknewBlock (size_t n, size_t elt_size, func_t *ctor)
 Create a new block. More...
 
static void destroy (ArenaBlock *p, func_t *dtor)
 Destroy a block. More...
 
static void destroyList (ArenaBlock *p, func_t *dtor)
 Destroy all blocks in a list. More...
 
static void appendList (ArenaBlock **headp, ArenaBlock *tail)
 Concatenate two lists of blocks. More...
 
static void applyList (ArenaBlock *p, func_t *func, size_t n)
 Call a function on elements in a list of blocks. More...
 
static size_t overhead ()
 Return the per-block memory overhead, in bytes. More...
 
static size_t nactive ()
 Return the global number of blocks in use. More...
 
static void protectList (ArenaBlock *p)
 Write-protect all blocks in a list. More...
 
static void unprotectList (ArenaBlock *p)
 Write-enable all blocks in a list. More...
 

Private Member Functions

 ArenaBlock (size_t n, size_t elt_size)
 Prohibit calling these. More...
 
 ~ArenaBlock ()
 
 ArenaBlock (const ArenaBlock &)
 
ArenaBlockoperator= (const ArenaBlock &)
 

Private Attributes

ArenaBlockm_link
 The link for the linked list. More...
 
size_t m_size
 Number of elements in this block. More...
 
size_t m_elt_size
 Size, in bytes, of each element in this block. More...
 
ArenaBlockAlignDetail::padForAlign m_dummy
 

Static Private Attributes

static std::atomic< size_t > s_nactive
 Global count of the number of blocks in use. More...
 

Friends

class ArenaAllocatorBase
 

Detailed Description

A large memory block that gets carved into smaller uniform elements.

See Arena.h for an overview of the arena-based memory allocators.

The block-based memory allocators allocate memory in large blocks and then carve them up into smaller, uniform elements. This class is used for those large blocks. Actually, the contents of this class is a fixed header for the block; the contents of the block itself will immediately follow the class contents.

Each block keeps some housekeeping information: the element size in bytes, the number of elements in the block, and a pointer that can be used to chain blocks together in singly-linked lists. There are a few functions available to help manage such lists.

Definition at line 42 of file ArenaBlock.h.

Member Typedef Documentation

◆ const_pointer

Definition at line 47 of file ArenaBlock.h.

◆ func_t

typedef void SG::ArenaBlock::func_t(pointer)

Function that operates on an element.

Definition at line 50 of file ArenaBlock.h.

◆ pointer

typedef char* SG::ArenaBlock::pointer

Type for a pointer to an element.

Definition at line 46 of file ArenaBlock.h.

Constructor & Destructor Documentation

◆ ArenaBlock() [1/2]

SG::ArenaBlock::ArenaBlock ( size_t  n,
size_t  elt_size 
)
private

Prohibit calling these.

◆ ~ArenaBlock()

SG::ArenaBlock::~ArenaBlock ( )
inlineprivate

Definition at line 227 of file ArenaBlock.h.

227 {}

◆ ArenaBlock() [2/2]

SG::ArenaBlock::ArenaBlock ( const ArenaBlock )
private

Member Function Documentation

◆ appendList()

void SG::ArenaBlock::appendList ( ArenaBlock **  link,
ArenaBlock tail 
)
static

Concatenate two lists of blocks.

Parameters
headpPointer to pointer to the head of the list.
tailPointer to list to append to the end.

The list tail is appended to the end of the list *headp. (headp is a pointer-to-pointer to be able to handle the case of an empty list.)

Definition at line 110 of file ArenaBlock.cxx.

111 {
112  while (*link) {
113  link = &(*link)->link();
114  }
115  *link = tail;
116 }

◆ applyList()

void SG::ArenaBlock::applyList ( ArenaBlock p,
func_t func,
size_t  n 
)
static

Call a function on elements in a list of blocks.

Parameters
pPointer to the head of the list.
funcFunction to apply.
nNumber of elements in the first block on which to call the function.

This will loop through the elements in all blocks on the list, calling func. In the first block, we apply the function only to the first n elements. In subsequent blocks, the function is applied to all elements.

Parameters
pPointer to the head of the list.
funcFunction to apply.
Numberof elements in the first block on which to call the function.

This will loop through the elements in all blocks on the list, calling func. In the first block, we apply the function only to the first n elements. In subsequent blocks, the function is applied to all elements.

Definition at line 131 of file ArenaBlock.cxx.

134 {
135  if (!p) return;
136  size_t elt_size = p->eltSize();
137  if (n > p->size()) {
138  n = p->size();
139  }
140  while (1) {
141  for (size_t i = 0; i < n; i++) {
142  func (p->index (i, elt_size));
143  }
144  p = p->link();
145  if (!p) break;
146  n = p->size();
147  }
148 }

◆ destroy()

void SG::ArenaBlock::destroy ( ArenaBlock p,
func_t dtor 
)
static

Destroy a block.

Parameters
pThe block to destroy.
dtorIf non-null, call this function on each element in the block.

Definition at line 69 of file ArenaBlock.cxx.

70 {
71  if (dtor) {
72  size_t elt_size = p->eltSize();
73  size_t n = p->size();
74  for (size_t i = 0; i < n; i++) {
75  dtor (p->index (i, elt_size));
76  }
77  }
78  --s_nactive;
79  std::free (p);
80 }

◆ destroyList()

void SG::ArenaBlock::destroyList ( ArenaBlock p,
func_t dtor 
)
static

Destroy all blocks in a list.

Parameters
pThe first block to destroy.
dtorIf non-null, call this function on each element in the blocks.

Will destroy all blocks in the linked list headed by p.

Parameters
pThe first block to destroy.
dtorIf non-null, call this function on each element the blocks.

Will destroy all blocks in the linked list headed by p.

Definition at line 91 of file ArenaBlock.cxx.

92 {
93  while (p) {
94  ArenaBlock* next = p->link();
95  destroy (p, dtor);
96  p = next;
97  }
98 }

◆ eltSize()

size_t SG::ArenaBlock::eltSize ( ) const

Return the size of the elements in the block.

◆ index() [1/4]

pointer SG::ArenaBlock::index ( size_t  i)

Return a pointer to element i in the block.

Parameters
iThe index of the desired element.

◆ index() [2/4]

const_pointer SG::ArenaBlock::index ( size_t  i) const

Return a pointer to element i in the block.

Parameters
iThe index of the desired element.

◆ index() [3/4]

pointer SG::ArenaBlock::index ( size_t  i,
size_t  elt_size 
)

Return a pointer to element i in the block.

Parameters
iThe index of the desired element.
elt_sizeThe block's element size.

This is provided in addition to the previous function as it may allow for better inlined code in when used in a loop, if elt_size is saved in a local.

◆ index() [4/4]

const_pointer SG::ArenaBlock::index ( size_t  i,
size_t  elt_size 
) const

Return a pointer to element i in the block.

Parameters
iThe index of the desired element.
elt_sizeThe block's element size.

This is provided in addition to the previous function as it may allow for better inlined code in when used in a loop, if elt_size is saved in a local.

◆ link() [1/2]

ArenaBlock* & SG::ArenaBlock::link ( )

Return the link pointer of the block.

◆ link() [2/2]

const ArenaBlock* SG::ArenaBlock::link ( ) const

Return the link pointer of the block.

◆ nactive()

static size_t SG::ArenaBlock::nactive ( )
static

Return the global number of blocks in use.

◆ newBlock()

ArenaBlock * SG::ArenaBlock::newBlock ( size_t  n,
size_t  elt_size,
func_t ctor 
)
static

Create a new block.

Parameters
nThe number of elements in the new block.
elt_sizeThe size in bytes of each element.
ctorIf non-null, call this function on each element in the new block.

The block will be allocated so that it entirely occupies a set of contiguous pages. The requested size may be rounded up for this.

Definition at line 41 of file ArenaBlock.cxx.

42 {
43  static const size_t pageSize = sysconf (_SC_PAGESIZE);
44  size_t tot_size = n*elt_size + ArenaBlockBodyOffset;
45  // Round up to a multiple of pageSize.
46  size_t tot_size_rounded = (tot_size + (pageSize-1)) & ~(pageSize-1);
47  // Number of elements after rounding up.
48  size_t n_rounded = (tot_size_rounded - ArenaBlockBodyOffset) / elt_size;
49  assert (n_rounded >= n);
50  ArenaBlock* p = reinterpret_cast<ArenaBlock*>
51  (std::aligned_alloc (pageSize, tot_size_rounded));
52  ++s_nactive;
53  p->m_link = nullptr;
54  p->m_elt_size = elt_size;
55  p->m_size = n_rounded;
56  if (ctor) {
57  for (size_t i = 0; i < n_rounded; i++)
58  ctor (p->index (i, elt_size));
59  }
60  return p;
61 }

◆ operator=()

ArenaBlock& SG::ArenaBlock::operator= ( const ArenaBlock )
private

◆ overhead()

size_t SG::ArenaBlock::overhead ( )
static

Return the per-block memory overhead, in bytes.

This tries to include malloc overhead as well, but that may just be an estimate. Don't rely on this to be exact.

Definition at line 157 of file ArenaBlock.cxx.

158 {
159  // The extra size_t is a guesstimate of malloc overhead.
160  return ArenaBlockBodyOffset + sizeof (size_t);
161 }

◆ protect()

void SG::ArenaBlock::protect ( )

Write-protect this block.

Adjust protection on the memory allocated for this block to disallow writes.

Definition at line 170 of file ArenaBlock.cxx.

171 {
172  int stat = mprotect (this, m_size*m_elt_size+ArenaBlockBodyOffset,
173  PROT_READ);
174  if (stat) {
175  throw SG::ExcProtection (errno);
176  }
177 }

◆ protectList()

void SG::ArenaBlock::protectList ( ArenaBlock p)
static

Write-protect all blocks in a list.

Parameters
pThe first block to protect.

Adjust protection on the memory allocated for these blocks to disallow writes.

Definition at line 203 of file ArenaBlock.cxx.

204 {
205  for (; p; p = p->link()) {
206  p->protect();
207  }
208 }

◆ size()

size_t SG::ArenaBlock::size ( ) const

Return the number of elements in the block.

◆ unprotect()

void SG::ArenaBlock::unprotect ( )

Write-enable this block.

Adjust protection on the memory allocated for this block to allow writes.

Definition at line 186 of file ArenaBlock.cxx.

187 {
188  int stat = mprotect (this, m_size*m_elt_size+ArenaBlockBodyOffset,
189  PROT_READ + PROT_WRITE);
190  if (stat) {
191  throw SG::ExcProtection (errno);
192  }
193 }

◆ unprotectList()

void SG::ArenaBlock::unprotectList ( ArenaBlock p)
static

Write-enable all blocks in a list.

Parameters
pThe first block to protect.

Adjust protection on the memory allocated for these blocks to allow writes.

Definition at line 218 of file ArenaBlock.cxx.

219 {
220  for (; p; p = p->link()) {
221  p->unprotect();
222  }
223 }

Friends And Related Function Documentation

◆ ArenaAllocatorBase

friend class ArenaAllocatorBase
friend

Definition at line 233 of file ArenaBlock.h.

Member Data Documentation

◆ m_dummy

ArenaBlockAlignDetail::padForAlign SG::ArenaBlock::m_dummy
private

Definition at line 246 of file ArenaBlock.h.

◆ m_elt_size

size_t SG::ArenaBlock::m_elt_size
private

Size, in bytes, of each element in this block.

Definition at line 242 of file ArenaBlock.h.

◆ m_link

ArenaBlock* SG::ArenaBlock::m_link
private

The link for the linked list.

Definition at line 236 of file ArenaBlock.h.

◆ m_size

size_t SG::ArenaBlock::m_size
private

Number of elements in this block.

Definition at line 239 of file ArenaBlock.h.

◆ s_nactive

std::atomic< size_t > SG::ArenaBlock::s_nactive
staticprivate

Global count of the number of blocks in use.

Global number of blocks in use.

Definition at line 249 of file ArenaBlock.h.


The documentation for this class was generated from the following files:
SG::ExcProtection
Exception — Attempt to change memory protection failed.
Definition: Control/AthAllocators/AthAllocators/exceptions.h:43
SG::ArenaBlock::ArenaBlock
ArenaBlock(size_t n, size_t elt_size)
Prohibit calling these.
SG::ArenaBlock::destroy
static void destroy(ArenaBlock *p, func_t *dtor)
Destroy a block.
Definition: ArenaBlock.cxx:69
tail
std::string tail(std::string s, const std::string &pattern)
tail of a string
Definition: computils.cxx:300
SG::ArenaBlock::link
ArenaBlock *& link()
Return the link pointer of the block.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
SG::ArenaBlock::m_size
size_t m_size
Number of elements in this block.
Definition: ArenaBlock.h:239
beamspotman.stat
stat
Definition: beamspotman.py:266
SG::ArenaBlock::s_nactive
static std::atomic< size_t > s_nactive
Global count of the number of blocks in use.
Definition: ArenaBlock.h:249
SG::ArenaBlock::m_elt_size
size_t m_elt_size
Size, in bytes, of each element in this block.
Definition: ArenaBlock.h:242