sxba_bag_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 /* 
00022    Gestionnaire de "reservoir" de SXBA 
00023 */
00024 
00025 
00026 
00027 
00028 #include "sxversion.h"
00029 #include "sxcommon.h"
00030 #include "sxba_bag.h"
00031 
00032 char WHAT_SXBA_BAG_MNGR[] = "@(#)SYNTAX - $Id: sxba_bag_mngr.c 1550 2008-09-03 11:39:24Z sagot $" WHAT_DEBUG;
00033 
00034 /* BAG_mngr */
00035 void
00036 sxba_bag_alloc (bag_header *pbag, char *name, SXBA_INDEX size, FILE *file_name)
00037 {
00038 
00039   pbag->name = name;
00040   pbag->hd_top = 0;
00041   pbag->hd_high = 0;
00042   pbag->hd_size = 1;
00043 
00044   if (size > MAX_SXBA_BAG_SIZE) {
00045 #if EBUG
00046     fprintf (stderr, "### WARNING: sxba_bag_alloc refuses to allocate sxba_bags greater than %ld. Your sxba_bag of size %ld has been reduced to this size.\n", MAX_SXBA_BAG_SIZE, size);
00047 #endif /* EBUG */
00048     size = MAX_SXBA_BAG_SIZE;
00049   }
00050 
00051   pbag->pool_size = size;
00052   pbag->hd = (struct bag_disp_hd*) sxalloc (pbag->hd_size, sizeof (struct bag_disp_hd));
00053   /* Pour pouvoir faire sxba_empty sur bag_clear */
00054   pbag->pool_top = (pbag->hd [0].set = sxba_calloc ((pbag->pool_size + 3)<<SXSHIFT_AMOUNT))+1;
00055   pbag->hd [0].size = pbag->pool_size;
00056   pbag->room = pbag->pool_size;
00057 
00058   if ((pbag->file_name = file_name)) {
00059     pbag->total_size = pbag->pool_size + 3;
00060     pbag->used_size = pbag->prev_used_size = 0;
00061   }
00062 }
00063 
00064 
00065 
00066 SXBA
00067 sxba_bag_get (bag_header *pbag, SXBA_INDEX bits_number)
00068 {
00069   SXBA_INDEX    slice_nb = SXNBLONGS (bits_number) + 1;
00070   SXBA  set;
00071 
00072   if (slice_nb > pbag->room) {
00073     if (++pbag->hd_top >= pbag->hd_size)
00074       pbag->hd = (struct bag_disp_hd*) sxrealloc (pbag->hd,
00075                           pbag->hd_size *= 2,
00076                           sizeof (struct bag_disp_hd));
00077 
00078     if (pbag->hd_top <= pbag->hd_high) {
00079       pbag->pool_size = pbag->hd [pbag->hd_top].size;
00080 
00081       while (slice_nb > (pbag->room = pbag->pool_size)) {
00082     if (++pbag->hd_top >  pbag->hd_high)
00083       break;
00084 
00085     pbag->pool_size = pbag->hd [pbag->hd_top].size;
00086       }
00087     }
00088 
00089     if (pbag->hd_top > pbag->hd_high) {     
00090       while (slice_nb > (pbag->room = pbag->pool_size))
00091     pbag->pool_size *= 2;
00092 
00093       if (pbag->file_name) {
00094     fprintf (pbag->file_name, "Bag %s: New bag of size %li is created.\n", pbag->name, (long)(pbag->pool_size *= 2));
00095     pbag->total_size += pbag->pool_size + 3;
00096       }
00097 
00098       pbag->hd_high = pbag->hd_top;
00099   /* Pour pouvoir faire sxba_empty sur bag_clear */
00100       pbag->hd [pbag->hd_top].set = sxba_calloc ((pbag->pool_size + 3)<<SXSHIFT_AMOUNT);
00101       pbag->hd [pbag->hd_top].size = pbag->pool_size;
00102     }
00103 
00104     pbag->pool_top = pbag->hd [pbag->hd_top].set+1;
00105   }
00106 
00107   *(set = pbag->pool_top) = bits_number;
00108   pbag->pool_top += slice_nb;
00109   pbag->room -= slice_nb;
00110 
00111   if (pbag->file_name) {
00112     pbag->used_size += slice_nb;
00113   }
00114 
00115   return set;
00116 }
00117     
00118 
00119 void
00120 sxba_bag_free (bag_header *pbag)
00121 {
00122   if (pbag->file_name) {
00123     fprintf (pbag->file_name, "Bag %s: used_size = %lu bytes, total_size = %lu bytes\n",
00124          pbag->name,
00125          (SXUINT)((pbag->used_size > pbag->prev_used_size ? pbag->used_size : pbag->prev_used_size) *
00126          sizeof (SXBA_ELT) + (pbag->hd_high + 1) * sizeof (struct bag_disp_hd)),
00127          (SXUINT)(pbag->total_size * sizeof (SXBA_ELT) + pbag->hd_size * sizeof (struct bag_disp_hd)));
00128   }
00129 
00130   do {
00131     sxfree (pbag->hd [pbag->hd_high].set);
00132   } while (--pbag->hd_high >= 0);
00133 
00134   sxfree (pbag->hd);
00135 }
00136     
00137 
00138 void
00139 sxba_bag_clear (bag_header *pbag)
00140 {
00141   SXBA_INDEX top;
00142 
00143   top = pbag->hd_high;
00144 
00145   do {
00146     sxba_empty (pbag->hd [top].set);
00147   } while (--top >= 0);
00148 
00149   pbag->hd_top = 0;
00150   pbag->pool_top = pbag->hd [0].set+1;
00151   pbag->pool_size = pbag->hd [0].size;
00152   pbag->room = pbag->pool_size;
00153 
00154   if (pbag->file_name) {
00155     if (pbag->prev_used_size < pbag->used_size)
00156       pbag->prev_used_size = pbag->used_size;
00157 
00158     pbag->used_size = 0;
00159   }
00160 }
00161 /* END BAG_mngr */
00162  

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