sizeof for space taken, kill wchar_t internally

This commit is contained in:
Vitor Santos Costa 2017-01-22 22:10:38 +00:00
parent 1e379e7635
commit 7d230dac0f

38
C/atomic.c Normal file → Executable file
View File

@ -36,10 +36,10 @@ The following predicates are used to manipulate atoms:
*/ */
#include "Yap.h" #include "Yap.h"
#include "YapEval.h"
#include "YapHeap.h" #include "YapHeap.h"
#include "YapText.h" #include "YapText.h"
#include "Yatom.h" #include "Yatom.h"
#include "eval.h"
#include "yapio.h" #include "yapio.h"
#ifdef TABLING #ifdef TABLING
#include "tab.macros.h" #include "tab.macros.h"
@ -1715,22 +1715,24 @@ static Int atom_split(USES_REGS1) {
ReleaseAndReturn((FALSE)); ReleaseAndReturn((FALSE));
} }
at = AtomOfTerm(t1); at = AtomOfTerm(t1);
unsigned char *s, *s1, *s10; char *s;
s = RepAtom(at)->UStrOfAE; unsigned char *s1, *s10, *s0;
if (len > (Int)strlen_utf8(s)) s = RepAtom(at)->StrOfAE;
if (len > strlen(s))
ReleaseAndReturn((FALSE)); ReleaseAndReturn((FALSE));
s1 = s10 = Malloc(len); s1 = s10 = Malloc(len);
s0 = RepAtom(at)->UStrOfAE;
if (s1 + len > (unsigned char *)ASP - 1024) if (s1 + len > (unsigned char *)ASP - 1024)
Yap_Error(RESOURCE_ERROR_STACK, t1, "$atom_split/4"); Yap_Error(RESOURCE_ERROR_STACK, t1, "$atom_split/4");
size_t j; size_t j;
for (j = 0; j < len; j++) { for (j = 0; j < len; j++) {
int32_t val; int32_t val;
s += get_utf8(s, 1, &val); s0 += get_utf8(s0, 1, &val);
s1 += put_utf8(s, val); s1 += put_utf8(s1, val);
} }
s1[0] = '\0'; s1[0] = '\0';
to1 = MkAtomTerm(Yap_ULookupAtom(s10)); to1 = MkAtomTerm(Yap_ULookupAtom(s10));
to2 = MkAtomTerm(Yap_ULookupAtom(s)); to2 = MkAtomTerm(Yap_LookupAtom(s));
ReleaseAndReturn( ReleaseAndReturn(
(Yap_unify_constant(ARG3, to1) && Yap_unify_constant(ARG4, to2))); (Yap_unify_constant(ARG3, to1) && Yap_unify_constant(ARG4, to2)));
} }
@ -1790,8 +1792,8 @@ restart_aux:
#define SUB_ATOM_HAS_ATOM 16 #define SUB_ATOM_HAS_ATOM 16
#define SUB_ATOM_HAS_UTF8 32 #define SUB_ATOM_HAS_UTF8 32
static Term build_new_atomic(int mask, wchar_t *wp, const unsigned char *p, static Term build_new_atomic(int mask, const unsigned char *p, size_t minv,
size_t minv, size_t len USES_REGS) { size_t len USES_REGS) {
int n; int n;
seq_tv_t outv[5], inp; seq_tv_t outv[5], inp;
size_t cuts[3]; size_t cuts[3];
@ -1876,7 +1878,6 @@ static Int cont_sub_atomic(USES_REGS1) {
Term tat5 = Deref(ARG5); Term tat5 = Deref(ARG5);
int mask; int mask;
size_t minv, len, after, sz; size_t minv, len, after, sz;
wchar_t *wp = NULL;
const unsigned char *p = NULL, *p5 = NULL; const unsigned char *p = NULL, *p5 = NULL;
mask = IntegerOfTerm(EXTRA_CBACK_ARG(5, 1)); mask = IntegerOfTerm(EXTRA_CBACK_ARG(5, 1));
@ -1936,7 +1937,7 @@ static Int cont_sub_atomic(USES_REGS1) {
cut_fail(); cut_fail();
} }
} else if (mask & SUB_ATOM_HAS_SIZE) { } else if (mask & SUB_ATOM_HAS_SIZE) {
Term nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
Yap_unify(ARG2, MkIntegerTerm(minv)); Yap_unify(ARG2, MkIntegerTerm(minv));
Yap_unify(ARG4, MkIntegerTerm(after)); Yap_unify(ARG4, MkIntegerTerm(after));
Yap_unify(ARG5, nat); Yap_unify(ARG5, nat);
@ -1945,7 +1946,7 @@ static Int cont_sub_atomic(USES_REGS1) {
cut_succeed(); cut_succeed();
} else if (mask & SUB_ATOM_HAS_MIN) { } else if (mask & SUB_ATOM_HAS_MIN) {
after = sz - (minv + len); after = sz - (minv + len);
Term nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
Yap_unify(ARG3, MkIntegerTerm(len)); Yap_unify(ARG3, MkIntegerTerm(len));
Yap_unify(ARG4, MkIntegerTerm(after)); Yap_unify(ARG4, MkIntegerTerm(after));
Yap_unify(ARG5, nat); Yap_unify(ARG5, nat);
@ -1954,7 +1955,7 @@ static Int cont_sub_atomic(USES_REGS1) {
cut_succeed(); cut_succeed();
} else if (mask & SUB_ATOM_HAS_AFTER) { } else if (mask & SUB_ATOM_HAS_AFTER) {
len = sz - (minv + after); len = sz - (minv + after);
Term nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
Yap_unify(ARG2, MkIntegerTerm(minv)); Yap_unify(ARG2, MkIntegerTerm(minv));
Yap_unify(ARG3, MkIntegerTerm(len)); Yap_unify(ARG3, MkIntegerTerm(len));
Yap_unify(ARG5, nat); Yap_unify(ARG5, nat);
@ -1962,7 +1963,7 @@ static Int cont_sub_atomic(USES_REGS1) {
if (len-- == 0) if (len-- == 0)
cut_succeed(); cut_succeed();
} else { } else {
Term nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
Yap_unify(ARG2, MkIntegerTerm(minv)); Yap_unify(ARG2, MkIntegerTerm(minv));
Yap_unify(ARG3, MkIntegerTerm(len)); Yap_unify(ARG3, MkIntegerTerm(len));
Yap_unify(ARG4, MkIntegerTerm(after)); Yap_unify(ARG4, MkIntegerTerm(after));
@ -1988,7 +1989,6 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
Term tat1, tbef, tsize, tafter, tout; Term tat1, tbef, tsize, tafter, tout;
int mask = SUB_ATOM_HAS_UTF8; int mask = SUB_ATOM_HAS_UTF8;
size_t minv, len, after, sz; size_t minv, len, after, sz;
wchar_t *wp = NULL;
const unsigned char *p = NULL; const unsigned char *p = NULL;
int bnds = 0; int bnds = 0;
Term nat = 0L; Term nat = 0L;
@ -2100,7 +2100,7 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
release_cut_fail(); release_cut_fail();
if ((Int)(after = (sz - (minv + len))) < 0) if ((Int)(after = (sz - (minv + len))) < 0)
release_cut_fail(); release_cut_fail();
nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (!nat) if (!nat)
release_cut_fail(); release_cut_fail();
out = Yap_unify(ARG4, MkIntegerTerm(after)) && Yap_unify(ARG5, nat); out = Yap_unify(ARG4, MkIntegerTerm(after)) && Yap_unify(ARG5, nat);
@ -2109,7 +2109,7 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
if (sz < minv + after) if (sz < minv + after)
release_cut_fail(); release_cut_fail();
len = sz - (minv + after); len = sz - (minv + after);
nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (!nat) if (!nat)
release_cut_fail(); release_cut_fail();
out = Yap_unify(ARG3, MkIntegerTerm(len)) && Yap_unify(ARG5, nat); out = Yap_unify(ARG3, MkIntegerTerm(len)) && Yap_unify(ARG5, nat);
@ -2118,7 +2118,7 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
if (len + after > sz) if (len + after > sz)
release_cut_fail(); release_cut_fail();
minv = sz - (len + after); minv = sz - (len + after);
nat = build_new_atomic(mask, wp, p, minv, len PASS_REGS); nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (!nat) if (!nat)
release_cut_fail(); release_cut_fail();
out = Yap_unify(ARG2, MkIntegerTerm(minv)) && Yap_unify(ARG5, nat); out = Yap_unify(ARG2, MkIntegerTerm(minv)) && Yap_unify(ARG5, nat);
@ -2141,7 +2141,7 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
if (!out) if (!out)
release_cut_fail(); release_cut_fail();
} else { } else {
out = (strlen_utf8(RepAtom(AtomOfTerm(tout))->UStrOfAE) == len); out = (strlen(RepAtom(AtomOfTerm(tout))->StrOfAE) == len);
if (!out) if (!out)
release_cut_fail(); release_cut_fail();
} }