a question of size

This commit is contained in:
Vitor Santos Costa
2017-09-19 13:19:13 +01:00
parent 53822922c8
commit 98a5538f45
17 changed files with 2070 additions and 1872 deletions

View File

@@ -184,9 +184,7 @@ LookupAtom(const unsigned char *atom) { /* lookup atom in atom table */
#endif
/* add new atom to start of chain */
size_t asz = strlen((const char *)atom);
ae = (AtomEntry *)Yap_AllocAtomSpace((sizeof *ae) +
asz+4);
ae = (AtomEntry *)Yap_AllocAtomSpace((sizeof *ae) + asz+1);
if (ae == NULL) {
WRITE_UNLOCK(HashChain[hash].AERWLock);
return NIL;
@@ -194,7 +192,7 @@ LookupAtom(const unsigned char *atom) { /* lookup atom in atom table */
NOfAtoms++;
na = AbsAtom(ae);
ae->PropsOfAE = NIL;
stpncpy((char *)ae->StrOfAE, (const char *)atom, asz+1);
strcpy(ae->StrOfAE, (const char *)atom);
ae->NextOfAE = a;
HashChain[hash].Entry = na;

View File

@@ -800,7 +800,8 @@ restart_aux:
ReleaseAndReturn(FALSE);
}
static Int cont_atom_concat3(USES_REGS1) {
static Int
cont_atom_concat3(USES_REGS1) {
Term t3;
Atom ats[2];
Int i, max;
@@ -858,7 +859,7 @@ restart_aux:
ot = ARG1;
} else if (g3) {
EXTRA_CBACK_ARG(3, 1) = MkIntTerm(0);
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_AtomToLength(t3 PASS_REGS));
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_AtomToUnicodeLength(t3 PASS_REGS));
ReleaseAndReturn(cont_atom_concat3(PASS_REGS1));
} else {
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
@@ -948,7 +949,7 @@ restart_aux:
ot = ARG1;
} else if (g3) {
EXTRA_CBACK_ARG(3, 1) = MkIntTerm(0);
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_AtomicToLength(t3 PASS_REGS));
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_AtomicToUnicodeLength(t3 PASS_REGS));
return cont_atomic_concat3(PASS_REGS1);
} else {
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
@@ -1032,7 +1033,7 @@ restart_aux:
ot = ARG1;
} else if (g3) {
EXTRA_CBACK_ARG(3, 1) = MkIntTerm(0);
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_StringToLength(t3 PASS_REGS));
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_StringToUnicodeLength(t3 PASS_REGS));
pop_text_stack(l);
return cont_string_concat3(PASS_REGS1);
} else {
@@ -1482,7 +1483,7 @@ static Int atom_length(USES_REGS1) {
}
}
restart_aux:
len = Yap_AtomicToLength(t1 PASS_REGS);
len = Yap_AtomToUnicodeLength(t1 PASS_REGS);
if (len != (size_t)-1)
ReleaseAndReturn(Yap_unify(ARG2, MkIntegerTerm(len)));
/* error handling */
@@ -1523,7 +1524,7 @@ static Int atomic_length(USES_REGS1) {
}
}
restart_aux:
len = Yap_AtomicToLength(t1 PASS_REGS);
len = Yap_AtomicToUnicodeLength(t1 PASS_REGS);
if (len != (size_t)-1)
ReleaseAndReturn(Yap_unify(ARG2, MkIntegerTerm(len)));
/* error handling */
@@ -1552,7 +1553,7 @@ static Int string_length(USES_REGS1) {
}
restart_aux:
t1 = Deref(ARG1);
len = Yap_AtomicToLength(t1 PASS_REGS);
len = Yap_StringToUnicodeLength(t1 PASS_REGS);
if (len != (size_t)-1)
ReleaseAndReturn(Yap_unify(ARG2, MkIntegerTerm(len)));
/* error handling */

141
C/text.c
View File

@@ -188,18 +188,18 @@ static Term Globalize(Term v USES_REGS) {
static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp,
Int *atoms, bool *wide, seq_tv_t *inp USES_REGS) {
Int length = 0;
Term *s; /* slow */
Term v; /* temporary */
*wide = false;
unsigned char *st0 = *bufp, *st;
bool atomst;
size_t max_lim = 1024;
if (!st0) {
st0 = Malloc(0);
st0 = Malloc(1024);
}
do_derefa(v, l, derefa_unk, derefa_nonvar);
*tailp = l;
s = l;
*bufp = st = st0;
@@ -208,66 +208,61 @@ static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp,
return 0;
}
if (IsPairTerm(*l)) {
Term hd0 = HeadOfTerm(*l);
if (IsVarTerm(hd0)) {
return -INSTANTIATION_ERROR;
}
// are we looking for atoms/codes?
// whatever the case, we should be consistent throughout,
// so we should be consistent with the first arg.
if (*atoms == 1) {
if (!IsIntegerTerm(hd0)) {
return -INSTANTIATION_ERROR;
}
} else if (*atoms == 2) {
if (!IsAtomTerm(hd0)) {
return -TYPE_ERROR_ATOM;
}
}
do {
int ch;
length++;
{
Term hd = Deref(RepPair(*l)[0]);
if (IsVarTerm(hd)) {
Term hd0 = HeadOfTerm(*l);
if (IsVarTerm(hd0)) {
return -INSTANTIATION_ERROR;
} else if (IsAtomTerm(hd)) {
(*atoms)++;
if (*atoms < length) {
*tailp = l;
return -REPRESENTATION_ERROR_CHARACTER_CODE;
} else {
AtomEntry *ae = RepAtom(AtomOfTerm(hd));
st = stpcpy(st, ae->StrOfAE);
}
} else if (IsIntegerTerm(hd)) {
ch = IntegerOfTerm(hd);
if (*atoms)
length = -REPRESENTATION_ERROR_CHARACTER;
else if (ch < 0) {
*tailp = l;
length = -REPRESENTATION_ERROR_CHARACTER_CODE;
} else {
*wide |= ch > 0x80;
}
} else {
length = -TYPE_ERROR_INTEGER;
}
if (length < 0) {
*tailp = l;
return length;
}
// now copy char to buffer
int chsz = put_utf8(st, ch);
if (chsz > 0) {
st += chsz;
}
}
// are we looking for atoms/codes?
// whatever the case, we should be consistent throughout,
// so we should be consistent with the first arg.
if (st > st0+max_lim) {
max_lim += 2048;
*bufp = st0 = Realloc(st0,max_lim);
}
if (IsAtomTerm(hd0)) {
atomst = true;
} else {
atomst = false;
}
l = RepPair(*l) + 1;
do_derefa(v, l, derefa2_unk, derefa2_nonvar);
} while (*l != *s && IsPairTerm(*l));
while ( IsPairTerm(*l)) {
int ch;
length++;
{
Term hd = Deref(RepPair(*l)[0]);
if (IsVarTerm(hd)) {
return -INSTANTIATION_ERROR;
} else if (IsAtomTerm(hd)) {
if (!atomst) {
return -REPRESENTATION_ERROR_CHARACTER;
} else {
AtomEntry *ae = RepAtom(AtomOfTerm(hd));
st = (unsigned char *) stpcpy( ( char *)st, ae->StrOfAE);
}
} else if (IsIntegerTerm(hd)) {
ch = IntegerOfTerm(hd);
if (atomst)
return -REPRESENTATION_ERROR_CHARACTER;
else if (ch < 0) {
*tailp = l;
return -REPRESENTATION_ERROR_CHARACTER_CODE;
} else {
st += put_utf8(st,ch);
}
} else {
return -TYPE_ERROR_INTEGER;
}
if (length < 0) {
*tailp = l;
return length;
}
}
l = RepPair(*l) + 1;
do_derefa(v, l, derefa2_unk, derefa2_nonvar);
}
}
if (IsVarTerm(*l)) {
return -INSTANTIATION_ERROR;
@@ -315,7 +310,7 @@ static unsigned char *wchar2utf8(seq_tv_t *inp, size_t *lengp) {
return buf;
}
static void *slice(size_t min, size_t max, unsigned char *buf USES_REGS);
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS);
static unsigned char *to_buffer(unsigned char *buf, Term t, seq_tv_t *inp,
bool *widep, Int *atoms,
@@ -537,11 +532,10 @@ static Term write_strings(unsigned char *s0, seq_tv_t *out,
Term t = init_tstring(PASS_REGS1);
LOCAL_TERM_ERROR(t, 2 * max);
unsigned char *buf = buf_from_tstring(HR);
strcpy( (char *)buf, s )
;
strcpy( (char *)buf, s );
if (max+1 < min) {
LOCAL_TERM_ERROR(t, 2 * min);
memset(buf+min, max, '\0');
memset(buf+min, '\0', max);
buf += min;
} else {
buf += max+1;
@@ -911,19 +905,6 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
return rc;
}
static int cmp_Text(const unsigned char *s1, const unsigned char *s2, int l) {
const unsigned char *w1 = s1;
utf8proc_int32_t chr1, chr2;
const unsigned char *w2 = s2;
int i;
for (i = 0; i < l; i++) {
w2 += get_utf8(w2, -1, &chr2);
w1 += get_utf8(w1, -1, &chr1);
if (chr1 - chr2)
return chr1 - chr2;
}
return 0;
}
static unsigned char *concat(int n, void *sv[] USES_REGS) {
void *buf;
@@ -947,7 +928,7 @@ static unsigned char *concat(int n, void *sv[] USES_REGS) {
return buf0;
}
static void *slice(size_t min, size_t max, unsigned char *buf USES_REGS) {
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS) {
unsigned char *nbuf = Malloc((max - min) * 4 + 1);
const unsigned char *ptr = skip_utf8(buf, min);
unsigned char *nptr = nbuf;
@@ -1018,7 +999,7 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
u_l1 = u_l - u_l0;
buf1 = slice(u_l0, u_l, buf PASS_REGS);
b_l1 = strlen(buf1);
b_l1 = strlen((const char *)buf1);
bool rc = write_Text(buf1, outv + 1, b_l1 PASS_REGS);
if (!rc) {
return false;
@@ -1032,7 +1013,7 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
u_l1 = strlen_utf8(buf1);
b_l0 = b_l - b_l1;
u_l0 = u_l - u_l1;
if (bcmp(skip_utf8((const char *)buf, b_l0), buf1, b_l1) !=
if (bcmp(skip_utf8((const unsigned char *)buf, b_l0), buf1, b_l1) !=
0) {
return false;
}

View File

@@ -389,7 +389,6 @@ int Yap_FormatFloat(Float f, char **s, size_t sz) {
wglb.lw = separator;
wglb.stream = GLOBAL_Stream + sno;
wrputf(f, &wglb);
wrputc('\0', wglb.stream);
so = Yap_MemExportStreamPtr(sno);
*s = Malloc( strlen(so) )+1;
strcpy(*s, so );