try to avoid unneeded malloc
This commit is contained in:
parent
6e2d2628c6
commit
d81e077cbe
27
C/text.c
27
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) {
|
||||
|
55
H/YapText.h
55
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<y ? x : y)
|
||||
#define min(x, y) (x < y ? x : y)
|
||||
#endif
|
||||
|
||||
#define MBYTE (1024*1024)
|
||||
#define MBYTE (1024 * 1024)
|
||||
|
||||
/* Character types for tokenizer and write.c */
|
||||
|
||||
@ -156,24 +158,26 @@ INLINE_ONLY EXTERN inline char_kind_t chtype(Int ch) {
|
||||
#define __android_log_print(...)
|
||||
#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);
|
||||
|
||||
INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr, size_t n,
|
||||
utf8proc_int32_t *valp) {
|
||||
INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr,
|
||||
size_t n,
|
||||
utf8proc_int32_t *valp) {
|
||||
return utf8proc_iterate(ptr, n, valp);
|
||||
}
|
||||
|
||||
INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
|
||||
utf8proc_int32_t val);
|
||||
INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
|
||||
utf8proc_int32_t val);
|
||||
|
||||
INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
|
||||
utf8proc_int32_t val) {
|
||||
INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
|
||||
utf8proc_int32_t val) {
|
||||
return utf8proc_encode_char(val, ptr);
|
||||
}
|
||||
|
||||
inline static const utf8proc_uint8_t *skip_utf8(const utf8proc_uint8_t *pt,
|
||||
utf8proc_ssize_t n) {
|
||||
utf8proc_ssize_t n) {
|
||||
utf8proc_ssize_t i;
|
||||
utf8proc_int32_t b;
|
||||
for (i = 0; i < n; i++) {
|
||||
@ -198,7 +202,7 @@ inline static utf8proc_ssize_t strlen_utf8(const utf8proc_uint8_t *pt) {
|
||||
return rc;
|
||||
else if (b > 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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user