yyjson_read_max_memory_usage

Function yyjson_read_max_memory_usage 

Source
pub unsafe extern "C" fn yyjson_read_max_memory_usage(
    len: usize,
    flg: yyjson_read_flag,
) -> usize
Expand description

Returns the size of maximum memory usage to read a JSON data.

You may use this value to avoid malloc() or calloc() call inside the reader to get better performance, or read multiple JSON with one piece of memory.

@param len The length of JSON data in bytes. @param flg The JSON read options. @return The maximum memory size to read this JSON, or 0 if overflow.

@par Example @code // read multiple JSON with same pre-allocated memory

char *dat1, *dat2, *dat3; // JSON data size_t len1, len2, len3; // JSON length size_t max_len = MAX(len1, MAX(len2, len3)); yyjson_doc *doc;

// use one allocator for multiple JSON size_t size = yyjson_read_max_memory_usage(max_len, 0); void *buf = malloc(size); yyjson_alc alc; yyjson_alc_pool_init(&alc, buf, size);

// no more alloc() or realloc() call during reading doc = yyjson_read_opts(dat1, len1, 0, &alc, NULL); yyjson_doc_free(doc); doc = yyjson_read_opts(dat2, len2, 0, &alc, NULL); yyjson_doc_free(doc); doc = yyjson_read_opts(dat3, len3, 0, &alc, NULL); yyjson_doc_free(doc);

free(buf); @endcode @see yyjson_alc_pool_init()