mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-11-24 20:31:00 +01:00
MEDIUM: shctx: Simplify shctx_row_reserve_hot loop
The shctx_row_reserve_hot relied on two loop levels in order to first look for the first block of a preused row and then iterate on all the blocks of this row to reserve them for the new row. This was not the simplest nor the easiest to read way so this logic could be replaced by a single iteration on the avail list members. The two use cases of calling this function with or without a preexisting "first" member were a bit cumbersome as well and were replaced by a more straightforward approach.
This commit is contained in:
parent
eccb97f60e
commit
610b67fd8b
83
src/shctx.c
83
src/shctx.c
@ -29,10 +29,9 @@ int use_shared_mem = 0;
|
|||||||
struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx,
|
struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx,
|
||||||
struct shared_block *first, int data_len)
|
struct shared_block *first, int data_len)
|
||||||
{
|
{
|
||||||
struct shared_block *last = NULL, *block, *sblock, *ret = NULL, *next;
|
struct shared_block *last = NULL, *block, *sblock;
|
||||||
int enough = 0;
|
struct shared_block *ret = first;
|
||||||
int freed = 0;
|
int remain = 1;
|
||||||
int remain;
|
|
||||||
|
|
||||||
BUG_ON(data_len < 0);
|
BUG_ON(data_len < 0);
|
||||||
|
|
||||||
@ -47,8 +46,6 @@ struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Note that <remain> is nul only if <first> is not nul. */
|
|
||||||
remain = 1;
|
|
||||||
if (first) {
|
if (first) {
|
||||||
/* Check that there is some block to reserve.
|
/* Check that there is some block to reserve.
|
||||||
* In this first block of code we compute the remaining room in the
|
* In this first block of code we compute the remaining room in the
|
||||||
@ -69,71 +66,43 @@ struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!enough && !LIST_ISEMPTY(&shctx->avail)) {
|
if (data_len <= 0 || LIST_ISEMPTY(&shctx->avail)) {
|
||||||
int count = 0;
|
return NULL;
|
||||||
int first_count = 0, first_len = 0;
|
}
|
||||||
|
|
||||||
next = block = LIST_NEXT(&shctx->avail, struct shared_block *, list);
|
/* Initialize the first block of a new row. */
|
||||||
if (ret == NULL)
|
if (!first) {
|
||||||
ret = next;
|
ret = LIST_NEXT(&shctx->avail, struct shared_block*, list);
|
||||||
|
ret->block_count = 0;
|
||||||
|
ret->last_append = NULL;
|
||||||
|
ret->refcount = 1;
|
||||||
|
}
|
||||||
|
|
||||||
first_count = next->block_count;
|
list_for_each_entry_safe(block, sblock, &shctx->avail, list) {
|
||||||
first_len = next->len;
|
|
||||||
/*
|
|
||||||
Should never been set to 0.
|
|
||||||
if (next->block_count == 0)
|
|
||||||
next->block_count = 1;
|
|
||||||
*/
|
|
||||||
|
|
||||||
list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) {
|
|
||||||
|
|
||||||
/* release callback */
|
/* release callback */
|
||||||
if (first_len && shctx->free_block)
|
if (block->len && shctx->free_block)
|
||||||
shctx->free_block(next, block);
|
shctx->free_block(block, block);
|
||||||
|
|
||||||
block->block_count = 1;
|
|
||||||
block->len = 0;
|
block->len = 0;
|
||||||
|
|
||||||
freed++;
|
|
||||||
|
|
||||||
BUG_ON(data_len < 0);
|
|
||||||
data_len -= shctx->block_size;
|
|
||||||
|
|
||||||
if (data_len > 0 || !enough) {
|
|
||||||
if (last) {
|
if (last) {
|
||||||
shctx_block_append_hot(shctx, &last->list, block);
|
shctx_block_append_hot(shctx, &last->list, block);
|
||||||
last = block;
|
last = block;
|
||||||
} else {
|
|
||||||
shctx_block_set_hot(shctx, block);
|
|
||||||
}
|
|
||||||
if (!remain) {
|
if (!remain) {
|
||||||
first->last_append = block;
|
first->last_append = block;
|
||||||
remain = 1;
|
remain = 1;
|
||||||
}
|
}
|
||||||
if (data_len <= 0) {
|
} else
|
||||||
ret->block_count = freed;
|
shctx_block_set_hot(shctx, block);
|
||||||
ret->refcount = 1;
|
|
||||||
ret->last_reserved = block;
|
|
||||||
ret->last_append = NULL;
|
|
||||||
enough = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
if (count >= first_count)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first) {
|
++ret->block_count;
|
||||||
first->block_count += ret->block_count;
|
|
||||||
first->last_reserved = ret->last_reserved;
|
data_len -= shctx->block_size;
|
||||||
/* Reset this block. */
|
|
||||||
ret->last_reserved = NULL;
|
if (data_len <= 0) {
|
||||||
ret->block_count = 1;
|
ret->last_reserved = block;
|
||||||
ret->refcount = 0;
|
break;
|
||||||
/* Return the first block. */
|
}
|
||||||
ret = first;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user