sxbag_mngr.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  *                                S Y N T A X
00003  *-----------------------------------------------------------------------------
00004  *   Copyright (C) 1972-2008 INRIA (Institut National de Recherche en
00005  *   Informatique et Automatique)
00006  *-----------------------------------------------------------------------------
00007  *   URL: http://syntax.gforge.inria.fr
00008  *-----------------------------------------------------------------------------
00009  *   The source code of SYNTAX is distributed with two different licenses,
00010  *   depending on the files:
00011  *   - The recursive content of src/ and incl/ and the non-recursive content
00012  *     of SYNTAX's root directory are distributed under the CeCILL-C license
00013  *   - The recursive content of all other repertories is distributed under
00014  *     the CeCILL license
00015  *   All code produced by SYNTAX must be considered as being under the
00016  *   CeCILL-C license. Information about the CeCILL and CeCILL-C licenses
00017  *   can be found at, e.g., http://www.cecill.fr
00018  *****************************************************************************/
00019 
00020 
00021 #include "sxversion.h"
00022 #include "sxcommon.h"
00023 #include "bag.h"
00024 
00025 char WHAT_SXBAG_MNGR[] = "@(#)SYNTAX - $Id: sxbag_mngr.c 1429 2008-07-03 14:28:41Z rlacroix $" WHAT_DEBUG;
00026 
00027 void
00028 bag_alloc (bag_header *pbag, char *name, SXINT size)
00029 {
00030     pbag->name = name;
00031     pbag->hd_top = 0;
00032     pbag->hd_high = 0;
00033     pbag->hd_size = 1;
00034     pbag->pool_size = size;
00035     pbag->hd = (struct bag_disp_hd*) sxalloc (pbag->hd_size, sizeof (struct bag_disp_hd));
00036     pbag->pool_top = pbag->hd [0].set = (SXBA_ELT*) sxcalloc ((SXUINT)pbag->pool_size + 2, sizeof (SXBA_ELT));
00037     pbag->hd [0].size = pbag->pool_size;
00038     pbag->room = pbag->pool_size;
00039 
00040 #if EBUG
00041     pbag->total_size = pbag->pool_size + 2;
00042     pbag->used_size = pbag->prev_used_size = 0;
00043 #endif
00044 }
00045 
00046 
00047 
00048 
00049 SXBA
00050 bag_get (bag_header *pbag, ssize_t size)
00051 {
00052     SXBA_INDEX  slice_nb = SXNBLONGS (size) + 1;
00053     SXBA    set;
00054 
00055     if (slice_nb > pbag->room)
00056     {
00057     if (++pbag->hd_top >= pbag->hd_size)
00058         pbag->hd = (struct bag_disp_hd*) sxrealloc (pbag->hd,
00059                             pbag->hd_size *= 2,
00060                             sizeof (struct bag_disp_hd));
00061 
00062     if (pbag->hd_top <= pbag->hd_high) {
00063         pbag->pool_size = pbag->hd [pbag->hd_top].size;
00064 
00065         while (slice_nb > (pbag->room = pbag->pool_size)) {
00066         if (++pbag->hd_top >  pbag->hd_high)
00067             break;
00068 
00069         pbag->pool_size = pbag->hd [pbag->hd_top].size;
00070         }
00071     }
00072 
00073     if (pbag->hd_top > pbag->hd_high) {     
00074         while (slice_nb > (pbag->room = pbag->pool_size))
00075         pbag->pool_size *= 2;
00076 
00077 #if EBUG
00078         printf ("Bag %s: New bag of size %li is created.\n", pbag->name, (long)pbag->pool_size);
00079         pbag->total_size += pbag->pool_size + 2;
00080 #endif
00081 
00082         pbag->hd_high = pbag->hd_top;
00083         pbag->hd [pbag->hd_top].set = (SXBA) sxcalloc ((SXUINT)pbag->pool_size + 2, sizeof (SXBA_ELT));
00084         pbag->hd [pbag->hd_top].size = pbag->pool_size;
00085     }
00086 
00087     pbag->pool_top = pbag->hd [pbag->hd_top].set;
00088     }
00089 
00090     *(set = pbag->pool_top) = size;
00091     pbag->pool_top += slice_nb;
00092     pbag->room -= slice_nb;
00093 
00094 #if EBUG
00095     pbag->used_size += slice_nb;
00096 #endif
00097 
00098     return set;
00099 }
00100 
00101 void
00102 bag_free (bag_header *pbag)
00103 {
00104 #if EBUG
00105     printf ("Bag %s: used_size = %lu bytes, total_size = %lu bytes\n",
00106         pbag->name,
00107         (SXUINT)((pbag->used_size > pbag->prev_used_size ? pbag->used_size : pbag->prev_used_size) * sizeof (SXBA_ELT) + (pbag->hd_high + 1) * sizeof (struct bag_disp_hd)),
00108             (SXUINT)(pbag->total_size * sizeof (SXBA_ELT) + pbag->hd_size * sizeof (struct bag_disp_hd)));
00109 #endif
00110 
00111     do {
00112     sxfree (pbag->hd [pbag->hd_high].set);
00113     } while (--pbag->hd_high >= 0);
00114 
00115     sxfree (pbag->hd);
00116 }
00117     
00118 
00119 void
00120 bag_clear (bag_header *pbag)
00121 {
00122     /* On suppose que les SXBA sont empty. */
00123     pbag->hd_top = 0;
00124     pbag->pool_top = pbag->hd [0].set;
00125     pbag->pool_size = pbag->hd [0].size;
00126     pbag->room = pbag->pool_size;
00127 
00128 #if EBUG
00129     if (pbag->prev_used_size < pbag->used_size)
00130     pbag->prev_used_size = pbag->used_size;
00131 
00132     pbag->used_size = 0;
00133 #endif
00134 }
00135     

Generated on Wed Apr 21 16:39:34 2010 for syntax-6.0b7 by  doxygen 1.6.1