19static char SccsId[] =
"%W% %G%";
52PutBBProp(
AtomEntry *ae, Term mod USES_REGS)
57 WRITE_LOCK(ae->ARWLock);
58 p = RepBBProp(p0 = ae->PropsOfAE);
59 while (p0 != NIL && (!IsBBProperty(p->KindOfPE) ||
60 (p->ModuleOfBB != mod))) {
61 p = RepBBProp(p0 = p->NextOfPE);
64 p = (
BBProp)Yap_AllocAtomSpace(
sizeof(*p));
66 WRITE_UNLOCK(ae->ARWLock);
67 Yap_Error(RESOURCE_ERROR_HEAP,ARG1,
"could not allocate space in bb_put/2");
73 p->KeyOfBB = AbsAtom(ae);
74 p->KindOfPE = BBProperty;
75 INIT_RWLOCK(p->BBRWLock);
77 WRITE_UNLOCK(ae->ARWLock);
82PutIntBBProp(Int key, Term mod USES_REGS)
88 if (INT_BB_KEYS == NULL) {
89 INT_BB_KEYS = (
Prop *)Yap_AllocCodeSpace(
sizeof(
Prop)*INT_BB_KEYS_SIZE);
90 if (INT_BB_KEYS != NULL) {
92 Prop *pp = INT_BB_KEYS;
93 for (i = 0; i < INT_BB_KEYS_SIZE; i++) {
98 Yap_Error(RESOURCE_ERROR_HEAP,ARG1,
"could not allocate space in bb_put/2");
102 hash_key = (CELL)key % INT_BB_KEYS_SIZE;
103 p0 = INT_BB_KEYS[hash_key];
105 while (p0 != NIL && (!IsBBProperty(p->KindOfPE) ||
106 key != (Int)(p->KeyOfBB) ||
107 (p->ModuleOfBB != mod))) {
108 p = RepBBProp(p0 = p->NextOfPE);
111 YAPEnterCriticalSection();
112 p = (
BBProp)Yap_AllocAtomSpace(
sizeof(*p));
114 YAPLeaveCriticalSection();
115 Yap_Error(RESOURCE_ERROR_HEAP,ARG1,
"could not allocate space in bb_put/2");
120 p->KeyOfBB = (
Atom)key;
121 p->KindOfPE = BBProperty;
122 p->NextOfPE = INT_BB_KEYS[hash_key];
123 INT_BB_KEYS[hash_key] = AbsBBProp(p);
124 YAPLeaveCriticalSection();
135 READ_LOCK(ae->ARWLock);
136 p = RepBBProp(p0 = ae->PropsOfAE);
137 while (p0 != NIL && (!IsBBProperty(p->KindOfPE) ||
138 (p->ModuleOfBB != mod))) {
139 p = RepBBProp(p0 = p->NextOfPE);
141 READ_UNLOCK(ae->ARWLock);
149GetIntBBProp(Int key, Term mod)
155 if (INT_BB_KEYS == NULL)
157 hash_key = (CELL)key % INT_BB_KEYS_SIZE;
158 p0 = INT_BB_KEYS[hash_key];
160 while (p0 != NIL && (!IsBBProperty(p->KindOfPE) ||
161 key != (Int)(p->KeyOfBB) ||
162 (p->ModuleOfBB != mod))) {
163 p = RepBBProp(p0 = p->NextOfPE);
172resize_bb_int_keys(UInt new_size) {
177 YAPEnterCriticalSection();
178 if (INT_BB_KEYS == NULL) {
179 INT_BB_KEYS_SIZE = new_size;
180 YAPLeaveCriticalSection();
183 new = (
Prop *)Yap_AllocCodeSpace(
sizeof(
Prop)*new_size);
185 YAPLeaveCriticalSection();
186 Yap_Error(RESOURCE_ERROR_HEAP,ARG1,
"could not allocate space");
189 for (i = 0; i < new_size; i++) {
192 for (i = 0; i < INT_BB_KEYS_SIZE; i++) {
193 if (INT_BB_KEYS[i] != NIL) {
194 Prop p0 = INT_BB_KEYS[i];
197 CELL key = (CELL)(p->KeyOfBB);
198 UInt hash_key = (CELL)key % new_size;
200 p->NextOfPE =
new[hash_key];
201 new[hash_key] = AbsBBProp(p);
205 Yap_FreeCodeSpace((
char *)INT_BB_KEYS);
207 INT_BB_KEYS_SIZE = new_size;
208 YAPLeaveCriticalSection();
213AddBBProp(Term t1,
char *msg, Term mod USES_REGS)
219 Yap_Error(INSTANTIATION_ERROR, t1, msg);
221 }
if (IsAtomTerm(t1)) {
222 p = PutBBProp(RepAtom(AtomOfTerm(t1)), mod PASS_REGS);
223 }
else if (IsIntegerTerm(t1)) {
224 p = PutIntBBProp(IntegerOfTerm(t1), mod PASS_REGS);
225 }
else if (IsApplTerm(t1) && FunctorOfTerm(t1) == FunctorModule) {
226 Term tmod = ArgOfTerm(1, t1);
227 if (!IsVarTerm(tmod) ) {
228 t1 = ArgOfTerm(2, t1);
232 Yap_Error(INSTANTIATION_ERROR, t1, msg);
236 Yap_Error(TYPE_ERROR_ATOM, t1, msg);
243FetchBBProp(Term t1,
char *msg, Term mod)
249 Yap_Error(INSTANTIATION_ERROR, t1, msg);
251 }
if (IsAtomTerm(t1)) {
252 p = GetBBProp(RepAtom(AtomOfTerm(t1)), mod);
253 }
else if (IsIntegerTerm(t1)) {
254 p = GetIntBBProp(IntegerOfTerm(t1), mod);
255 }
else if (IsApplTerm(t1) && FunctorOfTerm(t1) == FunctorModule) {
256 Term tmod = ArgOfTerm(1, t1);
257 if (!IsVarTerm(tmod) ) {
259 t1 = ArgOfTerm(2, t1);
262 Yap_Error(INSTANTIATION_ERROR, t1, msg);
266 Yap_Error(TYPE_ERROR_ATOM, t1, msg);
273BBPut(Term t0, Term t2)
275 if (!IsVarTerm(t0) && IsApplTerm(t0)) {
278 if (IsVarTerm(t2) || IsAtomOrIntTerm(t2)) {
284 Yap_ThrowError(SYSTEM_ERROR_INTERNAL,t0,
"failed to insert entry in blackboard ~a");
286 return MkDBRefTerm((
DBRef)cl);
299p_bb_put( USES_REGS1 )
301 Term t1 = Deref(ARG1);
302 Term mod = CurrentModule;
303 t1 = Yap_StripModule(t1,&mod);
304 BBProp p = AddBBProp(t1,
"bb_put/2", mod PASS_REGS);
309 WRITE_LOCK(p->BBRWLock);
314 p->Element = BBPut(p->Element, Deref(ARG2));
315 WRITE_UNLOCK(p->BBRWLock);
316 return (p->Element != 0L);
320BBGet(Term t, UInt arity USES_REGS)
324 }
else if (IsAtomOrIntTerm(t)) {
327 return Yap_LUInstance((
LogUpdClause *)DBRefOfTerm(t), arity);
340p_bb_get( USES_REGS1 )
342 Term t1 = Deref(ARG1);
343 Term mod = CurrentModule;
344 t1 = Yap_StripModule(t1,&mod);
345 BBProp p = FetchBBProp(t1,
"bb_get/2", mod);
347 if (p == NULL || p->Element == 0L)
349 READ_LOCK(p->BBRWLock);
355 READ_UNLOCK(p->BBRWLock);
356 out = BBGet(t0, 2 PASS_REGS);
357 return Yap_unify(ARG2,out);
369p_bb_delete( USES_REGS1 )
371 Term t1 = Deref(ARG1);
373 Term out, mod = CurrentModule;
375 p = FetchBBProp(t1,
"bb_delete/2", mod);
376 if (p == NULL || p->Element == 0L)
378 WRITE_LOCK(p->BBRWLock);
379 out = BBGet(p->Element, 2 PASS_REGS);
380 if (!IsVarTerm(p->Element) && IsApplTerm(p->Element)) {
381 Yap_ErLogUpdCl((
LogUpdClause *)DBRefOfTerm(p->Element));
384 WRITE_UNLOCK(p->BBRWLock);
385 return Yap_unify(ARG2,out);
397p_bb_update( USES_REGS1 )
399 Term t1 = Deref(ARG1);
402Term mod = CurrentModule;
403 t1 = Yap_StripModule(t1,&mod);
404 p = FetchBBProp(t1,
"bb_update/3", mod);
405 if (p == NULL || p->Element == 0L)
407 WRITE_LOCK(p->BBRWLock);
408 out = BBGet(p->Element, 3 PASS_REGS);
409 if (!Yap_unify(out,ARG2)) {
410 WRITE_UNLOCK(p->BBRWLock);
413 p->Element = BBPut(p->Element, Deref(ARG3));
414 WRITE_UNLOCK(p->BBRWLock);
415 return (p->Element != 0L);
419p_resize_bb_int_keys( USES_REGS1 )
421 Term t1 = Deref(ARG1);
423 return(Yap_unify(ARG1,MkIntegerTerm((Int)INT_BB_KEYS_SIZE)));
425 if (!IsIntegerTerm(t1)) {
426 Yap_Error(TYPE_ERROR_INTEGER, t1,
"yap_flag(resize_bb_int_keys,T)");
429 return(resize_bb_int_keys(IntegerOfTerm(t1)));
435 Yap_InitCPred(
"bb_put", 2, p_bb_put, 0);
436 Yap_InitCPred(
"bb_get", 2, p_bb_get, 0);
437 Yap_InitCPred(
"bb_delete", 2, p_bb_delete, 0);
438 Yap_InitCPred(
"bb_update", 3, p_bb_update, 0);
439 Yap_InitCPred(
"$resize_bb_int_keys", 1, p_resize_bb_int_keys, SafePredFlag|SyncPredFlag);