• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
TDEZoneAllocator Class Reference

#include <kallocator.h>

Public Member Functions

 TDEZoneAllocator (unsigned long _blockSize=8 *1024)
 
 ~TDEZoneAllocator ()
 
void * allocate (size_t _size)
 
void deallocate (void *ptr)
 
void free_since (void *ptr)
 

Protected Types

typedef TQValueList< MemBlock * > MemList
 

Protected Member Functions

void addBlock (MemBlock *b)
 
void delBlock (MemBlock *b)
 
void insertHash (MemBlock *b)
 
void initHash ()
 

Protected Attributes

MemBlock * currentBlock
 
unsigned long blockSize
 
unsigned long blockOffset
 
unsigned int log2
 
unsigned int num_blocks
 
MemList ** hashList
 
unsigned int hashSize
 
bool hashDirty
 

Detailed Description

Memory allocator for large groups of small objects.

This should be used for large groups of objects that are created and destroyed together. When used carefully for this purpose it is faster and more memory efficient than malloc. Additionally to a usual obstack like allocator you can also free the objects individually. Because it does no compaction it still is faster then malloc()/free(). Depending on the exact usage pattern that might come at the expense of some memory though.

Author
Waldo Bastian basti.nosp@m.an@k.nosp@m.de.or.nosp@m.g, Michael Matz matz@.nosp@m.kde..nosp@m.org

Definition at line 46 of file kallocator.h.

Member Typedef Documentation

◆ MemList

typedef TQValueList<MemBlock *> TDEZoneAllocator::MemList
protected

A list of chunks.

Definition at line 115 of file kallocator.h.

Constructor & Destructor Documentation

◆ TDEZoneAllocator()

TDEZoneAllocator::TDEZoneAllocator ( unsigned long  _blockSize = 8*1024)

Creates a TDEZoneAllocator object.

Parameters
_blockSizeSize in bytes of the blocks requested from malloc.

Definition at line 48 of file kallocator.cpp.

◆ ~TDEZoneAllocator()

TDEZoneAllocator::~TDEZoneAllocator ( )

Destructs the ZoneAllocator and free all memory allocated by it.

Definition at line 59 of file kallocator.cpp.

Member Function Documentation

◆ addBlock()

void TDEZoneAllocator::addBlock ( MemBlock *  b)
protected

Add a new memory block to the pool of blocks, and reorganize the hash lists if needed.

Parameters
bblock to add

Definition at line 102 of file kallocator.cpp.

◆ allocate()

void * TDEZoneAllocator::allocate ( size_t  _size)

Allocates a memory block.

Parameters
_sizeSize in bytes of the memory block. Memory is aligned to the size of a pointer.

Definition at line 184 of file kallocator.cpp.

◆ deallocate()

void TDEZoneAllocator::deallocate ( void *  ptr)

Gives back a block returned by allocate() to the zone allocator, and possibly deallocates the block holding it (when it's empty).

The first deallocate() after many allocate() calls (or the first at all) builds an internal data structure for speeding up deallocation. The consistency of that structure is maintained from then on (by allocate() and deallocate()) unless many more objects are allocated without any intervening deallocation, in which case it's thrown away and rebuilt at the next deallocate().

The effect of this is, that such initial deallocate() calls take more time then the normal calls, and that after this list is built, i.e. generally if deallocate() is used at all, also allocate() is a little bit slower. This means, that if you want to squeeze out the last bit performance you would want to use TDEZoneAllocator as an obstack, i.e. just use the functions allocate() and free_since(). All the remaining memory is returned to the system if the zone allocator is destroyed.

Parameters
ptrPointer as returned by allocate().

Definition at line 207 of file kallocator.cpp.

◆ delBlock()

void TDEZoneAllocator::delBlock ( MemBlock *  b)
protected

Delete a memory block.

This really returns the memory to the heap.

Parameters
bblock to delete

Definition at line 148 of file kallocator.cpp.

◆ free_since()

void TDEZoneAllocator::free_since ( void *  ptr)

Deallocate many objects at once.

free_since() deallocates all objects allocated after ptr, including ptr itself.

The intended use is something along the lines of:

TDEZoneAllocator alloc(8192);
void *remember_me = alloc.allocate(0);
for (int i = 0; i < 1000; i++)
do_something_with (alloc.allocate(12));
alloc.free_since (remember_me);
TDEZoneAllocator
Memory allocator for large groups of small objects.
Definition: kallocator.h:47

Note, that we don't need to remember all the pointers to the 12-byte objects for freeing them. The free_since() does deallocate them all at once.

Parameters
ptrPointer as returned by allocate(). It acts like a kind of mark of a certain position in the stack of all objects, off which you can throw away everything above that mark.

Definition at line 240 of file kallocator.cpp.

◆ initHash()

void TDEZoneAllocator::initHash ( )
protected

Reinitialize hash list.

Definition at line 122 of file kallocator.cpp.

◆ insertHash()

void TDEZoneAllocator::insertHash ( MemBlock *  b)
protected

Definition at line 83 of file kallocator.cpp.

Member Data Documentation

◆ blockOffset

unsigned long TDEZoneAllocator::blockOffset
protected

Store offset into current block; size-offset is free.

Definition at line 125 of file kallocator.h.

◆ blockSize

unsigned long TDEZoneAllocator::blockSize
protected

Store block size from constructor.

Definition at line 123 of file kallocator.h.

◆ currentBlock

MemBlock* TDEZoneAllocator::currentBlock
protected

One block is 'current' to satisfy requests.

Definition at line 121 of file kallocator.h.

◆ hashDirty

bool TDEZoneAllocator::hashDirty
protected

Flag the hashes as in need of reorganization.

Definition at line 135 of file kallocator.h.

◆ hashList

MemList** TDEZoneAllocator::hashList
protected

Collection of lists of blocks, for lookups.

Definition at line 131 of file kallocator.h.

◆ hashSize

unsigned int TDEZoneAllocator::hashSize
protected

Count of hashes.

Definition at line 133 of file kallocator.h.

◆ log2

unsigned int TDEZoneAllocator::log2
protected

base-2 log of the block size.

Definition at line 127 of file kallocator.h.

◆ num_blocks

unsigned int TDEZoneAllocator::num_blocks
protected

Count total number of allocated blocks.

Definition at line 129 of file kallocator.h.


The documentation for this class was generated from the following files:
  • kallocator.h
  • kallocator.cpp

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.