17#define YAP_HANDLES_H 1
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
72#define Yap_RebootHandles(wid)
73Yap_RebootHandles(wid PASS_REGS)
74#define Yap_RebootSlots(wid) Yap_RebootHandles__(wid PASS_REGS)
76static inline void Yap_RebootHandles__(
int wid USES_REGS) {
78 REMOTE_CurHandle(wid) = 0;
87#define Yap_StartHandles() Yap_StartHandles__(PASS_REGS1)
88#define Yap_StartSlots() Yap_StartHandles__(PASS_REGS1)
90INLINE_ONLY yhandle_t Yap_StartHandles__(USES_REGS1);
91INLINE_ONLY yhandle_t Yap_StartHandles__(USES_REGS1) {
94 if (LOCAL_CurHandle < 0) {
95 Yap_Error(SYSTEM_ERROR_INTERNAL, 0L,
" StartHandles = %ld",
98 return LOCAL_CurHandle;
107#define Yap_CloseHandles(slot) Yap_CloseHandles__(slot PASS_REGS)
108#define Yap_CloseSlots(slot) Yap_CloseHandles__(slot PASS_REGS)
110INLINE_ONLY
void Yap_CloseHandles__(yhandle_t slot USES_REGS);
111INLINE_ONLY
void Yap_CloseHandles__(yhandle_t slot USES_REGS) {
113 LOCAL_CurHandle = slot;
116#define Yap_CurrentHandle() Yap_CurrentHandle__(PASS_REGS1)
117#define Yap_CurrentSlot() Yap_CurrentHandle__(PASS_REGS1)
121INLINE_ONLY yhandle_t Yap_CurrentHandle__(USES_REGS1);
122INLINE_ONLY yhandle_t Yap_CurrentHandle__(USES_REGS1) {
123 return LOCAL_CurHandle;
126#define Yap_GetFromHandle(slot) Yap_GetFromHandle__(slot PASS_REGS)
127#define Yap_GetFromSlot(slot) Yap_GetFromHandle__(slot PASS_REGS)
130INLINE_ONLY Term Yap_GetFromHandle__(yhandle_t slot USES_REGS);
131INLINE_ONLY Term Yap_GetFromHandle__(yhandle_t slot USES_REGS) {
133 return Deref(LOCAL_HandleBase[slot]);
136#define Yap_GetDerefedFromHandle(slot) \
137 Yap_GetDerefedFromHandle__(slot PASS_REGS)
138#define Yap_GetDerefedFromSlot(slot) Yap_GetDerefedFromHandle__(slot PASS_REGS)
142Yap_GetDerefedFromHandle__(yhandle_t slot USES_REGS);
144Yap_GetDerefedFromHandle__(yhandle_t slot USES_REGS) {
146 return LOCAL_HandleBase[slot];
149#define Yap_GetPtrFromHandle(slot) Yap_GetPtrFromHandle__(slot PASS_REGS)
150#define Yap_GetPtrFromSlot(slot) Yap_GetPtrFromHandle__(slot PASS_REGS)
154Yap_GetPtrFromHandle__(yhandle_t slot USES_REGS);
156Yap_GetPtrFromHandle__(yhandle_t slot USES_REGS) {
158 return (Term *)LOCAL_HandleBase[slot];
161#define Yap_AddressFromHandle(slot) Yap_AddressFromHandle__(slot PASS_REGS)
162#define Yap_AddressFromSlot(slot) Yap_AddressFromHandle__(slot PASS_REGS)
165Yap_AddressFromHandle__(yhandle_t slot USES_REGS);
167Yap_AddressFromHandle__(yhandle_t slot USES_REGS) {
170 return LOCAL_HandleBase + slot;
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,
178INLINE_ONLY
void Yap_PutInHandle__(yhandle_t slot,
181 LOCAL_HandleBase[slot] = t;
185#define Yap_Max(X, Y) (X > Y ? X : Y)
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);
192 inc = Yap_Max(inc, (
size_t)N + 16);
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;
199 SYSTEM_ERROR_INTERNAL, 0 ,
200 "Out of memory for the term handles (term_t) aka slots, l needed",
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)
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;
220 ensure_slots(1 PASS_REGS);
223 }
else if (IsVarTerm(t) && (H0 > (CELL*)t || (CELL*)t > HR)) {
225 Yap_unify(t,(CELL)HR); t = (CELL)HR++;
227 LOCAL_HandleBase[old_slots] = t;
235#define Yap_NewHandles(n) Yap_NewHandles__(n PASS_REGS)
236#define Yap_NewSlots(n) Yap_NewHandles__(n PASS_REGS)
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;
244 ensure_slots(n PASS_REGS);
245 for (i = 0; i < n; i++) {
246 LOCAL_HandleBase[old_slots + i] = MkVarTerm();
248 LOCAL_CurHandle += n;
257#define Yap_InitHandles(n, ts) Yap_InitHandles__(n, ts PASS_REGS)
258#define Yap_InitSlots(n, ts) Yap_InitHandles__(n, ts PASS_REGS)
261INLINE_ONLY yhandle_t Yap_InitHandles__(
int n,
263INLINE_ONLY yhandle_t Yap_InitHandles__(
int n,
264 Term *ts USES_REGS) {
265 yhandle_t old_slots = LOCAL_CurHandle;
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;
275#define Yap_InitHandleWithAddress(pt) Yap_InitHandleWithAddress__(pt PASS_REGS)
278Yap_InitHandleWithAddress__( CELL *pt USES_REGS) {
281 yhandle_t old_slots = LOCAL_CurHandle;
283 ensure_slots(1 PASS_REGS);
284 LOCAL_HandleBase[old_slots] = (CELL)pt;
291#define Yap_RecoverHandles(n, ts) Yap_RecoverHandles__(n, ts PASS_REGS)
292#define Yap_RecoverSlots(n, ts) Yap_RecoverHandles__(n, ts PASS_REGS)
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)
301 if (n > LOCAL_CurHandle) {
302 Yap_Error(SYSTEM_ERROR_INTERNAL, 0,
303 "Inconsistent slot state in Yap_RecoverHandles.", 0);
307 LOCAL_CurHandle = topHandle;
312#define Yap_PopSlot(ts) Yap_PopHandle__(ts PASS_REGS)
313#define Yap_PopHandle(ts) Yap_PopHandle__(ts PASS_REGS)
316static inline Term Yap_PopHandle__(yhandle_t topHandle USES_REGS) {
317 if (LOCAL_CurHandle < topHandle)
320 LOCAL_CurHandle = topHandle;
322 return Deref(LOCAL_HandleBase[topHandle]);
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)
330static inline Term Yap_PopHandles__(yhandle_t topHandle,
size_t n, Term *ar USES_REGS) {
332 if (LOCAL_CurHandle < topHandle)
335 LOCAL_CurHandle = topHandle;
338 ar[i] = Deref(LOCAL_HandleBase[topHandle+i]);
344#define Yap_PopSlotWithAddress(ts) Yap_PopHandleWithAddress__(ts PASS_REGS)
345#define Yap_PopHandleWithAddress(ts) Yap_PopHandleWithAddress__(ts PASS_REGS)
348static inline CELL * Yap_PopHandleWithAddress__(yhandle_t topHandle USES_REGS) {
349 if (LOCAL_CurHandle < topHandle)
352 LOCAL_CurHandle = topHandle;
354 return (CELL*)(LOCAL_HandleBase[topHandle]);