DataChunker Class Reference

#include <dataChunker.h>

Inheritance diagram for DataChunker:

Inheritance graph
[legend]
List of all members.

Detailed Description

Implements a chunked data allocator.

Calling new/malloc all the time is a time consuming operation. Therefore, we provide the DataChunker, which allocates memory in blocks of chunkSize (by default 16k, see ChunkSize, though it can be set in the constructor), then doles it out as requested, in chunks of up to chunkSize in size.

It will assert if you try to get more than ChunkSize bytes at a time, and it deals with the logic of allocating new blocks and giving out word-aligned chunks.

Note that new/free/realloc WILL NOT WORK on memory gotten from the DataChunker. This also only grows (you can call freeBlocks to deallocate and reset things).


Public Types

 ChunkSize = 16376
 Default size of each DataBlock page in the DataChunker.
enum  { ChunkSize = 16376 }

Public Member Functions

voidalloc (S32 size)
 Return a pointer to a chunk of memory from a pre-allocated block.
void freeBlocks (bool keepOne=false)
 Free all allocated memory blocks.
 DataChunker (S32 size=ChunkSize)
 Initialize using blocks of a given size.
 ~DataChunker ()
void swap (DataChunker &d)
 Swaps the memory allocated in one data chunker for another.

Private Attributes

DataBlockmCurBlock
 current page we're allocating data from.
S32 mChunkSize
 The size allocated for each page in the DataChunker.

Classes

struct  DataBlock
 Block of allocated memory. More...


Member Enumeration Documentation

anonymous enum

Enumerator:
ChunkSize  Default size of each DataBlock page in the DataChunker.


Constructor & Destructor Documentation

DataChunker::DataChunker ( S32  size = ChunkSize  ) 

Initialize using blocks of a given size.

One new block is allocated at constructor-time.

Parameters:
size Size in bytes of the space to allocate for each block.

DataChunker::~DataChunker (  ) 


Member Function Documentation

void* DataChunker::alloc ( S32  size  ) 

Return a pointer to a chunk of memory from a pre-allocated block.

This memory goes away when you call freeBlocks.

This memory is word-aligned.

Parameters:
size Size of chunk to return. This must be less than chunkSize or else an assertion will occur.

void DataChunker::freeBlocks ( bool  keepOne = false  ) 

Free all allocated memory blocks.

This invalidates all pointers returned from alloc().

void DataChunker::swap ( DataChunker d  )  [inline]

Swaps the memory allocated in one data chunker for another.

This can be used to implement packing of memory stored in a DataChunker.


Member Data Documentation

current page we're allocating data from.

If the data size request is greater than the memory space currently available in the current page, a new page will be allocated.

The size allocated for each page in the DataChunker.