From d81e077cbea3864fddbce2f49858a8b3e0406d73 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sat, 10 Dec 2016 01:04:37 -0600 Subject: [PATCH] try to avoid unneeded malloc --- C/text.c | 27 +++++++++++++++++ H/YapText.h | 55 ++++++++++++++++++----------------- library/dialect/swi/fli/swi.c | 9 +++++- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/C/text.c b/C/text.c index e67a6a27d..648ca6c2a 100644 --- a/C/text.c +++ b/C/text.c @@ -70,6 +70,33 @@ int pop_text_stack(int i) { 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 *Malloc(size_t sz USES_REGS) { diff --git a/H/YapText.h b/H/YapText.h index 57f16f969..267667df0 100644 --- a/H/YapText.h +++ b/H/YapText.h @@ -35,16 +35,18 @@ /// extern void *Malloc(size_t sz USES_REGS); extern void *Realloc(void *buf, size_t sz USES_REGS); -extern void Free(void *buf USES_REGS); +extern void Free(void *buf USES_REGS); -extern int push_text_stack( USES_REGS1 ); -extern int pop_text_stack( int lvl USES_REGS ); +extern int push_text_stack(USES_REGS1); +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 -#define min(x,y) (x 0) { pt += l; - rc ++; + rc++; } else { pt++; } @@ -294,8 +298,8 @@ inline static int cmpn_utf8(const utf8proc_uint8_t *pt1, #define SURROGATE_OFFSET \ ((uint32_t)0x10000 - (uint32_t)(0xD800 << 10) - (uint32_t)0xDC00) -extern const char *Yap_tokText(void*tokptr); -extern Term Yap_tokRep(void*tokptr); +extern const char *Yap_tokText(void *tokptr); +extern Term Yap_tokRep(void *tokptr); // standard strings @@ -1383,17 +1387,16 @@ static inline Term Yap_UTF8ToString(const char *s USES_REGS) { } static inline Atom UTF32ToAtom(const wchar_t *s USES_REGS) { - seq_tv_t inp, out; - - inp.val.w0 = s; - inp.type = YAP_STRING_WCHARS ; - out.type = YAP_STRING_ATOM; - out.max = -1; - if (!Yap_CVT_Text(&inp, &out PASS_REGS)) - return 0L; - return out.val.a; -} + seq_tv_t inp, out; + inp.val.w0 = s; + inp.type = YAP_STRING_WCHARS; + out.type = YAP_STRING_ATOM; + out.max = -1; + if (!Yap_CVT_Text(&inp, &out PASS_REGS)) + return 0L; + return out.val.a; +} static inline Term Yap_WCharsToListOfCodes(const wchar_t *s USES_REGS) { seq_tv_t inp, out; diff --git a/library/dialect/swi/fli/swi.c b/library/dialect/swi/fli/swi.c index 07dfcacb1..ea3318708 100755 --- a/library/dialect/swi/fli/swi.c +++ b/library/dialect/swi/fli/swi.c @@ -203,6 +203,7 @@ X_API int PL_get_nchars(term_t l, size_t *lengthp, char **s, unsigned flags) { CACHE_REGS seq_tv_t inp, out; + int lvl = push_text_stack(); inp.val.t = Yap_GetFromSlot(l); inp.type = cvtFlags(flags); 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.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; + } + 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; return true; }