do not reallocate memory so often
This commit is contained in:
parent
198d91b498
commit
8d220356f2
|
|
@ -465,20 +465,28 @@ void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
|
||||||
struct abuf {
|
struct abuf {
|
||||||
char *b;
|
char *b;
|
||||||
int len;
|
int len;
|
||||||
|
int alloc_len;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void abInit(struct abuf *ab) {
|
static void abInit(struct abuf *ab) {
|
||||||
ab->b = NULL;
|
ab->b = NULL;
|
||||||
ab->len = 0;
|
ab->len = 0;
|
||||||
|
ab->alloc_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void abAppend(struct abuf *ab, const char *s, int len) {
|
static void abAppend(struct abuf *ab, const char *s, int len) {
|
||||||
char *new = realloc(ab->b,ab->len+len);
|
if ( ab->alloc_len > ab->len+len) {
|
||||||
|
memcpy(ab->b+ab->len,s,len);
|
||||||
|
ab->len += len;
|
||||||
|
} else {
|
||||||
|
char *new = realloc(ab->b,ab->len+len+32);
|
||||||
|
|
||||||
if (new == NULL) return;
|
if (new == NULL) return;
|
||||||
memcpy(new+ab->len,s,len);
|
memcpy(new+ab->len,s,len);
|
||||||
ab->b = new;
|
ab->b = new;
|
||||||
ab->len += len;
|
ab->len += len;
|
||||||
|
ab->alloc_len = ab->len+32;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void abFree(struct abuf *ab) {
|
static void abFree(struct abuf *ab) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue