| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | /*************************************************************************
 | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *	 YAP Prolog 							 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *	Yap Prolog was developed at NCCUP - Universidade do Porto	 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | ************************************************************************** | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * File:		bb.c							 * | 
					
						
							|  |  |  | * Last rev:	12/29/99						 * | 
					
						
							|  |  |  | * mods:									 * | 
					
						
							|  |  |  | * comments:	YAP's blackboard routines				 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *************************************************************************/ | 
					
						
							|  |  |  | #ifdef SCCS
 | 
					
						
							|  |  |  | static char     SccsId[] = "%W% %G%"; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Yap.h"
 | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  | #include "clause.h"
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | #ifndef NULL
 | 
					
						
							|  |  |  | #define NULL (void *)0
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BBProp  | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | PutBBProp(AtomEntry *ae, Term mod USES_REGS)		/* get BBentry for at; */ | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Prop          p0; | 
					
						
							|  |  |  |   BBProp        p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   WRITE_LOCK(ae->ARWLock); | 
					
						
							| 
									
										
										
										
											2001-10-30 16:42:05 +00:00
										 |  |  |   p = RepBBProp(p0 = ae->PropsOfAE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   while (p0 != NIL && (!IsBBProperty(p->KindOfPE) || | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  | 		(p->ModuleOfBB != mod))) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     p = RepBBProp(p0 = p->NextOfPE); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (p0 == NIL) { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     p = (BBProp)Yap_AllocAtomSpace(sizeof(*p)); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     if (p == NULL) { | 
					
						
							|  |  |  |       WRITE_UNLOCK(ae->ARWLock); | 
					
						
							| 
									
										
										
										
											2004-11-19 22:08:43 +00:00
										 |  |  |       Yap_Error(OUT_OF_HEAP_ERROR,ARG1,"could not allocate space in bb_put/2"); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return(NULL); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-17 11:16:21 -07:00
										 |  |  |     AddPropToAtom(ae, (PropEntry *)p); | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |     p->ModuleOfBB = mod; | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |     p->Element = 0L; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     p->KeyOfBB = AbsAtom(ae); | 
					
						
							|  |  |  |     p->KindOfPE = BBProperty; | 
					
						
							|  |  |  |     INIT_RWLOCK(p->BBRWLock);     | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   WRITE_UNLOCK(ae->ARWLock); | 
					
						
							|  |  |  |   return (p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BBProp  | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | PutIntBBProp(Int key, Term mod USES_REGS)	/* get BBentry for at; */ | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Prop          p0; | 
					
						
							|  |  |  |   BBProp        p; | 
					
						
							|  |  |  |   UInt hash_key; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (INT_BB_KEYS == NULL) { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     INT_BB_KEYS = (Prop *)Yap_AllocCodeSpace(sizeof(Prop)*INT_BB_KEYS_SIZE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     if (INT_BB_KEYS != NULL) { | 
					
						
							|  |  |  |       UInt i = 0; | 
					
						
							|  |  |  |       Prop *pp = INT_BB_KEYS; | 
					
						
							|  |  |  |       for (i = 0; i < INT_BB_KEYS_SIZE; i++) { | 
					
						
							|  |  |  | 	pp[0] = NIL; | 
					
						
							|  |  |  | 	pp++; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2004-11-19 22:08:43 +00:00
										 |  |  |       Yap_Error(OUT_OF_HEAP_ERROR,ARG1,"could not allocate space in bb_put/2"); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return(NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   hash_key = (CELL)key % INT_BB_KEYS_SIZE; | 
					
						
							|  |  |  |   p0 = INT_BB_KEYS[hash_key]; | 
					
						
							|  |  |  |   p = RepBBProp(p0); | 
					
						
							|  |  |  |   while (p0 != NIL && (!IsBBProperty(p->KindOfPE) || | 
					
						
							|  |  |  | 		       key != (Int)(p->KeyOfBB) || | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  | 		(p->ModuleOfBB != mod))) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     p = RepBBProp(p0 = p->NextOfPE); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (p0 == NIL) { | 
					
						
							|  |  |  |     YAPEnterCriticalSection(); | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     p = (BBProp)Yap_AllocAtomSpace(sizeof(*p)); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     if (p == NULL) { | 
					
						
							|  |  |  |       YAPLeaveCriticalSection(); | 
					
						
							| 
									
										
										
										
											2004-11-19 22:08:43 +00:00
										 |  |  |       Yap_Error(OUT_OF_HEAP_ERROR,ARG1,"could not allocate space in bb_put/2"); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return(NULL); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |     p->ModuleOfBB = mod; | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |     p->Element = 0L; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     p->KeyOfBB = (Atom)key; | 
					
						
							|  |  |  |     p->KindOfPE = BBProperty; | 
					
						
							|  |  |  |     p->NextOfPE = INT_BB_KEYS[hash_key]; | 
					
						
							|  |  |  |     INT_BB_KEYS[hash_key] = AbsBBProp(p); | 
					
						
							|  |  |  |     YAPLeaveCriticalSection(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return (p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BBProp  | 
					
						
							| 
									
										
										
										
											2004-02-12 12:37:12 +00:00
										 |  |  | GetBBProp(AtomEntry *ae, Term mod)		/* get BBentry for at; */ | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Prop          p0; | 
					
						
							|  |  |  |   BBProp        p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   READ_LOCK(ae->ARWLock); | 
					
						
							| 
									
										
										
										
											2001-10-30 16:42:05 +00:00
										 |  |  |   p = RepBBProp(p0 = ae->PropsOfAE); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   while (p0 != NIL && (!IsBBProperty(p->KindOfPE) || | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  | 		(p->ModuleOfBB != mod))) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     p = RepBBProp(p0 = p->NextOfPE); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   READ_UNLOCK(ae->ARWLock); | 
					
						
							|  |  |  |   if (p0 == NIL) { | 
					
						
							|  |  |  |     return(NULL); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return (p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BBProp  | 
					
						
							| 
									
										
										
										
											2004-02-12 12:37:12 +00:00
										 |  |  | GetIntBBProp(Int key, Term mod)		/* get BBentry for at; */ | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Prop          p0; | 
					
						
							|  |  |  |   BBProp        p; | 
					
						
							|  |  |  |   UInt hash_key; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (INT_BB_KEYS == NULL) | 
					
						
							|  |  |  |     return(NULL); | 
					
						
							|  |  |  |   hash_key = (CELL)key % INT_BB_KEYS_SIZE; | 
					
						
							|  |  |  |   p0 = INT_BB_KEYS[hash_key]; | 
					
						
							|  |  |  |   p = RepBBProp(p0); | 
					
						
							|  |  |  |   while (p0 != NIL && (!IsBBProperty(p->KindOfPE) || | 
					
						
							|  |  |  | 		       key != (Int)(p->KeyOfBB) || | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  | 		(p->ModuleOfBB != mod))) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     p = RepBBProp(p0 = p->NextOfPE); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (p0 == NIL) { | 
					
						
							|  |  |  |     return(NULL); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return (p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | resize_bb_int_keys(UInt new_size) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   Prop *new; | 
					
						
							|  |  |  |   UInt i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   YAPEnterCriticalSection(); | 
					
						
							|  |  |  |   if (INT_BB_KEYS == NULL) { | 
					
						
							|  |  |  |     INT_BB_KEYS_SIZE = new_size; | 
					
						
							|  |  |  |     YAPLeaveCriticalSection(); | 
					
						
							|  |  |  |     return(TRUE); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |   new = (Prop *)Yap_AllocCodeSpace(sizeof(Prop)*new_size); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   if (new == NULL) { | 
					
						
							|  |  |  |     YAPLeaveCriticalSection(); | 
					
						
							| 
									
										
										
										
											2004-11-19 22:08:43 +00:00
										 |  |  |     Yap_Error(OUT_OF_HEAP_ERROR,ARG1,"could not allocate space"); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(FALSE); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (i = 0; i < new_size; i++) { | 
					
						
							|  |  |  |     new[i] = NIL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (i = 0; i < INT_BB_KEYS_SIZE; i++) { | 
					
						
							|  |  |  |     if (INT_BB_KEYS[i] != NIL) { | 
					
						
							|  |  |  |       Prop p0 = INT_BB_KEYS[i]; | 
					
						
							|  |  |  |       while (p0 != NIL) { | 
					
						
							|  |  |  | 	BBProp p = RepBBProp(p0); | 
					
						
							|  |  |  | 	CELL key = (CELL)(p->KeyOfBB); | 
					
						
							|  |  |  | 	UInt hash_key = (CELL)key % new_size; | 
					
						
							|  |  |  | 	p0 = p->NextOfPE; | 
					
						
							|  |  |  | 	p->NextOfPE = new[hash_key]; | 
					
						
							|  |  |  | 	new[hash_key] = AbsBBProp(p); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |   Yap_FreeCodeSpace((char *)INT_BB_KEYS); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   INT_BB_KEYS = new; | 
					
						
							|  |  |  |   INT_BB_KEYS_SIZE = new_size; | 
					
						
							|  |  |  |   YAPLeaveCriticalSection(); | 
					
						
							|  |  |  |   return(TRUE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BBProp | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | AddBBProp(Term t1, char *msg, Term mod USES_REGS) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   BBProp p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |  restart: | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   if (IsVarTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     Yap_Error(INSTANTIATION_ERROR, t1, msg); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(NULL); | 
					
						
							|  |  |  |   } if (IsAtomTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |     p = PutBBProp(RepAtom(AtomOfTerm(t1)), mod PASS_REGS); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } else if (IsIntegerTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |     p = PutIntBBProp(IntegerOfTerm(t1), mod PASS_REGS); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } else if (IsApplTerm(t1) && FunctorOfTerm(t1) == FunctorModule) { | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |     Term tmod = ArgOfTerm(1, t1); | 
					
						
							|  |  |  |     if (!IsVarTerm(tmod) ) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       t1 = ArgOfTerm(2, t1); | 
					
						
							| 
									
										
										
										
											2004-02-12 12:37:12 +00:00
										 |  |  |       mod = tmod; | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |       goto restart; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |       Yap_Error(INSTANTIATION_ERROR, t1, msg); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return(NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     Yap_Error(TYPE_ERROR_ATOM, t1, msg); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(NULL); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return(p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BBProp | 
					
						
							| 
									
										
										
										
											2004-02-12 12:37:12 +00:00
										 |  |  | FetchBBProp(Term t1, char *msg, Term mod) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   BBProp p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |  restart: | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   if (IsVarTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     Yap_Error(INSTANTIATION_ERROR, t1, msg); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(NULL); | 
					
						
							|  |  |  |   } if (IsAtomTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |     p = GetBBProp(RepAtom(AtomOfTerm(t1)), mod); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } else if (IsIntegerTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |     p = GetIntBBProp(IntegerOfTerm(t1), mod); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } else if (IsApplTerm(t1) && FunctorOfTerm(t1) == FunctorModule) { | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |     Term tmod = ArgOfTerm(1, t1); | 
					
						
							|  |  |  |     if (!IsVarTerm(tmod) ) { | 
					
						
							| 
									
										
										
										
											2004-02-12 12:37:12 +00:00
										 |  |  |       mod = tmod; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       t1 = ArgOfTerm(2, t1); | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |       goto restart; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |       Yap_Error(INSTANTIATION_ERROR, t1, msg); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return(NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     Yap_Error(TYPE_ERROR_ATOM, t1, msg); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(NULL); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return(p); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  | static Term  | 
					
						
							|  |  |  | BBPut(Term t0, Term t2) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!IsVarTerm(t0) && IsApplTerm(t0)) { | 
					
						
							|  |  |  |     Yap_ErLogUpdCl((LogUpdClause *)DBRefOfTerm(t0)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (IsVarTerm(t2) || IsAtomOrIntTerm(t2)) { | 
					
						
							|  |  |  |     return t2; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     LogUpdClause *cl = Yap_new_ludbe(t2, NULL, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (cl == NULL) { | 
					
						
							|  |  |  |       return 0L; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return MkDBRefTerm((DBRef)cl); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | static Int | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | p_bb_put( USES_REGS1 ) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Term t1 = Deref(ARG1); | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   BBProp p = AddBBProp(t1, "bb_put/2", CurrentModule PASS_REGS); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (p == NULL) { | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(FALSE); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   WRITE_LOCK(p->BBRWLock);     | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |     if (p->Element) | 
					
						
							|  |  |  |     fprintf(stderr,"putting %p, size %d\n", p, p->Element->NOfCells); | 
					
						
							|  |  |  |   */ | 
					
						
							|  |  |  |   p->Element = BBPut(p->Element, Deref(ARG2)); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   WRITE_UNLOCK(p->BBRWLock); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   return (p->Element != 0L); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | BBGet(Term t, UInt arity USES_REGS) | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   if (IsVarTerm(t)) { | 
					
						
							|  |  |  |     return MkVarTerm(); | 
					
						
							|  |  |  |   } else if (IsAtomOrIntTerm(t)) { | 
					
						
							|  |  |  |     return t; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return Yap_LUInstance((LogUpdClause *)DBRefOfTerm(t), arity); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Int | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | p_bb_get( USES_REGS1 ) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Term t1 = Deref(ARG1); | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |   BBProp p = FetchBBProp(t1, "bb_get/2", CurrentModule); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   Term out, t0; | 
					
						
							|  |  |  |   if (p == NULL || p->Element == 0L) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(FALSE); | 
					
						
							|  |  |  |   READ_LOCK(p->BBRWLock);   | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |     if (p->Element) | 
					
						
							|  |  |  |       fprintf(stderr,"getting %p, size %d\n", p, p->Element->NOfCells); | 
					
						
							|  |  |  |   */ | 
					
						
							|  |  |  |   t0 = p->Element; | 
					
						
							|  |  |  |   READ_UNLOCK(p->BBRWLock);   | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   out = BBGet(t0, 2 PASS_REGS); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   return Yap_unify(ARG2,out); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Int | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | p_bb_delete( USES_REGS1 ) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Term t1 = Deref(ARG1); | 
					
						
							|  |  |  |   BBProp p; | 
					
						
							|  |  |  |   Term out; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |   p = FetchBBProp(t1, "bb_delete/2", CurrentModule); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   if (p == NULL || p->Element == 0L) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(FALSE); | 
					
						
							|  |  |  |   WRITE_LOCK(p->BBRWLock);   | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   out = BBGet(p->Element, 2 PASS_REGS); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   if (!IsVarTerm(p->Element) && IsApplTerm(p->Element)) { | 
					
						
							|  |  |  |     Yap_ErLogUpdCl((LogUpdClause *)DBRefOfTerm(p->Element)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   p->Element = 0L; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   WRITE_UNLOCK(p->BBRWLock);   | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   return Yap_unify(ARG2,out); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Int | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | p_bb_update( USES_REGS1 ) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Term t1 = Deref(ARG1); | 
					
						
							|  |  |  |   BBProp p; | 
					
						
							|  |  |  |   Term out; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-15 00:01:43 +00:00
										 |  |  |   p = FetchBBProp(t1, "bb_update/3", CurrentModule); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   if (p == NULL || p->Element == 0L) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   WRITE_LOCK(p->BBRWLock);   | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   out = BBGet(p->Element, 3 PASS_REGS); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   if (!Yap_unify(out,ARG2)) { | 
					
						
							|  |  |  |     WRITE_UNLOCK(p->BBRWLock); | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   p->Element = BBPut(p->Element, Deref(ARG3)); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   WRITE_UNLOCK(p->BBRWLock); | 
					
						
							| 
									
										
										
										
											2005-06-01 13:53:46 +00:00
										 |  |  |   return (p->Element != 0L); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Int | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  | p_resize_bb_int_keys( USES_REGS1 ) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Term t1 = Deref(ARG1); | 
					
						
							|  |  |  |   if (IsVarTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     return(Yap_unify(ARG1,MkIntegerTerm((Int)INT_BB_KEYS_SIZE))); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (!IsIntegerTerm(t1)) { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     Yap_Error(TYPE_ERROR_INTEGER, t1, "yap_flag(resize_bb_int_keys,T)"); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return(FALSE); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return(resize_bb_int_keys(IntegerOfTerm(t1))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void  | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  | Yap_InitBBPreds(void) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |   Yap_InitCPred("bb_put", 2, p_bb_put, 0); | 
					
						
							|  |  |  |   Yap_InitCPred("bb_get", 2, p_bb_get, 0); | 
					
						
							|  |  |  |   Yap_InitCPred("bb_delete", 2, p_bb_delete, 0); | 
					
						
							|  |  |  |   Yap_InitCPred("bb_update", 3, p_bb_update, 0); | 
					
						
							| 
									
										
										
										
											2004-11-18 22:32:40 +00:00
										 |  |  |   Yap_InitCPred("$resize_bb_int_keys", 1, p_resize_bb_int_keys, SafePredFlag|SyncPredFlag|HiddenPredFlag); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |