• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

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

#include <KZoneAllocator>

Public Member Functions

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

Protected Types

typedef QList< MemBlock * > MemList
 

Protected Member Functions

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

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 than 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 47 of file kallocator.h.

Member Typedef Documentation

typedef QList<MemBlock *> KZoneAllocator::MemList
protected

A list of chunks.

Definition at line 114 of file kallocator.h.

Constructor & Destructor Documentation

KZoneAllocator::KZoneAllocator ( unsigned long  _blockSize = 8*1024)
explicit

Creates a KZoneAllocator object.

Parameters
_blockSizeSize in bytes of the blocks requested from malloc.

Definition at line 79 of file kallocator.cpp.

KZoneAllocator::~KZoneAllocator ( )

Destructs the ZoneAllocator and free all memory allocated by it.

Definition at line 92 of file kallocator.cpp.

Member Function Documentation

void KZoneAllocator::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 136 of file kallocator.cpp.

void * KZoneAllocator::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 218 of file kallocator.cpp.

void KZoneAllocator::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 KZoneAllocator 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 241 of file kallocator.cpp.

void KZoneAllocator::delBlock ( MemBlock *  b)
protected

Delete a memory block.

This really returns the memory to the heap.

Parameters
bblock to delete

Definition at line 182 of file kallocator.cpp.

void KZoneAllocator::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:

KZoneAllocator 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);

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 274 of file kallocator.cpp.

void KZoneAllocator::initHash ( )
protected

Reinitialize hash list.

Definition at line 156 of file kallocator.cpp.

void KZoneAllocator::insertHash ( MemBlock *  b)
protected

Definition at line 117 of file kallocator.cpp.


The documentation for this class was generated from the following files:
  • kallocator.h
  • kallocator.cpp
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal