Mercurial > hg > batmud > maputils
changeset 1846:0c5cce86debb
Use a "static" LWS write buffer instead of dynamic allocation on each call.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 01 Nov 2017 02:16:24 +0200 |
parents | d34074ef2ea5 |
children | 91ec11702055 |
files | mapsearch.c |
diffstat | 1 files changed, 19 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/mapsearch.c Tue Oct 31 23:10:46 2017 +0200 +++ b/mapsearch.c Wed Nov 01 02:16:24 2017 +0200 @@ -16,6 +16,10 @@ #define SET_MAX_LISTEN 4 // Maximum number of interfaces to listen #define SET_MAX_MATCHES 64 // Maximum number of match results per query +// Define the static lws_write() buffer size +#define SET_LWS_BUF_SIZE (256 * 1024) // 256kB probably enough for our purposes(tm) +#define SET_LWS_BUF_PAD (((LWS_PRE / 16) + 1) * 16) + // List of default SSL/TLS ciphers to use/allowed #define SET_DEF_CIPHERS \ @@ -83,6 +87,7 @@ struct lws_context *setLWSContext = NULL; int optWorldXC = 0, optWorldYC = 0; char *optTest = NULL; +unsigned char *setLWSBuffer = NULL; /* Arguments @@ -644,21 +649,13 @@ // int mapLWSWrite(struct lws *wsi, const unsigned char *data, const size_t len) { - const size_t pad = ((LWS_PRE / sizeof(void *)) + 1) * sizeof(void); - unsigned char *buf; - int ret; - - if (wsi == NULL) - return 0; - - if ((buf = th_malloc(len + pad)) == NULL) + if (len >= SET_LWS_BUF_SIZE - SET_LWS_BUF_PAD) return -1; - memcpy(buf + pad, data, len); - ret = lws_write(wsi, buf + pad, len, LWS_WRITE_TEXT); + // Costs us an extra copy + memcpy(setLWSBuffer + SET_LWS_BUF_PAD, data, len); - th_free(buf); - return ret; + return lws_write(wsi, setLWSBuffer + SET_LWS_BUF_PAD, len, LWS_WRITE_TEXT); } @@ -1136,6 +1133,14 @@ // Initialize libwebsockets and create context THMSG(1, "Creating libwebsockets context.\n"); + if ((setLWSBuffer = th_malloc(SET_LWS_BUF_SIZE)) == NULL) + { + THERR("Could not allocate %d bytes of memory for LWS buffer.\n", + SET_LWS_BUF_SIZE); + + goto exit; + } + lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE, mapLogStr); MAPListenerCtx *ctx = optListenTo[0]; @@ -1236,5 +1241,7 @@ locFreeMapLocations(&info->loc); } + th_free(setLWSBuffer); + return 0; }