YAP 7.1.0
YapHandles.h
1/*************************************************************************
2* *
3* YAP Prolog %W% %G% *
4* Yap Prolog was developed at NCCUP - Universidade do Porto *
5* *
6* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
7* *
8**************************************************************************
9* *
10* File: YapHandles.h *
11* mods: *
12* comments: term handles for YAP: basic ops *
13* version: $Id: Yap.h,v 1.38 2008-06-18 10:02:27 vsc Exp $ *
14*************************************************************************/
15
16#ifndef YAP_HANDLES_H
17#define YAP_HANDLES_H 1
18
19#include "Regs.h"
20#include "Yatom.h"
21
22#define LOCAL_CurHandle LOCAL_CurSlot
23#define REMOTE_CurHandle REMOTE_CurSlot
24#define LOCAL_NHandles LOCAL_NSlots
25#define REMOTE_NHandles REMOTE_NSlots
26#define LOCAL_HandleBase LOCAL_SlotBase
27#define REMOTE_HanvdleBase SlotBase
28
68#include <stdio.h>
69
72#define Yap_RebootHandles(wid)
73Yap_RebootHandles(wid PASS_REGS)
74#define Yap_RebootSlots(wid) Yap_RebootHandles__(wid PASS_REGS)
75
76static inline void Yap_RebootHandles__(int wid USES_REGS) {
77 // fprintf(stderr, " StartHandles = %ld", LOCAL_CurHandle);
78 REMOTE_CurHandle(wid) = 0;
79}
80
84//#define Yap_StartHandles() (
85// printf("[<<<%s,%s,%d-%ld\n",__FILE__,__FUNCTION__,__LINE__,LOCAL_CurHandle)?Yap_StartHandles__(PASS_REGS1):
86//-1)
87#define Yap_StartHandles() Yap_StartHandles__(PASS_REGS1)
88#define Yap_StartSlots() Yap_StartHandles__(PASS_REGS1)
89
90INLINE_ONLY yhandle_t Yap_StartHandles__(USES_REGS1);
91INLINE_ONLY yhandle_t Yap_StartHandles__(USES_REGS1) {
92 // // fprintf(stderr, " StartHandles = %ld", LOCAL_CurHandle);
93 // fprintf(stderr,"SS %s:%d\n", __FILE__, __LINE__);;
94 if (LOCAL_CurHandle < 0) {
95 Yap_Error(SYSTEM_ERROR_INTERNAL, 0L, " StartHandles = %ld",
96 LOCAL_CurHandle);
97 }
98 return LOCAL_CurHandle;
99}
100
104//#define Yap_CloseHandles(slot) ( printf("- %s,%s,%d
105//%ld>>>]\n",__FILE__,__FUNCTION__,__LINE__, slot)?Yap_CloseHandles__(slot
106// PASS_REGS):-1)
107#define Yap_CloseHandles(slot) Yap_CloseHandles__(slot PASS_REGS)
108#define Yap_CloseSlots(slot) Yap_CloseHandles__(slot PASS_REGS)
109
110INLINE_ONLY void Yap_CloseHandles__(yhandle_t slot USES_REGS);
111INLINE_ONLY void Yap_CloseHandles__(yhandle_t slot USES_REGS) {
112 // fprintf(stderr,"CS %s:%d\n", __FILE__, __LINE__);
113 LOCAL_CurHandle = slot;
114}
115
116#define Yap_CurrentHandle() Yap_CurrentHandle__(PASS_REGS1)
117#define Yap_CurrentSlot() Yap_CurrentHandle__(PASS_REGS1)
118
121INLINE_ONLY yhandle_t Yap_CurrentHandle__(USES_REGS1);
122INLINE_ONLY yhandle_t Yap_CurrentHandle__(USES_REGS1) {
123 return LOCAL_CurHandle;
124}
125
126#define Yap_GetFromHandle(slot) Yap_GetFromHandle__(slot PASS_REGS)
127#define Yap_GetFromSlot(slot) Yap_GetFromHandle__(slot PASS_REGS)
128
130INLINE_ONLY Term Yap_GetFromHandle__(yhandle_t slot USES_REGS);
131INLINE_ONLY Term Yap_GetFromHandle__(yhandle_t slot USES_REGS) {
132 // fprintf(stderr, "GS %s:%d\n", __FILE__, __LINE__);
133 return Deref(LOCAL_HandleBase[slot]);
134}
135
136#define Yap_GetDerefedFromHandle(slot) \
137 Yap_GetDerefedFromHandle__(slot PASS_REGS)
138#define Yap_GetDerefedFromSlot(slot) Yap_GetDerefedFromHandle__(slot PASS_REGS)
139
141INLINE_ONLY Term
142Yap_GetDerefedFromHandle__(yhandle_t slot USES_REGS);
143INLINE_ONLY Term
144Yap_GetDerefedFromHandle__(yhandle_t slot USES_REGS) {
145 // fprintf(stderr,"GDS %s:%d\n", __FILE__, __LINE__);
146 return LOCAL_HandleBase[slot];
147}
148
149#define Yap_GetPtrFromHandle(slot) Yap_GetPtrFromHandle__(slot PASS_REGS)
150#define Yap_GetPtrFromSlot(slot) Yap_GetPtrFromHandle__(slot PASS_REGS)
151
153INLINE_ONLY Term *
154Yap_GetPtrFromHandle__(yhandle_t slot USES_REGS);
155INLINE_ONLY Term *
156Yap_GetPtrFromHandle__(yhandle_t slot USES_REGS) {
157 // fprintf(stderr,"GPS %s:%d\n", __FILE__, __LINE__);
158 return (Term *)LOCAL_HandleBase[slot];
159}
160
161#define Yap_AddressFromHandle(slot) Yap_AddressFromHandle__(slot PASS_REGS)
162#define Yap_AddressFromSlot(slot) Yap_AddressFromHandle__(slot PASS_REGS)
163
164INLINE_ONLY CELL *
165Yap_AddressFromHandle__(yhandle_t slot USES_REGS);
166INLINE_ONLY CELL *
167Yap_AddressFromHandle__(yhandle_t slot USES_REGS) {
169
170 return LOCAL_HandleBase + slot;
171}
172
173#define Yap_PutInSlot(slot, t) Yap_PutInHandle__(slot, t PASS_REGS)
174#define Yap_PutInHandle(slot, t) Yap_PutInHandle__(slot, t PASS_REGS)
176INLINE_ONLY void Yap_PutInHandle__(yhandle_t slot,
177 Term t USES_REGS);
178INLINE_ONLY void Yap_PutInHandle__(yhandle_t slot,
179 Term t USES_REGS) {
180 // fprintf(stderr,"PS %s:%d\n", __FILE__, __LINE__);
181 LOCAL_HandleBase[slot] = t;
182}
183
184#ifndef Yap_Max
185#define Yap_Max(X, Y) (X > Y ? X : Y)
186#endif
187
188#define ensure_handles ensure_slots
189INLINE_ONLY void ensure_slots(int N USES_REGS) {
190 if (LOCAL_CurHandle + N >= LOCAL_NHandles) {
191 size_t inc = Yap_Max(16 * 1024, LOCAL_NHandles / 2); // measured in cells
192 inc = Yap_Max(inc, (size_t)N + 16); // measured in cells
193 LOCAL_HandleBase = (CELL *)realloc(LOCAL_HandleBase,
194 (inc + LOCAL_NHandles) * sizeof(CELL));
195 LOCAL_NHandles += inc;
196 if (!LOCAL_HandleBase) {
197 size_t kneeds = ((inc + LOCAL_NHandles) * sizeof(CELL)) / 1024;
198 Yap_Error(
199 SYSTEM_ERROR_INTERNAL, 0 /* TermNil */,
200 "Out of memory for the term handles (term_t) aka slots, l needed",
201 kneeds);
202 }
203 }
204}
205
207// #define Yap_InitHandle(t)
208// (printf("+%d %ld %s,%s,%d>>>]\n", 1, LOCAL_CurHandle,__FILE__, __FUNCTION__,
209// __LINE__)
210// ? Yap_InitHandle__(t PASS_REGS)
211// : -1)
212#define Yap_InitHandle(t) Yap_InitHandle__(t PASS_REGS)
213#define Yap_PushHandle(t) Yap_InitHandle__(t PASS_REGS)
214#define Yap_InitSlot(t) Yap_InitHandle__(t PASS_REGS)
215
216INLINE_ONLY yhandle_t Yap_InitHandle__(Term t USES_REGS);
217INLINE_ONLY yhandle_t Yap_InitHandle__(Term t USES_REGS) {
218 yhandle_t old_slots = LOCAL_CurHandle;
219
220 ensure_slots(1 PASS_REGS);
221 if (t==0) {
222 t = MkVarTerm();
223 } else if (IsVarTerm(t) && (H0 > (CELL*)t || (CELL*)t > HR)) {
224 RESET_VARIABLE(HR);
225 Yap_unify(t,(CELL)HR); t = (CELL)HR++;
226 }
227 LOCAL_HandleBase[old_slots] = t;
228 LOCAL_CurHandle++;
229 return old_slots;
230}
231
232//#define Yap_NewHandles(n) ( printf("+%d %ld
233//%s,%s,%d>>>]\n",n,LOCAL_CurHandle,__FILE__,__FUNCTION__,__LINE__)
234//?Yap_NewHandles__(n PASS_REGS):-1)
235#define Yap_NewHandles(n) Yap_NewHandles__(n PASS_REGS)
236#define Yap_NewSlots(n) Yap_NewHandles__(n PASS_REGS)
237
238INLINE_ONLY yhandle_t Yap_NewHandles__(int n USES_REGS);
239INLINE_ONLY yhandle_t Yap_NewHandles__(int n USES_REGS) {
240 yhandle_t old_slots = LOCAL_CurHandle;
241 int i;
242 // fprintf(stderr, "NS %s:%d\n", __FILE__, __LINE__);
243
244 ensure_slots(n PASS_REGS);
245 for (i = 0; i < n; i++) {
246 LOCAL_HandleBase[old_slots + i] = MkVarTerm();
247 }
248 LOCAL_CurHandle += n;
249 return old_slots;
250}
251
252//#define Yap_InitHandles(n, ts)
253// (printf("+%d %d %s,%s,%d>>>]\n", n, LOCAL_CurHandle, __FILE__, __FUNCTION__,
254// __LINE__)
255// ? Yap_InitHandles__(n, ts PASS_REGS)
256// : -1)
257#define Yap_InitHandles(n, ts) Yap_InitHandles__(n, ts PASS_REGS)
258#define Yap_InitSlots(n, ts) Yap_InitHandles__(n, ts PASS_REGS)
259
261INLINE_ONLY yhandle_t Yap_InitHandles__(int n,
262 Term *ts USES_REGS);
263INLINE_ONLY yhandle_t Yap_InitHandles__(int n,
264 Term *ts USES_REGS) {
265 yhandle_t old_slots = LOCAL_CurHandle;
266 int i;
267 ensure_slots(n PASS_REGS);
268 for (i = 0; i < n; i++)
269 LOCAL_HandleBase[old_slots + i] = ts[i];
270 LOCAL_CurHandle += n;
271 return old_slots;
272}
273
274
275#define Yap_InitHandleWithAddress(pt) Yap_InitHandleWithAddress__(pt PASS_REGS)
276
277INLINE_ONLY yhandle_t
278Yap_InitHandleWithAddress__( CELL *pt USES_REGS) {
280
281 yhandle_t old_slots = LOCAL_CurHandle;
282
283 ensure_slots(1 PASS_REGS);
284 LOCAL_HandleBase[old_slots] = (CELL)pt;
285 LOCAL_CurHandle++;
286 return old_slots;
287}
288
289
290
291#define Yap_RecoverHandles(n, ts) Yap_RecoverHandles__(n, ts PASS_REGS)
292#define Yap_RecoverSlots(n, ts) Yap_RecoverHandles__(n, ts PASS_REGS)
293
296static inline bool Yap_RecoverHandles__(int n, yhandle_t topHandle USES_REGS);
297static inline bool Yap_RecoverHandles__(int n, yhandle_t topHandle USES_REGS) {
298 if (topHandle + n < LOCAL_CurHandle)
299 return false;
300#ifdef DEBUG
301 if (n > LOCAL_CurHandle) {
302 Yap_Error(SYSTEM_ERROR_INTERNAL, 0,
303 "Inconsistent slot state in Yap_RecoverHandles.", 0);
304 return false;
305 }
306#endif
307 LOCAL_CurHandle = topHandle;
308 // fprintf(stderr,"RS %ld %s:%d\n", LOCAL_CurHandle, __FILE__, __LINE__);
309 return true;
310}
311
312#define Yap_PopSlot(ts) Yap_PopHandle__(ts PASS_REGS)
313#define Yap_PopHandle(ts) Yap_PopHandle__(ts PASS_REGS)
314
316static inline Term Yap_PopHandle__(yhandle_t topHandle USES_REGS) {
317 if (LOCAL_CurHandle < topHandle)
318 return 0;
319 else {
320 LOCAL_CurHandle = topHandle;
321 // fprintf(stderr,"RS %ld %s:%d\n", LOCAL_CurHandle, __FILE__, __LINE__);≈
322 return Deref(LOCAL_HandleBase[topHandle]);
323 }
324}
325
326#define Yap_PopSlots(ts,s,ar) Yap_PopHandles__(ts, s, ar PASS_REGS)
327#define Yap_PopHandles(ts,s,ar) Yap_PopHandles__(ts, s, ar PASS_REGS)
328
330static inline Term Yap_PopHandles__(yhandle_t topHandle, size_t n, Term *ar USES_REGS) {
331 size_t i;
332 if (LOCAL_CurHandle < topHandle)
333 return false;
334 else {
335 LOCAL_CurHandle = topHandle;
336 // fprintf(stderr,"RS %ld %s:%d\n", LOCAL_CurHandle, __FILE__, __LINE__);≈
337 for (i=0; i<n; i++)
338 ar[i] = Deref(LOCAL_HandleBase[topHandle+i]);
339 }
340 return true;
341}
342
343
344#define Yap_PopSlotWithAddress(ts) Yap_PopHandleWithAddress__(ts PASS_REGS)
345#define Yap_PopHandleWithAddress(ts) Yap_PopHandleWithAddress__(ts PASS_REGS)
346
348static inline CELL * Yap_PopHandleWithAddress__(yhandle_t topHandle USES_REGS) {
349 if (LOCAL_CurHandle < topHandle)
350 return NULL;
351 else {
352 LOCAL_CurHandle = topHandle;
353 // fprintf(stderr,"RS %ld %s:%d\n", LOCAL_CurHandle, __FILE__, __LINE__);≈
354 return (CELL*)(LOCAL_HandleBase[topHandle]);
355 }
356}
357
358#endif