OpenDDS  Snapshot(2023/04/07-19:43)
Public Member Functions | Protected Attributes | List of all members
OpenDDS::DCPS::AllocHeader Class Reference

#include <MemoryPool.h>

Inheritance diagram for OpenDDS::DCPS::AllocHeader:
Inheritance graph
[legend]

Public Member Functions

 AllocHeader ()
 
unsigned int size () const
 
unsigned int prev_size () const
 
bool is_free () const
 
unsigned char * ptr () const
 
AllocHeadernext_adjacent ()
 
AllocHeaderprev_adjacent ()
 
void allocate (size_t size)
 
void set_size (size_t size)
 
void set_prev_size (int size)
 
void set_allocated ()
 
void join_next ()
 

Protected Attributes

int alloc_size_
 Size of my buffer, negative if free, positive if alloc. More...
 
int prev_size_
 Size of previous buffer, or 0 if first, never negative. More...
 

Detailed Description

Header of all allocations - found at beginning of allocation inside pool. Extra room must be added to each allocation for this header.

Definition at line 24 of file MemoryPool.h.

Constructor & Destructor Documentation

◆ AllocHeader()

OpenDDS::DCPS::AllocHeader::AllocHeader ( )

Construct

Definition at line 35 of file MemoryPool.cpp.

Referenced by OpenDDS::DCPS::FreeHeader::init_free_block(), join_next(), and prev_adjacent().

36 : alloc_size_(0)
37 , prev_size_(0)
38 {
39 }
int prev_size_
Size of previous buffer, or 0 if first, never negative.
Definition: MemoryPool.h:61
int alloc_size_
Size of my buffer, negative if free, positive if alloc.
Definition: MemoryPool.h:60

Member Function Documentation

◆ allocate()

void OpenDDS::DCPS::AllocHeader::allocate ( size_t  size)

Allocate from this block: change size, and mark as allocated

Definition at line 67 of file MemoryPool.cpp.

References set_allocated(), and set_size().

67  {
68  set_allocated();
69  set_size(size);
70 }
void set_size(size_t size)
Definition: MemoryPool.cpp:73
unsigned int size() const
Definition: MemoryPool.h:30

◆ is_free()

bool OpenDDS::DCPS::AllocHeader::is_free ( ) const
inline

Is this alloc free

Definition at line 34 of file MemoryPool.h.

Referenced by OpenDDS::DCPS::MemoryPool::joinable_next(), OpenDDS::DCPS::MemoryPool::joinable_prev(), OpenDDS::DCPS::FreeHeader::set_free(), and set_size().

34 { return alloc_size_ < 0; }
int alloc_size_
Size of my buffer, negative if free, positive if alloc.
Definition: MemoryPool.h:60

◆ join_next()

void OpenDDS::DCPS::AllocHeader::join_next ( )

Join with the next adjacent block

Definition at line 82 of file MemoryPool.cpp.

References AllocHeader(), next_adjacent(), set_size(), and size().

Referenced by OpenDDS::DCPS::MemoryPool::join_free_allocs().

82  {
83  // All unsigned, set_size will make negative if free (it is)
84  size_t next_size = this->next_adjacent()->size();
85  size_t joined_size = this->size() + next_size + sizeof(AllocHeader);
86  this->set_size(joined_size);
87 }
void set_size(size_t size)
Definition: MemoryPool.cpp:73
unsigned int size() const
Definition: MemoryPool.h:30
AllocHeader * next_adjacent()
Definition: MemoryPool.cpp:49

◆ next_adjacent()

AllocHeader * OpenDDS::DCPS::AllocHeader::next_adjacent ( )

Go to next header

Definition at line 49 of file MemoryPool.cpp.

References ptr(), and size().

Referenced by OpenDDS::DCPS::MemoryPool::allocate(), OpenDDS::DCPS::MemoryPool::join_free_allocs(), join_next(), OpenDDS::DCPS::MemoryPool::joinable_next(), and OpenDDS::DCPS::MemoryPool::joinable_prev().

49  {
50  unsigned char* past_buffer_end = ptr() + size();
51  return reinterpret_cast<AllocHeader*>(past_buffer_end);
52 }
unsigned char * ptr() const
Definition: MemoryPool.cpp:42
unsigned int size() const
Definition: MemoryPool.h:30

◆ prev_adjacent()

