\doxysection{listelem\+\_\+alloc.\+c}
\label{listelem__alloc_8c_source}\index{src/libsphinxbase/util/listelem\_alloc.c@{src/libsphinxbase/util/listelem\_alloc.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999-\/2004\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00039\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{00040\ }
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/listelem\_alloc.h"{}}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/glist.h"{}}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00065\ \textcolor{keyword}{struct\ }listelem\_alloc\_s\ \{}
\DoxyCodeLine{00066\ \ \ \ \ \textcolor{keywordtype}{char}\ **freelist;\ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00067\ \ \ \ \ glist\_t\ blocks;\ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00068\ \ \ \ \ glist\_t\ blocksize;\ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ elemsize;\ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ blk\_alloc;\ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ n\_blocks;}
\DoxyCodeLine{00072\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ n\_alloc;}
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ n\_freed;}
\DoxyCodeLine{00074\ \};}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ \textcolor{preprocessor}{\#define\ MIN\_ALLOC\ \ \ \ \ \ \ 50\ \ \ \ \ \ }}
\DoxyCodeLine{00077\ \textcolor{preprocessor}{\#define\ BLKID\_SHIFT\ \ \ \ \ 16\ \ \ \ \ \ }}
\DoxyCodeLine{00078\ \textcolor{preprocessor}{\#define\ BLKID\_MASK\ ((1<<BLKID\_SHIFT)-\/1)}}
\DoxyCodeLine{00079\ }
\DoxyCodeLine{00083\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ listelem\_add\_block(listelem\_alloc\_t\ *list,}
\DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *caller\_file,\ \textcolor{keywordtype}{int}\ caller\_line);}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ listelem\_alloc\_t\ *}
\DoxyCodeLine{00087\ listelem\_alloc\_init(\textcolor{keywordtype}{size\_t}\ elemsize)}
\DoxyCodeLine{00088\ \{}
\DoxyCodeLine{00089\ \ \ \ \ listelem\_alloc\_t\ *list;}
\DoxyCodeLine{00090\ }
\DoxyCodeLine{00091\ \ \ \ \ \textcolor{keywordflow}{if}\ ((elemsize\ \%\ \textcolor{keyword}{sizeof}(\textcolor{keywordtype}{void}\ *))\ !=\ 0)\ \{}
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ rounded\ =\ (elemsize\ +\ \textcolor{keyword}{sizeof}(\textcolor{keywordtype}{void}\ *)\ -\/\ 1)\ \&\ \string~(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{void}\ *)-\/1);}
\DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ E\_WARN}
\DoxyCodeLine{00094\ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}List\ item\ size\ (\%lu)\ not\ multiple\ of\ sizeof(void\ *),\ rounding\ to\ \%lu\(\backslash\)n"{}},}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})elemsize,}
\DoxyCodeLine{00096\ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})rounded);}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ elemsize\ =\ rounded;}
\DoxyCodeLine{00098\ \ \ \ \ \}}
\DoxyCodeLine{00099\ \ \ \ \ list\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*list));}
\DoxyCodeLine{00100\ \ \ \ \ list-\/>freelist\ =\ NULL;}
\DoxyCodeLine{00101\ \ \ \ \ list-\/>blocks\ =\ NULL;}
\DoxyCodeLine{00102\ \ \ \ \ list-\/>elemsize\ =\ elemsize;}
\DoxyCodeLine{00103\ \ \ \ \ \textcolor{comment}{/*\ Intent\ of\ this\ is\ to\ increase\ block\ size\ once\ we\ allocate}}
\DoxyCodeLine{00104\ \textcolor{comment}{\ \ \ \ \ *\ 256KiB\ (i.e.\ 1<<18).\ If\ somehow\ the\ element\ size\ is\ big\ enough}}
\DoxyCodeLine{00105\ \textcolor{comment}{\ \ \ \ \ *\ to\ overflow\ that,\ just\ fail,\ people\ should\ use\ malloc\ anyway.\ */}}
\DoxyCodeLine{00106\ \ \ \ \ list-\/>blk\_alloc\ =\ (1\ <<\ 18)\ /\ (MIN\_ALLOC\ *\ elemsize);}
\DoxyCodeLine{00107\ \ \ \ \ \textcolor{keywordflow}{if}\ (list-\/>blk\_alloc\ <=\ 0)\ \{}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Element\ size\ *\ block\ size\ exceeds\ 256k,\ use\ malloc\ instead.\(\backslash\)n"{}});}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ ckd\_free(list);}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00111\ \ \ \ \ \}}
\DoxyCodeLine{00112\ \ \ \ \ list-\/>n\_alloc\ =\ 0;}
\DoxyCodeLine{00113\ \ \ \ \ list-\/>n\_freed\ =\ 0;}
\DoxyCodeLine{00114\ }
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{comment}{/*\ Allocate\ an\ initial\ block\ to\ minimize\ latency.\ */}}
\DoxyCodeLine{00116\ \ \ \ \ listelem\_add\_block(list,\ \_\_FILE\_\_,\ \_\_LINE\_\_);}
\DoxyCodeLine{00117\ \ \ \ \ \textcolor{keywordflow}{return}\ list;}
\DoxyCodeLine{00118\ \}}
\DoxyCodeLine{00119\ }
\DoxyCodeLine{00120\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00121\ listelem\_alloc\_free(listelem\_alloc\_t\ *list)}
\DoxyCodeLine{00122\ \{}
\DoxyCodeLine{00123\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00124\ \ \ \ \ \textcolor{keywordflow}{if}\ (list\ ==\ NULL)}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00126\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ list-\/>blocks;\ gn;\ gn\ =\ gnode\_next(gn))}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ ckd\_free(gnode\_ptr(gn));}
\DoxyCodeLine{00128\ \ \ \ \ glist\_free(list-\/>blocks);}
\DoxyCodeLine{00129\ \ \ \ \ glist\_free(list-\/>blocksize);}
\DoxyCodeLine{00130\ \ \ \ \ ckd\_free(list);}
\DoxyCodeLine{00131\ \}}
\DoxyCodeLine{00132\ }
\DoxyCodeLine{00133\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00134\ listelem\_add\_block(listelem\_alloc\_t\ *list,\ \textcolor{keywordtype}{char}\ *caller\_file,\ \textcolor{keywordtype}{int}\ caller\_line)}
\DoxyCodeLine{00135\ \{}
\DoxyCodeLine{00136\ \ \ \ \ \textcolor{keywordtype}{char}\ **cpp,\ *cp;}
\DoxyCodeLine{00137\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ j;}
\DoxyCodeLine{00138\ \ \ \ \ int32\ blocksize;}
\DoxyCodeLine{00139\ }
\DoxyCodeLine{00140\ \ \ \ \ blocksize\ =\ list-\/>blocksize\ ?\ gnode\_int32(list-\/>blocksize)\ :\ MIN\_ALLOC;}
\DoxyCodeLine{00141\ \ \ \ \ \textcolor{comment}{/*\ Check\ if\ block\ size\ should\ be\ increased\ (if\ many\ requests\ for\ this\ size)\ */}}
\DoxyCodeLine{00142\ \ \ \ \ \textcolor{keywordflow}{if}\ (list-\/>blk\_alloc\ ==\ 0)\ \{}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ See\ above.\ \ No\ sense\ in\ allocating\ blocks\ bigger\ than}}
\DoxyCodeLine{00144\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ 256KiB\ (well,\ actually,\ there\ might\ be,\ but\ we'll\ worry}}
\DoxyCodeLine{00145\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ about\ that\ later).\ */}}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ blocksize\ <<=\ 1;}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (blocksize\ *\ list-\/>elemsize\ >\ (1\ <<\ 18))}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ blocksize\ =\ (1\ <<\ 18)\ /\ list-\/>elemsize;}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ list-\/>blk\_alloc\ =\ (1\ <<\ 18)\ /\ (blocksize\ *\ list-\/>elemsize);}
\DoxyCodeLine{00150\ \ \ \ \ \}}
\DoxyCodeLine{00151\ }
\DoxyCodeLine{00152\ \ \ \ \ \textcolor{comment}{/*\ Allocate\ block\ */}}
\DoxyCodeLine{00153\ \ \ \ \ cpp\ =\ list-\/>freelist\ =}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{char}\ **)\ \_\_ckd\_calloc\_\_(blocksize,\ list-\/>elemsize,}
\DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ caller\_file,\ caller\_line);}
\DoxyCodeLine{00156\ \ \ \ \ list-\/>blocks\ =\ glist\_add\_ptr(list-\/>blocks,\ cpp);}
\DoxyCodeLine{00157\ \ \ \ \ list-\/>blocksize\ =\ glist\_add\_int32(list-\/>blocksize,\ blocksize);}
\DoxyCodeLine{00158\ \ \ \ \ cp\ =\ (\textcolor{keywordtype}{char}\ *)\ cpp;}
\DoxyCodeLine{00159\ \ \ \ \ \textcolor{comment}{/*\ Link\ up\ the\ blocks\ via\ their\ first\ machine\ word.\ */}}
\DoxyCodeLine{00160\ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ blocksize\ -\/\ 1;\ j\ >\ 0;\ -\/-\/j)\ \{}
\DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ cp\ +=\ list-\/>elemsize;}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ *cpp\ =\ cp;}
\DoxyCodeLine{00163\ \ \ \ \ \ \ \ \ cpp\ =\ (\textcolor{keywordtype}{char}\ **)\ cp;}
\DoxyCodeLine{00164\ \ \ \ \ \}}
\DoxyCodeLine{00165\ \ \ \ \ \textcolor{comment}{/*\ Make\ sure\ the\ last\ element's\ forward\ pointer\ is\ NULL\ */}}
\DoxyCodeLine{00166\ \ \ \ \ *cpp\ =\ NULL;}
\DoxyCodeLine{00167\ \ \ \ \ -\/-\/list-\/>blk\_alloc;}
\DoxyCodeLine{00168\ \ \ \ \ ++list-\/>n\_blocks;}
\DoxyCodeLine{00169\ \}}
\DoxyCodeLine{00170\ }
\DoxyCodeLine{00171\ }
\DoxyCodeLine{00172\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00173\ \_\_listelem\_malloc\_\_(listelem\_alloc\_t\ *list,\ \textcolor{keywordtype}{char}\ *caller\_file,\ \textcolor{keywordtype}{int}\ caller\_line)}
\DoxyCodeLine{00174\ \{}
\DoxyCodeLine{00175\ \ \ \ \ \textcolor{keywordtype}{char}\ **ptr;}
\DoxyCodeLine{00176\ }
\DoxyCodeLine{00177\ \ \ \ \ \textcolor{comment}{/*\ Allocate\ a\ new\ block\ if\ list\ empty\ */}}
\DoxyCodeLine{00178\ \ \ \ \ \textcolor{keywordflow}{if}\ (list-\/>freelist\ ==\ NULL)}
\DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ listelem\_add\_block(list,\ caller\_file,\ caller\_line);}
\DoxyCodeLine{00180\ }
\DoxyCodeLine{00181\ \ \ \ \ \textcolor{comment}{/*\ Unlink\ and\ return\ first\ element\ in\ freelist\ */}}
\DoxyCodeLine{00182\ \ \ \ \ ptr\ =\ list-\/>freelist;}
\DoxyCodeLine{00183\ \ \ \ \ list-\/>freelist\ =\ (\textcolor{keywordtype}{char}\ **)\ (*(list-\/>freelist));}
\DoxyCodeLine{00184\ \ \ \ \ (list-\/>n\_alloc)++;}
\DoxyCodeLine{00185\ }
\DoxyCodeLine{00186\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{void}\ *)ptr;}
\DoxyCodeLine{00187\ \}}
\DoxyCodeLine{00188\ }
\DoxyCodeLine{00189\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00190\ \_\_listelem\_malloc\_id\_\_(listelem\_alloc\_t\ *list,\ \textcolor{keywordtype}{char}\ *caller\_file,}
\DoxyCodeLine{00191\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ caller\_line,\ int32\ *out\_id)}
\DoxyCodeLine{00192\ \{}
\DoxyCodeLine{00193\ \ \ \ \ \textcolor{keywordtype}{char}\ **ptr;}
\DoxyCodeLine{00194\ }
\DoxyCodeLine{00195\ \ \ \ \ \textcolor{comment}{/*\ Allocate\ a\ new\ block\ if\ list\ empty\ */}}
\DoxyCodeLine{00196\ \ \ \ \ \textcolor{keywordflow}{if}\ (list-\/>freelist\ ==\ NULL)}
\DoxyCodeLine{00197\ \ \ \ \ \ \ \ \ listelem\_add\_block(list,\ caller\_file,\ caller\_line);}
\DoxyCodeLine{00198\ }
\DoxyCodeLine{00199\ \ \ \ \ \textcolor{comment}{/*\ Unlink\ and\ return\ first\ element\ in\ freelist\ */}}
\DoxyCodeLine{00200\ \ \ \ \ ptr\ =\ list-\/>freelist;}
\DoxyCodeLine{00201\ \ \ \ \ list-\/>freelist\ =\ (\textcolor{keywordtype}{char}\ **)\ (*(list-\/>freelist));}
\DoxyCodeLine{00202\ \ \ \ \ (list-\/>n\_alloc)++;}
\DoxyCodeLine{00203\ }
\DoxyCodeLine{00204\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_id)\ \{}
\DoxyCodeLine{00205\ \ \ \ \ \ \ \ \ int32\ blksize,\ blkidx,\ ptridx;}
\DoxyCodeLine{00206\ \ \ \ \ \ \ \ \ gnode\_t\ *gn,\ *gn2;}
\DoxyCodeLine{00207\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ **block;}
\DoxyCodeLine{00208\ }
\DoxyCodeLine{00209\ \ \ \ \ \ \ \ \ gn2\ =\ list-\/>blocksize;}
\DoxyCodeLine{00210\ \ \ \ \ \ \ \ \ block\ =\ NULL;}
\DoxyCodeLine{00211\ \ \ \ \ \ \ \ \ blkidx\ =\ 0;}
\DoxyCodeLine{00212\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ list-\/>blocks;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00213\ \ \ \ \ \ \ \ \ \ \ \ \ block\ =\ gnode\_ptr(gn);}
\DoxyCodeLine{00214\ \ \ \ \ \ \ \ \ \ \ \ \ blksize\ =\ gnode\_int32(gn2)\ *\ list-\/>elemsize\ /\ \textcolor{keyword}{sizeof}(*block);}
\DoxyCodeLine{00215\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (ptr\ >=\ block\ \&\&\ ptr\ <\ block\ +\ blksize)}
\DoxyCodeLine{00216\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00217\ \ \ \ \ \ \ \ \ \ \ \ \ gn2\ =\ gnode\_next(gn2);}
\DoxyCodeLine{00218\ \ \ \ \ \ \ \ \ \ \ \ \ ++blkidx;}
\DoxyCodeLine{00219\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00220\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (gn\ ==\ NULL)\ \{}
\DoxyCodeLine{00221\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ find\ block\ index\ for\ pointer\ \%p!\(\backslash\)n"{}},\ ptr);}
\DoxyCodeLine{00222\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00223\ \ \ \ \ \ \ \ \ ptridx\ =\ (ptr\ -\/\ block)\ /\ (list-\/>elemsize\ /\ \textcolor{keyword}{sizeof}(*block));}
\DoxyCodeLine{00224\ \ \ \ \ \ \ \ \ E\_DEBUG(4,(\textcolor{stringliteral}{"{}ptr\ \%p\ block\ \%p\ blkidx\ \%d\ ptridx\ \%d\(\backslash\)n"{}},}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ptr,\ block,\ list-\/>n\_blocks\ -\/\ blkidx\ -\/\ 1,\ ptridx));}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ *out\_id\ =\ ((list-\/>n\_blocks\ -\/\ blkidx\ -\/\ 1)\ <<\ BLKID\_SHIFT)\ |\ ptridx;}
\DoxyCodeLine{00227\ \ \ \ \ \}}
\DoxyCodeLine{00228\ }
\DoxyCodeLine{00229\ \ \ \ \ \textcolor{keywordflow}{return}\ ptr;}
\DoxyCodeLine{00230\ \}}
\DoxyCodeLine{00231\ }
\DoxyCodeLine{00232\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00233\ listelem\_get\_item(listelem\_alloc\_t\ *list,\ int32\ \textcolor{keywordtype}{id})}
\DoxyCodeLine{00234\ \{}
\DoxyCodeLine{00235\ \ \ \ \ int32\ blkidx,\ ptridx,\ i;}
\DoxyCodeLine{00236\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00237\ }
\DoxyCodeLine{00238\ \ \ \ \ blkidx\ =\ (\textcolor{keywordtype}{id}\ >>\ BLKID\_SHIFT)\ \&\ BLKID\_MASK;}
\DoxyCodeLine{00239\ \ \ \ \ ptridx\ =\ \textcolor{keywordtype}{id}\ \&\ BLKID\_MASK;}
\DoxyCodeLine{00240\ }
\DoxyCodeLine{00241\ \ \ \ \ i\ =\ 0;}
\DoxyCodeLine{00242\ \ \ \ \ blkidx\ =\ list-\/>n\_blocks\ -\/\ blkidx;}
\DoxyCodeLine{00243\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ list-\/>blocks;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (++i\ ==\ blkidx)}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00246\ \ \ \ \ \}}
\DoxyCodeLine{00247\ \ \ \ \ \textcolor{keywordflow}{if}\ (gn\ ==\ NULL)\ \{}
\DoxyCodeLine{00248\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ find\ block\ index\ \%d\(\backslash\)n"{}},\ blkidx);}
\DoxyCodeLine{00249\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00250\ \ \ \ \ \}}
\DoxyCodeLine{00251\ }
\DoxyCodeLine{00252\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{void}\ *)((\textcolor{keywordtype}{char}\ **)gnode\_ptr(gn)}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ +\ ptridx\ *\ (list-\/>elemsize\ /\ \textcolor{keyword}{sizeof}(\textcolor{keywordtype}{void}\ *)));}
\DoxyCodeLine{00254\ \}}
\DoxyCodeLine{00255\ }
\DoxyCodeLine{00256\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00257\ \_\_listelem\_free\_\_(listelem\_alloc\_t\ *list,\ \textcolor{keywordtype}{void}\ *elem,}
\DoxyCodeLine{00258\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *caller\_file,\ \textcolor{keywordtype}{int}\ caller\_line)}
\DoxyCodeLine{00259\ \{}
\DoxyCodeLine{00260\ \ \ \ \ \textcolor{keywordtype}{char}\ **cpp;}
\DoxyCodeLine{00261\ }
\DoxyCodeLine{00262\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{00263\ \textcolor{comment}{\ \ \ \ \ *\ Insert\ freed\ item\ at\ head\ of\ list.}}
\DoxyCodeLine{00264\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{00265\ \ \ \ \ cpp\ =\ (\textcolor{keywordtype}{char}\ **)\ elem;}
\DoxyCodeLine{00266\ \ \ \ \ *cpp\ =\ (\textcolor{keywordtype}{char}\ *)\ list-\/>freelist;}
\DoxyCodeLine{00267\ \ \ \ \ list-\/>freelist\ =\ cpp;}
\DoxyCodeLine{00268\ \ \ \ \ (list-\/>n\_freed)++;}
\DoxyCodeLine{00269\ \}}
\DoxyCodeLine{00270\ }
\DoxyCodeLine{00271\ }
\DoxyCodeLine{00272\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00273\ listelem\_stats(listelem\_alloc\_t\ *list)}
\DoxyCodeLine{00274\ \{}
\DoxyCodeLine{00275\ \ \ \ \ gnode\_t\ *gn,\ *gn2;}
\DoxyCodeLine{00276\ \ \ \ \ \textcolor{keywordtype}{char}\ **cpp;}
\DoxyCodeLine{00277\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ n;}
\DoxyCodeLine{00278\ }
\DoxyCodeLine{00279\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Linklist\ stats:\(\backslash\)n"{}});}
\DoxyCodeLine{00280\ \ \ \ \ \textcolor{keywordflow}{for}\ (n\ =\ 0,\ cpp\ =\ list-\/>freelist;\ cpp;}
\DoxyCodeLine{00281\ \ \ \ \ \ \ \ \ \ cpp\ =\ (\textcolor{keywordtype}{char}\ **)\ (*cpp),\ n++);}
\DoxyCodeLine{00282\ \ \ \ \ E\_INFO}
\DoxyCodeLine{00283\ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}elemsize\ \%lu,\ \#alloc\ \%lu,\ \#freed\ \%lu,\ \#freelist\ \%lu\(\backslash\)n"{}},}
\DoxyCodeLine{00284\ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})list-\/>elemsize,}
\DoxyCodeLine{00285\ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})list-\/>n\_alloc,}
\DoxyCodeLine{00286\ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})list-\/>n\_freed,}
\DoxyCodeLine{00287\ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})n);}
\DoxyCodeLine{00288\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Allocated\ blocks:\(\backslash\)n"{}});}
\DoxyCodeLine{00289\ \ \ \ \ gn2\ =\ list-\/>blocksize;}
\DoxyCodeLine{00290\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ list-\/>blocks;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00291\ \ \ \ \ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}\%p\ (\%d\ *\ \%d\ bytes)\(\backslash\)n"{}},\ gnode\_ptr(gn),\ gnode\_int32(gn2),\ list-\/>elemsize);}
\DoxyCodeLine{00292\ \ \ \ \ \ \ \ \ gn2\ =\ gnode\_next(gn2);}
\DoxyCodeLine{00293\ \ \ \ \ \}}
\DoxyCodeLine{00294\ \}}

\end{DoxyCode}
