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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef _MEMORY_XSB_H_
00049 #define _MEMORY_XSB_H_
00050
00051
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
00062
00063 extern System_Stack pdl,
00064 glstack,
00065 tcpstack,
00066 complstack;
00067
00068
00069
00070
00071
00072
00073
00074
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
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
00107
00108
00109 #define OVERFLOW_MARGIN (8192 * ZOOM_FACTOR)
00110
00111
00112
00113
00114 #define resize_stack(stack_size,min_exp) \
00115 (((unsigned long)stack_size) < (min_exp)/K ? (stack_size) + (min_exp)/K : 2 * (stack_size))
00116
00117
00118
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
00138
00139 #define NUM_CATS_SPACE 18
00140
00141
00142
00143 extern long pspacesize[NUM_CATS_SPACE];
00144
00145
00146
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
00167
00168 extern byte *inst_begin_gl;
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
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