Skip to content

Commit

Permalink
core.packet: configurable freelist size
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneia committed Oct 17, 2022
1 parent c7bb164 commit 7b6ee58
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/core/packet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,28 @@ end

-- Freelist containing empty packets ready for use.

local max_packets = 1e6
local default_max_packets = 1e6

ffi.cdef([[
struct freelist {
int nfree;
int max;
struct packet *list[]]..max_packets..[[];
struct packet *list[?];
};
]])

local function freelist_create(name)
local fl = shm.create(name, "struct freelist")
local function freelist_create(name, max_packets)
max_packets = max_packets or default_max_packets
local fl = shm.create(name, "struct freelist", max_packets)
fl.max = max_packets
return fl
end

local function freelist_open(name, readonly)
return shm.open(name, "struct freelist", readonly)
local fl = shm.open(name, "struct freelist", 'read-only', 1)
local max = fl.max
shm.unmap(fl)
return shm.open(name, "struct freelist", readonly, max)
end

local function freelist_full(freelist)
Expand Down Expand Up @@ -104,8 +108,13 @@ local packets_allocated = 0
local packets_fl, group_fl

-- Call to ensure packet freelist is enabled.
function initialize ()
packets_fl = freelist_create("engine/packets.freelist")
function initialize (max_packets)
if packets_fl then
assert(packets_fl.nfree == 0, "freelist is already in use")
shm.unmap(packets_fl)
shm.unlink("engine/packets.freelist")
end
packets_fl = freelist_create("engine/packets.freelist", max_packets)
end

-- Call to ensure group freelist is enabled.
Expand Down Expand Up @@ -310,7 +319,7 @@ end

function preallocate_step()
assert(packets_allocated + packet_allocation_step
<= max_packets - group_fl_chunksize,
<= packets_fl.max - group_fl_chunksize,
"packet allocation overflow")

for i=1, packet_allocation_step do
Expand All @@ -321,6 +330,12 @@ function preallocate_step()
end

function selftest ()
initialize(10000)
assert(packets_fl.max == 10000)
allocate()
local ok, err = pcall(initialize)
assert(not ok and err:match("freelist is already in use"))

assert(is_aligned(0, 1))
assert(is_aligned(1, 1))
assert(is_aligned(2, 1))
Expand Down

0 comments on commit 7b6ee58

Please sign in to comment.