\section{KZone\-Allocator Class Reference}
\label{classKZoneAllocator}\index{KZoneAllocator@{KZoneAllocator}}
Memory allocator for large groups of small objects.  


{\tt \#include $<$kallocator.h$>$}

Collaboration diagram for KZone\-Allocator:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=66pt]{classKZoneAllocator__coll__graph}
\end{center}
\end{figure}
\subsection*{Public Member Functions}
\begin{CompactItemize}
\item 
{\bf KZone\-Allocator} (unsigned long \_\-block\-Size=8 $\ast$1024)
\begin{CompactList}\small\item\em Creates a KZone\-Allocator object. \item\end{CompactList}\item 
{\bf $\sim$KZone\-Allocator} ()
\begin{CompactList}\small\item\em Destructs the Zone\-Allocator and free all memory allocated by it. \item\end{CompactList}\item 
void $\ast$ {\bf allocate} (size\_\-t \_\-size)
\begin{CompactList}\small\item\em Allocates a memory block. \item\end{CompactList}\item 
void {\bf deallocate} (void $\ast$ptr)
\begin{CompactList}\small\item\em Gives back a block returned by {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}() to the zone allocator, and possibly deallocates the block holding it (when it's empty). \item\end{CompactList}\item 
void {\bf free\_\-since} (void $\ast$ptr)
\begin{CompactList}\small\item\em Deallocate many objects at once. \item\end{CompactList}\end{CompactItemize}
\subsection*{Protected Types}
\begin{CompactItemize}
\item 
typedef {\bf QValue\-List}$<$ Mem\-Block $\ast$ $>$ {\bf Mem\-List}\label{classKZoneAllocator_x0}

\end{CompactItemize}
\subsection*{Protected Member Functions}
\begin{CompactItemize}
\item 
void {\bf add\-Block} (Mem\-Block $\ast$b)\label{classKZoneAllocator_b0}

\item 
void {\bf del\-Block} (Mem\-Block $\ast$b)\label{classKZoneAllocator_b1}

\item 
void {\bf insert\-Hash} (Mem\-Block $\ast$b)\label{classKZoneAllocator_b2}

\item 
void {\bf init\-Hash} ()\label{classKZoneAllocator_b3}

\end{CompactItemize}
\subsection*{Protected Attributes}
\begin{CompactItemize}
\item 
Mem\-Block $\ast$ {\bf current\-Block}\label{classKZoneAllocator_p0}

\item 
unsigned long {\bf block\-Size}\label{classKZoneAllocator_p1}

\item 
unsigned long {\bf block\-Offset}\label{classKZoneAllocator_p2}

\item 
unsigned int {\bf log2}\label{classKZoneAllocator_p3}

\item 
unsigned int {\bf num\_\-blocks}\label{classKZoneAllocator_p4}

\item 
{\bf Mem\-List} $\ast$$\ast$ {\bf hash\-List}\label{classKZoneAllocator_p5}

\item 
unsigned int {\bf hash\-Size}\label{classKZoneAllocator_p6}

\item 
bool {\bf hash\-Dirty}\label{classKZoneAllocator_p7}

\end{CompactItemize}


\subsection{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. \begin{Desc}
\item[Author:]Waldo Bastian $<${\tt bastian@kde.org}$>$, Michael Matz $<${\tt matz@kde.org}$>$ \end{Desc}
\begin{Desc}
\item[Version:]\begin{Desc}
\item[Id]{\bf kallocator.h}{\rm (p.\,\pageref{kallocator_8h})},v 1.8 2002/09/09 20:22:16 tjansen Exp \end{Desc}
\end{Desc}




Definition at line 47 of file kallocator.h.

\subsection{Constructor \& Destructor Documentation}
\index{KZoneAllocator@{KZone\-Allocator}!KZoneAllocator@{KZoneAllocator}}
\index{KZoneAllocator@{KZoneAllocator}!KZoneAllocator@{KZone\-Allocator}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KZone\-Allocator::KZone\-Allocator (unsigned long {\em \_\-block\-Size} = 8 $\ast$1024)}\label{classKZoneAllocator_a0}


Creates a KZone\-Allocator object. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-block\-Size}]Size in bytes of the blocks requested from malloc. \end{description}
\end{Desc}


