129static Int compile_array_refs(USES_REGS1);
130static Int array_refs_compiled(USES_REGS1);
131static Int sync_mmapped_arrays(USES_REGS1);
210static Int create_array(USES_REGS1);
211static Int create_mmapped_array(USES_REGS1);
212static Int array_references(USES_REGS1);
213static Int static_array(USES_REGS1);
214static Int resize_static_array(USES_REGS1);
215static Int close_static_array(USES_REGS1);
216static Int access_array(USES_REGS1);
217static Int assign_static(USES_REGS1);
218static Int assign_dynamic(USES_REGS1);
237typedef struct MMAP_ARRAY_BLOCK {
243 struct MMAP_ARRAY_BLOCK *next;
247 mmap_array_block *ptr = GLOBAL_mmap_arrays, *optr = GLOBAL_mmap_arrays;
249 while (ptr != NULL && ptr->start != area) {
254#if !defined(USE_SYSTEM_MALLOC)
255 Yap_FullError(SYSTEM_ERROR_INTERNAL, ARG1,
256 "close_mmapped_array (array chain incoherent)",
261 if (munmap(ptr->start, ptr->size) == -1) {
262 Yap_FullError(SYSTEM_ERROR_INTERNAL, ARG1,
263 "close_mmapped_array (munmap: %s)", strerror(errno));
266 optr->next = ptr->next;
267 pp->ValueOfVE.ints = NULL;
269 if (close(ptr->fd) < 0) {
270 Yap_FullError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1,
271 "close_mmapped_array (close: %s)", strerror(errno));
274 Yap_FreeAtomSpace((
char *)ptr);
279 void *area USES_REGS) {
280 mmap_array_block *ptr = GLOBAL_mmap_arrays;
282 while (ptr != NULL && ptr->start != area) {
292 if (munmap(ptr->start, ptr->size) == -1) {
293 Yap_FullError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1,
294 "resize_mmapped_array (munmap: %s)", strerror(errno));
297 total_size = (ptr->size / ptr->items) * dim;
298 if (ftruncate(ptr->fd, total_size) < 0) {
299 Yap_FullError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1,
300 "resize_mmapped_array (ftruncate: %s)", strerror(errno));
303 if (lseek(ptr->fd, total_size - 1, SEEK_SET) < 0) {
304 Yap_ThrowError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1,
305 "resize_mmapped_array (lseek: %s)", strerror(errno));
308 if (write(ptr->fd,
"", 1) < 0) {
309 Yap_FullError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1,
310 "resize_mmapped_array (write: %s)", strerror(errno));
313 if ((ptr->start = (
void *)mmap(0, (
size_t)total_size, PROT_READ | PROT_WRITE,
314 MAP_SHARED, ptr->fd, 0)) == (
void *)-1) {
315 Yap_FullError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1,
316 "resize_mmapped_array (mmap: %s)", ___LINE__, __FUNCTION__,
317 -__FILE__, strerror(errno));
320 ptr->size = total_size;
322 pp->ValueOfVE.chars = ptr->start;
327static Term GetTermFromArray(
DBTerm *ref USES_REGS) {
331 while ((TRef = Yap_FetchTermFromDB(ref)) == 0L) {
333 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
339 Yap_ThrowError(DOMAIN_ERROR_NOT_ZERO, ARG1,
"Null reference.");
344static Term GetNBTerm(
live_term *ar, Int indx USES_REGS) {
346 Term livet = ar[indx].tlive;
348 if (!IsVarTerm(livet)) {
349 if (!IsApplTerm(livet)) {
351 }
else if (FunctorOfTerm(livet) == FunctorAtFoundOne) {
352 return Yap_ReadTimedVar(livet);
357 Term termt = ar[indx].tstore;
359 if (!IsUnboundVar(&(ar[indx].tlive))) {
362 if (IsVarTerm(termt)) {
364 }
else if (IsAtomicTerm(termt)) {
368 if ((livet = GetTermFromArray(ref PASS_REGS)) == 0) {
372 YapBind(&(ar[indx].tlive), livet);
382 READ_LOCK(ae->ARWLock);
383 pp = RepArrayProp(ae->PropsOfAE);
384 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty
386 && pp->owner_id != worker_id
389 pp = RepArrayProp(pp->NextOfPE);
390 READ_UNLOCK(ae->ARWLock);
394static Term AccessNamedArray(
Atom a, Int indx USES_REGS) {
398 pp = GetArrayEntry(ae, worker_id);
400 if (!EndOfPAEntr(pp)) {
401 if (ArrayIsDynamic(pp)) {
403 READ_LOCK(pp->ArRWLock);
404 if (IsVarTerm(pp->ValueOfVE)) {
405 READ_UNLOCK(pp->ArRWLock);
406 Yap_ThrowError(INSTANTIATION_ERROR, ARG1,
"unbound static array", indx);
408 if (pp->ArrayEArity <= indx || indx < 0) {
409 READ_UNLOCK(pp->ArRWLock);
410 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, ARG1,
"bad index %ld", indx);
412 out = RepAppl(pp->ValueOfVE)[indx + 1];
413 READ_UNLOCK(pp->ArRWLock);
418 READ_LOCK(ptr->ArRWLock);
419 if (pp->ArrayEArity <= indx || indx < 0) {
420 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, ARG1,
"bad index %ld", indx);
422 switch (ptr->ArrayType) {
424 case array_of_ints: {
426 out = MkIntegerTerm(ptr->ValueOfVE.ints[indx]);
427 READ_UNLOCK(ptr->ArRWLock);
430 case array_of_doubles: {
432 out = MkEvalFl(ptr->ValueOfVE.floats[indx]);
433 READ_UNLOCK(ptr->ArRWLock);
436 case array_of_ptrs: {
438 out = MkIntegerTerm((Int)(ptr->ValueOfVE.ptrs[indx]));
439 READ_UNLOCK(ptr->ArRWLock);
442 case array_of_atoms: {
444 out = ptr->ValueOfVE.atoms[indx];
445 READ_UNLOCK(ptr->ArRWLock);
452 case array_of_chars: {
454 out = MkIntegerTerm((Int)(ptr->ValueOfVE.chars[indx]));
455 READ_UNLOCK(ptr->ArRWLock);
458 case array_of_uchars: {
460 out = MkIntegerTerm((Int)(ptr->ValueOfVE.uchars[indx]));
461 READ_UNLOCK(ptr->ArRWLock);
464 case array_of_dbrefs: {
466 Term TRef = ptr->ValueOfVE.dbrefs[indx];
468 READ_UNLOCK(ptr->ArRWLock);
470 DBRef ref = DBRefOfTerm(TRef);
474 INC_DBREF_COUNT(ref);
478 if (ref->Flags & LogUpdMask) {
481 if (!(cl->ClFlags & InUseMask)) {
482 cl->ClFlags |= InUseMask;
486 if (!(ref->Flags & InUseMask)) {
487 ref->Flags |= InUseMask;
493 P = (
yamop *)FAILCODE;
498 case array_of_nb_terms: {
500 Term out = GetNBTerm(ptr->ValueOfVE.lterms, indx PASS_REGS);
501 READ_UNLOCK(ptr->ArRWLock);
505 case array_of_terms: {
507 DBTerm *ref = ptr->ValueOfVE.terms[indx];
509 READ_UNLOCK(ptr->ArRWLock);
510 return GetTermFromArray(ref PASS_REGS);
513 READ_UNLOCK(ptr->ArRWLock);
518 Yap_ThrowError(EXISTENCE_ERROR_ARRAY, MkAtomTerm(a),
"named array");
536static Int access_array(USES_REGS1) {
537 Term t = Deref(ARG1);
538 Term ti = Deref(ARG2);
542 if (IsNonVarTerm(ti)) {
544 if (IsIntegerTerm(nti = Yap_Eval(ti)))
545 indx = IntegerOfTerm(nti);
547 Yap_ThrowError(TYPE_ERROR_INTEGER, ti,
"access_array");
551 Yap_ThrowError(INSTANTIATION_ERROR, ti,
"access_array");
555 if (IsNonVarTerm(t)) {
557 if (indx >= ArityOfFunctor(FunctorOfTerm(t)) || indx < 0) {
560 P = (
yamop *)FAILCODE;
563 tf = (RepAppl(t))[indx + 1];
564 }
else if (IsAtomTerm(t)) {
565 tf = AccessNamedArray(AtomOfTerm(t), indx PASS_REGS);
566 if (tf == MkAtomTerm(AtomFoundVar)) {
570 Yap_ThrowError(TYPE_ERROR_ARRAY, t,
"access_array");
574 Yap_ThrowError(INSTANTIATION_ERROR, t,
"access_array");
577 return Yap_unify(tf, ARG3);
580static Int array_arg(USES_REGS1) {
581 register Term ti = Deref(ARG3), t;
584 if (IsNonVarTerm(ti)) {
586 if (IsIntegerTerm(nti = Yap_Eval(ti)))
587 indx = IntegerOfTerm(nti);
589 Yap_ThrowError(TYPE_ERROR_INTEGER, ti,
"access_array");
593 Yap_ThrowError(INSTANTIATION_ERROR, ti,
"array_arg");
598 if (IsNonVarTerm(t)) {
600 return (Yap_unify(((RepAppl(t))[indx + 1]), ARG1));
601 }
else if (IsAtomTerm(t)) {
602 Term tf = AccessNamedArray(AtomOfTerm(t), indx PASS_REGS);
603 if (tf == MkAtomTerm(AtomFoundVar)) {
606 return (Yap_unify(tf, ARG1));
608 Yap_ThrowError(TYPE_ERROR_ARRAY, t,
"array_arg");
610 Yap_ThrowError(INSTANTIATION_ERROR, t,
"array_arg");
615static void InitNamedArray(
ArrayEntry *p, Int dim USES_REGS) {
618 WRITE_LOCK(p->ArRWLock);
622 Bind_Global(&(p->ValueOfVE), AbsAppl(HR));
624 tp[0] = (CELL)Yap_MkFunctor(AtomArray, dim);
626 p->ArrayEArity = dim;
629 for (; tp < HR; tp++) {
632 WRITE_UNLOCK(p->ArRWLock);
639 p = (
ArrayEntry *)Yap_AllocAtomSpace(
sizeof(*p));
640 p->KindOfPE = ArrayProperty;
641 p->TypeOfAE = DYNAMIC_ARRAY;
643 INIT_RWLOCK(p->ArRWLock);
645 p->owner_id = worker_id;
647 p->NextAE = LOCAL_DynamicArrays;
648 LOCAL_DynamicArrays = p;
649 InitNamedArray(p, dim PASS_REGS);
653 static_array_types atype,
void *old,
654 size_t array_size USES_REGS) {
657 case array_of_doubles:
658 asize = array_size *
sizeof(Float);
661 asize = array_size *
sizeof(Int);
664 asize = array_size *
sizeof(char);
666 case array_of_uchars:
667 asize = array_size *
sizeof(
unsigned char);
670 asize = array_size *
sizeof(
AtomEntry *);
674 case array_of_nb_terms:
677 case array_of_dbrefs:
678 asize = array_size *
sizeof(
DBRef);
682 while ((p->ValueOfVE.floats = (Float *)Yap_AllocCodeSpace(asize)) == NULL) {
683 YAPLeaveCriticalSection();
684 if (!Yap_growheap(FALSE, asize, NULL)) {
685 Yap_ThrowError(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
688 YAPEnterCriticalSection();
691 while ((p->ValueOfVE.floats = (Float *)Yap_ReallocCodeSpace(old, asize)) ==
693 YAPLeaveCriticalSection();
694 if (!Yap_growheap(FALSE, asize, NULL)) {
695 Yap_ThrowError(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
703void * YAP_FetchArray(Term t1, intptr_t *sz,
int *type)
707 READ_LOCK(ae->ARWLock);
709while (!EndOfPAEntr(p) && p->KindOfPE != ArrayProperty){
710 p = RepStaticArrayProp(p->NextOfPE);
711} READ_UNLOCK(ae->ARWLock);
713 if (EndOfPAEntr(p)) {
722 return p->ValueOfVE.floats;
728 return p->ValueOfVE.ints;
733static Int update_all( USES_REGS1) {
739 Yap_ThrowError(INSTANTIATION_ERROR, t1,
"update_array");
745 READ_LOCK(ae->ARWLock);
746 p = RepStaticArrayProp(ae->PropsOfAE);
747 while (!EndOfPAEntr(p) && p->KindOfPE != ArrayProperty)
748 p = RepStaticArrayProp(p->NextOfPE);
750 if (EndOfPAEntr(p)) {
751 READ_UNLOCK(ae->ARWLock);
752 Yap_ThrowError(EXISTENCE_ERROR_ARRAY, t1,
"assign_static %s",
753 RepAtom(AtomOfTerm(t1))->StrOfAE);
756 Int dim = p->ArrayEArity;
757 switch (p->ArrayType) {
760 Int n = IntegerOfTerm(t), i;
761 for (i = 0; i < dim; i++)
762 p->ValueOfVE.ints[i] = n;
767 Int c = IntegerOfTerm(t), i;
768 for (i = 0; i < dim; i++)
769 p->ValueOfVE.chars[i] = c;
772 case array_of_uchars:
774 UInt c = IntegerOfTerm(t);
775 for (i = 0; i < dim; i++)
776 p->ValueOfVE.uchars[i] = c;
779 case array_of_doubles:
782 Float f = FloatOfTerm(t);
783 for (i = 0; i < dim; i++)
784 p->ValueOfVE.uchars[i] = f;
790 void *pt = AddressOfTerm(t);
791 for (i = 0; i < dim; i++)
792 p->ValueOfVE.ptrs[i] = pt;
798 for (i = 0; i < dim; i++)
799 p->ValueOfVE.atoms[i] = t;
802 case array_of_dbrefs:
806 for (i = 0; i < dim; i++)
807 p->ValueOfVE.terms[i] = TermToDBTerm(t);
810 case array_of_nb_terms:
813 Term tn = Yap_SaveTerm(t);
814 for (i = 0; i < dim; i++) {
815 p->ValueOfVE.lterms[i].tstore = tn;
825 static_array_types type,
828 if (EndOfPAEntr(p)) {
830 if (!Yap_growheap(FALSE,
sizeof(*p), NULL)) {
831 Yap_ThrowError(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
835 p->KindOfPE = ArrayProperty;
836 p->ValueOfVE.ints = NULL;
837 INIT_RWLOCK(p->ArRWLock);
839 p->NextAE = LOCAL_StaticArrays;
840 LOCAL_StaticArrays = p;
842 WRITE_LOCK(p->ArRWLock);
843 p->ArrayEArity = dim;
845 p->TypeOfAE = STATIC_ARRAY;
846 if (start_addr == NULL) {
848 AllocateStaticArraySpace(p, type, NULL, dim PASS_REGS);
849 if (p->ValueOfVE.ints == NULL) {
850 WRITE_UNLOCK(p->ArRWLock);
855 for (i = 0; i < dim; i++)
856 p->ValueOfVE.ints[i] = 0;
859 for (i = 0; i < dim; i++)
860 p->ValueOfVE.chars[i] =
'\0';
862 case array_of_uchars:
863 for (i = 0; i < dim; i++)
864 p->ValueOfVE.uchars[i] =
'\0';
866 case array_of_doubles:
867 for (i = 0; i < dim; i++)
868 p->ValueOfVE.floats[i] = 0.0;
871 for (i = 0; i < dim; i++)
872 p->ValueOfVE.ptrs[i] = NULL;
875 case array_of_dbrefs:
876 for (i = 0; i < dim; i++)
877 p->ValueOfVE.atoms[i] = 0L;
880 for (i = 0; i < dim; i++)
881 p->ValueOfVE.terms[i] = NULL;
883 case array_of_nb_terms:
884 for (i = 0; i < dim; i++) {
885 RESET_VARIABLE(&(p->ValueOfVE.lterms[i].tlive));
886 p->ValueOfVE.lterms[i].tstore = TermNil;
892 p->TypeOfAE |= MMAP_ARRAY;
893 p->ValueOfVE.chars = (
char *)start_addr;
895 WRITE_UNLOCK(p->ArRWLock);
904 ArrayEntry *e0 = GetArrayEntry(RepAtom(na), worker_id);
905 if (e0 && ArrayIsDynamic(e0)) {
909 e = RepStaticArrayProp(AbsArrayProp(e0));
911 e = CreateStaticArray(RepAtom(na), dim, type, NULL, e PASS_REGS);
917 static_array_types type = pp->ArrayType;
918 size_t old_dim = pp->ArrayEArity;
919 size_t mindim = (dim < old_dim ? dim : old_dim), i;
922 if (pp->ArrayEArity == 0) {
925 WRITE_LOCK(pp->ArRWLock);
926 pp->ArrayEArity = dim;
928 if (pp->TypeOfAE & MMAP_ARRAY) {
929 ResizeMmappedArray(pp, dim, (
void *)(pp->ValueOfVE.chars)PASS_REGS);
930 WRITE_UNLOCK(pp->ArRWLock);
934 AllocateStaticArraySpace(pp, type, old_v.chars, dim PASS_REGS);
937 for (i = mindim; i < dim; i++)
938 pp->ValueOfVE.ints[i] = 0;
941 for (i = mindim; i < dim; i++)
942 pp->ValueOfVE.chars[i] =
'\0';
944 case array_of_uchars:
945 for (i = mindim; i < dim; i++)
946 pp->ValueOfVE.uchars[i] =
'\0';
948 case array_of_doubles:
949 for (i = mindim; i < dim; i++)
950 pp->ValueOfVE.floats[i] = 0.0;
953 for (i = mindim; i < dim; i++)
954 pp->ValueOfVE.ptrs[i] = NULL;
957 for (i = mindim; i < dim; i++)
958 pp->ValueOfVE.atoms[i] = TermNil;
960 case array_of_dbrefs:
961 for (i = mindim; i < dim; i++)
962 pp->ValueOfVE.dbrefs[i] = 0L;
965 for (i = mindim; i < dim; i++)
966 pp->ValueOfVE.terms[i] = NULL;
968 case array_of_nb_terms:
969 for (i = mindim; i < dim; i++) {
970 RESET_VARIABLE(&(pp->ValueOfVE.lterms[i].tlive));
971 pp->ValueOfVE.lterms[i].tstore = TermNil;
975 WRITE_UNLOCK(pp->ArRWLock);
980 static_array_types type = pp->ArrayType;
981 Int dim = pp->ArrayEArity, i;
984 if (pp->ArrayEArity == 0) {
987 WRITE_LOCK(pp->ArRWLock);
990 memset((
void *)pp->ValueOfVE.ints, 0,
sizeof(Int) * dim);
993 memset((
void *)pp->ValueOfVE.chars, 0,
sizeof(
char) * dim);
995 case array_of_uchars:
996 memset((
void *)pp->ValueOfVE.uchars, 0,
sizeof(
unsigned char) * dim);
998 case array_of_doubles:
999 memset((
void *)pp->ValueOfVE.floats, 0,
sizeof(
double) * dim);
1002 memset((
void *)pp->ValueOfVE.ptrs, 0,
sizeof(
void *) * dim);
1004 case array_of_atoms:
1005 for (i = 0; i < dim; i++)
1006 pp->ValueOfVE.atoms[i] = TermNil;
1008 case array_of_dbrefs:
1009 for (i = 0; i < dim; i++) {
1010 Term t0 = pp->ValueOfVE.dbrefs[i];
1012 DBRef ptr = DBRefOfTerm(t0);
1014 if (ptr->Flags & LogUpdMask) {
1018 if (lup->ClRefCount == 0 && (lup->ClFlags & ErasedMask) &&
1019 !(lup->ClFlags & InUseMask)) {
1021 Yap_ErLogUpdCl(lup);
1027 if (ptr->NOfRefsTo == 0 && (ptr->Flags & ErasedMask) &&
1028 !(ptr->Flags & InUseMask)) {
1033 pp->ValueOfVE.dbrefs[i] = 0L;
1036 case array_of_terms:
1037 for (i = 0; i < dim; i++) {
1038 DBTerm *ref = pp->ValueOfVE.terms[i];
1041 Yap_ReleaseTermFromDB(ref);
1043 pp->ValueOfVE.terms[i] = NULL;
1046 case array_of_nb_terms:
1047 for (i = 0; i < dim; i++) {
1048 Term told = pp->ValueOfVE.lterms[i].tstore;
1049 CELL *livep = &(pp->ValueOfVE.lterms[i].tlive);
1051 RESET_VARIABLE(livep);
1053 if (IsApplTerm(told)) {
1054 Yap_ReleaseTermFromDB((
DBTerm *)RepAppl(told));
1056 pp->ValueOfVE.lterms[i].tstore = old_v.lterms[i].tstore;
1060 WRITE_UNLOCK(pp->ArRWLock);
1069static Int create_array(USES_REGS1) {
1079 if (IsVarTerm(ti)) {
1080 Yap_ThrowError(INSTANTIATION_ERROR, ti,
"create_array");
1083 if (IsIntegerTerm(nti = Yap_Eval(ti)))
1084 size = IntegerOfTerm(nti);
1086 Yap_ThrowError(TYPE_ERROR_INTEGER, ti,
"create_array");
1095 farray = Yap_MkFunctor(AtomArray, size);
1096 if (HR + 1 + size > ASP - 1024) {
1098 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
1101 if (HR + 1 + size > ASP - 1024) {
1102 if (!Yap_growstack(
sizeof(CELL) * (size + 1 - (HR - ASP - 1024)))) {
1103 Yap_ThrowError(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
1111 *HR++ = (CELL)farray;
1112 for (; size >= 0; size--) {
1116 return (Yap_unify(t, ARG1));
1117 }
else if(IsAtomTerm(t)) {
1122 WRITE_LOCK(ae->ARWLock);
1123 pp = RepProp(ae->PropsOfAE);
1124 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty
1129 pp = RepProp(pp->NextOfPE);
1130 if (EndOfPAEntr(pp)) {
1131 if (HR + 1 + size > ASP - 1024) {
1132 WRITE_UNLOCK(ae->ARWLock);
1133 if (!Yap_dogc(PASS_REGS1)) {
1134 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
1139 CreateNamedArray(pp, size, ae PASS_REGS);
1140 WRITE_UNLOCK(ae->ARWLock);
1145 WRITE_UNLOCK(ae->ARWLock);
1146 if (!IsVarTerm(app->ValueOfVE) || !IsUnboundVar(&app->ValueOfVE)) {
1147 if (size == app->ArrayEArity)
1149 Yap_ThrowError(PERMISSION_ERROR_CREATE_ARRAY, t,
"create_array",
1152 if (HR + 1 + size > ASP - 1024) {
1153 if (!Yap_dogc(PASS_REGS1)) {
1154 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
1156 if (size == app->ArrayEArity)
1158 Yap_ThrowError(PERMISSION_ERROR_CREATE_ARRAY, t,
"create_array",
1161 if (HR + 1 + size > ASP - 1024) {
1162 if (!Yap_dogc(PASS_REGS1)) {
1163 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
1168 InitNamedArray(app, size PASS_REGS);
1191static_array(USES_REGS1) {
1192 Term ti = Deref(ARG2);
1193 Term t = Deref(ARG1);
1194 Term tprops = Deref(ARG3);
1196 static_array_types props;
1198 if (IsVarTerm(ti)) {
1199 Yap_ThrowError(INSTANTIATION_ERROR, ti,
"create static array");
1204 if (IsIntegerTerm(nti = Yap_Eval(ti)))
1205 size = IntegerOfTerm(nti);
1207 Yap_ThrowError(TYPE_ERROR_INTEGER, ti,
"create static array");
1212 if (IsVarTerm(tprops)) {
1213 Yap_ThrowError(INSTANTIATION_ERROR, tprops,
"create static array");
1215 }
else if (IsAtomTerm(tprops)) {
1216 char *atname = (
char *)RepAtom(AtomOfTerm(tprops))->StrOfAE;
1218 int l=push_text_stack();
1219 if((sz=strlen(atname))==0)
1220 Yap_ThrowError(DOMAIN_ERROR_ARRAY_TYPE, tprops,
"create static array");
1221 if(atname[sz-1]==
's')
1223 char *natname = malloc(sz);
1224 strncpy(natname,atname,sz-1);
1227 if (!strcmp(atname,
"int"))
1228 props = array_of_ints;
1229 else if (!strcmp(atname,
"dbref"))
1230 props = array_of_dbrefs;
1231 else if (!strcmp(atname,
"float"))
1232 props = array_of_doubles;
1233 else if (!strcmp(atname,
"ptr"))
1234 props = array_of_ptrs;
1235 else if (!strcmp(atname,
"atom"))
1236 props = array_of_atoms;
1237 else if (!strcmp(atname,
"char"))
1238 props = array_of_chars;
1239 else if (!strcmp(atname,
"unsigned_char"))
1240 props = array_of_uchars;
1241 else if (!strcmp(atname,
"term"))
1242 props = array_of_terms;
1243 else if (!strcmp(atname,
"nb_term"))
1244 props = array_of_nb_terms;
1246 Yap_ThrowError(DOMAIN_ERROR_ARRAY_TYPE, tprops,
"create static array");
1251 Yap_ThrowError(TYPE_ERROR_ATOM, tprops,
"create static array");
1257 Yap_ThrowError(INSTANTIATION_ERROR, t,
"create static array");
1259 }
else if (IsAtomTerm(t)) {
1264 WRITE_LOCK(ae->ARWLock);
1265 pp = RepStaticArrayProp(ae->PropsOfAE);
1266 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
1267 pp = RepStaticArrayProp(pp->NextOfPE);
1270 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
1271 pp = CreateStaticArray(ae, size, props, NULL, pp PASS_REGS);
1272 if (pp == NULL || pp->ValueOfVE.ints == NULL) {
1275 }
else if (ArrayIsDynamic(app)) {
1276 if (IsVarTerm(app->ValueOfVE) && IsUnboundVar(&app->ValueOfVE)) {
1277 pp = CreateStaticArray(ae, size, props, NULL, pp PASS_REGS);
1279 Yap_ThrowError(PERMISSION_ERROR_CREATE_ARRAY, t,
1280 "cannot create static array over dynamic array");
1283 if (pp->ArrayType != props) {
1284 Yap_ThrowError(TYPE_ERROR_ATOM, t,
"create static array %d/%d %d/%d", pp->ArrayEArity,size,pp->ArrayType,props);
1287 AllocateStaticArraySpace(pp, props, pp->ValueOfVE.ints, size PASS_REGS);
1290 WRITE_UNLOCK(ae->ARWLock);
1302 static_array_types props) {
1306 WRITE_LOCK(ae->ARWLock);
1308 RepStaticArrayProp(AbsArrayProp(GetArrayEntry(ae, worker_id)));
1309 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
1310 pp = CreateStaticArray(ae, size, props, NULL, pp PASS_REGS);
1311 if (pp == NULL || pp->ValueOfVE.ints == NULL) {
1312 WRITE_UNLOCK(ae->ARWLock);
1315 WRITE_UNLOCK(ae->ARWLock);
1328static Int static_array_properties(USES_REGS1) {
1329 Term t = Deref(ARG1);
1333 }
else if (IsAtomTerm(t)) {
1338 READ_LOCK(ae->ARWLock);
1339 pp = RepStaticArrayProp(ae->PropsOfAE);
1340 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
1341 pp = RepStaticArrayProp(pp->NextOfPE);
1342 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
1343 READ_UNLOCK(ae->ARWLock);
1346 static_array_types tp = pp->ArrayType;
1347 Int dim = pp->ArrayEArity;
1349 READ_UNLOCK(ae->ARWLock);
1350 if (dim <= 0 || !Yap_unify(ARG2, MkIntegerTerm(dim)))
1354 return (Yap_unify(ARG3, MkAtomTerm(AtomInt)));
1355 case array_of_dbrefs:
1356 return (Yap_unify(ARG3, MkAtomTerm(AtomDBref)));
1357 case array_of_doubles:
1358 return (Yap_unify(ARG3, MkAtomTerm(AtomFloat)));
1360 return (Yap_unify(ARG3, TermPointer));
1361 case array_of_chars:
1362 return (Yap_unify(ARG3, MkAtomTerm(AtomChar)));
1363 case array_of_uchars:
1364 return (Yap_unify(ARG3, MkAtomTerm(AtomUnsignedChar)));
1365 case array_of_terms:
1366 return (Yap_unify(ARG3, TermTerm));
1367 case array_of_nb_terms:
1368 return (Yap_unify(ARG3, TermTerm));
1369 case array_of_atoms:
1370 return (Yap_unify(ARG3, MkAtomTerm(AtomAtom)));
1379static Int resize_static_array(USES_REGS1) {
1380 Term ti = Deref(ARG3);
1381 Term t = Deref(ARG1);
1384 if (IsVarTerm(ti)) {
1385 Yap_ThrowError(INSTANTIATION_ERROR, ti,
"resize a static array");
1390 if (IsIntegerTerm(nti = Yap_Eval(ti)))
1391 size = IntegerOfTerm(nti);
1393 Yap_ThrowError(TYPE_ERROR_INTEGER, ti,
"resize a static array");
1399 Yap_ThrowError(INSTANTIATION_ERROR, t,
"resize a static array");
1401 }
else if (IsAtomTerm(t)) {
1403 Atom a = AtomOfTerm(t);
1406 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
1407 pp = RepStaticArrayProp(pp->NextOfPE);
1408 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
1409 Yap_ThrowError(PERMISSION_ERROR_RESIZE_ARRAY, t,
"resize a static array");
1412 size_t osize = pp->ArrayEArity;
1413 ResizeStaticArray(pp, size PASS_REGS);
1414 return (Yap_unify(ARG2, MkIntegerTerm(osize)));
1417 Yap_ThrowError(TYPE_ERROR_ATOM, t,
"resize a static array");
1431static Int clear_static_array(USES_REGS1) {
1432 Term t = Deref(ARG1);
1435 Yap_ThrowError(INSTANTIATION_ERROR, t,
"clear a static array");
1437 }
else if (IsAtomTerm(t)) {
1439 Atom a = AtomOfTerm(t);
1442 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
1443 pp = RepStaticArrayProp(pp->NextOfPE);
1444 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
1445 Yap_ThrowError(PERMISSION_ERROR_RESIZE_ARRAY, t,
"clear a static array");
1448 ClearStaticArray(pp);
1452 Yap_ThrowError(TYPE_ERROR_ATOM, t,
"clear a static array");
1467static Int close_static_array(USES_REGS1) {
1469 Term t = Deref(ARG1);
1472 Yap_ThrowError(INSTANTIATION_ERROR, t,
"close static array");
1474 }
else if (IsAtomTerm(t)) {
1479 READ_LOCK(ae->ARWLock);
1480 pp = RepProp(ae->PropsOfAE);
1481 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
1482 pp = RepProp(pp->NextOfPE);
1483 READ_UNLOCK(ae->ARWLock);
1484 if (EndOfPAEntr(pp)) {
1488 if (ptr->ValueOfVE.ints != NULL) {
1491 CloseMmappedArray(ptr, (
void *)ptr->ValueOfVE.chars PASS_REGS);
1492#if USE_SYSTEM_MALLOC
1496#if USE_SYSTEM_MALLOC
1500 Yap_FreeAtomSpace((
char *)(ptr->ValueOfVE.ints));
1501 ptr->ValueOfVE.ints = NULL;
1502 ptr->ArrayEArity = 0;
1509 Yap_ThrowError(TYPE_ERROR_ATOM, t,
"close static array");
1527static Int create_mmapped_array(USES_REGS1) {
1529 Term ti = Deref(ARG2);
1530 Term t = Deref(ARG1);
1531 Term tprops = Deref(ARG3);
1532 Term tfile = Deref(ARG4);
1534 static_array_types props;
1536 CODEADDR array_addr;
1539 if (IsVarTerm(ti)) {
1540 Yap_ThrowError(INSTANTIATION_ERROR, ti,
"create_mmapped_array");
1545 if (IsIntegerTerm(nti = Yap_Eval(ti)))
1546 size = IntegerOfTerm(nti);
1548 Yap_ThrowError(TYPE_ERROR_INTEGER, ti,
"create_mmapped_array");
1553 if (IsVarTerm(tprops)) {
1554 Yap_ThrowError(INSTANTIATION_ERROR, tprops,
"create_mmapped_array");
1556 }
else if (IsAtomTerm(tprops)) {
1557 char *atname = RepAtom(AtomOfTerm(tprops))->StrOfAE;
1558 if (!strcmp(atname,
"int")) {
1559 props = array_of_ints;
1560 total_size = size *
sizeof(Int);
1561 }
else if (!strcmp(atname,
"dbref")) {
1562 props = array_of_dbrefs;
1563 total_size = size *
sizeof(Int);
1564 }
else if (!strcmp(atname,
"float")) {
1565 props = array_of_doubles;
1566 total_size = size *
sizeof(Float);
1567 }
else if (!strcmp(atname,
"ptr")) {
1568 props = array_of_ptrs;
1569 total_size = size *
sizeof(
AtomEntry *);
1570 }
else if (!strcmp(atname,
"atom")) {
1571 props = array_of_atoms;
1572 total_size = size *
sizeof(Term);
1573 }
else if (!strcmp(atname,
"char")) {
1574 props = array_of_chars;
1575 total_size = size *
sizeof(char);
1576 }
else if (!strcmp(atname,
"unsigned_char")) {
1577 props = array_of_uchars;
1578 total_size = size *
sizeof(
unsigned char);
1580 Yap_ThrowError(DOMAIN_ERROR_ARRAY_TYPE, tprops,
"create_mmapped_array");
1584 Yap_ThrowError(TYPE_ERROR_ATOM, tprops,
"create_mmapped_array");
1588 if (IsVarTerm(tfile)) {
1589 Yap_ThrowError(INSTANTIATION_ERROR, tfile,
"create_mmapped_array");
1591 }
else if (IsAtomTerm(tfile)) {
1592 char *filename = RepAtom(AtomOfTerm(tfile))->StrOfAE;
1594 fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1596 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, ARG1,
"create_mmapped_array (open: %s)",
1600 if (lseek(fd, total_size - 1, SEEK_SET) < 0)
1601 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, tfile,
1602 "create_mmapped_array (lseek: %s)", strerror(errno));
1603 if (write(fd,
"", 1) < 0)
1604 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, tfile,
1605 "create_mmapped_array (write: %s)", strerror(errno));
1611 (CODEADDR)mmap(0, (
size_t)total_size, PROT_READ | PROT_WRITE,
1612 MAP_SHARED, fd, 0)) == (CODEADDR)-1)
1613 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, tfile,
"create_mmapped_array (mmap: %s)",
1616 Yap_ThrowError(TYPE_ERROR_ATOM, tfile,
"create_mmapped_array");
1621 Yap_ThrowError(INSTANTIATION_ERROR, t,
"create_mmapped_array");
1623 }
else if (IsAtomTerm(t)) {
1628 WRITE_LOCK(ae->ARWLock);
1629 pp = RepStaticArrayProp(ae->PropsOfAE);
1630 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
1631 pp = RepStaticArrayProp(pp->NextOfPE);
1632 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
1633 mmap_array_block *ptr;
1635 if (EndOfPAEntr(pp)) {
1636 WRITE_UNLOCK(ae->ARWLock);
1639 WRITE_LOCK(pp->ArRWLock);
1641 CreateStaticArray(ae, size, props, array_addr, pp PASS_REGS);
1642 ptr = (mmap_array_block *)Yap_AllocAtomSpace(
sizeof(mmap_array_block));
1643 ptr->name = AbsAtom(ae);
1644 ptr->size = total_size;
1646 ptr->start = (
void *)array_addr;
1648 ptr->next = GLOBAL_mmap_arrays;
1649 GLOBAL_mmap_arrays = ptr;
1650 WRITE_UNLOCK(pp->ArRWLock);
1651 WRITE_UNLOCK(ae->ARWLock);
1654 WRITE_UNLOCK(ae->ARWLock);
1655 Yap_ThrowError(DOMAIN_ERROR_ARRAY_TYPE, t,
"create_mmapped_array",
1660 Yap_ThrowError(TYPE_ERROR_ATOM, t,
"create_mmapped_array");
1664 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, ARG1,
"create_mmapped_array (mmap)");
1670static void replace_array_references_complex(
register CELL *pt0,
1671 register CELL *pt0_end,
1673 Term Var USES_REGS) {
1675 register CELL **tovisit = (CELL **)Yap_PreAllocCodeSpace();
1676 CELL **tovisit_base = tovisit;
1679 while (pt0 < pt0_end) {
1684 if (IsVarTerm(d0)) {
1686 }
else if (IsPairTerm(d0)) {
1688 *ptn++ = AbsPair(HR);
1689#ifdef RATIONAL_TREES
1691 tovisit[1] = pt0_end;
1693 tovisit[3] = (CELL *)*pt0;
1697 if (pt0 < pt0_end) {
1699 tovisit[1] = pt0_end;
1704 pt0 = RepPair(d0) - 1;
1705 pt0_end = RepPair(d0) + 1;
1709 }
else if (IsApplTerm(d0)) {
1712 f = FunctorOfTerm(d0);
1714 if (IsExtensionFunctor(f)) {
1720 *ptn++ = AbsAppl(HR);
1722#ifdef RATIONAL_TREES
1724 tovisit[1] = pt0_end;
1726 tovisit[3] = (CELL *)*pt0;
1730 if (pt0 < pt0_end) {
1732 tovisit[1] = pt0_end;
1738 d0 = ArityOfFunctor(f);
1751 if (tovisit > (CELL **)tovisit_base) {
1752#ifdef RATIONAL_TREES
1755 pt0_end = tovisit[1];
1757 *pt0 = (CELL)tovisit[3];
1761 pt0_end = tovisit[1];
1767 Bind_Global(PtrOfTerm(Var), TermNil);
1768 Yap_ReleasePreAllocCodeSpace((ADDR)tovisit);
1778static Term replace_array_references(Term t0 USES_REGS) {
1784 return (MkPairTerm(t, TermNil));
1785 }
else if (IsAtomOrIntTerm(t)) {
1786 return (MkPairTerm(t, TermNil));
1787 }
else if (IsPairTerm(t)) {
1788 Term VList = MkVarTerm();
1792 replace_array_references_complex(RepPair(t) - 1, RepPair(t) + 1, h0,
1794 return MkPairTerm(AbsPair(h0), VList);
1796 Term VList = MkVarTerm();
1801 HR += ArityOfFunctor(f);
1802 replace_array_references_complex(
1803 RepAppl(t), RepAppl(t) + ArityOfFunctor(FunctorOfTerm(t)), h0 + 1,
1805 return (MkPairTerm(AbsAppl(h0), VList));
1809static Int array_references(USES_REGS1) {
1810 Term t = replace_array_references(ARG1 PASS_REGS);
1811 Term t1 = HeadOfTerm(t);
1812 Term t2 = TailOfTerm(t);
1814 return (Yap_unify(t1, ARG2) && Yap_unify(t2, ARG3));
1833static Int assign_static(USES_REGS1) {
1839 if (IsNonVarTerm(t2)) {
1842 if (IsIntegerTerm(nti = Yap_Eval(t2)))
1843 indx = IntegerOfTerm(nti);
1845 Yap_ThrowError(TYPE_ERROR_INTEGER, t2,
"update_array");
1849 Yap_ThrowError(INSTANTIATION_ERROR, t2,
"update_array");
1855 if (IsVarTerm(t1)) {
1856 Yap_ThrowError(INSTANTIATION_ERROR, t1,
"update_array");
1859 if (!IsAtomTerm(t1)) {
1860 if (IsApplTerm(t1)) {
1862 Functor f = FunctorOfTerm(t1);
1864 if (IsExtensionFunctor(f)) {
1865 Yap_ThrowError(TYPE_ERROR_ARRAY, t1,
"update_array");
1868 if (indx > 0 && indx > ArityOfFunctor(f)) {
1869 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"update_array");
1872 ptr = RepAppl(t1) + indx + 1;
1873#ifdef MULTI_ASSIGNMENT_VARIABLES
1877 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t2,
"update_array");
1881 Yap_ThrowError(TYPE_ERROR_ATOM, t1,
"update_array");
1886 AtomEntry *ae = RepAtom(AtomOfTerm(t1));
1888 READ_LOCK(ae->ARWLock);
1889 ptr = RepStaticArrayProp(ae->PropsOfAE);
1890 while (!EndOfPAEntr(ptr) && ptr->KindOfPE != ArrayProperty)
1891 ptr = RepStaticArrayProp(ptr->NextOfPE);
1893 if (EndOfPAEntr(ptr)) {
1894 READ_UNLOCK(ae->ARWLock);
1895 Yap_ThrowError(EXISTENCE_ERROR_ARRAY, t1,
"assign_static %s",
1896 RepAtom(AtomOfTerm(t1))->StrOfAE);
1904 WRITE_LOCK(pp->ArRWLock);
1905 READ_UNLOCK(ae->ARWLock);
1906 if (indx < 0 || indx >= pp->ArrayEArity) {
1907 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"assign_static");
1908 WRITE_UNLOCK(pp->ArRWLock);
1911 pt = RepAppl(pp->ValueOfVE) + indx + 1;
1912 WRITE_UNLOCK(pp->ArRWLock);
1913#ifdef MULTI_ASSIGNMENT_VARIABLES
1918 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t2,
"update_array");
1923 WRITE_LOCK(ptr->ArRWLock);
1924 READ_UNLOCK(ae->ARWLock);
1926 if (indx < 0 || indx >= ptr->ArrayEArity) {
1927 WRITE_UNLOCK(ptr->ArRWLock);
1928 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"assign_static");
1931 switch (ptr->ArrayType) {
1932 case array_of_ints: {
1936 if (IsVarTerm(t3)) {
1937 WRITE_UNLOCK(ptr->ArRWLock);
1938 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
1942 if (IsIntegerTerm(nti = Yap_Eval(t3)))
1943 i = IntegerOfTerm(nti);
1945 WRITE_UNLOCK(ptr->ArRWLock);
1946 Yap_ThrowError(TYPE_ERROR_INTEGER, t3,
"assign_static");
1949 ptr->ValueOfVE.ints[indx] = i;
1952 case array_of_chars: {
1956 if (IsVarTerm(t3)) {
1957 WRITE_UNLOCK(ptr->ArRWLock);
1958 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
1961 if (IsIntegerTerm(nti = Yap_Eval(t3)))
1962 i = IntegerOfTerm(nti);
1964 Yap_ThrowError(TYPE_ERROR_INTEGER, t3,
"assign_static");
1967 if (i > 127 || i < -128) {
1968 WRITE_UNLOCK(ptr->ArRWLock);
1969 Yap_ThrowError(TYPE_ERROR_CHAR, t3,
"assign_static");
1972 ptr->ValueOfVE.chars[indx] = i;
1975 case array_of_uchars: {
1979 if (IsVarTerm(t3)) {
1980 WRITE_UNLOCK(ptr->ArRWLock);
1981 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
1984 if (IsIntegerTerm(nti = Yap_Eval(t3)))
1985 i = IntegerOfTerm(nti);
1987 WRITE_UNLOCK(ptr->ArRWLock);
1988 Yap_ThrowError(TYPE_ERROR_INTEGER, t3,
"assign_static");
1991 if (i > 255 || i < 0) {
1992 WRITE_UNLOCK(ptr->ArRWLock);
1993 Yap_ThrowError(TYPE_ERROR_UCHAR, t3,
"assign_static");
1996 ptr->ValueOfVE.chars[indx] = i;
1999 case array_of_doubles: {
2003 if (IsVarTerm(t3)) {
2004 WRITE_UNLOCK(ptr->ArRWLock);
2005 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
2008 if (IsFloatTerm(nti = Yap_Eval(t3)))
2009 f = FloatOfTerm(nti);
2010 else if (IsIntegerTerm(nti))
2011 f = IntegerOfTerm(nti);
2013 WRITE_UNLOCK(ptr->ArRWLock);
2014 Yap_ThrowError(TYPE_ERROR_FLOAT, t3,
"assign_static");
2017 ptr->ValueOfVE.floats[indx] = f;
2020 case array_of_ptrs: {
2023 if (IsVarTerm(t3)) {
2024 WRITE_UNLOCK(ptr->ArRWLock);
2025 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
2028 if (IsIntegerTerm(t3))
2029 r = IntegerOfTerm(t3);
2031 WRITE_UNLOCK(ptr->ArRWLock);
2032 Yap_ThrowError(TYPE_ERROR_PTR, t3,
"assign_static");
2035 ptr->ValueOfVE.ptrs[indx] = (
AtomEntry *)r;
2038 case array_of_atoms: {
2039 if (IsVarTerm(t3)) {
2040 WRITE_UNLOCK(ptr->ArRWLock);
2041 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
2044 if (!IsAtomTerm(t3)) {
2045 WRITE_UNLOCK(ptr->ArRWLock);
2046 Yap_ThrowError(TYPE_ERROR_ATOM, t3,
"assign_static");
2049 ptr->ValueOfVE.atoms[indx] = t3;
2052 case array_of_dbrefs: {
2054 Term t0 = ptr->ValueOfVE.dbrefs[indx];
2055 DBRef p = DBRefOfTerm(t3);
2057 if (IsVarTerm(t3)) {
2058 WRITE_UNLOCK(ptr->ArRWLock);
2059 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"assign_static");
2062 if (!IsDBRefTerm(t3)) {
2063 WRITE_UNLOCK(ptr->ArRWLock);
2064 Yap_ThrowError(TYPE_ERROR_DBREF, t3,
"assign_static");
2067 ptr->ValueOfVE.dbrefs[indx] = t3;
2069 DBRef ptr = DBRefOfTerm(t0);
2071 if (ptr->Flags & LogUpdMask) {
2075 if (lup->ClRefCount == 0 && (lup->ClFlags & ErasedMask) &&
2076 !(lup->ClFlags & InUseMask)) {
2078 Yap_ErLogUpdCl(lup);
2084 if (ptr->NOfRefsTo == 0 && (ptr->Flags & ErasedMask) &&
2085 !(ptr->Flags & InUseMask)) {
2091 if (p->Flags & LogUpdMask) {
2101 case array_of_nb_terms:
2104 Term told = ptr->ValueOfVE.lterms[indx].tstore;
2106 CELL *livep = &(ptr->ValueOfVE.lterms[indx].tlive);
2107 RESET_VARIABLE(livep);
2109 if (IsApplTerm(told)) {
2110 Yap_ReleaseTermFromDB((
DBTerm *)RepAppl(told));
2112 if (IsVarTerm(t3)) {
2113 RESET_VARIABLE(&(ptr->ValueOfVE.lterms[indx].tstore));
2114 }
else if (IsAtomicTerm(t3)) {
2115 ptr->ValueOfVE.lterms[indx].tstore = t3;
2117 DBTerm *
new = Yap_StoreTermInDB(t3, 3);
2119 WRITE_UNLOCK(ptr->ArRWLock);
2122 ptr->ValueOfVE.lterms[indx].tstore = AbsAppl((CELL *)
new);
2126 case array_of_terms: {
2128 DBTerm *ref = ptr->ValueOfVE.terms[indx];
2131 Yap_ReleaseTermFromDB(ref);
2133 ptr->ValueOfVE.terms[indx] = Yap_StoreTermInDB(t3, 3);
2134 if (ptr->ValueOfVE.terms[indx] == NULL) {
2135 WRITE_UNLOCK(ptr->ArRWLock);
2140 WRITE_UNLOCK(ptr->ArRWLock);
2145static Int assign_dynamic(USES_REGS1) {
2151 if (IsNonVarTerm(t2)) {
2153 if (IsIntegerTerm(nti = Yap_Eval(t2))) {
2154 indx = IntegerOfTerm(nti);
2156 Yap_ThrowError(TYPE_ERROR_INTEGER, t2,
"update_array");
2160 Yap_ThrowError(INSTANTIATION_ERROR, t2,
"update_array");
2166 if (IsVarTerm(t1)) {
2167 Yap_ThrowError(INSTANTIATION_ERROR, t1,
"update_array");
2170 if (!IsAtomTerm(t1)) {
2171 if (IsApplTerm(t1)) {
2173 Functor f = FunctorOfTerm(t1);
2175 if (IsExtensionFunctor(f)) {
2176 Yap_ThrowError(TYPE_ERROR_ARRAY, t1,
"update_array");
2179 if (indx > 0 && indx > ArityOfFunctor(f)) {
2180 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"update_array");
2183 ptr = RepAppl(t1) + indx + 1;
2184#ifdef MULTI_ASSIGNMENT_VARIABLES
2188 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t2,
"update_array");
2192 Yap_ThrowError(TYPE_ERROR_ATOM, t1,
"update_array");
2197 AtomEntry *ae = RepAtom(AtomOfTerm(t1));
2199 READ_LOCK(ae->ARWLock);
2200 ptr = RepStaticArrayProp(ae->PropsOfAE);
2201 while (!EndOfPAEntr(ptr) && ptr->KindOfPE != ArrayProperty)
2202 ptr = RepStaticArrayProp(ptr->NextOfPE);
2203 READ_UNLOCK(ae->ARWLock);
2206 if (EndOfPAEntr(ptr)) {
2207 Yap_ThrowError(EXISTENCE_ERROR_ARRAY, t1,
"assign_static %s",
2208 RepAtom(AtomOfTerm(t1))->StrOfAE);
2215 WRITE_LOCK(pp->ArRWLock);
2216 if (indx < 0 || indx >= pp->ArrayEArity) {
2217 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"assign_static");
2218 WRITE_UNLOCK(pp->ArRWLock);
2221 pt = RepAppl(pp->ValueOfVE) + indx + 1;
2222 WRITE_UNLOCK(pp->ArRWLock);
2223#ifdef MULTI_ASSIGNMENT_VARIABLES
2228 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t2,
"update_array");
2233 WRITE_LOCK(ptr->ArRWLock);
2235 if (indx < 0 || indx >= ptr->ArrayEArity) {
2236 WRITE_UNLOCK(ptr->ArRWLock);
2237 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"assign_static");
2240 switch (ptr->ArrayType) {
2242 case array_of_chars:
2243 case array_of_uchars:
2244 case array_of_doubles:
2246 case array_of_atoms:
2247 case array_of_dbrefs:
2248 case array_of_terms:
2249 WRITE_UNLOCK(ptr->ArRWLock);
2250 Yap_ThrowError(DOMAIN_ERROR_ARRAY_TYPE, t3,
"assign_static");
2253 case array_of_nb_terms:
2254#ifdef MULTI_ASSIGNMENT_VARIABLES
2256 Term t = ptr->ValueOfVE.lterms[indx].tlive;
2260 if (IsVarTerm(t) || !IsApplTerm(t) ||
2261 (f = FunctorOfTerm(t)) != FunctorAtFoundOne) {
2262 Term tn = Yap_NewTimedVar(t3);
2263 CELL *sp = RepAppl(tn);
2264 *sp = (CELL)FunctorAtFoundOne;
2265 YapBind(&(ptr->ValueOfVE.lterms[indx].tlive), tn);
2267 Yap_UpdateTimedVar(t, t3);
2270 WRITE_UNLOCK(ptr->ArRWLock);
2273 WRITE_UNLOCK(ptr->ArRWLock);
2274 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t2,
"update_array");
2278 WRITE_UNLOCK(ptr->ArRWLock);
2305static Int add_to_array_element(USES_REGS1) {
2311 if (IsNonVarTerm(t2)) {
2313 if (IsIntegerTerm(nti = Yap_Eval(t2))) {
2314 indx = IntegerOfTerm(nti);
2316 Yap_ThrowError(TYPE_ERROR_INTEGER, t2,
"add_to_array_element");
2320 Yap_ThrowError(INSTANTIATION_ERROR, t2,
"add_to_array_element");
2325 if (IsVarTerm(t1)) {
2326 Yap_ThrowError(INSTANTIATION_ERROR, t1,
"add_to_array_element");
2330 if (IsVarTerm(t3)) {
2331 Yap_ThrowError(INSTANTIATION_ERROR, t3,
"add_to_array_element");
2334 if (!IsAtomTerm(t1)) {
2335 if (IsApplTerm(t1)) {
2337 Functor f = FunctorOfTerm(t1);
2341 if (IsExtensionFunctor(f)) {
2342 Yap_ThrowError(TYPE_ERROR_ARRAY, t1,
"add_to_array_element");
2345 if (indx > 0 && indx > ArityOfFunctor(f)) {
2346 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"add_to_array_element");
2349 ptr = RepAppl(t1) + indx + 1;
2350 ta = RepAppl(t1)[indx + 1];
2351 if (IsIntegerTerm(ta)) {
2352 if (IsIntegerTerm(t3)) {
2353 ta = MkIntegerTerm(IntegerOfTerm(ta) + IntegerOfTerm(t3));
2354 }
else if (IsFloatTerm(t3)) {
2355 ta = MkFloatTerm(IntegerOfTerm(ta) + FloatOfTerm(t3));
2357 Yap_ThrowError(TYPE_ERROR_NUMBER, t3,
"add_to_array_element");
2360 }
else if (IsFloatTerm(ta)) {
2361 if (IsFloatTerm(t3)) {
2362 ta = MkFloatTerm(FloatOfTerm(ta) + IntegerOfTerm(t3));
2363 }
else if (IsFloatTerm(t3)) {
2364 ta = MkFloatTerm(FloatOfTerm(ta) + FloatOfTerm(t3));
2366 Yap_ThrowError(TYPE_ERROR_NUMBER, t3,
"add_to_array_element");
2370 Yap_ThrowError(TYPE_ERROR_NUMBER, ta,
"add_to_array_element");
2373#ifdef MULTI_ASSIGNMENT_VARIABLES
2375 return (Yap_unify(ARG4, ta));
2377 Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t2,
"add_to_array_element");
2381 Yap_ThrowError(TYPE_ERROR_ATOM, t1,
"add_to_array_element");
2386 AtomEntry *ae = RepAtom(AtomOfTerm(t1));
2388 READ_LOCK(ae->ARWLock);
2389 ptr = RepStaticArrayProp(ae->PropsOfAE);
2390 while (!EndOfPAEntr(ptr) && ptr->KindOfPE != ArrayProperty)
2391 ptr = RepStaticArrayProp(ptr->NextOfPE);
2392 READ_UNLOCK(ae->ARWLock);
2395 if (EndOfPAEntr(ptr)) {
2396 Yap_ThrowError(EXISTENCE_ERROR_ARRAY, t1,
"add_to_array_element %s",
2397 RepAtom(AtomOfTerm(t1))->StrOfAE);
2406 WRITE_LOCK(pp->ArRWLock);
2407 if (indx < 0 || indx >= pp->ArrayEArity) {
2408 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"add_to_array_element");
2409 READ_UNLOCK(pp->ArRWLock);
2412 pt = RepAppl(pp->ValueOfVE) + indx + 1;
2413 ta = RepAppl(pp->ValueOfVE)[indx + 1];
2414 if (IsIntegerTerm(ta)) {
2415 if (IsIntegerTerm(t3)) {
2416 ta = MkIntegerTerm(IntegerOfTerm(ta) + IntegerOfTerm(t3));
2417 }
else if (IsFloatTerm(t3)) {
2418 ta = MkFloatTerm(IntegerOfTerm(ta) + FloatOfTerm(t3));
2420 WRITE_UNLOCK(pp->ArRWLock);
2421 Yap_ThrowError(TYPE_ERROR_NUMBER, t3,
"add_to_array_element");
2424 }
else if (IsFloatTerm(ta)) {
2425 if (IsFloatTerm(t3)) {
2426 ta = MkFloatTerm(FloatOfTerm(ta) + IntegerOfTerm(t3));
2427 }
else if (IsFloatTerm(t3)) {
2428 ta = MkFloatTerm(FloatOfTerm(ta) + FloatOfTerm(t3));
2430 WRITE_UNLOCK(pp->ArRWLock);
2431 Yap_ThrowError(TYPE_ERROR_NUMBER, t3,
"add_to_array_element");
2435 WRITE_UNLOCK(pp->ArRWLock);
2436 Yap_ThrowError(TYPE_ERROR_NUMBER, ta,
"add_to_array_element");
2441 WRITE_UNLOCK(pp->ArRWLock);
2442 return Yap_unify(ARG4, t3);
2445 WRITE_LOCK(ptr->ArRWLock);
2447 if (indx < 0 || indx >= ptr->ArrayEArity) {
2448 WRITE_UNLOCK(ptr->ArRWLock);
2449 Yap_ThrowError(DOMAIN_ERROR_ARRAY_OVERFLOW, t2,
"add_to_array_element");
2452 switch (ptr->ArrayType) {
2453 case array_of_ints: {
2454 Int i = ptr->ValueOfVE.ints[indx];
2455 if (!IsIntegerTerm(t3)) {
2456 WRITE_UNLOCK(ptr->ArRWLock);
2457 Yap_ThrowError(TYPE_ERROR_INTEGER, t3,
"add_to_array_element");
2460 i += IntegerOfTerm(t3);
2461 ptr->ValueOfVE.ints[indx] = i;
2462 WRITE_UNLOCK(ptr->ArRWLock);
2463 return Yap_unify(ARG4, MkIntegerTerm(i));
2465 case array_of_doubles: {
2466 Float fl = ptr->ValueOfVE.floats[indx];
2468 if (IsFloatTerm(t3)) {
2469 fl += FloatOfTerm(t3);
2470 }
else if (IsIntegerTerm(t3)) {
2471 fl += IntegerOfTerm(t3);
2473 WRITE_UNLOCK(ptr->ArRWLock);
2474 Yap_ThrowError(TYPE_ERROR_NUMBER, t3,
"add_to_array_element");
2477 ptr->ValueOfVE.floats[indx] = fl;
2478 WRITE_UNLOCK(ptr->ArRWLock);
2479 return Yap_unify(ARG4, MkFloatTerm(fl));
2482 WRITE_UNLOCK(ptr->ArRWLock);
2483 Yap_ThrowError(TYPE_ERROR_NUMBER, t2,
"add_to_array_element");
2488static Int compile_array_refs(USES_REGS1) {
2489 compile_arrays = TRUE;
2493static Int array_refs_compiled(USES_REGS1) {
return compile_arrays; }
2495static Int sync_mmapped_arrays(USES_REGS1) {
2497 mmap_array_block *ptr = GLOBAL_mmap_arrays;
2498 while (ptr != NULL) {
2499 msync(ptr->start, ptr->size, MS_SYNC);
2517static Int static_array_to_term(USES_REGS1) {
2518 Term t = Deref(ARG1);
2522 }
else if (IsAtomTerm(t)) {
2527 READ_LOCK(ae->ARWLock);
2528 pp = RepStaticArrayProp(ae->PropsOfAE);
2529 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
2530 pp = RepStaticArrayProp(pp->NextOfPE);
2531 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
2532 READ_UNLOCK(ae->ARWLock);
2535 static_array_types tp = pp->ArrayType;
2536 Int dim = pp->ArrayEArity, indx;
2539 while (HR + 1 + dim > ASP - 1024) {
2540 if (!Yap_dogc(PASS_REGS1)) {
2541 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
2544 if (HR + 1 + dim > ASP - 1024) {
2545 if (!Yap_growstack(
sizeof(CELL) * (dim + 1 - (HR - ASP - 1024)))) {
2546 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
2552 READ_LOCK(pp->ArRWLock);
2553 READ_UNLOCK(ae->ARWLock);
2555 *HR++ = (CELL)Yap_MkFunctor(AbsAtom(ae), dim);
2557 case array_of_ints: {
2560 for (indx = 0; indx < dim; indx++) {
2561 *sptr++ = MkIntegerTerm(pp->ValueOfVE.ints[indx]);
2564 case array_of_dbrefs:
2565 for (indx = 0; indx < dim; indx++) {
2567 Term TRef = pp->ValueOfVE.dbrefs[indx];
2570 DBRef ref = DBRefOfTerm(TRef);
2573 INC_DBREF_COUNT(ref);
2576 if (!(ref->Flags & InUseMask)) {
2577 ref->Flags |= InUseMask;
2588 case array_of_doubles: {
2591 for (indx = 0; indx < dim; indx++) {
2592 *sptr++ = MkEvalFl(pp->ValueOfVE.floats[indx]);
2595 case array_of_ptrs: {
2598 for (indx = 0; indx < dim; indx++) {
2599 *sptr++ = MkAddressTerm(pp->ValueOfVE.ptrs[indx]);
2602 case array_of_chars: {
2606 for (indx = 0; indx < dim; indx++) {
2607 *sptr++ = MkIntTerm(pp->ValueOfVE.chars[indx]);
2610 case array_of_uchars: {
2614 for (indx = 0; indx < dim; indx++) {
2615 *sptr++ = MkIntTerm(pp->ValueOfVE.uchars[indx]);
2618 case array_of_terms: {
2621 for (indx = 0; indx < dim; indx++) {
2623 DBTerm *ref = pp->ValueOfVE.terms[indx];
2625 Term TRef = GetTermFromArray(ref PASS_REGS);
2627 if (P == FAILCODE) {
2634 case array_of_nb_terms: {
2637 for (indx = 0; indx < dim; indx++) {
2639 Term To = GetNBTerm(pp->ValueOfVE.lterms, indx PASS_REGS);
2641 if (P == FAILCODE) {
2648 case array_of_atoms:
2649 for (indx = 0; indx < dim; indx++) {
2651 out = pp->ValueOfVE.atoms[indx];
2658 READ_UNLOCK(pp->ArRWLock);
2659 return Yap_unify(AbsAppl(base), ARG2);
2662 Yap_ThrowError(TYPE_ERROR_ATOM, t,
"add_to_array_element");
2672static Int static_array_location(USES_REGS1) {
2673 Term t = Deref(ARG1);
2677 }
else if (IsAtomTerm(t)) {
2682 READ_LOCK(ae->ARWLock);
2683 pp = RepStaticArrayProp(ae->PropsOfAE);
2684 while (!EndOfPAEntr(pp) && pp->KindOfPE != ArrayProperty)
2685 pp = RepStaticArrayProp(pp->NextOfPE);
2686 if (EndOfPAEntr(pp) || pp->ValueOfVE.ints == NULL) {
2687 READ_UNLOCK(ae->ARWLock);
2690 ptr = pp->ValueOfVE.ints;
2691 READ_UNLOCK(ae->ARWLock);
2693 return Yap_unify(ARG2, MkAddressTerm(ptr));
2698 void Yap_InitArrayPreds(
void) {
2699 Yap_InitCPred(
"$create_array", 2, create_array, SyncPredFlag);
2700 Yap_InitCPred(
"$array_references", 3, array_references, SafePredFlag);
2701 Yap_InitCPred(
"$array_arg", 3, array_arg, SafePredFlag);
2702 Yap_InitCPred(
"static_array", 3, static_array,
2703 SafePredFlag | SyncPredFlag);
2704 Yap_InitCPred(
"resize_static_array", 3, resize_static_array,
2705 SafePredFlag | SyncPredFlag);
2706 Yap_InitCPred(
"mmapped_array", 4, create_mmapped_array,
2707 SafePredFlag | SyncPredFlag);
2708 Yap_InitCPred(
"update_array", 3, assign_static, SafePredFlag);
2709 Yap_InitCPred(
"update_whole_array", 2, update_all, SafePredFlag);
2710 Yap_InitCPred(
"dynamic_update_array", 3, assign_dynamic, SafePredFlag); Yap_InitCPred(
"add_to_array_element", 4, add_to_array_element, SafePredFlag);
2711 Yap_InitCPred(
"array_element", 3, access_array, 0);
2712 Yap_InitCPred(
"reset_static_array", 1, clear_static_array, SafePredFlag);
2713 Yap_InitCPred(
"close_static_array", 1, close_static_array, SafePredFlag);
2714 Yap_InitCPred(
"$sync_mmapped_arrays", 0, sync_mmapped_arrays, SafePredFlag);
2715 Yap_InitCPred(
"$compile_array_refs", 0, compile_array_refs, SafePredFlag);
2716 Yap_InitCPred(
"$array_refs_compiled", 0, array_refs_compiled, SafePredFlag);
2717 Yap_InitCPred(
"$static_array_properties", 3, static_array_properties,
2719 Yap_InitCPred(
"static_array_to_term", 2, static_array_to_term, 0L);
2720 Yap_InitCPred(
"static_array_location", 2, static_array_location, 0L);
StaticArrayEntry * Yap_StaticVector(Atom Name, size_t size, static_array_types props)
create a new vectir in a given name Name