dynamic_stack.c

00001 /* File:      dynamic_stack.c
00002 ** Author(s): Ernie Johnson
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: dynamic_stack.c,v 1.7 2005/11/16 17:32:03 dwarren Exp $
00022 ** 
00023 */
00024 
00025 
00026 #include "xsb_config.h"
00027 #include "xsb_debug.h"
00028 
00029 #include "debugs/debug_tries.h"
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 
00034 #include "auxlry.h"
00035 #include "cell_xsb.h"
00036 #include "tst_aux.h"  /* needs cell_xsb.h */
00037 #include "error_xsb.h"
00038 #include "debug_xsb.h"
00039 #include "flags_xsb.h"
00040 #include "memory_xsb.h"
00041 
00042 /*-------------------------------------------------------------------------*/
00043 
00044 /*
00045  * Prints the fields of the given DynamicStack.  `comment' is useful during
00046  * debugging, e.g. "just after initialization".
00047  */
00048 
00049 void dsPrint(DynamicStack ds, char *comment) {
00050 
00051   xsb_dbgmsg((LOG_DEBUG, "Dynamic Stack: %s (%s)\n"
00052              "  Stack Base:    %8p\tFrame Size:   %u bytes\n"
00053              "  Stack Top:     %8p\tCurrent Size: %u frames\n"
00054              "  Stack Ceiling: %8p\tInitial Size: %u frames",
00055              DynStk_Name(ds), comment,
00056              DynStk_Base(ds), DynStk_FrameSize(ds),
00057              DynStk_Top(ds), DynStk_CurSize(ds),
00058              DynStk_Ceiling(ds), DynStk_InitSize(ds)));
00059 }
00060 
00061 /*-------------------------------------------------------------------------*/
00062 
00063 /*
00064  * Initialize a stack for use.  Allocates a block of memory for the stack
00065  * area and sets the fields of the DynamicStack structure.
00066  */
00067 
00068 void dsInit(DynamicStack *ds, size_t stack_size, size_t frame_size,
00069             char *name) {
00070 
00071   size_t total_bytes;
00072 
00073   xsb_dbgmsg((LOG_TRIE_STACK, "Initializing %s", name));
00074 
00075   total_bytes = stack_size * frame_size;
00076   if (total_bytes > 0) {
00077     DynStk_Base(*ds) = mem_alloc(total_bytes,TABLE_SPACE);
00078     if ( IsNULL(DynStk_Base(*ds)) )
00079       xsb_abort("Ran out of memory in allocation of %s", DynStk_Name(*ds));
00080   } else DynStk_Base(*ds) = NULL;
00081   DynStk_Top(*ds) = DynStk_Base(*ds);
00082   DynStk_Ceiling(*ds) = (char *)DynStk_Base(*ds) + total_bytes;
00083   DynStk_FrameSize(*ds) = frame_size;
00084   DynStk_InitSize(*ds) = DynStk_CurSize(*ds) = stack_size;
00085   DynStk_Name(*ds) = name;
00086 }
00087 
00088 /*-------------------------------------------------------------------------*/
00089 
00090 /*
00091  * `num_frames' are the number of frames that are needed immediately.
00092  * Here we make sure that the expanded size can accommodate this need.
00093  */
00094 
00095 void dsExpand(DynamicStack *ds, int num_frames) {
00096 
00097   size_t new_size, total_bytes;
00098   char *new_base;
00099 
00100   if ( num_frames < 1 )
00101     return;
00102   if ( DynStk_CurSize(*ds) > 0 )
00103     new_size = 2 * DynStk_CurSize(*ds);
00104   else
00105     new_size = DynStk_InitSize(*ds);
00106   if ( new_size < DynStk_CurSize(*ds) + num_frames )
00107     new_size = new_size + num_frames;
00108 
00109   xsb_dbgmsg((LOG_TRIE_STACK, "Expanding %s: %d -> %d", DynStk_Name(*ds),
00110              DynStk_CurSize(*ds), new_size));
00111   dbg_dsPrint(LOG_TRIE_STACK, *ds, "Before expansion");
00112 
00113   total_bytes = new_size * DynStk_FrameSize(*ds);
00114   new_base = mem_realloc(DynStk_Base(*ds),DynStk_CurSize(*ds)*DynStk_FrameSize(*ds),total_bytes,TABLE_SPACE);
00115   if ( IsNULL(new_base) && total_bytes > 0)
00116     xsb_abort("Ran out of memory during expansion of %s", DynStk_Name(*ds));
00117   DynStk_Top(*ds) =
00118     new_base + ((char *)DynStk_Top(*ds) - (char *)DynStk_Base(*ds));
00119   DynStk_Base(*ds) = new_base;
00120   DynStk_Ceiling(*ds) = new_base + total_bytes;
00121   DynStk_CurSize(*ds) = new_size;
00122 
00123   dbg_dsPrint(LOG_TRIE_STACK, *ds, "After expansion");
00124 }
00125 
00126 /*-------------------------------------------------------------------------*/
00127 
00128 /*
00129  * Reduces the size of the memory block allocated back to the initial size
00130  * specified during initialization.
00131  */
00132 
00133 void dsShrink(DynamicStack *ds) {
00134 
00135   size_t total_bytes;
00136   char *new_base;
00137 
00138   if ( DynStk_CurSize(*ds) <= DynStk_InitSize(*ds) )
00139     return;
00140   total_bytes = DynStk_InitSize(*ds) * DynStk_FrameSize(*ds);
00141   new_base = mem_realloc(DynStk_Base(*ds),DynStk_CurSize(*ds)*DynStk_FrameSize(*ds),total_bytes,TABLE_SPACE);
00142 
00143   xsb_dbgmsg((LOG_TRIE_STACK, "Shrinking %s: %d -> %d", DynStk_Name(*ds),
00144              DynStk_CurSize(*ds), DynStk_InitSize(*ds)));
00145 
00146   if ( IsNULL(new_base) && total_bytes > 0 )
00147     xsb_abort("Ran out of memory during expansion of %s", DynStk_Name(*ds));
00148   DynStk_Top(*ds) =
00149     new_base + ((char *)DynStk_Top(*ds) - (char *)DynStk_Base(*ds));
00150   DynStk_Base(*ds) = new_base;
00151   DynStk_Ceiling(*ds) = new_base + total_bytes;
00152   DynStk_CurSize(*ds) = DynStk_InitSize(*ds);
00153 }

Generated on Wed Jul 26 13:30:40 2006 for XSB by  doxygen 1.4.5