From 9a70abef2428d1b2e35cfbd09522ec7bb78d0c44 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Wed, 5 Dec 2018 20:46:00 +0000 Subject: [PATCH] Add mbuf_append_and_free() and mbuf_move() CL: Add mbuf_append_and_free() and mbuf_move() PUBLISHED_FROM=bdf73906dd93b2ebeace160596508e15480b54b4 --- docs/c-api/mbuf.h/intro.md | 2 ++ docs/c-api/mbuf.h/mbuf_append_and_free.md | 13 +++++++++++++ docs/c-api/mbuf.h/mbuf_move.md | 10 ++++++++++ mongoose.c | 22 ++++++++++++++++++++++ mongoose.h | 11 +++++++++++ 5 files changed, 58 insertions(+) create mode 100644 docs/c-api/mbuf.h/mbuf_append_and_free.md create mode 100644 docs/c-api/mbuf.h/mbuf_move.md diff --git a/docs/c-api/mbuf.h/intro.md b/docs/c-api/mbuf.h/intro.md index 5779ab9c..600a1e09 100644 --- a/docs/c-api/mbuf.h/intro.md +++ b/docs/c-api/mbuf.h/intro.md @@ -4,10 +4,12 @@ symbol_kind: "intro" decl_name: "mbuf.h" items: - { name: mbuf_append.md } + - { name: mbuf_append_and_free.md } - { name: mbuf_clear.md } - { name: mbuf_free.md } - { name: mbuf_init.md } - { name: mbuf_insert.md } + - { name: mbuf_move.md } - { name: mbuf_remove.md } - { name: mbuf_resize.md } - { name: mbuf_trim.md } diff --git a/docs/c-api/mbuf.h/mbuf_append_and_free.md b/docs/c-api/mbuf.h/mbuf_append_and_free.md new file mode 100644 index 00000000..e7be33ac --- /dev/null +++ b/docs/c-api/mbuf.h/mbuf_append_and_free.md @@ -0,0 +1,13 @@ +--- +title: "mbuf_append_and_free()" +decl_name: "mbuf_append_and_free" +symbol_kind: "func" +signature: | + size_t mbuf_append_and_free(struct mbuf *, void *data, size_t data_size); +--- + +Appends data to the Mbuf and frees it (data must be heap-allocated). + +Returns the number of bytes appended or 0 if out of memory. +data is freed irrespective of return value. + diff --git a/docs/c-api/mbuf.h/mbuf_move.md b/docs/c-api/mbuf.h/mbuf_move.md new file mode 100644 index 00000000..61e20853 --- /dev/null +++ b/docs/c-api/mbuf.h/mbuf_move.md @@ -0,0 +1,10 @@ +--- +title: "mbuf_move()" +decl_name: "mbuf_move" +symbol_kind: "func" +signature: | + void mbuf_move(struct mbuf *from, struct mbuf *to); +--- + +Moves the state from one mbuf to the other. + diff --git a/mongoose.c b/mongoose.c index 3b0faf95..ffa97c9b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1612,6 +1612,22 @@ size_t mbuf_append(struct mbuf *a, const void *buf, size_t len) { return mbuf_insert(a, a->len, buf, len); } +size_t mbuf_append_and_free(struct mbuf *a, void *buf, size_t len) WEAK; +size_t mbuf_append_and_free(struct mbuf *a, void *data, size_t len) { + size_t ret; + /* Optimization: if the buffer is currently empty, + * take over the user-provided buffer. */ + if (a->len == 0) { + if (a->buf != NULL) free(a->buf); + a->buf = (char *) data; + a->len = a->size = len; + return len; + } + ret = mbuf_insert(a, a->len, data, len); + free(data); + return ret; +} + void mbuf_remove(struct mbuf *mb, size_t n) WEAK; void mbuf_remove(struct mbuf *mb, size_t n) { if (n > 0 && n <= mb->len) { @@ -1625,6 +1641,12 @@ void mbuf_clear(struct mbuf *mb) { mb->len = 0; } +void mbuf_move(struct mbuf *from, struct mbuf *to) WEAK; +void mbuf_move(struct mbuf *from, struct mbuf *to) { + memcpy(to, from, sizeof(*to)); + memset(from, 0, sizeof(*from)); +} + #endif /* EXCLUDE_COMMON */ #ifdef MG_MODULE_LINES #line 1 "common/mg_str.c" diff --git a/mongoose.h b/mongoose.h index 85d62e3d..8d9e8f29 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2368,6 +2368,14 @@ void mbuf_free(struct mbuf *); */ size_t mbuf_append(struct mbuf *, const void *data, size_t data_size); +/* + * Appends data to the Mbuf and frees it (data must be heap-allocated). + * + * Returns the number of bytes appended or 0 if out of memory. + * data is freed irrespective of return value. + */ +size_t mbuf_append_and_free(struct mbuf *, void *data, size_t data_size); + /* * Inserts data at a specified offset in the Mbuf. * @@ -2388,6 +2396,9 @@ void mbuf_remove(struct mbuf *, size_t data_size); */ void mbuf_resize(struct mbuf *, size_t new_size); +/* Moves the state from one mbuf to the other. */ +void mbuf_move(struct mbuf *from, struct mbuf *to); + /* Removes all the data from mbuf (if any). */ void mbuf_clear(struct mbuf *);