AllocHeader * OpenDDS::DCPS::AllocHeader::prev_adjacent ( )

Go to prev header

Definition at line 55 of file MemoryPool.cpp.

References AllocHeader(), and prev_size_.

Referenced by OpenDDS::DCPS::MemoryPool::join_free_allocs(), and OpenDDS::DCPS::MemoryPool::joinable_prev().

55  {
56  AllocHeader* result = NULL;
57  if (prev_size_) {
58  unsigned char* self = reinterpret_cast<unsigned char*>(this);
59  unsigned char* prev_buffer_start = self - prev_size_;
60  unsigned char* past_alloc = prev_buffer_start - sizeof(AllocHeader);
61  result = reinterpret_cast<AllocHeader*>(past_alloc);
62  }
63  return result;
64 }
int prev_size_
Size of previous buffer, or 0 if first, never negative.
Definition: MemoryPool.h:61

◆ prev_size()

unsigned int OpenDDS::DCPS::AllocHeader::prev_size ( ) const
inline

Get prev alloc size

Definition at line 32 of file MemoryPool.h.

Referenced by OpenDDS::DCPS::MemoryPool::joinable_prev().

32 { return prev_size_; }
int prev_size_
Size of previous buffer, or 0 if first, never negative.
Definition: MemoryPool.h:61

◆ ptr()

unsigned char * OpenDDS::DCPS::AllocHeader::ptr ( void  ) const

Get pointer to start of my buffer

Definition at line 42 of file MemoryPool.cpp.

Referenced by OpenDDS::DCPS::MemoryPool::allocate(), OpenDDS::DCPS::MemoryPool::joinable_prev(), next_adjacent(), and OpenDDS::DCPS::FreeIndex::node_index().

43 {
44  const unsigned char* buff = reinterpret_cast<const unsigned char*>(this + 1);
45  return const_cast<unsigned char*>(buff);
46 }

◆ set_allocated()

void OpenDDS::DCPS::AllocHeader::set_allocated ( )
inline

Mark this block as allocated

Definition at line 53 of file MemoryPool.h.

Referenced by allocate(), and OpenDDS::DCPS::MemoryPool::allocate().

53 { if (alloc_size_ < 0) alloc_size_ = - alloc_size_; }
int alloc_size_
Size of my buffer, negative if free, positive if alloc.
Definition: MemoryPool.h:60

◆ set_prev_size()

void OpenDDS::DCPS::AllocHeader::set_prev_size ( int  size)
inline

Set the prev size value stored in this header

Definition at line 51 of file MemoryPool.h.

Referenced by OpenDDS::DCPS::MemoryPool::allocate(), and OpenDDS::DCPS::MemoryPool::join_free_allocs().

51 { prev_size_ = size; }
int prev_size_
Size of previous buffer, or 0 if first, never negative.
Definition: MemoryPool.h:61
unsigned int size() const
Definition: MemoryPool.h:30

◆ set_size()

void OpenDDS::DCPS::AllocHeader::set_size ( size_t  size)

Set the size value stored in this header

Definition at line 73 of file MemoryPool.cpp.

References alloc_size_, and is_free().

Referenced by allocate(), OpenDDS::DCPS::MemoryPool::allocate(), and join_next().

74 {
75  if (is_free()) {
76  size *= -1;
77  }
78  alloc_size_ = (int)size;
79 }
unsigned int size() const
Definition: MemoryPool.h:30
int alloc_size_
Size of my buffer, negative if free, positive if alloc.
Definition: MemoryPool.h:60

◆ size()

unsigned int OpenDDS::DCPS::AllocHeader::size ( void  ) const
inline

Member Data Documentation

◆ alloc_size_

int OpenDDS::DCPS::AllocHeader::alloc_size_
protected

Size of my buffer, negative if free, positive if alloc.

Sizes are those of buffers, does not include size of headers

Definition at line 60 of file MemoryPool.h.

Referenced by OpenDDS::DCPS::FreeHeader::init_free_block(), OpenDDS::DCPS::FreeHeader::set_free(), and set_size().

◆ prev_size_

int OpenDDS::DCPS::AllocHeader::prev_size_
protected

Size of previous buffer, or 0 if first, never negative.

Definition at line 61 of file MemoryPool.h.

Referenced by OpenDDS::DCPS::FreeHeader::init_free_block(), and prev_adjacent().


The documentation for this class was generated from the following files: