be more aggressive at recovering memory

This commit is contained in:
Vitor Santos Costa 2017-08-27 22:05:46 +01:00
parent 4574cdb4df
commit 7ee2813f90

View File

@ -51,11 +51,13 @@ typedef struct TextBuffer_manager {
int lvl; int lvl;
} text_buffer_t; } text_buffer_t;
int push_text_stack(USES_REGS1) { return LOCAL_TextBuffer->lvl++; } int push_text_stack(USES_REGS1) {
return LOCAL_TextBuffer->lvl++; }
int pop_text_stack(int i) { int pop_text_stack(int i) {
int lvl = LOCAL_TextBuffer->lvl; int lvl = LOCAL_TextBuffer->lvl;
while (lvl > i) { while (lvl >= i) {
struct mblock *p = LOCAL_TextBuffer->first[lvl]; struct mblock *p = LOCAL_TextBuffer->first[lvl];
while (p) { while (p) {
struct mblock *np = p->next; struct mblock *np = p->next;
@ -66,7 +68,7 @@ int pop_text_stack(int i) {
LOCAL_TextBuffer->last[lvl] = NULL; LOCAL_TextBuffer->last[lvl] = NULL;
lvl--; lvl--;
} }
LOCAL_TextBuffer->lvl = lvl; LOCAL_TextBuffer->lvl = i;
return lvl; return lvl;
} }
@ -105,6 +107,8 @@ void *Malloc(size_t sz USES_REGS) {
sz = 1024; sz = 1024;
sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL); sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL);
struct mblock *o = malloc(sz); struct mblock *o = malloc(sz);
if (!o)
return NULL;
o->prev = LOCAL_TextBuffer->last[lvl]; o->prev = LOCAL_TextBuffer->last[lvl];
if (o->prev) { if (o->prev) {
o->prev->next = o; o->prev->next = o;
@ -418,16 +422,29 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
// this is a term, extract to a buffer, and representation is wide // this is a term, extract to a buffer, and representation is wide
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
Atom at = AtomOfTerm(inp->val.t); Atom at = AtomOfTerm(inp->val.t);
size_t sz = strlen(at->StrOfAE);
if (lengp) if (lengp)
*lengp = strlen(at->StrOfAE); *lengp = sz;
if (inp->type & YAP_STRING_WITH_BUFFER)
return at->UStrOfAE; return at->UStrOfAE;
inp->type |= YAP_STRING_IN_TMP;
char *o = Malloc(sz+1);
strcpy(o, at->StrOfAE);
return (unsigned char *)o;
} }
if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) { if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) {
// this is a term, extract to a buffer, and representation is wide // this is a term, extract to a buffer, and representation is wide
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
const char *s = StringOfTerm(inp->val.t);
size_t sz = strlen( s );
if (lengp) if (lengp)
*lengp = strlen(StringOfTerm(inp->val.t)); *lengp = sz;
return (unsigned char *)UStringOfTerm(inp->val.t); if (inp->type & YAP_STRING_WITH_BUFFER)
return UStringOfTerm(inp->val.t);
inp->type |= YAP_STRING_IN_TMP;
char *o = Malloc(sz+1);
strcpy(o, s);
return (unsigned char *)o;
} }
if (((inp->type & (YAP_STRING_CODES | YAP_STRING_ATOMS)) == if (((inp->type & (YAP_STRING_CODES | YAP_STRING_ATOMS)) ==
(YAP_STRING_CODES | YAP_STRING_ATOMS)) && (YAP_STRING_CODES | YAP_STRING_ATOMS)) &&
@ -514,38 +531,25 @@ static Term write_strings(unsigned char *s0, seq_tv_t *out,
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->type & YAP_STRING_NCHARS) if (out->type & YAP_STRING_NCHARS)
min = out->max; min = out->max;
if (out->type & YAP_STRING_TRUNC && out->max < max) if (out->type & YAP_STRING_TRUNC && out->max < max) {
max = out->max; max = out->max;
s0[max] = '\0';
}
} }
unsigned char *s = s0, *lim = s + (max = strlen_utf8(s)); char *s = (char *)s0, *lim = s + max;
Term t = init_tstring(PASS_REGS1); Term t = init_tstring(PASS_REGS1);
unsigned char *cp = s, *buf; LOCAL_TERM_ERROR(t, 2 * max);
unsigned char *buf = buf_from_tstring(HR);
LOCAL_TERM_ERROR(t, 2 * (lim - s)); strcpy( (char *)buf, s )
buf = buf_from_tstring(HR); ;
while (*cp && cp < lim) { if (max+1 < min) {
utf8proc_int32_t chr; LOCAL_TERM_ERROR(t, 2 * min);
int off; memset(buf+min, max, '\0');
off = get_utf8(cp, -1, &chr); buf += min;
if (off > 0) } else {
cp += off; buf += max+1;
else {
// Yap_Error(TYPE_ERROR_TEXT, t, NULL);
cp++;
} }
off = put_utf8(buf, chr);
if (off > 0)
buf += off;
}
if (max >= min)
*buf++ = '\0';
else
while (max < min) {
max++;
buf += put_utf8(buf, '\0');
}
close_tstring(buf PASS_REGS); close_tstring(buf PASS_REGS);
out->val.t = t; out->val.t = t;
@ -967,12 +971,13 @@ bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
int i; int i;
size_t leng; size_t leng;
bufv = Malloc(tot * sizeof(unsigned char *)); bufv = Malloc(tot * sizeof(unsigned char *));
if (!bufv) { if (!bufv) {
return NULL; return NULL;
} }
for (i = 0; i < tot; i++) { for (i = 0; i < tot; i++) {
inp[i].type |= YAP_STRING_IN_TMP; inp[i].type |= YAP_STRING_WITH_BUFFER;
unsigned char *nbuf = Yap_readText(inp + i, &leng PASS_REGS); unsigned char *nbuf = Yap_readText(inp + i, &leng PASS_REGS);
if (!nbuf) { if (!nbuf) {
@ -982,6 +987,7 @@ bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
} }
buf = concat(tot, bufv PASS_REGS); buf = concat(tot, bufv PASS_REGS);
bool rc = write_Text(buf, out, strlen_utf8(buf) PASS_REGS); bool rc = write_Text(buf, out, strlen_utf8(buf) PASS_REGS);
return rc; return rc;
} }