aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/crand.c
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2017-12-03 15:03:25 +0100
committerKim Alvefur <zash@zash.se>2017-12-03 15:03:25 +0100
commita2f2567e8fa4a8c9cdca1f1a909c287b2935f899 (patch)
tree39059da358fe92758d409757eacb62448fd420dc /util-src/crand.c
parentfa07c8f62b0ded8075296f70164cc20926b3538b (diff)
downloadprosody-a2f2567e8fa4a8c9cdca1f1a909c287b2935f899.tar.gz
prosody-a2f2567e8fa4a8c9cdca1f1a909c287b2935f899.zip
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Diffstat (limited to 'util-src/crand.c')
-rw-r--r--util-src/crand.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/util-src/crand.c b/util-src/crand.c
index d15fc090..ab2f4ad1 100644
--- a/util-src/crand.c
+++ b/util-src/crand.c
@@ -58,9 +58,19 @@ int getrandom(void *buf, size_t buflen, unsigned int flags) {
#error util.crand compiled without a random source
#endif
+#ifndef SMALLBUFSIZ
+#define SMALLBUFSIZ 32
+#endif
+
int Lrandom(lua_State *L) {
+ char smallbuf[SMALLBUFSIZ];
+ char *buf = &smallbuf[0];
const size_t len = luaL_checkinteger(L, 1);
- void *buf = lua_newuserdata(L, len);
+
+ if(len > SMALLBUFSIZ) {
+ buf = lua_newuserdata(L, len);
+ }
+
#if defined(WITH_GETRANDOM)
/*