eg_memslab.ex.c

00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /** @file
00021  * @ingroup EGmemSlab
00022  * */
00023 /** @addtogroup EGmemSlab */
00024 /** @{ */
00025 #include "eg_memslab.h"
00026 #include <stdio.h>
00027 #include "eg_eheap.h"
00028 /* ========================================================================= */
00029 /** brief simple destructor for heap connectors */
00030 static void my_constr (void *ptr)
00031 {
00032   EGeHeapCnInit ((EGeHeapCn_t *) ptr);
00033 }
00034 
00035 /* ========================================================================= */
00036 /** @brief simple destructor for heap connectors */
00037 static void my_dest (void *ptr)
00038 {
00039   EGeHeapCnClear ((EGeHeapCn_t *) ptr);
00040   ptr = 0;
00041 }
00042 
00043 /* ========================================================================= */
00044 /** @brief this program expect only one parameter, the number of elements to be
00045  * created. */
00046 int main (int argc,
00047           char **argv)
00048 {
00049   unsigned int n_elem = 0;
00050   unsigned int cnt = 0;
00051   EGeHeap_t my_heap;
00052   EGeHeapCn_t *hp_cn;
00053   EGmemSlabPool_t pool;
00054   double dbl = 0;
00055   /* parsing input */
00056   srandom (1);
00057   if (argc != 2)
00058   {
00059   USAGE:
00060     fprintf (stderr, "Usage: %s N\n\twhere N is the number of elements to "
00061              "create randomly", argv[0]);
00062     return 1;
00063   }
00064   n_elem = atoi (argv[1]);
00065   if (!n_elem)
00066     goto USAGE;
00067   /* initializing internals */
00068   EGmemSlabPoolInit (&pool, sizeof (EGeHeapCn_t), my_constr, my_dest);
00069   EGeHeapInit (&my_heap);
00070   EGeHeapChangeD (&my_heap, 4);
00071   /* rounds of 1000, create 1000, delete 875 smallers */
00072   while (n_elem >= 1000)
00073   {
00074     for (cnt = 1000; cnt--;)
00075     {
00076       if (EGeHeapIsFull (&my_heap))
00077         EGeHeapResize (&my_heap, 1000 + my_heap.sz);
00078       hp_cn = (EGeHeapCn_t *) EGmemSlabPoolAlloc (&pool);
00079       dbl = random ();
00080       dbl /= EGRAND_MAX;
00081       EGlpNumSet (hp_cn->val, dbl);
00082       EGeHeapAdd (&my_heap, hp_cn);
00083     }
00084     for (cnt = 875; cnt--;)
00085     {
00086       hp_cn = EGeHeapGetMin (&my_heap);
00087       EGeHeapDel (&my_heap, hp_cn);
00088       EGmemSlabPoolFree (hp_cn);
00089     }
00090     n_elem -= 1000;
00091   }
00092   /* last tail, add the elements on the heap */
00093   while (n_elem--)
00094   {
00095     if (EGeHeapIsFull (&my_heap))
00096       EGeHeapResize (&my_heap, 1000 + my_heap.sz);
00097     hp_cn = (EGeHeapCn_t *) EGmemSlabPoolAlloc (&pool);
00098     dbl = random ();
00099     dbl /= EGRAND_MAX;
00100     EGlpNumSet (hp_cn->val, dbl);
00101     EGeHeapAdd (&my_heap, hp_cn);
00102   }
00103   /* now we free the remaining elements in batchs of 1000, and then shrink the
00104    * memory pool */
00105   while (my_heap.sz)
00106   {
00107     for (cnt = 1000; cnt-- && my_heap.sz;)
00108     {
00109       hp_cn = EGeHeapGetMin (&my_heap);
00110       EGeHeapDel (&my_heap, hp_cn);
00111       EGmemSlabPoolFree (hp_cn);
00112     }
00113     EGmemSlabPoolShrink (&pool);
00114   }
00115 
00116   /* ending */
00117   EGeHeapResize (&my_heap, 0);
00118   EGeHeapClear (&my_heap);
00119   EGmemSlabPoolClear (&pool);
00120   return 0;
00121 }
00122 
00123 /* ========================================================================= */
00124 /** @} */

Generated on Mon Jan 30 08:48:52 2006 for EGlib by  doxygen 1.4.5