00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "eg_memslab.h"
00026 #include <stdio.h>
00027 #include "eg_eheap.h"
00028
00029
00030 static void my_constr (void *ptr)
00031 {
00032 EGeHeapCnInit ((EGeHeapCn_t *) ptr);
00033 }
00034
00035
00036
00037 static void my_dest (void *ptr)
00038 {
00039 EGeHeapCnClear ((EGeHeapCn_t *) ptr);
00040 ptr = 0;
00041 }
00042
00043
00044
00045
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
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
00068 EGmemSlabPoolInit (&pool, sizeof (EGeHeapCn_t), my_constr, my_dest);
00069 EGeHeapInit (&my_heap);
00070 EGeHeapChangeD (&my_heap, 4);
00071
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
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
00104
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
00117 EGeHeapResize (&my_heap, 0);
00118 EGeHeapClear (&my_heap);
00119 EGmemSlabPoolClear (&pool);
00120 return 0;
00121 }
00122
00123
00124