Definition at line 48 of file kallocator.cpp.\index{KZoneAllocator@{KZone\-Allocator}!~KZoneAllocator@{$\sim$KZoneAllocator}}
\index{~KZoneAllocator@{$\sim$KZoneAllocator}!KZoneAllocator@{KZone\-Allocator}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KZone\-Allocator::$\sim${\bf KZone\-Allocator} ()}\label{classKZoneAllocator_a1}


Destructs the Zone\-Allocator and free all memory allocated by it. 



Definition at line 59 of file kallocator.cpp.

References KStd\-Action::next().

\subsection{Member Function Documentation}
\index{KZoneAllocator@{KZone\-Allocator}!allocate@{allocate}}
\index{allocate@{allocate}!KZoneAllocator@{KZone\-Allocator}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void $\ast$ KZone\-Allocator::allocate (size\_\-t {\em \_\-size})}\label{classKZoneAllocator_a2}


Allocates a memory block. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em \_\-size}]Size in bytes of the memory block. Memory is aligned to the size of a pointer. \end{description}
\end{Desc}


Definition at line 171 of file kallocator.cpp.\index{KZoneAllocator@{KZone\-Allocator}!deallocate@{deallocate}}
\index{deallocate@{deallocate}!KZoneAllocator@{KZone\-Allocator}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KZone\-Allocator::deallocate (void $\ast$ {\em ptr})}\label{classKZoneAllocator_a3}


Gives back a block returned by {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}() to the zone allocator, and possibly deallocates the block holding it (when it's empty). 

The first {\bf deallocate}{\rm (p.\,\pageref{classKZoneAllocator_a3})}() after many {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}() 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 {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}() and {\bf deallocate}{\rm (p.\,\pageref{classKZoneAllocator_a3})}()) unless many more objects are allocated without any intervening deallocation, in which case it's thrown away and rebuilt at the next {\bf deallocate}{\rm (p.\,\pageref{classKZoneAllocator_a3})}().

The effect of this is, that such initial {\bf deallocate}{\rm (p.\,\pageref{classKZoneAllocator_a3})}() calls take more time then the normal calls, and that after this list is built, i.e. generally if {\bf deallocate}{\rm (p.\,\pageref{classKZoneAllocator_a3})}() is used at all, also {\bf allocate()}{\rm (p.\,\pageref{classKZoneAllocator_a2})} is a little bit slower. This means, that if you want to squeeze out the last bit performance you would want to use KZone\-Allocator as an obstack, i.e. just use the functions {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}() and {\bf free\_\-since}{\rm (p.\,\pageref{classKZoneAllocator_a4})}(). All the remaining memory is returned to the system if the zone allocator is destroyed. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em ptr}]Pointer as returned by {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}(). \end{description}
\end{Desc}


Definition at line 194 of file kallocator.cpp.

References QValue\-List::begin(), QValue\-List::end(), and KStd\-Accel::key().\index{KZoneAllocator@{KZone\-Allocator}!free_since@{free\_\-since}}
\index{free_since@{free\_\-since}!KZoneAllocator@{KZone\-Allocator}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KZone\-Allocator::free\_\-since (void $\ast$ {\em ptr})}\label{classKZoneAllocator_a4}


Deallocate many objects at once. 

{\bf free\_\-since}{\rm (p.\,\pageref{classKZoneAllocator_a4})}() deallocates all objects allocated after {\tt ptr}, {\em including\/} {\tt ptr} itself.

The intended use is something along the lines of: \small\begin{alltt}
 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);
 \end{alltt}\normalsize 
 Note, that we don't need to remember all the pointers to the 12-byte objects for freeing them. The {\bf free\_\-since}{\rm (p.\,\pageref{classKZoneAllocator_a4})}() does deallocate them all at once. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em ptr}]Pointer as returned by {\bf allocate}{\rm (p.\,\pageref{classKZoneAllocator_a2})}(). 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. \end{description}
\end{Desc}


Definition at line 227 of file kallocator.cpp.

The documentation for this class was generated from the following files:\begin{CompactItemize}
\item 
kallocator.h\item 
kallocator.cpp\end{CompactItemize}
