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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <tle_memtank.h>
24 struct memchunk *chunk; /* ptr to the chunk it belongs to */
32 #define RED_ZONE_V1 UINT64_C(0xBADECAFEBADECAFE)
33 #define RED_ZONE_V2 UINT64_C(0xDEADBEEFDEADBEEF)
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;
44 TAILQ_HEAD(mchunk_head, memchunk);
48 struct mchunk_head chunk; /* list of chunks */
49 } __rte_cache_aligned;
52 MC_FULL, /* all memchunk objs are free */
53 MC_USED, /* some of memchunk objs are allocated */
58 /* user provided data */
59 struct tle_memtank_prm prm;
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 */
69 struct mchunk_list chl[MC_NUM]; /* lists of memchunks */
71 struct tle_memtank pub;
75 * Obtain pointer to interal memtank struct from public one
77 static inline struct memtank *
78 tank_pub_full(const void *p)
82 v = (uintptr_t)p - offsetof(struct memtank, pub);
83 return (struct memtank *)v;
87 * Obtain pointer to interal memobj struct from public one
89 static inline struct memobj *
90 obj_pub_full(uintptr_t p, uint32_t obj_sz)
94 v = p + obj_sz - sizeof(struct memobj);
95 return (struct memobj *)v;
99 memobj_verify(const struct memobj *mo, uint32_t finc)
101 if (mo->red_zone1 != RED_ZONE_V1 || mo->red_zone2 != RED_ZONE_V2 ||
102 mo->dbg.nb_alloc != mo->dbg.nb_free + finc)
107 #endif /* _MEMTANK_H_ */