mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-28 23:49:44 +08:00
Dont use realloc
This commit is contained in:
parent
434378eda5
commit
e8f050c194
@ -8,8 +8,7 @@ CFLAGS ?= -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion \
|
||||
-I. -I$(ROOT) -DMG_ARCH=MG_ARCH_NEWLIB -DMIP_DEBUG=1 \
|
||||
-DMG_ENABLE_CUSTOM_MILLIS=1 -DMG_ENABLE_LINES=1 -DMG_ENABLE_MIP=1 \
|
||||
-mcpu=cortex-m7 -mthumb -mfloat-abi=softfp -mfpu=vfpv4 $(EXTRA)
|
||||
LDFLAGS ?= -Tlink.ld -nostartfiles -nostdlib --specs nano.specs \
|
||||
-lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map
|
||||
LDFLAGS ?= -Tlink.ld -nostartfiles -nostdlib --specs nano.specs -lc -lgcc -Wl,--gc-sections -Wl,-Map=$@.map
|
||||
SOURCES = boot.c main.c syscalls.c $(ROOT)/mongoose.c
|
||||
|
||||
all build example: $(TARGET).bin
|
||||
@ -20,6 +19,12 @@ $(TARGET).bin: $(TARGET).elf
|
||||
$(TARGET).elf: $(SOURCES) mcu.h
|
||||
$(DOCKER) $(CROSS)-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
# Build on Windows. Download Win32 Zip from ARM GCC downloads, unzip to c:\armgcc
|
||||
ARMGCC ?= c:/armgcc
|
||||
windows: $(TARGET).bin
|
||||
windows: CROSS = $(ARMGCC)/bin/arm-none-eabi
|
||||
windows: DOCKER =
|
||||
|
||||
# Build an interactive device dashboard
|
||||
dash: $(TARGET).elf
|
||||
dash: CFLAGS += -DDASH -DMG_ENABLE_PACKED_FS=1
|
||||
|
106
mongoose.c
106
mongoose.c
@ -552,6 +552,53 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
|
||||
|
||||
|
||||
|
||||
size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
|
||||
va_list ap_copy;
|
||||
size_t len;
|
||||
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, size, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
|
||||
if (len >= size) {
|
||||
// Allocate a buffer that is large enough
|
||||
if ((*buf = (char *) calloc(1, len + 1)) == NULL) {
|
||||
len = 0;
|
||||
} else {
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, len + 1, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
|
||||
size_t ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = mg_vasprintf(buf, size, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *mg_vmprintf(const char *fmt, va_list ap) {
|
||||
char *s = NULL;
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *mg_mprintf(const char *fmt, ...) {
|
||||
char *s = NULL;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
|
||||
size_t len = 0;
|
||||
va_list ap;
|
||||
@ -561,26 +608,28 @@ size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
|
||||
return len;
|
||||
}
|
||||
|
||||
static void mg_putchar_iobuf(char ch, void *param) {
|
||||
static void mg_putchar_iobuf_static(char ch, void *param) {
|
||||
struct mg_iobuf *io = (struct mg_iobuf *) param;
|
||||
if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch;
|
||||
}
|
||||
|
||||
// We don't use realloc() in mongoose, so resort to inefficient calloc
|
||||
// Every new character reallocates the whole string
|
||||
void mg_putchar_realloc(char ch, void *param) {
|
||||
char *s, *buf = *(char **) param;
|
||||
size_t len = buf == NULL ? 0 : strlen(buf), chunksize = 256;
|
||||
size_t new_size = len + 1 + 1 + chunksize;
|
||||
new_size -= new_size % chunksize;
|
||||
if ((s = (char *) realloc(buf, new_size)) != NULL) {
|
||||
size_t len = buf == NULL ? 0 : strlen(buf);
|
||||
if ((s = (char *) calloc(1, len + 2)) != NULL) {
|
||||
if (buf != NULL) memcpy(s, buf, len);
|
||||
s[len] = ch;
|
||||
s[len + 1] = '\0';
|
||||
free(buf);
|
||||
*(char **) param = s;
|
||||
}
|
||||
}
|
||||
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
|
||||
struct mg_iobuf io = {(uint8_t *) buf, len, 0};
|
||||
size_t n = mg_vrprintf(mg_putchar_iobuf, &io, fmt, ap);
|
||||
size_t n = mg_vrprintf(mg_putchar_iobuf_static, &io, fmt, ap);
|
||||
if (n < len) buf[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
@ -4717,51 +4766,6 @@ void mg_unhex(const char *buf, size_t len, unsigned char *to) {
|
||||
}
|
||||
}
|
||||
|
||||
size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
|
||||
va_list ap_copy;
|
||||
size_t len;
|
||||
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, size, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
|
||||
if (len >= size) {
|
||||
// Allocate a buffer that is large enough
|
||||
if ((*buf = (char *) calloc(1, len + 1)) == NULL) {
|
||||
len = 0;
|
||||
} else {
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, len + 1, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
|
||||
size_t ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = mg_vasprintf(buf, size, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *mg_vmprintf(const char *fmt, va_list ap) {
|
||||
char *s = NULL;
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *mg_mprintf(const char *fmt, ...) {
|
||||
char *s = NULL;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
uint64_t mg_tou64(struct mg_str str) {
|
||||
uint64_t result = 0;
|
||||
|
61
src/fmt.c
61
src/fmt.c
@ -1,6 +1,53 @@
|
||||
#include "iobuf.h"
|
||||
#include "str.h"
|
||||
|
||||
size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
|
||||
va_list ap_copy;
|
||||
size_t len;
|
||||
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, size, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
|
||||
if (len >= size) {
|
||||
// Allocate a buffer that is large enough
|
||||
if ((*buf = (char *) calloc(1, len + 1)) == NULL) {
|
||||
len = 0;
|
||||
} else {
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, len + 1, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
|
||||
size_t ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = mg_vasprintf(buf, size, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *mg_vmprintf(const char *fmt, va_list ap) {
|
||||
char *s = NULL;
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *mg_mprintf(const char *fmt, ...) {
|
||||
char *s = NULL;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
|
||||
size_t len = 0;
|
||||
va_list ap;
|
||||
@ -10,26 +57,28 @@ size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
|
||||
return len;
|
||||
}
|
||||
|
||||
static void mg_putchar_iobuf(char ch, void *param) {
|
||||
static void mg_putchar_iobuf_static(char ch, void *param) {
|
||||
struct mg_iobuf *io = (struct mg_iobuf *) param;
|
||||
if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch;
|
||||
}
|
||||
|
||||
// We don't use realloc() in mongoose, so resort to inefficient calloc
|
||||
// Every new character reallocates the whole string
|
||||
void mg_putchar_realloc(char ch, void *param) {
|
||||
char *s, *buf = *(char **) param;
|
||||
size_t len = buf == NULL ? 0 : strlen(buf), chunksize = 256;
|
||||
size_t new_size = len + 1 + 1 + chunksize;
|
||||
new_size -= new_size % chunksize;
|
||||
if ((s = (char *) realloc(buf, new_size)) != NULL) {
|
||||
size_t len = buf == NULL ? 0 : strlen(buf);
|
||||
if ((s = (char *) calloc(1, len + 2)) != NULL) {
|
||||
if (buf != NULL) memcpy(s, buf, len);
|
||||
s[len] = ch;
|
||||
s[len + 1] = '\0';
|
||||
free(buf);
|
||||
*(char **) param = s;
|
||||
}
|
||||
}
|
||||
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
|
||||
struct mg_iobuf io = {(uint8_t *) buf, len, 0};
|
||||
size_t n = mg_vrprintf(mg_putchar_iobuf, &io, fmt, ap);
|
||||
size_t n = mg_vrprintf(mg_putchar_iobuf_static, &io, fmt, ap);
|
||||
if (n < len) buf[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
45
src/str.c
45
src/str.c
@ -196,51 +196,6 @@ void mg_unhex(const char *buf, size_t len, unsigned char *to) {
|
||||
}
|
||||
}
|
||||
|
||||
size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
|
||||
va_list ap_copy;
|
||||
size_t len;
|
||||
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, size, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
|
||||
if (len >= size) {
|
||||
// Allocate a buffer that is large enough
|
||||
if ((*buf = (char *) calloc(1, len + 1)) == NULL) {
|
||||
len = 0;
|
||||
} else {
|
||||
va_copy(ap_copy, ap);
|
||||
len = mg_vsnprintf(*buf, len + 1, fmt, &ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
|
||||
size_t ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = mg_vasprintf(buf, size, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *mg_vmprintf(const char *fmt, va_list ap) {
|
||||
char *s = NULL;
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *mg_mprintf(const char *fmt, ...) {
|
||||
char *s = NULL;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
mg_vasprintf(&s, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
uint64_t mg_tou64(struct mg_str str) {
|
||||
uint64_t result = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user