Main Page   Class Hierarchy   Compound List   File List   Compound Members  

lppool.h

00001 /*
00002     Copyright (C) 1998 by Jorrit Tyberghein
00003   
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008   
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013   
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_LPPOOL_H__
00020 #define __CS_LPPOOL_H__
00021 
00022 #include "csengine/light.h"
00023 
00030 class csLightPatchPool
00031 {
00032 private:
00033   struct PoolObj
00034   {
00035     PoolObj* next;
00036     csLightPatch* lp;
00037   };
00039   PoolObj* alloced;
00041   PoolObj* freed;
00042 
00043 public:
00045   csLightPatchPool () : alloced (NULL), freed (NULL) { }
00046 
00048   ~csLightPatchPool ()
00049   {
00050     while (alloced)
00051     {
00052       PoolObj* n = alloced->next;
00053       //delete alloced->lp; @@@ This free is not valid!
00054       // We should use a ref count on the pool itself so that we
00055       // now when all objects in the pool are freed and the
00056       // 'alloced' list will be empty.
00057       delete alloced;
00058       alloced = n;
00059     }
00060     while (freed)
00061     {
00062       PoolObj* n = freed->next;
00063       delete freed->lp;
00064       delete freed;
00065       freed = n;
00066     }
00067   }
00068 
00070   csLightPatch* Alloc ()
00071   {
00072     PoolObj* pnew;
00073     if (freed)
00074     {
00075       pnew = freed;
00076       freed = freed->next;
00077     }
00078     else
00079     {
00080       pnew = new PoolObj ();
00081       pnew->lp = new csLightPatch ();
00082     }
00083     pnew->next = alloced;
00084     alloced = pnew;
00085     return pnew->lp;
00086   }
00087 
00093   void Free (csLightPatch* lp)
00094   {
00095     lp->RemovePatch ();
00096     if (alloced)
00097     {
00098       PoolObj* po = alloced;
00099       alloced = alloced->next;
00100       po->lp = lp;
00101       po->next = freed;
00102       freed = po;
00103     }
00104     else
00105     {
00106       // Cannot happen!
00107       CS_ASSERT (false);
00108     }
00109   }
00110 
00112   void Dump ()
00113   {
00114     int cnt;
00115     cnt = 0;
00116     PoolObj* po = alloced;
00117     while (po) { cnt++; po = po->next; }
00118     printf ("LightPatch pool: %d allocated, ", cnt);
00119     cnt = 0;
00120     po = freed;
00121     while (po) { cnt++; po = po->next; }
00122     printf ("%d freed.\n", cnt);
00123   }
00124 };
00125 
00126 #endif // __CS_LPPOOL_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000