1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
4 #ifndef LTHREAD_OBJCACHE_H_
5 #define LTHREAD_OBJCACHE_H_
13 #include <rte_per_lcore.h>
14 #include <rte_malloc.h>
15 #include <rte_memory.h>
17 #include "lthread_int.h"
18 #include "lthread_diag.h"
19 #include "lthread_queue.h"
22 RTE_DECLARE_PER_LCORE(struct lthread_sched *, this_sched);
24 struct lthread_objcache {
25 struct lthread_queue *q;
28 char name[LT_MAX_NAME_SIZE];
30 DIAG_COUNT_DEFINE(rd);
31 DIAG_COUNT_DEFINE(wr);
32 DIAG_COUNT_DEFINE(prealloc);
33 DIAG_COUNT_DEFINE(capacity);
34 DIAG_COUNT_DEFINE(available);
41 lthread_objcache *_lthread_objcache_create(const char *name,
45 struct lthread_objcache *c =
46 rte_malloc_socket(NULL, sizeof(struct lthread_objcache),
52 c->q = _lthread_queue_create("cache queue");
57 c->obj_size = obj_size;
58 c->prealloc_size = prealloc_size;
61 strncpy(c->name, name, LT_MAX_NAME_SIZE);
62 c->name[sizeof(c->name)-1] = 0;
64 DIAG_COUNT_INIT(c, rd);
65 DIAG_COUNT_INIT(c, wr);
66 DIAG_COUNT_INIT(c, prealloc);
67 DIAG_COUNT_INIT(c, capacity);
68 DIAG_COUNT_INIT(c, available);
76 _lthread_objcache_destroy(struct lthread_objcache *c)
78 if (_lthread_queue_destroy(c->q) == 0) {
86 * Allocate an object from an object cache
89 _lthread_objcache_alloc(struct lthread_objcache *c)
93 struct lthread_queue *q = c->q;
94 size_t obj_size = c->obj_size;
95 int prealloc_size = c->prealloc_size;
97 data = _lthread_queue_remove(q);
100 DIAG_COUNT_INC(c, prealloc);
101 for (i = 0; i < prealloc_size; i++) {
103 rte_zmalloc_socket(NULL, obj_size,
109 DIAG_COUNT_INC(c, available);
110 DIAG_COUNT_INC(c, capacity);
111 _lthread_queue_insert_mp(q, data);
113 data = _lthread_queue_remove(q);
115 DIAG_COUNT_INC(c, rd);
116 DIAG_COUNT_DEC(c, available);
121 * free an object to a cache
124 _lthread_objcache_free(struct lthread_objcache *c, void *obj)
126 DIAG_COUNT_INC(c, wr);
127 DIAG_COUNT_INC(c, available);
128 _lthread_queue_insert_mp(c->q, obj);
136 #endif /* LTHREAD_OBJCACHE_H_ */