Skip to content

Commit

Permalink
Merge PR snabbco#1018 (Max's synergized assert) into kbara-next
Browse files Browse the repository at this point in the history
  • Loading branch information
Katerina Barone-Adesi committed Oct 4, 2016
2 parents 2674ef0 + 4411ad3 commit 82292cd
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 35 deletions.
7 changes: 0 additions & 7 deletions src/apps/socket/raw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ local ethernet = require("lib.protocol.ethernet")
local ffi = require("ffi")
local C = ffi.C

--ljsyscall returns error as a a cdata instead of a string,
--and the standard assert() doesn't use tostring() on it.
local function assert(v, ...)
if not v then error(tostring(... or 'assertion failed'), 2) end
return v, ...
end

local c, t = S.c, S.types.t

RawSocket = {}
Expand Down
7 changes: 0 additions & 7 deletions src/apps/socket/unix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ local link = require("core.link")
local packet = require("core.packet")
local S = require("syscall")

--ljsyscall returns error as a a cdata instead of a string,
--and the standard assert() doesn't use tostring() on it.
local function assert(v, ...)
if not v then error(tostring(... or 'assertion failed'), 2) end
return v, ...
end

UnixSocket = {}
UnixSocket.__index = UnixSocket

Expand Down
17 changes: 11 additions & 6 deletions src/core/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ local S = require("syscall")
require("lib.lua.strict")
require("lib.lua.class")

-- ljsyscall returns error as a cdata instead of a string, and the standard
-- assert doesn't use tostring on it.
_G.assert = function (v, ...)
if v then return v, ... end
error(tostring(... or "assertion failed!"))
end

-- Reserve names that we want to use for global module.
-- (This way we avoid errors from the 'strict' module.)
_G.config, _G.engine, _G.memory, _G.link, _G.packet, _G.timer,
Expand Down Expand Up @@ -164,7 +171,7 @@ function selftest ()
end

-- Fork into worker process and supervisor
local worker_pid = S.fork()
local worker_pid = assert(S.fork())
if worker_pid == 0 then
-- Worker: Use prctl to ensure we are killed (SIGHUP) when our parent quits
-- and run main.
Expand All @@ -179,16 +186,14 @@ else
while true do
-- Read signals from signalfd. Only process the first signal because any
-- signal causes shutdown.
local signals, err = S.util.signalfd_read(signalfd)
assert(signals, tostring(err))
local signals = assert(S.util.signalfd_read(signalfd))
for i = 1, #signals do
local exit_status
if signals[i].chld then
-- SIGCHILD means worker state changed: retrieve its status using
-- waitpid and set exit status accordingly.
local status, err, worker =
S.waitpid(worker_pid, "stopped,continued")
assert(status, tostring(err))
local status, _, worker =
assert(S.waitpid(worker_pid, "stopped,continued"))
if worker.WIFEXITED then exit_status = worker.EXITSTATUS
elseif worker.WIFSIGNALED then exit_status = 128 + worker.WTERMSIG
-- WIFSTOPPED and WIFCONTINUED are ignored.
Expand Down
3 changes: 1 addition & 2 deletions src/core/memory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ end
--- ### HugeTLB: Allocate contiguous memory in bulk from Linux

function allocate_hugetlb_chunk ()
local fd, err = syscall.open("/proc/sys/vm/nr_hugepages","rdonly")
assert(fd, tostring(err))
local fd = assert(syscall.open("/proc/sys/vm/nr_hugepages","rdonly"))
fd:flock("ex")
for i =1, 3 do
local page = C.allocate_huge_page(huge_page_size)
Expand Down
12 changes: 4 additions & 8 deletions src/lib/hardware/pci.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,12 @@ function map_pci_memory (device, n, lock)
if lock then
assert(f:flock("ex, nb"), "failed to lock " .. filepath)
end
local st, err = f:stat()
assert(st, tostring(err))
local mem, err = f:mmap(nil, st.size, "read, write", "shared", 0)
assert(mem, tostring(err))
local st = assert(f:stat())
local mem = assert(f:mmap(nil, st.size, "read, write", "shared", 0))
return ffi.cast("uint32_t *", mem), f
end
function close_pci_resource (fd, base)
local st, err = fd:stat()
assert(st, tostring(err))
local st = assert(fd:stat())
S.munmap(base, st.size)
fd:close()
end
Expand All @@ -159,8 +156,7 @@ end
--- mastering is enabled.
function set_bus_master (device, enable)
root_check()
local f,err = S.open(path(device).."/config", "rdwr")
assert(f, tostring(err))
local f = assert(S.open(path(device).."/config", "rdwr"))
local fd = f:getfd()

local value = ffi.new("uint16_t[1]")
Expand Down
5 changes: 0 additions & 5 deletions src/program/lisper/lisper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ local ntohl = lib.ntohl
local getenv = lib.getenv
local hexdump = lib.hexdump

local function assert(v, ...) --assert overload because
if v then return v, ... end
error(tostring((...) or "Assertion failed"), 2)
end

local function parsehex(s)
return (s:gsub("[0-9a-fA-F][0-9a-fA-F]", function(cc)
return string.char(tonumber(cc, 16))
Expand Down

0 comments on commit 82292cd

Please sign in to comment.