Browse Source

MT#55283 use embedded links in bufferpool

Avoids repeated calls to allocator

Change-Id: I2b85fef3d0fd976bc9bf8a4b0cb9b29b62318cbf
pull/1923/head
Richard Fuchs 9 months ago
parent
commit
95f75d690b
1 changed files with 12 additions and 11 deletions
  1. +12
    -11
      lib/bufferpool.c

+ 12
- 11
lib/bufferpool.c View File

@ -25,6 +25,7 @@ struct bpool_shard {
void *end; void *end;
void *head; void *head;
bool full; bool full;
shard_list link;
unsigned int (*recycle)(void *); unsigned int (*recycle)(void *);
void *arg; void *arg;
}; };
@ -49,9 +50,8 @@ static void bufferpool_recycle(struct bpool_shard *shard) {
if (shard->refs == 0) { if (shard->refs == 0) {
shard->full = false; shard->full = false;
shard_list *link = t_queue_find(&bp->full_shards, shard); // XXX avoid this
t_queue_delete_link(&bp->full_shards, link);
t_queue_push_tail(&bp->empty_shards, shard);
t_queue_unlink(&bp->full_shards, &shard->link);
t_queue_push_tail_link(&bp->empty_shards, &shard->link);
} }
} }
@ -80,6 +80,7 @@ static struct bpool_shard *bufferpool_new_shard(struct bufferpool *bp) {
ret->bp = bp; ret->bp = bp;
ret->buf = buf; ret->buf = buf;
ret->end = buf + BUFFERPOOL_SHARD_SIZE; ret->end = buf + BUFFERPOOL_SHARD_SIZE;
ret->link.data = ret;
struct bpool_shard **head = buf; struct bpool_shard **head = buf;
*head = ret; *head = ret;
@ -107,15 +108,15 @@ void *bufferpool_alloc(struct bufferpool *bp, size_t len) {
while (true) { while (true) {
if (!bp->empty_shards.length) { if (!bp->empty_shards.length) {
shard = bufferpool_new_shard(bp); shard = bufferpool_new_shard(bp);
t_queue_push_tail(&bp->empty_shards, shard);
t_queue_push_tail_link(&bp->empty_shards, &shard->link);
break; break;
} }
shard = bp->empty_shards.head->data; shard = bp->empty_shards.head->data;
if (shard->head + len <= shard->end) if (shard->head + len <= shard->end)
break; break;
t_queue_pop_head(&bp->empty_shards);
t_queue_push_tail(&bp->full_shards, shard);
t_queue_unlink(&bp->empty_shards, &shard->link);
t_queue_push_tail_link(&bp->full_shards, &shard->link);
shard->full = true; shard->full = true;
shard_check_full(shard); shard_check_full(shard);
@ -136,14 +137,14 @@ void *bufferpool_reserve(struct bufferpool *bp, unsigned int refs, unsigned int
struct bpool_shard *shard = t_queue_peek_head(&bp->empty_shards); struct bpool_shard *shard = t_queue_peek_head(&bp->empty_shards);
if (shard && shard->head == shard->empty && shard->refs == 0) if (shard && shard->head == shard->empty && shard->refs == 0)
t_queue_pop_head(&bp->empty_shards);
t_queue_unlink(&bp->empty_shards, &shard->link);
else else
shard = bufferpool_new_shard(bp); shard = bufferpool_new_shard(bp);
// set references, set recycle callback, move to full list // set references, set recycle callback, move to full list
shard->refs = refs; shard->refs = refs;
shard->full = true; shard->full = true;
t_queue_push_tail(&bp->full_shards, shard);
t_queue_push_tail_link(&bp->full_shards, &shard->link);
shard->recycle = recycle; shard->recycle = recycle;
shard->arg = arg; shard->arg = arg;
@ -163,11 +164,11 @@ static void bpool_shard_destroy(struct bpool_shard *shard) {
static void bpool_shard_delayed_destroy(struct bufferpool *bp, struct bpool_shard *shard) { static void bpool_shard_delayed_destroy(struct bufferpool *bp, struct bpool_shard *shard) {
if (shard->full) { if (shard->full) {
shard_list *link = t_queue_find(&bp->full_shards, shard); shard_list *link = t_queue_find(&bp->full_shards, shard);
t_queue_delete_link(&bp->full_shards, link);
t_queue_unlink(&bp->full_shards, link);
} }
else { else {
shard_list *link = t_queue_find(&bp->empty_shards, shard); shard_list *link = t_queue_find(&bp->empty_shards, shard);
t_queue_delete_link(&bp->empty_shards, link);
t_queue_unlink(&bp->empty_shards, link);
} }
bpool_shard_destroy(shard); bpool_shard_destroy(shard);
} }
@ -232,8 +233,8 @@ static void bpool_destroy_shards(shard_q *q) {
shard_list *n = l->next; shard_list *n = l->next;
struct bpool_shard *shard = l->data; struct bpool_shard *shard = l->data;
if (shard->refs == 0) { if (shard->refs == 0) {
t_queue_unlink(q, l);
bpool_shard_destroy(shard); bpool_shard_destroy(shard);
t_queue_delete_link(q, l);
} }
l = n; l = n;
} }


Loading…
Cancel
Save