1 /* 2 * Copyright (c) 2018 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 * @item_size: the size of the items the pool will allocate 48 */ 49 struct qdf_flex_mem_pool { 50 qdf_list_t seg_list; 51 struct qdf_spinlock lock; 52 uint16_t item_size; 53 }; 54 55 /** 56 * qdf_flex_mem_segment - a memory pool segment 57 * @node: the list node for membership in the memory pool 58 * @dynamic: true if this segment was dynamically allocated 59 * @used_bitmap: bitmap for tracking which items in the segment are in use 60 * @bytes: raw memory for allocating items from 61 */ 62 struct qdf_flex_mem_segment { 63 qdf_list_node_t node; 64 bool dynamic; 65 QDF_FM_BITMAP used_bitmap; 66 uint8_t *bytes; 67 }; 68 69 /** 70 * DEFINE_QDF_FLEX_MEM_POOL() - define a new flex mem pool with one segment 71 * @name: the name of the pool variable 72 * @size_of_item: size of the items the pool will allocate 73 */ 74 #define DEFINE_QDF_FLEX_MEM_POOL(name, size_of_item) \ 75 struct qdf_flex_mem_pool name; \ 76 uint8_t __ ## name ## _head_bytes[QDF_FM_BITMAP_BITS * (size_of_item)];\ 77 struct qdf_flex_mem_segment __ ## name ## _head = { \ 78 .node = QDF_LIST_NODE_INIT_SINGLE( \ 79 QDF_LIST_ANCHOR(name.seg_list)), \ 80 .bytes = __ ## name ## _head_bytes, \ 81 }; \ 82 struct qdf_flex_mem_pool name = { \ 83 .seg_list = QDF_LIST_INIT_SINGLE(__ ## name ## _head.node), \ 84 .item_size = (size_of_item), \ 85 } 86 87 /** 88 * qdf_flex_mem_init() - initialize a qdf_flex_mem_pool 89 * @pool: the pool to initialize 90 * 91 * Return: None 92 */ 93 void qdf_flex_mem_init(struct qdf_flex_mem_pool *pool); 94 95 /** 96 * qdf_flex_mem_deinit() - deinitialize a qdf_flex_mem_pool 97 * @pool: the pool to deinitialize 98 * 99 * Return: None 100 */ 101 void qdf_flex_mem_deinit(struct qdf_flex_mem_pool *pool); 102 103 /** 104 * qdf_flex_mem_alloc() - logically allocate memory from the pool 105 * @pool: the pool to allocate from 106 * 107 * This function returns any unused item from any existing segment in the pool. 108 * If there are no unused items in the pool, a new segment is dynamically 109 * allocated to service the request. The size of the allocated memory is the 110 * size originally used to create the pool. 111 * 112 * Return: Point to newly allocated memory, NULL on failure 113 */ 114 void *qdf_flex_mem_alloc(struct qdf_flex_mem_pool *pool); 115 116 /** 117 * qdf_flex_mem_free() - logically frees @ptr from the pool 118 * @pool: the pool to return the memory to 119 * @ptr: a pointer recieved via a call to qdf_flex_mem_alloc() 120 * 121 * This function marks the item corresponding to @ptr as unused. If that item 122 * was the last used item in the segment it belongs to, and the segment was 123 * dynamically allocated, the segment will be freed. 124 * 125 * Return: None 126 */ 127 void qdf_flex_mem_free(struct qdf_flex_mem_pool *pool, void *ptr); 128 129 #endif /* __QDF_FLEX_MEM_H */ 130