From ded6b2435c9726d022c2e3f014379bc072b084da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 21 Jun 2011 15:11:07 +0100 Subject: [PATCH] improve BigNum handling. --- C/bignum.c | 77 ++++++++++++++++++++++++----------------------------- C/scanner.c | 4 +++ C/write.c | 14 ++++++++++ H/TermExt.h | 2 +- 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/C/bignum.c b/C/bignum.c index 3a2929b72..c34d19c1f 100755 --- a/C/bignum.c +++ b/C/bignum.c @@ -284,6 +284,7 @@ Yap_MkBlobStringTerm(const char *s, size_t len) MP_INT *dst = (MP_INT *)(H+2); blob_string_t *sp; size_t siz; + char *dest; sz = strlen(s); if (len > 0 && sz > len) sz = len; @@ -292,17 +293,16 @@ Yap_MkBlobStringTerm(const char *s, size_t len) } H[0] = (CELL)FunctorBigInt; H[1] = BLOB_STRING; - - siz = (sizeof(size_t)+len+sizeof(CELL))/sizeof(CELL); + siz = ALIGN_YAPTYPE((len+1+sizeof(blob_string_t)),CELL); dst->_mp_size = 0L; - dst->_mp_alloc = siz; + dst->_mp_alloc = siz/sizeof(mp_limb_t); sp = (blob_string_t *)(dst+1); - H = (CELL *)sp; sp->len = sz; - strncpy((char *)(sp+1), s, sz+1); - H += siz; - H[0] = EndSpecials; - H++; + dest = (char *)(sp+1); + strncpy(dest, s, sz); + dest[sz] = '\0'; + H += (siz + 2*sizeof(CELL)+sizeof(MP_INT)+sizeof(Functor))/sizeof(CELL); + H[-1] = EndSpecials; return AbsAppl(ret); } @@ -313,56 +313,49 @@ Yap_MkBlobWideStringTerm(const wchar_t *s, size_t len) CELL *ret = H; size_t sz; MP_INT *dst = (MP_INT *)(H+2); - blob_string_t *sp; + blob_string_t *sp = (blob_string_t *)(dst+1); size_t siz, i = 0; + H[0] = (CELL)FunctorBigInt; + dst->_mp_size = 0L; sz = wcslen(s); - if (len > 0 && sz > len) sz = len; + if (len > 0 && sz > len) { + sz = len; + } + if ((len/sizeof(CELL)) > (ASP-ret)-1024) { + return TermNil; + } while (i < sz) { if (s[i++] >= 255) break; } if (i == sz) { + /* we have a standard ascii string */ char *target; size_t i = 0; - if (len/sizeof(CELL) > (ASP-ret)-1024) { - return TermNil; - } - H[0] = (CELL)FunctorBigInt; H[1] = BLOB_STRING; - - siz = (sizeof(size_t)+len+sizeof(CELL))/sizeof(CELL); - dst->_mp_size = 0L; - dst->_mp_alloc = siz; - sp = (blob_string_t *)(dst+1); - H = (CELL *)sp; + siz = ALIGN_YAPTYPE((sz+1+sizeof(blob_string_t)),CELL); + dst->_mp_alloc = siz/sizeof(mp_limb_t); sp->len = sz; target = (char *)(sp+1); - while (i < sz+1) { + for (i = 0 ; i < sz; i++) { target[i] = s[i]; - i++; } - H += siz; - H[0] = EndSpecials; - H++; - return AbsAppl(ret); - } - if (len/sizeof(CELL) > (ASP-ret)-1024) { - return TermNil; - } - H[0] = (CELL)FunctorBigInt; - H[1] = BLOB_WIDE_STRING; + target[sz] = '\0'; + H += (siz+2*sizeof(CELL)+sizeof(MP_INT)+sizeof(Functor))/sizeof(CELL); + } else { + wchar_t * target; - siz = (sizeof(size_t)+(len+2)*sizeof(wchar_t))/sizeof(CELL); - dst->_mp_size = 0L; - dst->_mp_alloc = siz; - sp = (blob_string_t *)(dst+1); - H = (CELL *)sp; - sp->len = sz; - wcsncpy((wchar_t *)(sp+1), s, sz+1); - H += siz; - H[0] = EndSpecials; - H++; + H[1] = BLOB_WIDE_STRING; + siz = ALIGN_YAPTYPE((sz+1)*sizeof(wchar_t)+sizeof(blob_string_t),CELL); + dst->_mp_alloc = siz/sizeof(mp_limb_t); + sp->len = sz; + target = (wchar_t *)(sp+1); + wcsncpy(target, s, sz); + target[sz] = '\0'; + H += (siz + 2*sizeof(CELL)+sizeof(MP_INT)+sizeof(Functor))/sizeof(CELL); + } + H[-1] = EndSpecials; return AbsAppl(ret); } diff --git a/C/scanner.c b/C/scanner.c index 4ed7b9616..b093e8131 100755 --- a/C/scanner.c +++ b/C/scanner.c @@ -813,6 +813,7 @@ close_comment( USES_REGS1 ) { LOCAL_CommentsBuff[LOCAL_CommentsBuffPos] = '\0'; *LOCAL_CommentsNextChar = Yap_MkBlobWideStringTerm(LOCAL_CommentsBuff, LOCAL_CommentsBuffPos); free(LOCAL_CommentsBuff); + LOCAL_CommentsBuff = NULL; LOCAL_CommentsBuffLim = 0; } @@ -1372,5 +1373,8 @@ Yap_clean_tokenizer(TokEntry *tokstart, VarEntry *vartable, VarEntry *anonvartab } LOCAL_Comments = TermNil; LOCAL_CommentsNextChar = LOCAL_CommentsTail = NULL; + free(LOCAL_CommentsBuff); + LOCAL_CommentsBuff = NULL; + LOCAL_CommentsBuffLim = 0; } diff --git a/C/write.c b/C/write.c index 0e70abf69..3f0c87e95 100755 --- a/C/write.c +++ b/C/write.c @@ -210,6 +210,20 @@ writebig(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, stru return; } #endif + if (pt[0] == BLOB_STRING) { + wrputc('"',wglb->writewch); + wrputs(Yap_BlobStringOfTerm(t),wglb->writewch); + wrputc('"',wglb->writewch); + return; + } else if (pt[0] == BLOB_STRING) { + wchar_t *s = Yap_BlobWideStringOfTerm(t); + wrputc('"', wglb->writewch); + while (*s) { + wrputc(*s++, wglb->writewch); + } + wrputc('"',wglb->writewch); + return; + } wrputs("0",wglb->writewch); } diff --git a/H/TermExt.h b/H/TermExt.h index 4a9d46a2e..61c617e6b 100755 --- a/H/TermExt.h +++ b/H/TermExt.h @@ -397,7 +397,7 @@ IsLargeIntTerm (Term t) #endif typedef struct string_struct { - size_t len; + UInt len; } blob_string_t; Term STD_PROTO (Yap_MkBlobStringTerm, (const char *, size_t len));