memory_xsb.h

00001 /* File:      memory_xsb.h
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: memory_xsb.h,v 1.28 2006/01/27 20:29:47 tswift Exp $
00022 ** 
00023 */
00024 
00025 
00026 /*===========================================================================*/
00027 
00028 /*
00029  *                      SLG-WAM Stack Management
00030  *                      ========================
00031  *
00032  * Information on each independent data area needed by the slg-wam is kept
00033  * in one of these structures.  "low" and "high" point into memory to
00034  * delineate the bounds of the data area: "low" gets the address returned
00035  * by an allocation routine, while "high" gets "low" + "size" * K.  Note:
00036  * this means that (1) "size" represents the number of K-byte blocks
00037  * allocated for this stack, and (2) "high" points off the end of the
00038  * allocated area.
00039  *
00040  * The logical, data structure specific details of how a region is used,
00041  * e.g., which pointer represents the bottom of a stack, is represented in
00042  * the code and the documentation.  "init_size" is used for storing the size
00043  * the stack was initialized to, for purposes of restoring it to this size
00044  * after each query via the trimcore/0 predicate.
00045  */
00046 
00047 
00048 #ifndef _MEMORY_XSB_H_
00049 #define _MEMORY_XSB_H_
00050 
00051 /* Info Structure for the Data Regions
00052    ----------------------------------- */
00053 typedef struct stack_info {
00054    byte *low;
00055    byte *high;
00056    long size;
00057    long init_size;
00058 } System_Stack;
00059 
00060 
00061 /* The SLG-WAM System Data Regions
00062    ------------------------------- */
00063 extern System_Stack pdl,            /* PDL                        */
00064                     glstack,        /* Global + Local Stacks      */
00065                     tcpstack,       /* Trail + Choice Point Stack */
00066                     complstack;     /* Completion Stack           */
00067 
00068 
00069 /*
00070  *  Finding the tops of stacks.
00071  *  ---------------------------
00072  *    In this form, the result can be used immediately for deref, recast,
00073  *    etc., as well as for assignment.  ALL macros return a pointer to the
00074  *    topmost USED cell on their respective stack.
00075  */
00076 #define top_of_heap      (hreg - 1)
00077 #define top_of_localstk  ( ((efreg < ebreg) && (efreg < ereg)) \
00078                            ? efreg  \
00079                            : ( (ereg < ebreg) \
00080                                ? ereg - *(cpreg - 2*sizeof(Cell)+3) + 1 \
00081                                : ebreg ) )
00082 #define top_of_trail     ((trreg > trfreg) ? trreg : trfreg)
00083 #define top_of_cpstack   ((breg < bfreg) ? breg : bfreg)
00084 
00085 #define top_of_complstk  openreg
00086 
00087 /* Testing pointer addresses
00088    ------------------------- */
00089 #define IsInHeap(Ptr)   ( ( (CPtr)(Ptr) <= top_of_heap ) &&     \
00090                           ( (CPtr)(Ptr) >= (CPtr)glstack.low ) )
00091 
00092 #define IsInEnv(Ptr)    ( ( (CPtr)(Ptr) < (CPtr)glstack.high ) &&       \
00093                           ( (CPtr)(Ptr) >= top_of_localstk) )
00094 
00095 #define IsInTrail(Ptr)  ( ( (CPtr)(Ptr) <= (CPtr)top_of_trail ) &&      \
00096                           ( (CPtr)(Ptr) >= (CPtr)cpstack.low ) )
00097 
00098 #define IsInCPS(Ptr)    ( ( (CPtr)(Ptr) < (CPtr)cpstack.high ) &&       \
00099                           ( (CPtr)(Ptr) >= top_of_cpstack) )
00100 
00101 
00102 #define COMPLSTACKBOTTOM ((CPtr) complstack.high)
00103 
00104 
00105 /*
00106  *  Size of margin between facing stacks before reallocating a larger area.
00107  */
00108 //#define OVERFLOW_MARGIN       (2048 * ZOOM_FACTOR)
00109 #define OVERFLOW_MARGIN (8192 * ZOOM_FACTOR)
00110 
00111 
00112 /* Calculate New Stack Size
00113    ------------------------ */
00114 #define resize_stack(stack_size,min_exp) /*"stack_size" is in K-byte blocks*/\
00115    (((unsigned long)stack_size) < (min_exp)/K ? (stack_size) + (min_exp)/K : 2 * (stack_size))
00116 
00117 
00118 /* Categories of permanent space use: */
00119 #define ATOM_SPACE              0
00120 #define STRING_SPACE            1
00121 #define ASSERT_SPACE            2
00122 #define COMPILED_SPACE          3
00123 #define FOR_CODE_SPACE          4
00124 #define TABLE_SPACE             5
00125 #define FINDALL_SPACE           6
00126 #define PROFILE_SPACE           7
00127 #define MT_PRIVATE_SPACE        8
00128 #define BUFF_SPACE              9
00129 #define GC_SPACE                10
00130 #define HASH_SPACE              11
00131 #define INTERPROLOG_SPACE       12
00132 #define THREAD_SPACE            13
00133 #define READ_CAN_SPACE          14
00134 #define LEAK_SPACE              15
00135 #define SPECIAL_SPACE           16
00136 #define OTHER_SPACE             17
00137 // VARSTRING_SPACE??  some other to thread?
00138 
00139 #define NUM_CATS_SPACE          18
00140 
00141 /* Program and Symbol Tables Space (in Bytes)
00142    ------------------------------------------ */
00143 extern long pspacesize[NUM_CATS_SPACE];
00144 
00145 
00146 /* Memory Function Prototypes
00147    -------------------------- */
00148 extern void *mem_alloc(unsigned long, int);
00149 extern void *mem_alloc_nocheck(unsigned long, int);
00150 extern void *mem_calloc(unsigned long, unsigned long, int);
00151 extern void *mem_realloc(void *, unsigned long, unsigned long, int);
00152 extern void *mem_realloc_nocheck(void *, unsigned long, unsigned long, int);
00153 extern void mem_dealloc(void *, unsigned long, int);
00154 #ifndef MULTI_THREAD
00155 extern void tcpstack_realloc(long);
00156 extern void complstack_realloc(long);
00157 extern void handle_tcpstack_overflow(void);
00158 #else
00159 struct th_context ;
00160 extern void tcpstack_realloc(struct th_context *, long);
00161 extern void complstack_realloc(struct th_context *, long);
00162 extern void handle_tcpstack_overflow(struct th_context *);
00163 #endif
00164 
00165 
00166 /* Instruction Externs
00167    ------------------- */
00168 extern byte *inst_begin_gl;       /* ptr to beginning of instruction array. */
00169 
00170 extern Cell answer_return_inst, check_complete_inst, hash_handle_inst,
00171             resume_compl_suspension_inst, fail_inst, dynfail_inst, 
00172             halt_inst, proceed_inst, 
00173  resume_compl_suspension_inst2,
00174             reset_inst, trie_fail_unlock_inst;
00175 
00176 
00177 /* Stack Overflow Checkers
00178    ----------------------- */
00179 #define local_global_exception "! Local/Global Stack Overflow Exception\n"
00180 
00181 #define complstack_exception "! Completion Stack Overflow Exception\n"
00182 
00183 #define trail_cp_exception "! Trail/CP Stack Overflow Exception\n"
00184 
00185 #ifdef DEBUG_ASSERTIONS
00186 
00187 #define check_tcpstack_overflow {                                       \
00188                                                                         \
00189    CPtr cps_top;                                                        \
00190    cps_top = top_of_cpstack;                                            \
00191                                                                         \
00192    if ((pb)cps_top < (pb)top_of_trail + OVERFLOW_MARGIN) {              \
00193      if ((pb)cps_top < (pb)top_of_trail) {                              \
00194        xsb_error("Trail clobbered Choice Point Stack");                 \
00195        print_statistics(CTXTc 1);                                       \
00196        xsb_basic_abort(trail_cp_exception);                             \
00197      }                                                                  \
00198      else {                                                             \
00199        fprintf(stdwarn,                                                 \
00200                "\n++Warning: Trail / Choice Point Stack overflow:   "); \
00201        if (pflags[STACK_REALLOC]) {                                     \
00202          fprintf(stdwarn, "Expanding ...\n");                           \
00203          tcpstack_realloc(CTXTc resize_stack(tcpstack.size,0));         \
00204        }                                                                \
00205        else {                                                           \
00206          fprintf(stdwarn, "Reallocation turned OFF!\n");                \
00207          print_statistics(CTXTc 1);                                     \
00208          xsb_basic_abort(trail_cp_exception);                           \
00209        }                                                                \
00210      }                                                                  \
00211    }                                                                    \
00212  }
00213 
00214 #define check_glstack_overflow(arity,PCREG,EXTRA)                          \
00215    if ((pb)top_of_localstk < (pb)top_of_heap + OVERFLOW_MARGIN + EXTRA) {  \
00216      if ((pb)top_of_localstk < (pb)top_of_heap) {                          \
00217        xsb_basic_abort("\nFatal ERROR:  -- "                               \
00218                                  "Local Stack clobbered Heap --\n");       \
00219      }                                                                     \
00220      else {                                                                \
00221        fprintf(stdwarn, "\n++Warning: Heap / Local Stack overflow:   ");   \
00222        if (pflags[STACK_REALLOC]) {                                        \
00223          fprintf(stdwarn, "Expanding ...\n");                              \
00224          glstack_realloc(CTXTc resize_stack(glstack.size,                  \
00225                          EXTRA+OVERFLOW_MARGIN), arity);                   \
00226        }                                                                   \
00227        else {                                                              \
00228          fprintf(stdwarn, "Reallocation turned OFF!\n");                   \
00229          print_statistics(CTXTc 1);                                        \
00230          xsb_basic_abort(local_global_exception);                          \
00231        }                                                                   \
00232      }                                                                     \
00233    }
00234 
00235 #define check_completion_stack_overflow                                 \
00236    if ( (pb)openreg < (pb)complstack.low + OVERFLOW_MARGIN ) {          \
00237      fprintf(stdwarn, "\n++Warning: Completion Stack overflow:   ");    \
00238      if (pflags[STACK_REALLOC]) {                                       \
00239        fprintf(stdwarn, "Expanding ...\n");                             \
00240        complstack_realloc(CTXTc resize_stack(complstack.size,0));       \
00241      }                                                                  \
00242      else {                                                             \
00243        fprintf(stdwarn, "Reallocation turned OFF!\n");                  \
00244        print_statistics(CTXTc 1);                                       \
00245        xsb_basic_abort(complstack_exception);                           \
00246      }                                                                  \
00247    }
00248 
00249 
00250 #else
00251 
00252 
00253 #define check_tcpstack_overflow {                                       \
00254                                                                         \
00255    CPtr cps_top = top_of_cpstack;                                       \
00256                                                                         \
00257    if ((pb)cps_top < (pb)top_of_trail + OVERFLOW_MARGIN) {              \
00258      if ((pb)cps_top < (pb)top_of_trail) {                              \
00259        xsb_basic_abort("\nFatal ERROR:  -- Trail "                      \
00260                                   "clobbered Choice Point Stack --\n"); \
00261      }                                                                  \
00262      else {                                                             \
00263        if (pflags[STACK_REALLOC])                                       \
00264          tcpstack_realloc(CTXTc resize_stack(tcpstack.size,0));         \
00265        else {                                                           \
00266          xsb_basic_abort(trail_cp_exception);                           \
00267        }                                                                \
00268      }                                                                  \
00269    }                                                                    \
00270  }
00271 
00272 #define check_glstack_overflow(arity,PCREG,EXTRA)                             \
00273    if ((pb)top_of_localstk < (pb)top_of_heap + OVERFLOW_MARGIN + EXTRA) {     \
00274      if ((pb)top_of_localstk < (pb)top_of_heap) {                             \
00275        xsb_basic_abort("\nFatal ERROR:  -- "                                  \
00276                                  "Local Stack clobbered Heap --\n");          \
00277      }                                                                        \
00278      else {                                                                   \
00279        if ((pflags[STACK_REALLOC] == FALSE) ||                                \
00280            (glstack_ensure_space(CTXTc EXTRA,arity) != 0)) {                          \
00281          xsb_basic_abort(local_global_exception);                             \
00282        }                                                                      \
00283      }                                                                        \
00284    }
00285 
00286 #define check_completion_stack_overflow                         \
00287    if ( (pb)openreg < (pb)complstack.low + OVERFLOW_MARGIN ) {  \
00288      if (pflags[STACK_REALLOC])                                 \
00289        complstack_realloc(CTXTc resize_stack(complstack.size,0));\
00290      else {                                                     \
00291        xsb_basic_abort(complstack_exception);                   \
00292      }                                                          \
00293    }
00294 
00295 #endif
00296 
00297 #endif /* _MEMORY_XSB_H_ */

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