From 7b6ee588f1f1e4d3fde835f200065c6323db17d1 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Mon, 17 Oct 2022 13:36:27 +0200 Subject: [PATCH] core.packet: configurable freelist size --- src/core/packet.lua | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/core/packet.lua b/src/core/packet.lua index d2f88efde4..d11a304864 100644 --- a/src/core/packet.lua +++ b/src/core/packet.lua @@ -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) @@ -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. @@ -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 @@ -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))