Skip to content

Commit

Permalink
ctable: fix power-of-2 issue
Browse files Browse the repository at this point in the history
LuaJIT dislikes pointer subtractions when the size of the pointed-to
objects is not a power of two, which leads to trace aborts. ctable
suffers from this problem. The patch pads the cdata type used as entry
for the table to the next power of two.

We also disable tail-call optimization for functions that end with a
call to a built-in, which also causes trace aborts in certain cases.
  • Loading branch information
alexandergall committed Mar 15, 2018
1 parent d15cbff commit 54780c2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/lib/ctable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,28 @@ local function make_entry_type(key_type, value_type)
else
entry_types[key_type] = {}
end
local raw_size = ffi.sizeof(key_type) + ffi.sizeof(value_type) + 4
local padding = 2^ceil(math.log(raw_size)/math.log(2)) - raw_size
local ret = ffi.typeof([[struct {
uint32_t hash;
$ key;
$ value;
uint8_t padding[$];
} __attribute__((packed))]],
key_type,
value_type)
value_type,
padding)
entry_types[key_type][value_type] = ret
return ret
end

local function make_entries_type(entry_type)
return ffi.typeof('$[?]', entry_type)
return (ffi.typeof('$[?]', entry_type))
end

-- hash := [0,HASH_MAX); scale := size/HASH_MAX
local function hash_to_index(hash, scale)
return floor(hash*scale)
return (floor(hash*scale))
end

local function make_equal_fn(key_type)
Expand Down

0 comments on commit 54780c2

Please sign in to comment.