kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 2cf83aea3c3224a1e2ec6008812a9609d9399715
parent 33c0b3c7535efac6ab42fcfcb6530ed05e65a0b4
Author: Joris Vink <joris@coders.se>
Date:   Thu, 14 Jul 2016 12:42:40 +0200

Merge branch 'raphaelmonrouzeau-kore_buf_noalloc'

Diffstat:
.gitignore | 2++
examples/generic/src/example.c | 6+++---
examples/integers/src/check_integers.c | 2+-
examples/json_yajl/src/json_yajl.c | 2+-
examples/parameters/src/parameters.c | 2+-
examples/tasks/src/tasks.c | 2+-
includes/kore.h | 8+++++---
src/buf.c | 59++++++++++++++++++++++++++++++++++++-----------------------
src/cli.c | 12++++++------
src/http.c | 12++++++------
src/utils.c | 4++--
src/websocket.c | 6+++---
12 files changed, 67 insertions(+), 50 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,7 +1,9 @@ kore *.o *.swp +*.swo *.module *.DSYM cert obj +.lvimrc diff --git a/examples/generic/src/example.c b/examples/generic/src/example.c @@ -122,7 +122,7 @@ serve_b64test(struct http_request *req) struct kore_buf *res; u_int8_t *data; - res = kore_buf_create(1024); + res = kore_buf_alloc(1024); for (i = 0; b64tests[i] != NULL; i++) test_base64((u_int8_t *)b64tests[i], strlen(b64tests[i]), res); @@ -144,7 +144,7 @@ serve_file_upload(struct http_request *req) size_t len; char *name, buf[BUFSIZ]; - b = kore_buf_create(asset_len_upload_html); + b = kore_buf_alloc(asset_len_upload_html); kore_buf_append(b, asset_upload_html, asset_len_upload_html); if (req->method == HTTP_METHOD_POST) { @@ -247,7 +247,7 @@ serve_params_test(struct http_request *req) else if (req->method == HTTP_METHOD_POST) http_populate_post(req); - b = kore_buf_create(asset_len_params_html); + b = kore_buf_alloc(asset_len_params_html); kore_buf_append(b, asset_params_html, asset_len_params_html); /* diff --git a/examples/integers/src/check_integers.c b/examples/integers/src/check_integers.c @@ -17,7 +17,7 @@ page(struct http_request *req) u_int8_t c, *data; http_populate_get(req); - buf = kore_buf_create(128); + buf = kore_buf_alloc(128); if (http_argument_get_byte(req, "id", &c)) kore_buf_appendf(buf, "byte\t%c\n", c); diff --git a/examples/json_yajl/src/json_yajl.c b/examples/json_yajl/src/json_yajl.c @@ -43,7 +43,7 @@ page(struct http_request *req) /* * Read the entire received body into a memory buffer. */ - buf = kore_buf_create(128); + buf = kore_buf_alloc(128); for (;;) { ret = http_body_read(req, data, sizeof(data)); if (ret == -1) { diff --git a/examples/parameters/src/parameters.c b/examples/parameters/src/parameters.c @@ -56,7 +56,7 @@ page(struct http_request *req) * and Kore will return an error when trying to read it as such. */ - buf = kore_buf_create(128); + buf = kore_buf_alloc(128); /* Grab it as a string, we shouldn't free the result in sid. */ if (http_argument_get_string(req, "id", &sid)) diff --git a/examples/tasks/src/tasks.c b/examples/tasks/src/tasks.c @@ -193,7 +193,7 @@ run_curl(struct kore_task *t) if ((curl = curl_easy_init()) == NULL) return (KORE_RESULT_ERROR); - b = kore_buf_create(128); + b = kore_buf_alloc(128); /* Do CURL magic. */ curl_easy_setopt(curl, CURLOPT_POST, 1); diff --git a/includes/kore.h b/includes/kore.h @@ -323,10 +323,11 @@ struct kore_validator { }; #endif -#define KORE_BUF_INCREMENT 4096 +#define KORE_BUF_OWNER_API 0x0001 struct kore_buf { u_int8_t *data; + int flags; size_t length; size_t offset; }; @@ -622,15 +623,16 @@ void net_send_stream(struct connection *, void *, size_t, int (*cb)(struct netbuf *), struct netbuf **); void kore_buf_free(struct kore_buf *); -struct kore_buf *kore_buf_create(size_t); +struct kore_buf *kore_buf_alloc(size_t); +void kore_buf_init(struct kore_buf *, size_t); void kore_buf_append(struct kore_buf *, const void *, size_t); u_int8_t *kore_buf_release(struct kore_buf *, size_t *); void kore_buf_reset(struct kore_buf *); +void kore_buf_cleanup(struct kore_buf *); char *kore_buf_stringify(struct kore_buf *, size_t *); void kore_buf_appendf(struct kore_buf *, const char *, ...); void kore_buf_appendv(struct kore_buf *, const char *, va_list); -void kore_buf_appendb(struct kore_buf *, struct kore_buf *); void kore_buf_replace_string(struct kore_buf *, char *, void *, size_t); void kore_keymgr_run(void); diff --git a/src/buf.c b/src/buf.c @@ -22,16 +22,45 @@ #include "kore.h" struct kore_buf * -kore_buf_create(size_t initial) +kore_buf_alloc(size_t initial) { struct kore_buf *buf; buf = kore_malloc(sizeof(*buf)); - buf->data = kore_malloc(initial); + kore_buf_init(buf, initial); + buf->flags = KORE_BUF_OWNER_API; + + return (buf); +} + +void +kore_buf_init(struct kore_buf *buf, size_t initial) +{ + if (initial > 0) + buf->data = kore_malloc(initial); + else + buf->data = NULL; + buf->length = initial; buf->offset = 0; + buf->flags = 0; +} - return (buf); +void +kore_buf_cleanup(struct kore_buf *buf) +{ + kore_free(buf->data); + buf->data = NULL; + buf->offset = 0; + buf->length = 0; +} + +void +kore_buf_free(struct kore_buf *buf) +{ + kore_buf_cleanup(buf); + if (buf->flags & KORE_BUF_OWNER_API) + kore_free(buf); } void @@ -41,7 +70,7 @@ kore_buf_append(struct kore_buf *buf, const void *d, size_t len) fatal("overflow in kore_buf_append"); if ((buf->offset + len) > buf->length) { - buf->length += len + KORE_BUF_INCREMENT; + buf->length += len; buf->data = kore_realloc(buf->data, buf->length); } @@ -50,17 +79,6 @@ kore_buf_append(struct kore_buf *buf, const void *d, size_t len) } void -kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src) -{ - u_int8_t *d; - size_t len; - - d = kore_buf_release(src, &len); - kore_buf_append(buf, d, len); - kore_free(d); -} - -void kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args) { int l; @@ -114,16 +132,11 @@ kore_buf_release(struct kore_buf *buf, size_t *len) p = buf->data; *len = buf->offset; - kore_free(buf); - return (p); -} + buf->data = NULL; + kore_buf_free(buf); -void -kore_buf_free(struct kore_buf *buf) -{ - kore_free(buf->data); - kore_free(buf); + return (p); } void diff --git a/src/cli.c b/src/cli.c @@ -1381,7 +1381,7 @@ cli_buildopt_cflags(struct buildopt *bopt, const char *string) bopt = cli_buildopt_default(); if (bopt->cflags == NULL) - bopt->cflags = kore_buf_create(128); + bopt->cflags = kore_buf_alloc(128); kore_buf_appendf(bopt->cflags, "%s ", string); } @@ -1393,7 +1393,7 @@ cli_buildopt_cxxflags(struct buildopt *bopt, const char *string) bopt = cli_buildopt_default(); if (bopt->cxxflags == NULL) - bopt->cxxflags = kore_buf_create(128); + bopt->cxxflags = kore_buf_alloc(128); kore_buf_appendf(bopt->cxxflags, "%s ", string); } @@ -1405,7 +1405,7 @@ cli_buildopt_ldflags(struct buildopt *bopt, const char *string) bopt = cli_buildopt_default(); if (bopt->ldflags == NULL) - bopt->ldflags = kore_buf_create(128); + bopt->ldflags = kore_buf_alloc(128); kore_buf_appendf(bopt->ldflags, "%s ", string); } @@ -1488,7 +1488,7 @@ cli_build_cflags(struct buildopt *bopt) cli_fatal("no such build flavor: %s", flavor); if (bopt->cflags == NULL) - bopt->cflags = kore_buf_create(128); + bopt->cflags = kore_buf_alloc(128); cli_build_flags_common(bopt->cflags); @@ -1515,7 +1515,7 @@ cli_build_cxxflags(struct buildopt *bopt) cli_fatal("no such build flavor: %s", flavor); if (bopt->cxxflags == NULL) - bopt->cxxflags = kore_buf_create(128); + bopt->cxxflags = kore_buf_alloc(128); cli_build_flags_common(bopt->cxxflags); @@ -1540,7 +1540,7 @@ cli_build_ldflags(struct buildopt *bopt) cli_fatal("no such build flavor: %s", flavor); if (bopt->ldflags == NULL) - bopt->ldflags = kore_buf_create(128); + bopt->ldflags = kore_buf_alloc(128); if (bopt->single_binary == 0) { #if defined(__MACH__) diff --git a/src/http.c b/src/http.c @@ -79,7 +79,7 @@ http_init(void) TAILQ_INIT(&http_requests); TAILQ_INIT(&http_requests_sleeping); - header_buf = kore_buf_create(1024); + header_buf = kore_buf_alloc(1024); l = snprintf(http_version, sizeof(http_version), "server: kore (%d.%d.%d-%s)\r\n", KORE_VERSION_MAJOR, @@ -702,7 +702,7 @@ http_header_recv(struct netbuf *nb) } } else { req->http_body_fd = -1; - req->http_body = kore_buf_create(req->content_length); + req->http_body = kore_buf_alloc(req->content_length); kore_buf_append(req->http_body, end_headers, (nb->s_off - len)); } @@ -917,7 +917,7 @@ http_populate_post(struct http_request *req) req->http_body->offset = req->content_length; string = kore_buf_stringify(req->http_body, NULL); } else { - body = kore_buf_create(128); + body = kore_buf_alloc(128); for (;;) { ret = http_body_read(req, data, sizeof(data)); if (ret == -1) @@ -990,8 +990,8 @@ http_populate_multipart_form(struct http_request *req) if (blen == -1 || (size_t)blen >= sizeof(boundary)) return; - in = kore_buf_create(128); - out = kore_buf_create(128); + in = kore_buf_alloc(128); + out = kore_buf_alloc(128); if (!multipart_find_data(in, NULL, NULL, req, boundary, blen)) goto cleanup; @@ -1239,7 +1239,7 @@ multipart_add_field(struct http_request *req, struct kore_buf *in, struct kore_buf *data; char *string; - data = kore_buf_create(128); + data = kore_buf_alloc(128); if (!multipart_find_data(in, data, NULL, req, boundary, blen)) { kore_buf_free(data); diff --git a/src/utils.c b/src/utils.c @@ -400,7 +400,7 @@ kore_base64_encode(u_int8_t *data, size_t len, char **out) pdata = data; } - res = kore_buf_create(plen); + res = kore_buf_alloc(plen); i = 2; b = 0; @@ -456,7 +456,7 @@ kore_base64_decode(char *in, u_int8_t **out, size_t *olen) d = 0; c = 0; len = strlen(in); - res = kore_buf_create(len); + res = kore_buf_alloc(len); for (idx = 0; idx < len; idx++) { c = in[idx]; diff --git a/src/websocket.c b/src/websocket.c @@ -73,7 +73,7 @@ kore_websocket_handshake(struct http_request *req, struct kore_wscbs *wscbs) return; } - buf = kore_buf_create(128); + buf = kore_buf_alloc(128); kore_buf_appendf(buf, "%s%s", key, WEBSOCKET_SERVER_RESPONSE); (void)SHA1_Init(&sctx); @@ -116,7 +116,7 @@ kore_websocket_send(struct connection *c, u_int8_t op, const void *data, { struct kore_buf *frame; - frame = kore_buf_create(len); + frame = kore_buf_alloc(len); websocket_frame_build(frame, op, data, len); net_send_queue(c, frame->data, frame->offset); kore_buf_free(frame); @@ -129,7 +129,7 @@ kore_websocket_broadcast(struct connection *src, u_int8_t op, const void *data, struct connection *c; struct kore_buf *frame; - frame = kore_buf_create(len); + frame = kore_buf_alloc(len); websocket_frame_build(frame, op, data, len); TAILQ_FOREACH(c, &connections, list) {