ba3f160fef57b439a976f63f6756e0d2d9ac2c6a
[tldk.git] / lib / libtle_memtank / memtank.h
1 /*
2  * Copyright (c) 2019  Intel Corporation.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef _MEMTANK_H_
17 #define _MEMTANK_H_
18
19 #include <tle_memtank.h>
20 #include <stdalign.h>
21
22 struct memobj {
23         uint64_t red_zone1;
24         struct memchunk *chunk; /* ptr to the chunk it belongs to */
25         struct {
26                 uint32_t nb_alloc;
27                 uint32_t nb_free;
28         } dbg;
29         uint64_t red_zone2;
30 };
31
32 #define RED_ZONE_V1     UINT64_C(0xBADECAFEBADECAFE)
33 #define RED_ZONE_V2     UINT64_C(0xDEADBEEFDEADBEEF)
34
35 struct memchunk {
36         TAILQ_ENTRY(memchunk) link;  /* link to the next chunk in the tank */
37         void *raw;                   /* un-aligned ptr returned by alloc() */
38         uint32_t nb_total;           /* total number of objects in the chunk */
39         uint32_t nb_free;            /*  number of free object in the chunk */
40         void *free[];                /* array of free objects */
41 } __rte_cache_aligned;
42
43
44 TAILQ_HEAD(mchunk_head, memchunk);
45
46 struct mchunk_list {
47         rte_spinlock_t lock;
48         struct mchunk_head chunk;  /* list of chunks */
49 } __rte_cache_aligned;
50
51 enum {
52         MC_FULL,  /* all memchunk objs are free */
53         MC_USED,  /* some of memchunk objs are allocated */
54         MC_NUM,
55 };
56
57 struct memtank {
58         /* user provided data */
59         struct tle_memtank_prm prm;
60
61         /*run-time data */
62         void *raw;                   /* un-aligned ptr returned by alloc() */
63         size_t chunk_size;           /* full size of each memchunk */
64         uint32_t obj_size;           /* full size of each memobj */
65         uint32_t max_chunk;          /* max allowed number of chunks */
66         uint32_t flags;              /* behavior flags */
67         rte_atomic32_t nb_chunks;    /* number of allocated chunks */
68
69         struct mchunk_list chl[MC_NUM];  /* lists of memchunks */
70
71         struct tle_memtank pub;
72 };
73
74 /*
75  * Obtain pointer to interal memtank struct from public one
76  */
77 static inline struct memtank *
78 tank_pub_full(const void *p)
79 {
80         uintptr_t v;
81
82         v = (uintptr_t)p - offsetof(struct memtank, pub);
83         return (struct memtank *)v;
84 }
85
86 /*
87  * Obtain pointer to interal memobj struct from public one
88  */
89 static inline struct memobj *
90 obj_pub_full(uintptr_t p, uint32_t obj_sz)
91 {
92         uintptr_t v;
93
94         v = p + obj_sz - sizeof(struct memobj);
95         return (struct memobj *)v;
96 }
97
98 static inline int
99 memobj_verify(const struct memobj *mo, uint32_t finc)
100 {
101         if (mo->red_zone1 != RED_ZONE_V1 || mo->red_zone2 != RED_ZONE_V2 ||
102                         mo->dbg.nb_alloc != mo->dbg.nb_free + finc)
103                 return -EINVAL;
104         return 0;
105 }
106
107 #endif  /* _MEMTANK_H_ */