2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +00:00

Add a static pre-allocated buffer to isc_buffer_t

When the buffer is allocated via isc_buffer_allocate() and the size is
smaller or equal ISC_BUFFER_STATIC_SIZE (currently 512 bytes), the
buffer will be allocated as a flexible array member in the buffer
structure itself instead of allocating it on the heap.  This should help
when the buffer is used on the hot-path with small allocations.
This commit is contained in:
Ondřej Surý
2022-12-15 22:51:52 +01:00
parent 6bd2b34180
commit 2ddea1e41c
3 changed files with 69 additions and 61 deletions

View File

@@ -42,48 +42,44 @@ ISC_RUN_TEST_IMPL(isc_buffer_reserve) {
UNUSED(state);
b = NULL;
isc_buffer_allocate(mctx, &b, 1024);
assert_int_equal(b->length, 1024);
isc_buffer_allocate(mctx, &b, ISC_BUFFER_INCR);
assert_int_equal(b->length, ISC_BUFFER_INCR);
/*
* 1024 bytes should already be available, so this call does
* 512 bytes should already be available, so this call does
* nothing.
*/
result = isc_buffer_reserve(b, 1024);
result = isc_buffer_reserve(b, 512);
assert_int_equal(result, ISC_R_SUCCESS);
assert_true(ISC_BUFFER_VALID(b));
assert_non_null(b);
assert_int_equal(b->length, 1024);
assert_int_equal(b->length, ISC_BUFFER_INCR);
/*
* This call should grow it to 2048 bytes as only 1024 bytes are
* This call should grow it to 1536 bytes as only 1024 bytes are
* available in the buffer.
*/
result = isc_buffer_reserve(b, 1025);
assert_int_equal(result, ISC_R_SUCCESS);
assert_true(ISC_BUFFER_VALID(b));
assert_non_null(b);
assert_int_equal(b->length, 2048);
assert_int_equal(b->length, 3 * ISC_BUFFER_INCR);
/*
* 2048 bytes should already be available, so this call does
* 1536 bytes should already be available, so this call does
* nothing.
*/
result = isc_buffer_reserve(b, 2000);
result = isc_buffer_reserve(b, 1500);
assert_int_equal(result, ISC_R_SUCCESS);
assert_true(ISC_BUFFER_VALID(b));
assert_non_null(b);
assert_int_equal(b->length, 2048);
assert_int_equal(b->length, 3 * ISC_BUFFER_INCR);
/*
* This call should grow it to 4096 bytes as only 2048 bytes are
* This call should grow it to 4096 bytes as only 1536 bytes are
* available in the buffer.
*/
result = isc_buffer_reserve(b, 3000);
result = isc_buffer_reserve(b, 3585);
assert_int_equal(result, ISC_R_SUCCESS);
assert_true(ISC_BUFFER_VALID(b));
assert_non_null(b);
assert_int_equal(b->length, 4096);
assert_int_equal(b->length, 8 * ISC_BUFFER_INCR);
/* Consume some of the buffer so we can run the next test. */
isc_buffer_add(b, 4096);
@@ -93,9 +89,8 @@ ISC_RUN_TEST_IMPL(isc_buffer_reserve) {
*/
result = isc_buffer_reserve(b, UINT_MAX);
assert_int_equal(result, ISC_R_NOMEMORY);
assert_true(ISC_BUFFER_VALID(b));
assert_non_null(b);
assert_int_equal(b->length, 4096);
assert_int_equal(b->length, 8 * ISC_BUFFER_INCR);
isc_buffer_free(&b);
}