try to avoid unneeded malloc

This commit is contained in:
Vitor Santos Costa 2016-12-10 01:04:37 -06:00
parent 6e2d2628c6
commit d81e077cbe
3 changed files with 64 additions and 27 deletions

View File

@ -70,6 +70,33 @@ int pop_text_stack(int i) {
return lvl; return lvl;
} }
void *protected_pop_text_stack(int i, void *protected, bool tmp,
size_t sz USES_REGS) {
void *out = protected;
int lvl = LOCAL_TextBuffer->lvl;
while (lvl > i) {
struct mblock *p = LOCAL_TextBuffer->first[lvl];
while (p) {
struct mblock *np = p->next;
if (p + 1 == protected) {
if (tmp)
out = LOCAL_FileNameBuf;
else
out = p;
memcpy(out, protected, sz);
} else {
free(p);
}
p = np;
}
LOCAL_TextBuffer->first[lvl] = NULL;
LOCAL_TextBuffer->last[lvl] = NULL;
lvl--;
}
LOCAL_TextBuffer->lvl = lvl;
return out;
}
// void pop_text_stack(int i) { LOCAL_TextBuffer->lvl = i; } // void pop_text_stack(int i) { LOCAL_TextBuffer->lvl = i; }
void *Malloc(size_t sz USES_REGS) { void *Malloc(size_t sz USES_REGS) {

View File

@ -39,6 +39,8 @@ extern void Free(void *buf USES_REGS);
extern int push_text_stack(USES_REGS1); extern int push_text_stack(USES_REGS1);
extern int pop_text_stack(int lvl USES_REGS); extern int pop_text_stack(int lvl USES_REGS);
extern void *protected_pop_text_stack(int lvl, void *safe, bool tmp,
size_t sz USES_REGS);
#ifndef min #ifndef min
#define min(x, y) (x < y ? x : y) #define min(x, y) (x < y ? x : y)
@ -156,10 +158,12 @@ INLINE_ONLY EXTERN inline char_kind_t chtype(Int ch) {
#define __android_log_print(...) #define __android_log_print(...)
#endif #endif
INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr, size_t n, INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr,
size_t n,
utf8proc_int32_t *valp); utf8proc_int32_t *valp);
INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr, size_t n, INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr,
size_t n,
utf8proc_int32_t *valp) { utf8proc_int32_t *valp) {
return utf8proc_iterate(ptr, n, valp); return utf8proc_iterate(ptr, n, valp);
} }
@ -1394,7 +1398,6 @@ static inline Atom UTF32ToAtom(const wchar_t *s USES_REGS) {
return out.val.a; return out.val.a;
} }
static inline Term Yap_WCharsToListOfCodes(const wchar_t *s USES_REGS) { static inline Term Yap_WCharsToListOfCodes(const wchar_t *s USES_REGS) {
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.w0 = s; inp.val.w0 = s;

View File

@ -203,6 +203,7 @@ X_API int PL_get_nchars(term_t l, size_t *lengthp, char **s, unsigned flags) {
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
int lvl = push_text_stack();
inp.val.t = Yap_GetFromSlot(l); inp.val.t = Yap_GetFromSlot(l);
inp.type = cvtFlags(flags); inp.type = cvtFlags(flags);
out.type = YAP_STRING_CHARS; out.type = YAP_STRING_CHARS;
@ -220,8 +221,14 @@ X_API int PL_get_nchars(term_t l, size_t *lengthp, char **s, unsigned flags) {
out.type |= YAP_STRING_NCHARS; out.type |= YAP_STRING_NCHARS;
out.max = *lengthp; out.max = *lengthp;
} }
if (!Yap_CVT_Text(&inp, &out PASS_REGS)) const char *buf;
if (!Yap_CVT_Text(&inp, &out PASS_REGS)) {
pop_text_stack(lvl);
return false; return false;
}
out.val.c = protected_pop_text_stack(lvl, out.val.c,
flags & (BUF_RING | BUF_DISCARDABLE),
strlen(out.val.c) + 1 PASS_REGS);
*s = out.val.c; *s = out.val.c;
return true; return true;
} }