Function yyjson_alc_pool_init

Source
pub unsafe extern "C" fn yyjson_alc_pool_init(
    alc: *mut yyjson_alc,
    buf: *mut c_void,
    size: usize,
) -> bool
Expand description

A pool allocator uses fixed length pre-allocated memory.

This allocator may be used to avoid malloc/realloc calls. The pre-allocated memory should be held by the caller. The maximum amount of memory required to read a JSON can be calculated using the yyjson_read_max_memory_usage() function, but the amount of memory required to write a JSON cannot be directly calculated.

This is not a general-purpose allocator. It is designed to handle a single JSON data at a time. If it is used for overly complex memory tasks, such as parsing multiple JSON documents using the same allocator but releasing only a few of them, it may cause memory fragmentation, resulting in performance degradation and memory waste.

@param alc The allocator to be initialized. If this parameter is NULL, the function will fail and return false. If buf or size is invalid, this will be set to an empty allocator. @param buf The buffer memory for this allocator. If this parameter is NULL, the function will fail and return false. @param size The size of buf, in bytes. If this parameter is less than 8 words (32/64 bytes on 32/64-bit OS), the function will fail and return false. @return true if the alc has been successfully initialized.

@b Example @code // parse JSON with stack memory char buf[1024]; yyjson_alc alc; yyjson_alc_pool_init(&alc, buf, 1024);

const char *json = “{"name":"Helvetica","size":16}” yyjson_doc *doc = yyjson_read_opts(json, strlen(json), 0, &alc, NULL); // the memory of doc is on the stack @endcode

@warning This Allocator is not thread-safe.