CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MallocHeap Class Reference

#include <malloc_heap.h>

Collaboration diagram for MallocHeap:

Classes

struct  BlockPtr
 
struct  Tag
 

Public Types

typedef Callbackable< BlockPtr >
::CallbackTN * 
CallbackPtr
 

Public Member Functions

 MallocHeap (uint64_t capacity, CallbackPtr callback_ptr)
 
 ~MallocHeap ()
 
void * Allocate (uint64_t size, void *header, unsigned header_size)
 
void * Expand (void *block, uint64_t new_size)
 
void MarkFree (void *block)
 
uint64_t GetSize (void *block)
 
void Compact ()
 
uint64_t num_blocks ()
 
uint64_t used_bytes ()
 
uint64_t stored_bytes ()
 
uint64_t compacted_bytes ()
 
uint64_t capacity ()
 
double utilization ()
 
bool HasSpaceFor (uint64_t nbytes)
 

Private Attributes

CallbackPtr callback_ptr_
 
uint64_t capacity_
 
uint64_t gauge_
 
uint64_t stored_
 
uint64_t num_blocks_
 
unsigned char * heap_
 

Static Private Attributes

static const unsigned kMinCapacity = 1024
 

Detailed Description

This file is part of the CernVM File System. Fills an ever-growing heap. Free calls simply mark a block as free but allocation continues to take place at the top of the heap. Hence Realloc() is inefficient. For the common pattern of doubling a buffer with realloc, twice the final buffer size is required.

The allocator is copying and can collect garbage. In order to react on the move of a block, a callback can be registered that gets called with the new pointer address. The user of the MallocHeap has to make sure to identify any block based on the first bytes. Therefore, allocation requires these first bytes, a user-defined "header" if you will. Note that during a Compact() not even reading from any of the pointers is allowed!

MallocHeap is used by the in-memory object cache. The header is the object's content hash, so the cache manager can identify any block in its hash table and move the pointers when MallocHeap runs a garbage collection.

All memory blocks are 8-byte aligned and they have an 8-byte header containing their size. The size is negative for free blocks.

Definition at line 36 of file malloc_heap.h.

Member Typedef Documentation

Definition at line 46 of file malloc_heap.h.

Constructor & Destructor Documentation

MallocHeap::MallocHeap ( uint64_t  capacity,
CallbackPtr  callback_ptr 
)

Definition at line 113 of file malloc_heap.cc.

Here is the call graph for this function:

MallocHeap::~MallocHeap ( )

Definition at line 128 of file malloc_heap.cc.

Member Function Documentation

void * MallocHeap::Allocate ( uint64_t  size,
void *  header,
unsigned  header_size 
)

Definition at line 16 of file malloc_heap.cc.

Referenced by MemoryKvStore::DoMalloc().

Here is the call graph for this function:

Here is the caller graph for this function:

uint64_t MallocHeap::capacity ( )
inline

Definition at line 63 of file malloc_heap.h.

void MallocHeap::Compact ( )

Definition at line 39 of file malloc_heap.cc.

Referenced by MemoryKvStore::CompactMemory().

Here is the call graph for this function:

Here is the caller graph for this function:

uint64_t MallocHeap::compacted_bytes ( )
inline

Definition at line 60 of file malloc_heap.h.

void * MallocHeap::Expand ( void *  block,
uint64_t  new_size 
)

Definition at line 79 of file malloc_heap.cc.

Here is the call graph for this function:

uint64_t MallocHeap::GetSize ( void *  block)

Definition at line 106 of file malloc_heap.cc.

Here is the call graph for this function:

bool MallocHeap::HasSpaceFor ( uint64_t  nbytes)

Definition at line 89 of file malloc_heap.cc.

Here is the call graph for this function:

void MallocHeap::MarkFree ( void *  block)

Definition at line 94 of file malloc_heap.cc.

Referenced by MemoryKvStore::DoFree().

Here is the call graph for this function:

Here is the caller graph for this function:

uint64_t MallocHeap::num_blocks ( )
inline

Definition at line 57 of file malloc_heap.h.

uint64_t MallocHeap::stored_bytes ( )
inline

Definition at line 59 of file malloc_heap.h.

uint64_t MallocHeap::used_bytes ( )
inline

Definition at line 58 of file malloc_heap.h.

double MallocHeap::utilization ( )
inline

Definition at line 64 of file malloc_heap.h.

Referenced by MemoryKvStore::CompactMemory().

Here is the caller graph for this function:

Member Data Documentation

CallbackPtr MallocHeap::callback_ptr_
private

Invoked when a block is moved during compact.

Definition at line 103 of file malloc_heap.h.

uint64_t MallocHeap::capacity_
private

Total size of mmap'd arena.

Definition at line 107 of file malloc_heap.h.

Referenced by capacity(), MallocHeap(), and ~MallocHeap().

uint64_t MallocHeap::gauge_
private

End of the area of reserved blocks, used for the next allocation.

Definition at line 111 of file malloc_heap.h.

Referenced by used_bytes(), and utilization().

unsigned char* MallocHeap::heap_
private

The big mmap'd memory block used to serve allocation requests.

Definition at line 123 of file malloc_heap.h.

Referenced by MallocHeap(), and ~MallocHeap().

const unsigned MallocHeap::kMinCapacity = 1024
staticprivate

Minimum number of bytes of the heap.

Definition at line 73 of file malloc_heap.h.

Referenced by MallocHeap().

uint64_t MallocHeap::num_blocks_
private

Number of reserved blocks

Definition at line 119 of file malloc_heap.h.

Referenced by compacted_bytes(), and num_blocks().

uint64_t MallocHeap::stored_
private

Sum of the non-free block sizes.

Definition at line 115 of file malloc_heap.h.

Referenced by compacted_bytes(), stored_bytes(), and utilization().


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