Use Lua auto buffers when possible

This commit is contained in:
Justine Tunney 2022-05-29 14:47:14 -07:00
parent 13ee75150c
commit da6d610056
8 changed files with 204 additions and 101 deletions

View file

@ -7,14 +7,11 @@ function main()
else
cmd = 'ls'
end
syscall = 'commandv'
ls = assert(unix.commandv(cmd))
syscall = 'pipe'
reader, writer = assert(unix.pipe(unix.O_CLOEXEC))
oldint = assert(unix.sigaction(unix.SIGINT, unix.SIG_IGN))
oldquit = assert(unix.sigaction(unix.SIGQUIT, unix.SIG_IGN))
oldmask = assert(unix.sigprocmask(unix.SIG_BLOCK, unix.Sigset(unix.SIGCHLD)))
syscall = 'fork'
child = assert(unix.fork())
if child == 0 then
unix.close(0)

View file

@ -153,16 +153,17 @@ int LuaRdseed(lua_State *L) {
}
int LuaDecimate(lua_State *L) {
char *p;
size_t n, m;
const char *s;
unsigned char *p;
luaL_Buffer buf;
s = luaL_checklstring(L, 1, &n);
m = ROUNDUP(n, 16);
p = LuaAllocOrDie(L, m);
p = luaL_buffinitsize(L, &buf, m);
bzero(p + n, m - n);
cDecimate2xUint8x8(m, p, (signed char[8]){-1, -3, 3, 17, 17, 3, -3, -1});
lua_pushlstring(L, (char *)p, (n + 1) >> 1);
free(p);
cDecimate2xUint8x8(m, (unsigned char *)p,
(signed char[8]){-1, -3, 3, 17, 17, 3, -3, -1});
luaL_pushresultsize(&buf, (n + 1) >> 1);
return 1;
}
@ -259,11 +260,15 @@ int LuaParseParams(lua_State *L) {
struct UrlParams h;
data = luaL_checklstring(L, 1, &size);
bzero(&h, sizeof(h));
m = ParseParams(data, size, &h);
LuaPushUrlParams(L, &h);
free(h.p);
free(m);
return 1;
if ((m = ParseParams(data, size, &h))) {
LuaPushUrlParams(L, &h);
free(h.p);
free(m);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
}
int LuaParseHost(lua_State *L) {
@ -273,12 +278,16 @@ int LuaParseHost(lua_State *L) {
const char *p;
bzero(&h, sizeof(h));
p = luaL_checklstring(L, 1, &n);
m = ParseHost(p, n, &h);
lua_newtable(L);
LuaPushUrlView(L, &h.host);
LuaPushUrlView(L, &h.port);
free(m);
return 1;
if ((m = ParseHost(p, n, &h))) {
lua_newtable(L);
LuaPushUrlView(L, &h.host);
LuaPushUrlView(L, &h.port);
free(m);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
}
int LuaPopcnt(lua_State *L) {
@ -395,23 +404,26 @@ int LuaEncodeLatin1(lua_State *L) {
size_t n;
p = luaL_checklstring(L, 1, &n);
f = LuaCheckControlFlags(L, 2);
p = EncodeLatin1(p, n, &n, f);
lua_pushlstring(L, p, n);
free(p);
return 1;
if ((p = EncodeLatin1(p, n, &n, f))) {
lua_pushlstring(L, p, n);
free(p);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
}
int LuaGetRandomBytes(lua_State *L) {
char *p;
size_t n = luaL_optinteger(L, 1, 16);
size_t n;
luaL_Buffer buf;
n = luaL_optinteger(L, 1, 16);
if (!(n > 0 && n <= 256)) {
luaL_argerror(L, 1, "not in range 1..256");
unreachable;
}
p = LuaAllocOrDie(L, n);
CHECK_EQ(n, getrandom(p, n, 0));
lua_pushlstring(L, p, n);
free(p);
CHECK_EQ(n, getrandom(luaL_buffinitsize(L, &buf, n), n, 0));
luaL_pushresult(&buf);
return 1;
}
@ -701,32 +713,30 @@ static void LuaCompress2(lua_State *L, void *dest, size_t *destLen,
}
int LuaCompress(lua_State *L) {
int rc;
bool raw;
size_t n, m;
char *q, *e;
uint32_t crc;
const char *p;
luaL_Buffer buf;
int level, hdrlen;
p = luaL_checklstring(L, 1, &n);
level = luaL_optinteger(L, 2, Z_DEFAULT_COMPRESSION);
m = compressBound(n);
if (lua_toboolean(L, 3)) {
// raw mode
q = LuaAllocOrDie(L, m);
q = luaL_buffinitsize(L, &buf, m);
LuaCompress2(L, q, &m, p, n, level);
lua_pushlstring(L, q, m);
} else {
// easy mode
q = LuaAllocOrDie(L, 10 + 4 + m);
q = luaL_buffinitsize(L, &buf, 10 + 4 + m);
crc = crc32_z(0, p, n);
e = uleb64(q, n);
e = WRITE32LE(e, crc);
hdrlen = e - q;
LuaCompress2(L, q + hdrlen, &m, p, n, level);
lua_pushlstring(L, q, hdrlen + m);
m += hdrlen;
}
free(q);
luaL_pushresultsize(&buf, m);
return 1;
}
@ -735,6 +745,7 @@ int LuaUncompress(lua_State *L) {
uint32_t crc;
int rc, level;
const char *p;
luaL_Buffer buf;
size_t n, m, len;
p = luaL_checklstring(L, 1, &n);
if (lua_isnoneornil(L, 2)) {
@ -744,23 +755,20 @@ int LuaUncompress(lua_State *L) {
}
len = m;
crc = READ32LE(p + rc);
q = LuaAllocOrDie(L, m);
q = luaL_buffinitsize(L, &buf, m);
if (uncompress((void *)q, &m, (unsigned char *)p + rc + 4, n) != Z_OK ||
m != len || crc32_z(0, q, m) != crc) {
free(q);
luaL_error(L, "compressed value is corrupted");
unreachable;
}
} else {
len = m = luaL_checkinteger(L, 2);
q = LuaAllocOrDie(L, m);
q = luaL_buffinitsize(L, &buf, m);
if (uncompress((void *)q, &m, (void *)p, n) != Z_OK || m != len) {
free(q);
luaL_error(L, "compressed value is corrupted");
unreachable;
}
}
lua_pushlstring(L, q, m);
free(q);
luaL_pushresultsize(&buf, m);
return 1;
}

View file

@ -11,10 +11,6 @@ int LuaUnix(lua_State *);
int luaopen_argon2(lua_State *);
int luaopen_lsqlite3(lua_State *);
void *LuaRealloc(lua_State *, void *, size_t);
void *LuaAlloc(lua_State *, size_t);
void *LuaAllocOrDie(lua_State *, size_t);
int LuaBenchmark(lua_State *);
int LuaBin(lua_State *);
int LuaBsf(lua_State *);

View file

@ -103,7 +103,7 @@ struct UnixErrno {
static lua_State *GL;
void *LuaRealloc(lua_State *L, void *p, size_t n) {
static void *LuaRealloc(lua_State *L, void *p, size_t n) {
void *p2;
if ((p2 = realloc(p, n))) {
return p2;
@ -116,11 +116,11 @@ void *LuaRealloc(lua_State *L, void *p, size_t n) {
return p2;
}
void *LuaAlloc(lua_State *L, size_t n) {
static void *LuaAlloc(lua_State *L, size_t n) {
return LuaRealloc(L, 0, n);
}
void *LuaAllocOrDie(lua_State *L, size_t n) {
static void *LuaAllocOrDie(lua_State *L, size_t n) {
void *p;
if ((p = LuaAlloc(L, n))) {
return p;