mongoose/src/iobuf.c

67 lines
1.9 KiB
C
Raw Normal View History

2020-12-05 19:26:32 +08:00
#include "iobuf.h"
#include "log.h"
#include <string.h>
2021-04-29 15:49:23 +08:00
// Not using memset for zeroing memory, cause it can be dropped by compiler
// See https://github.com/cesanta/mongoose/pull/1265
static void zeromem(volatile unsigned char *buf, size_t len) {
if (buf != NULL) {
while (len--) *buf++ = 0;
}
}
2021-01-29 20:32:34 +08:00
int mg_iobuf_resize(struct mg_iobuf *io, size_t new_size) {
int ok = 1;
2020-12-05 19:26:32 +08:00
if (new_size == 0) {
2021-04-29 15:49:23 +08:00
zeromem(io->buf, io->size);
2020-12-05 19:26:32 +08:00
free(io->buf);
io->buf = NULL;
io->len = io->size = 0;
} else if (new_size != io->size) {
// NOTE(lsm): do not use realloc here. Use calloc/free only, to ease the
2020-12-05 19:26:32 +08:00
// porting to some obscure platforms like FreeRTOS
void *p = calloc(1, new_size);
2020-12-05 19:26:32 +08:00
if (p != NULL) {
2021-03-22 19:30:29 +08:00
size_t len = new_size < io->len ? new_size : io->len;
if (len > 0) memcpy(p, io->buf, len);
2021-04-29 15:49:23 +08:00
zeromem(io->buf, io->size);
2020-12-05 19:26:32 +08:00
free(io->buf);
io->buf = (unsigned char *) p;
io->size = new_size;
} else {
2021-01-29 20:32:34 +08:00
ok = 0;
2020-12-05 19:26:32 +08:00
LOG(LL_ERROR,
("%lu->%lu", (unsigned long) io->size, (unsigned long) new_size));
}
}
2021-01-29 20:32:34 +08:00
return ok;
2020-12-05 19:26:32 +08:00
}
2021-01-29 20:32:34 +08:00
int mg_iobuf_init(struct mg_iobuf *io, size_t size) {
2021-02-15 22:42:22 +08:00
return mg_iobuf_resize(io, size);
2020-12-05 19:26:32 +08:00
}
size_t mg_iobuf_append(struct mg_iobuf *io, const void *buf, size_t len,
size_t chunk_size) {
size_t new_size = io->len + len + chunk_size;
new_size -= new_size % chunk_size;
if (new_size != io->size) mg_iobuf_resize(io, new_size);
if (new_size != io->size) len = 0; // Realloc failure, append nothing
if (buf != NULL) memmove(io->buf + io->len, buf, len);
io->len += len;
return len;
}
size_t mg_iobuf_delete(struct mg_iobuf *io, size_t len) {
if (len > io->len) len = 0;
memmove(io->buf, io->buf + len, io->len - len);
2021-04-29 15:49:23 +08:00
zeromem(io->buf + io->len - len, len);
2020-12-05 19:26:32 +08:00
io->len -= len;
return len;
}
void mg_iobuf_free(struct mg_iobuf *io) {
2021-02-15 22:42:22 +08:00
mg_iobuf_resize(io, 0);
2020-12-05 19:26:32 +08:00
}