rspamd_mempoolRspamd memory pool is used to allocate memory attached to specific objects, namely it was initially used for memory allocation for rspamd_task.
All memory allocated by the pool is destroyed when the associated object is destroyed. This allows a sort of controlled garbage collection for memory allocated from the pool. Memory pools are extensively used by rspamd internal components and provide some powerful features, such as destructors or persistent variables.
local mempool = require "rspamd_mempool"
local pool = mempool.create()
pool:set_variable('a', 'bcd', 1, 1.01, false)
local v1, v2, v3, v4 = pool:get_variable('a', 'string,double,double,bool')
pool:destroy()Functions:
Methods:
The module rspamd_mempool defines the following functions.
mempool.create([size])Creates a memory pool of a specified size or platform dependent optimal size (normally, a page size)
Parameters:
size {number}: size of a page inside poolReturns:
{rspamd_mempool}: new pool object (that should be removed by explicit call to pool:destroy())Back to module description.
The module rspamd_mempool defines the following methods.
mempool:add_destructor(func)Adds new destructor function to the pool
Parameters:
func {function}: function to be called when the pool is destroyedReturns:
No return
Back to module description.
mempool:destroy()Destroys memory pool cleaning all variables and calling all destructors registered (both C and Lua ones)
Parameters:
No parameters
Returns:
No return
Back to module description.
mempool:set_variable(name, [value1[, value2 ...]])Sets a variable that’s valid during memory pool lifetime. This function allows to pack multiple values inside a single variable. Currently supported types are:
string: packed as null terminated C string (so no \0 are allowed)number: packed as C doubleboolean: packed as boolParameters:
name {string}: variable’s name to setReturns:
No return
Back to module description.
mempool:get_variable(name[, type])Unpacks mempool variable to lua If type is not specified, then a variable is
assumed to be zero-terminated C string. Otherwise, type is a comma separated (spaces are ignored)
list of types that should be unpacked from a variable’s content. The following types
are supported:
string: null terminated C string (so no \0 are allowed)double: returned as lua numberint: unpack a single integerint64: unpack 64-bits integerboolean: unpack booleanParameters:
name {string}: variable’s name to gettype {string}: list of types to be extractedReturns:
{variable list}: list of variables extracted (but not a table)Back to module description.
mempool:has_variable(name)Checks if the specified variable name exists in the memory pool
Parameters:
name {string}: variable’s name to getReturns:
{boolean}: true if variable exists and false otherwiseBack to module description.
mempool:delete_variable(name)Removes the specified variable name from the memory pool
Parameters:
name {string}: variable’s name to removeReturns:
{boolean}: true if variable exists and has been removedBack to module description.
Back to top.