From 4612a7cc5716e4d6c9372dee024eab68de9c10dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Fri, 20 Dec 2013 00:34:15 +0000 Subject: [PATCH 1/3] ISO --- C/write.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C/write.c b/C/write.c index 60f347f49..133a0633d 100644 --- a/C/write.c +++ b/C/write.c @@ -437,7 +437,7 @@ legalAtom(unsigned char *s) /* Is this a legal atom ? */ return (s[1] == '}' && !s[2]); } else if (Yap_chtype[ch] == SL) { return (!s[1]); - } else if ((ch == ',' /* || ch == '.' */) && !s[1]) { + } else if ((ch == ',' || ch == '.') && !s[1]) { return FALSE; } else { if (ch == '/') { From 233829ec527dd49282c5ae246a82f798a64f83fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 2 Jan 2014 19:11:06 +0000 Subject: [PATCH 2/3] fix array expansion to use realloc and fix arrays to separate static/dynamic and malloced --- C/arrays.c | 101 ++++++++++++++++++++++++----------------------------- H/Yatom.h | 10 +++++- H/arrays.h | 2 -- 3 files changed, 54 insertions(+), 59 deletions(-) diff --git a/C/arrays.c b/C/arrays.c index 90550d778..23cf27dc7 100644 --- a/C/arrays.c +++ b/C/arrays.c @@ -330,7 +330,7 @@ AccessNamedArray(Atom a, Int indx USES_REGS) StaticArrayEntry *ptr = (StaticArrayEntry *)pp; READ_LOCK(ptr->ArRWLock); - if (-(pp->ArrayEArity) <= indx || indx < 0) { + if (pp->ArrayEArity <= indx || indx < 0) { /* Yap_Error(DOMAIN_ERROR_ARRAY_OVERFLOW, MkIntegerTerm(indx), "access_array");*/ READ_UNLOCK(ptr->ArRWLock); P = (yamop *)FAILCODE; @@ -566,6 +566,7 @@ CreateNamedArray(PropEntry * pp, Int dim, AtomEntry *ae USES_REGS) p = (ArrayEntry *) Yap_AllocAtomSpace(sizeof(*p)); p->KindOfPE = ArrayProperty; + p->TypeOfAE = DYNAMIC_ARRAY; AddPropToAtom(ae, (PropEntry *)p); INIT_RWLOCK(p->ArRWLock); #if THREADS @@ -578,9 +579,9 @@ CreateNamedArray(PropEntry * pp, Int dim, AtomEntry *ae USES_REGS) } static void -AllocateStaticArraySpace(StaticArrayEntry *p, static_array_types atype, Int array_size USES_REGS) +AllocateStaticArraySpace(StaticArrayEntry *p, static_array_types atype, void *old, size_t array_size USES_REGS) { - Int asize = 0; + size_t asize = 0; switch (atype) { case array_of_doubles: asize = array_size*sizeof(Float); @@ -606,22 +607,33 @@ AllocateStaticArraySpace(StaticArrayEntry *p, static_array_types atype, Int arra asize = array_size*sizeof(DBRef); break; } - while ((p->ValueOfVE.floats = (Float *) Yap_AllocAtomSpace(asize) ) == NULL) { - YAPLeaveCriticalSection(); - if (!Yap_growheap(FALSE, asize, NULL)) { - Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); - return; + if (old == NULL) { + while ((p->ValueOfVE.floats = (Float *) Yap_AllocCodeSpace(asize) ) == NULL) { + YAPLeaveCriticalSection(); + if (!Yap_growheap(FALSE, asize, NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); + return; + } + YAPEnterCriticalSection(); + } + } else { + while ((p->ValueOfVE.floats = (Float *) Yap_ReallocCodeSpace(old, asize) ) == NULL) { + YAPLeaveCriticalSection(); + if (!Yap_growheap(FALSE, asize, NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); + return; + } + YAPEnterCriticalSection(); } - YAPEnterCriticalSection(); } } /* ae and p are assumed to be locked, if they exist */ static StaticArrayEntry * -CreateStaticArray(AtomEntry *ae, Int dim, static_array_types type, CODEADDR start_addr, StaticArrayEntry *p USES_REGS) +CreateStaticArray(AtomEntry *ae, size_t dim, static_array_types type, CODEADDR start_addr, StaticArrayEntry *p USES_REGS) { if (EndOfPAEntr(p)) { - while ((p = (StaticArrayEntry *) Yap_AllocAtomSpace(sizeof(*p))) == NULL) { + while ((p = (StaticArrayEntry *) Yap_AllocCodeSpace(sizeof(*p))) == NULL) { if (!Yap_growheap(FALSE, sizeof(*p), NULL)) { Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); return NULL; @@ -634,12 +646,13 @@ CreateStaticArray(AtomEntry *ae, Int dim, static_array_types type, CODEADDR star LOCAL_StaticArrays = p; } WRITE_LOCK(p->ArRWLock); - p->ArrayEArity = -dim; + p->ArrayEArity = dim; p->ArrayType = type; + p->TypeOfAE = STATIC_ARRAY; if (start_addr == NULL) { Int i; - AllocateStaticArraySpace(p, type, dim PASS_REGS); + AllocateStaticArraySpace(p, type, NULL, dim PASS_REGS); if (p->ValueOfVE.ints == NULL) { WRITE_UNLOCK(p->ArRWLock); return p; @@ -683,6 +696,7 @@ CreateStaticArray(AtomEntry *ae, Int dim, static_array_types type, CODEADDR star } } else { /* external array */ + p->TypeOfAE |= MMAP_ARRAY; p->ValueOfVE.chars = (char *)start_addr; } WRITE_UNLOCK(p->ArRWLock); @@ -690,7 +704,7 @@ CreateStaticArray(AtomEntry *ae, Int dim, static_array_types type, CODEADDR star } static void -ResizeStaticArray(StaticArrayEntry *pp, Int dim USES_REGS) +ResizeStaticArray(StaticArrayEntry *pp, size_t dim USES_REGS) { statarray_elements old_v = pp->ValueOfVE; static_array_types type = pp->ArrayType; @@ -698,78 +712,56 @@ ResizeStaticArray(StaticArrayEntry *pp, Int dim USES_REGS) Int mindim = (dim < old_dim ? dim : old_dim), i; /* change official size */ - if (pp->ArrayEArity >= 0){ + if (pp->ArrayEArity == 0){ return; } WRITE_LOCK(pp->ArRWLock); - pp->ArrayEArity = -dim; + pp->ArrayEArity = dim; #if HAVE_MMAP - if (pp->ValueOfVE.chars < (char *)Yap_HeapBase || - pp->ValueOfVE.chars > (char *)HeapTop) { + if (pp->TypeOfAE & MMAP_ARRAY) { ResizeMmappedArray(pp, dim, (void *)(pp->ValueOfVE.chars) PASS_REGS); WRITE_UNLOCK(pp->ArRWLock); return; } #endif - AllocateStaticArraySpace(pp, type, dim PASS_REGS); + AllocateStaticArraySpace(pp, type, old_v.chars, dim PASS_REGS); switch(type) { case array_of_ints: - for (i = 0; i ValueOfVE.ints[i] = old_v.ints[i]; for (i = mindim; iValueOfVE.ints[i] = 0; break; case array_of_chars: - for (i = 0; i ValueOfVE.chars[i] = old_v.chars[i]; for (i = mindim; iValueOfVE.chars[i] = '\0'; break; case array_of_uchars: - for (i = 0; i ValueOfVE.uchars[i] = old_v.uchars[i]; for (i = mindim; iValueOfVE.uchars[i] = '\0'; break; case array_of_doubles: - for (i = 0; i ValueOfVE.floats[i] = old_v.floats[i]; for (i = mindim; iValueOfVE.floats[i] = 0.0; break; case array_of_ptrs: - for (i = 0; i ValueOfVE.ptrs[i] = old_v.ptrs[i]; for (i = mindim; iValueOfVE.ptrs[i] = NULL; break; case array_of_atoms: - for (i = 0; i ValueOfVE.atoms[i] = old_v.atoms[i]; for (i = mindim; iValueOfVE.atoms[i] = TermNil; break; case array_of_dbrefs: - for (i = 0; i ValueOfVE.dbrefs[i] = old_v.dbrefs[i]; for (i = mindim; iValueOfVE.dbrefs[i] = 0L; break; case array_of_terms: - for (i = 0; i ValueOfVE.terms[i] = old_v.terms[i]; for (i = mindim; iValueOfVE.terms[i] = NULL; break; case array_of_nb_terms: - for (i = 0; i ValueOfVE.lterms[i].tlive; - if (IsVarTerm(tlive) && IsUnboundVar(&(pp->ValueOfVE.lterms[i].tlive))) { - RESET_VARIABLE(&(pp->ValueOfVE.lterms[i].tlive)); - } else { - pp->ValueOfVE.lterms[i].tlive = tlive; - } - pp->ValueOfVE.lterms[i].tstore = old_v.lterms[i].tstore; + for (i = mindim; i ValueOfVE.lterms[i].tlive)); + pp->ValueOfVE.lterms[i].tstore = TermNil; } break; } @@ -784,7 +776,7 @@ ClearStaticArray(StaticArrayEntry *pp) Int dim = - pp->ArrayEArity, i; /* change official size */ - if (pp->ArrayEArity >= 0){ + if (pp->ArrayEArity == 0){ return; } WRITE_LOCK(pp->ArRWLock); @@ -949,8 +941,7 @@ p_create_array( USES_REGS1 ) WRITE_UNLOCK(ae->ARWLock); if (!IsVarTerm(app->ValueOfVE) || !IsUnboundVar(&app->ValueOfVE)) { - if (size == app->ArrayEArity || - size == -app->ArrayEArity) + if (size == app->ArrayEArity) return TRUE; Yap_Error(PERMISSION_ERROR_CREATE_ARRAY,t,"create_array", ae->StrOfAE); @@ -1064,7 +1055,7 @@ p_create_static_array( USES_REGS1 ) return FALSE; } } else { - if (pp->ArrayEArity == -size && + if (pp->ArrayEArity == size && pp->ArrayType == props) { WRITE_UNLOCK(ae->ARWLock); return TRUE; @@ -1101,7 +1092,7 @@ p_static_array_properties( USES_REGS1 ) return (FALSE); } else { static_array_types tp = pp->ArrayType; - Int dim = -pp->ArrayEArity; + Int dim = pp->ArrayEArity; READ_UNLOCK(ae->ARWLock); if (dim <= 0 || !Yap_unify(ARG2,MkIntegerTerm(dim))) @@ -1169,7 +1160,7 @@ p_resize_static_array( USES_REGS1 ) Yap_Error(PERMISSION_ERROR_RESIZE_ARRAY,t,"resize a static array"); return(FALSE); } else { - Int osize = - pp->ArrayEArity; + size_t osize = pp->ArrayEArity; ResizeStaticArray(pp, size PASS_REGS); return(Yap_unify(ARG2,MkIntegerTerm(osize))); } @@ -1237,11 +1228,9 @@ p_close_static_array( USES_REGS1 ) StaticArrayEntry *ptr = (StaticArrayEntry *)pp; if (ptr->ValueOfVE.ints != NULL) { #if HAVE_MMAP - if (ptr->ValueOfVE.chars < (char *)Yap_HeapBase || - ptr->ValueOfVE.chars > (char *)HeapTop) { - Int val = CloseMmappedArray(ptr, (void *)ptr->ValueOfVE.chars PASS_REGS); + Int val = CloseMmappedArray(ptr, (void *)ptr->ValueOfVE.chars PASS_REGS); #if USE_SYSTEM_MALLOC - if (val) + if (val) { #endif return(val); } @@ -1651,7 +1640,7 @@ p_assign_static( USES_REGS1 ) WRITE_LOCK(ptr->ArRWLock); READ_UNLOCK(ae->ARWLock); /* a static array */ - if (indx < 0 || indx >= - ptr->ArrayEArity) { + if (indx < 0 || indx >= ptr->ArrayEArity) { WRITE_UNLOCK(ptr->ArRWLock); Yap_Error(DOMAIN_ERROR_ARRAY_OVERFLOW,t2,"assign_static"); return FALSE; @@ -1980,7 +1969,7 @@ p_assign_dynamic( USES_REGS1 ) WRITE_LOCK(ptr->ArRWLock); /* a static array */ - if (indx < 0 || indx >= - ptr->ArrayEArity) { + if (indx < 0 || indx >= ptr->ArrayEArity) { WRITE_UNLOCK(ptr->ArRWLock); Yap_Error(DOMAIN_ERROR_ARRAY_OVERFLOW,t2,"assign_static"); return FALSE; @@ -2179,7 +2168,7 @@ p_add_to_array_element( USES_REGS1 ) WRITE_LOCK(ptr->ArRWLock); /* a static array */ - if (indx < 0 || indx >= - ptr->ArrayEArity) { + if (indx < 0 || indx >= ptr->ArrayEArity) { WRITE_UNLOCK(ptr->ArRWLock); Yap_Error(DOMAIN_ERROR_ARRAY_OVERFLOW,t2,"add_to_array_element"); return FALSE; diff --git a/H/Yatom.h b/H/Yatom.h index fc6c9bbf2..163bdce6d 100644 --- a/H/Yatom.h +++ b/H/Yatom.h @@ -1281,6 +1281,12 @@ IsTranslationProperty (int flags) } +typedef enum { + STATIC_ARRAY = 1, + DYNAMIC_ARRAY = 2, + MMAP_ARRAY = 4, + FIXED_ARRAY = 8 +} array_type; /* array property entry structure */ @@ -1290,6 +1296,7 @@ typedef struct array_entry Prop NextOfPE; /* used to chain properties */ PropFlags KindOfPE; /* kind of property */ Int ArrayEArity; /* Arity of Array (positive) */ + array_type TypeOfAE; #if defined(YAPOR) || defined(THREADS) rwlock_t ArRWLock; /* a read-write lock to protect the entry */ #if THREADS @@ -1341,6 +1348,7 @@ typedef struct static_array_entry Prop NextOfPE; /* used to chain properties */ PropFlags KindOfPE; /* kind of property */ Int ArrayEArity; /* Arity of Array (negative) */ + array_type TypeOfAE; #if defined(YAPOR) || defined(THREADS) rwlock_t ArRWLock; /* a read-write lock to protect the entry */ #endif @@ -1441,7 +1449,7 @@ INLINE_ONLY inline EXTERN int ArrayIsDynamic (ArrayEntry *); INLINE_ONLY inline EXTERN int ArrayIsDynamic (ArrayEntry * are) { - return (int) (((are)->ArrayEArity > 0)); + return (int) (((are)->TypeOfAE & DYNAMIC_ARRAY)); } diff --git a/H/arrays.h b/H/arrays.h index 4eea57f9f..6ee09a473 100644 --- a/H/arrays.h +++ b/H/arrays.h @@ -26,5 +26,3 @@ typedef struct array_access_struct { keep it as an integer! */ } array_access; - - From f3e5639439fafe9429dcd06c6d1b1585ab4241b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 2 Jan 2014 21:59:02 +0000 Subject: [PATCH 3/3] fix sign problems with arrays --- C/arrays.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/C/arrays.c b/C/arrays.c index 23cf27dc7..94c334479 100644 --- a/C/arrays.c +++ b/C/arrays.c @@ -708,8 +708,8 @@ ResizeStaticArray(StaticArrayEntry *pp, size_t dim USES_REGS) { statarray_elements old_v = pp->ValueOfVE; static_array_types type = pp->ArrayType; - Int old_dim = - pp->ArrayEArity; - Int mindim = (dim < old_dim ? dim : old_dim), i; + size_t old_dim = pp->ArrayEArity; + size_t mindim = (dim < old_dim ? dim : old_dim), i; /* change official size */ if (pp->ArrayEArity == 0){ @@ -773,7 +773,7 @@ ClearStaticArray(StaticArrayEntry *pp) { statarray_elements old_v = pp->ValueOfVE; static_array_types type = pp->ArrayType; - Int dim = - pp->ArrayEArity, i; + Int dim = pp->ArrayEArity, i; /* change official size */ if (pp->ArrayEArity == 0){ @@ -2260,7 +2260,7 @@ p_static_array_to_term( USES_REGS1 ) return (FALSE); } else { static_array_types tp = pp->ArrayType; - Int dim = -pp->ArrayEArity, indx; + Int dim = pp->ArrayEArity, indx; CELL *base; while (H+1+dim > ASP-1024) {