xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_flex_mem.h (revision 8ddef7dd9a290d4a9b1efd5d3efacf51d78a1a0d)
1 /*
2  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: qdf_flex_mem (flexibly sized memory allocator)
21  * QCA driver framework (QDF) flex mem APIs
22  *
23  * A flex memory allocator is a memory pool which not only dynamically expands,
24  * but also dynamically reduces as well. Benefits over full dynamic memory
25  * allocation are amoritized allocation cost, and reduced memory fragmentation.
26  *
27  * The allocator consists of 3 parts: the pool, segments, and items. Items are
28  * the smallest chuncks of memory that are handed out via the alloc call, and
29  * are all of a uniform size. Segments are groups of items, representing the
30  * smallest amount of memory that can be dynamically allocated or freed. A pool
31  * is simply a collection of segments.
32  */
33 
34 #ifndef __QDF_FLEX_MEM_H
35 #define __QDF_FLEX_MEM_H
36 
37 #include "qdf_list.h"
38 #include "qdf_lock.h"
39 
40 #define QDF_FM_BITMAP uint32_t
41 #define QDF_FM_BITMAP_BITS (sizeof(QDF_FM_BITMAP) * 8)
42 
43 /**
44  * qdf_flex_mem_pool - a pool of memory segments
45  * @seg_list: the list containing the memory segments
46  * @lock: spinlock for protecting internal data structures
47  * @reduction_limit: the minimum number of segments to keep during reduction
48  * @item_size: the size of the items the pool will allocate
49  */
50 struct qdf_flex_mem_pool {
51 	qdf_list_t seg_list;
52 	struct qdf_spinlock lock;
53 	uint16_t reduction_limit;
54 	uint16_t item_size;
55 };
56 
57 /**
58  * qdf_flex_mem_segment - a memory pool segment
59  * @node: the list node for membership in the memory pool
60  * @dynamic: true if this segment was dynamically allocated
61  * @used_bitmap: bitmap for tracking which items in the segment are in use
62  * @bytes: raw memory for allocating items from
63  */
64 struct qdf_flex_mem_segment {
65 	qdf_list_node_t node;
66 	bool dynamic;
67 	QDF_FM_BITMAP used_bitmap;
68 	uint8_t *bytes;
69 };
70 
71 /**
72  * DEFINE_QDF_FLEX_MEM_POOL() - define a new flex mem pool with one segment
73  * @name: the name of the pool variable
74  * @size_of_item: size of the items the pool will allocate
75  * @rm_limit: min number of segments to keep during reduction
76  */
77 #define DEFINE_QDF_FLEX_MEM_POOL(name, size_of_item, rm_limit) \
78 	struct qdf_flex_mem_pool name; \
79 	uint8_t __ ## name ## _head_bytes[QDF_FM_BITMAP_BITS * (size_of_item)];\
80 	struct qdf_flex_mem_segment __ ## name ## _head = { \
81 		.node = QDF_LIST_NODE_INIT_SINGLE( \
82 			QDF_LIST_ANCHOR(name.seg_list)), \
83 		.bytes = __ ## name ## _head_bytes, \
84 	}; \
85 	struct qdf_flex_mem_pool name = { \
86 		.seg_list = QDF_LIST_INIT_SINGLE(__ ## name ## _head.node), \
87 		.reduction_limit = (rm_limit), \
88 		.item_size = (size_of_item), \
89 	}
90 
91 /**
92  * qdf_flex_mem_init() - initialize a qdf_flex_mem_pool
93  * @pool: the pool to initialize
94  *
95  * Return: None
96  */
97 void qdf_flex_mem_init(struct qdf_flex_mem_pool *pool);
98 
99 /**
100  * qdf_flex_mem_deinit() - deinitialize a qdf_flex_mem_pool
101  * @pool: the pool to deinitialize
102  *
103  * Return: None
104  */
105 void qdf_flex_mem_deinit(struct qdf_flex_mem_pool *pool);
106 
107 /**
108  * qdf_flex_mem_alloc() - logically allocate memory from the pool
109  * @pool: the pool to allocate from
110  *
111  * This function returns any unused item from any existing segment in the pool.
112  * If there are no unused items in the pool, a new segment is dynamically
113  * allocated to service the request. The size of the allocated memory is the
114  * size originally used to create the pool.
115  *
116  * Return: Point to newly allocated memory, NULL on failure
117  */
118 void *qdf_flex_mem_alloc(struct qdf_flex_mem_pool *pool);
119 
120 /**
121  * qdf_flex_mem_free() - logically frees @ptr from the pool
122  * @pool: the pool to return the memory to
123  * @ptr: a pointer received via a call to qdf_flex_mem_alloc()
124  *
125  * This function marks the item corresponding to @ptr as unused. If that item
126  * was the last used item in the segment it belongs to, and the segment was
127  * dynamically allocated, the segment will be freed.
128  *
129  * Return: None
130  */
131 void qdf_flex_mem_free(struct qdf_flex_mem_pool *pool, void *ptr);
132 
133 #endif /* __QDF_FLEX_MEM_H */
134