Emulex Logo
OneCoreā„¢ Storage SDK Release 11.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ocs_pool.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015, Emulex
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 /**
34  * @file
35  *
36  * The ocs_pool_t data structure consists of:
37  *
38  * pool->a An ocs_array_t.
39  * pool->freelist A linked list of free items.
40  *
41  * When a pool is allocated using ocs_pool_alloc(), the caller provides the
42  * size in bytes of each memory pool item (size), and a count of items (count).
43  * Since ocs_pool_alloc() has no visibility into the object the caller is
44  * allocating, a link for the linked list is "pre-pended". Thus when allocating the
45  * ocs_array_t, the size used is the size of the pool_hdr_t plus the requested
46  * memory pool item size.
47  *
48  * array item layout:
49  *
50  * pool_hdr_t
51  * pool data[size]
52  *
53  * The address of the pool data is returned when allocated (using ocs_pool_get(), or
54  * ocs_pool_get_instance()), and received when being freed (using ocs_pool_put().
55  * So the address returned by the array item (ocs_array_get()) must be offset by
56  * the size of pool_hdr_t.
57  */
58 
59 #include "ocs_os.h"
60 #include "ocs_pool.h"
61 
62 struct ocs_pool_s {
63  ocs_os_handle_t os;
64  ocs_array_t *a;
65  ocs_list_t freelist;
66  uint32_t use_lock:1;
67  ocs_lock_t lock;
68 };
69 
70 typedef struct {
72 } pool_hdr_t;
73 
74 
75 /**
76  * @brief Allocate a memory pool.
77  *
78  * A memory pool of given size and item count is allocated.
79  *
80  * @param os OS handle.
81  * @param size Size in bytes of item.
82  * @param count Number of items in a memory pool.
83  * @param use_lock TRUE to enable locking of pool.
84  *
85  * @return Returns pointer to allocated memory pool, or NULL.
86  */
87 ocs_pool_t *
88 ocs_pool_alloc(ocs_os_handle_t os, uint32_t size, uint32_t count, uint32_t use_lock)
89 {
90  ocs_pool_t *pool;
91  uint32_t i;
92 
93  pool = ocs_malloc(os, sizeof(*pool), OCS_M_ZERO | OCS_M_NOWAIT);
94  if (pool == NULL) {
95  return NULL;
96  }
97 
98  pool->os = os;
99  pool->use_lock = use_lock;
100  if (pool->use_lock) {
101  ocs_lock_init(os, &pool->lock, "ocs_pool:%p", pool);
102  }
103 
104  /* Allocate an array where each array item is the size of a pool_hdr_t plus
105  * the requested memory item size (size)
106  */
107  pool->a = ocs_array_alloc(os, size + sizeof(pool_hdr_t), count);
108  if (pool->a == NULL) {
109  ocs_pool_free(pool);
110  return NULL;
111  }
112 
113  ocs_list_init(&pool->freelist, pool_hdr_t, link);
114  for (i = 0; i < count; i++) {
115  ocs_list_add_tail(&pool->freelist, ocs_array_get(pool->a, i));
116  }
117 
118  return pool;
119 }
120 
121 /**
122  * @brief Reset a memory pool.
123  *
124  * Place all pool elements on the free list, and zero them.
125  *
126  * @param pool Pointer to the pool object.
127  *
128  * @return None.
129  */
130 void
131 ocs_pool_reset(ocs_pool_t *pool)
132 {
133  uint32_t i;
134  uint32_t count = ocs_array_get_count(pool->a);
135  uint32_t size = ocs_array_get_size(pool->a);
136 
137  if (pool->use_lock) {
138  ocs_lock(&pool->lock);
139  }
140 
141  /*
142  * Remove all the entries from the free list, otherwise we will
143  * encountered linked list asserts when they are re-added.
144  */
145  while (!ocs_list_empty(&pool->freelist)) {
146  ocs_list_remove_head(&pool->freelist);
147  }
148 
149  /* Reset the free list */
150  ocs_list_init(&pool->freelist, pool_hdr_t, link);
151 
152  /* Return all elements to the free list and zero the elements */
153  for (i = 0; i < count; i++) {
154  ocs_memset(ocs_pool_get_instance(pool, i), 0, size - sizeof(pool_hdr_t));
155  ocs_list_add_tail(&pool->freelist, ocs_array_get(pool->a, i));
156  }
157  if (pool->use_lock) {
158  ocs_unlock(&pool->lock);
159  }
160 
161 }
162 
163 /**
164  * @brief Free a previously allocated memory pool.
165  *
166  * The memory pool is freed.
167  *
168  * @param pool Pointer to memory pool.
169  *
170  * @return None.
171  */
172 void
173 ocs_pool_free(ocs_pool_t *pool)
174 {
175  if (pool != NULL) {
176  if (pool->a != NULL) {
177  ocs_array_free(pool->a);
178  }
179  if (pool->use_lock) {
180  ocs_lock_free(&pool->lock);
181  }
182  ocs_free(pool->os, pool, sizeof(*pool));
183  }
184 }
185 
186 /**
187  * @brief Allocate a memory pool item
188  *
189  * A memory pool item is taken from the free list and returned.
190  *
191  * @param pool Pointer to memory pool.
192  *
193  * @return Pointer to allocated item, otherwise NULL if there are no unallocated
194  * items.
195  */
196 void *
197 ocs_pool_get(ocs_pool_t *pool)
198 {
199  pool_hdr_t *h;
200  void *item = NULL;
201 
202  if (pool->use_lock) {
203  ocs_lock(&pool->lock);
204  }
205 
206  h = ocs_list_remove_head(&pool->freelist);
207 
208  if (h != NULL) {
209  /* Return the array item address offset by the size of pool_hdr_t */
210  item = &h[1];
211  }
212 
213  if (pool->use_lock) {
214  ocs_unlock(&pool->lock);
215  }
216  return item;
217 }
218 
219 /**
220  * @brief free memory pool item
221  *
222  * A memory pool item is freed.
223  *
224  * @param pool Pointer to memory pool.
225  * @param item Pointer to item to free.
226  *
227  * @return None.
228  */
229 void
230 ocs_pool_put(ocs_pool_t *pool, void *item)
231 {
232  pool_hdr_t *h;
233 
234  if (pool->use_lock) {
235  ocs_lock(&pool->lock);
236  }
237 
238  /* Fetch the address of the array item, which is the item address negatively offset
239  * by size of pool_hdr_t (note the index of [-1]
240  */
241  h = &((pool_hdr_t*)item)[-1];
242 
243  ocs_list_add_tail(&pool->freelist, h);
244 
245  if (pool->use_lock) {
246  ocs_unlock(&pool->lock);
247  }
248 }
249 
250 /**
251  * @brief free memory pool item
252  *
253  * A memory pool item is freed to head of list.
254  *
255  * @param pool Pointer to memory pool.
256  * @param item Pointer to item to free.
257  *
258  * @return None.
259  */
260 void
261 ocs_pool_put_head(ocs_pool_t *pool, void *item)
262 {
263  pool_hdr_t *h;
264 
265  if (pool->use_lock) {
266  ocs_lock(&pool->lock);
267  }
268 
269  /* Fetch the address of the array item, which is the item address negatively offset
270  * by size of pool_hdr_t (note the index of [-1]
271  */
272  h = &((pool_hdr_t*)item)[-1];
273 
274  ocs_list_add_head(&pool->freelist, h);
275 
276  if (pool->use_lock) {
277  ocs_unlock(&pool->lock);
278  }
279 }
280 
281 /**
282  * @brief Return memory pool item count.
283  *
284  * Returns the allocated number of items.
285  *
286  * @param pool Pointer to memory pool.
287  *
288  * @return Returns count of allocated items.
289  */
290 uint32_t
291 ocs_pool_get_count(ocs_pool_t *pool)
292 {
293  uint32_t count;
294  if (pool->use_lock) {
295  ocs_lock(&pool->lock);
296  }
297  count = ocs_array_get_count(pool->a);
298  if (pool->use_lock) {
299  ocs_unlock(&pool->lock);
300  }
301  return count;
302 }
303 
304 /**
305  * @brief Return item given an index.
306  *
307  * A pointer to a memory pool item is returned given an index.
308  *
309  * @param pool Pointer to memory pool.
310  * @param idx Index.
311  *
312  * @return Returns pointer to item, or NULL if index is invalid.
313  */
314 void *
315 ocs_pool_get_instance(ocs_pool_t *pool, uint32_t idx)
316 {
317  pool_hdr_t *h = ocs_array_get(pool->a, idx);
318 
319  if (h == NULL) {
320  return NULL;
321  }
322  return &h[1];
323 }
324 
325 /**
326  * @brief Return count of free objects in a pool.
327  *
328  * The number of objects on a pool's free list.
329  *
330  * @param pool Pointer to memory pool.
331  *
332  * @return Returns count of objects on free list.
333  */
334 uint32_t
336 {
337  uint32_t count = 0;
338  void *item;
339 
340  if (pool->use_lock) {
341  ocs_lock(&pool->lock);
342  }
343 
344  ocs_list_foreach(&pool->freelist, item) {
345  count++;
346  }
347 
348  if (pool->use_lock) {
349  ocs_unlock(&pool->lock);
350  }
351  return count;
352 }
ocs_list_t ocs_list_link_t
Definition: ocs_list.h:84
ocs_array_t * a
Definition: ocs_pool.c:64
void * ocs_pool_get(ocs_pool_t *pool)
Allocate a memory pool item.
Definition: ocs_pool.c:197
uint32_t ocs_pool_get_freelist_count(ocs_pool_t *pool)
Return count of free objects in a pool.
Definition: ocs_pool.c:335
static int32_t ocs_list_empty(ocs_list_t *list)
Test if a list is empty.
Definition: ocs_list.h:125
ocs_list_link_t link
Definition: ocs_pool.c:71
void ocs_pool_put_head(ocs_pool_t *pool, void *item)
free memory pool item
Definition: ocs_pool.c:261
void ocs_pool_put(ocs_pool_t *pool, void *item)
free memory pool item
Definition: ocs_pool.c:230
uint32_t use_lock
Definition: ocs_pool.c:66
ocs_pool_t * ocs_pool_alloc(ocs_os_handle_t os, uint32_t size, uint32_t count, uint32_t use_lock)
Allocate a memory pool.
Definition: ocs_pool.c:88
uint32_t ocs_array_get_size(ocs_array_t *array)
Return size of array elements in bytes.
Definition: ocs_array.c:203
void ocs_pool_free(ocs_pool_t *pool)
Free a previously allocated memory pool.
Definition: ocs_pool.c:173
ocs_lock_t lock
Definition: ocs_pool.c:67
#define ocs_list_init(head, type, link)
Definition: ocs_list.h:113
ocs_array_t * ocs_array_alloc(ocs_os_handle_t os, uint32_t size, uint32_t count)
Allocate an array object.
Definition: ocs_array.c:87
void ocs_array_free(ocs_array_t *array)
Free an array object.
Definition: ocs_array.c:139
uint32_t ocs_array_get_count(ocs_array_t *array)
Return number of elements in an array.
Definition: ocs_array.c:188
void * ocs_pool_get_instance(ocs_pool_t *pool, uint32_t idx)
Return item given an index.
Definition: ocs_pool.c:315
void ocs_pool_reset(ocs_pool_t *pool)
Reset a memory pool.
Definition: ocs_pool.c:131
static void ocs_list_add_tail(ocs_list_t *list, void *item)
Add an item to the tail of the list.
Definition: ocs_list.h:263
#define ocs_list_remove_head(list)
Remove and return an item from the head of the list.
Definition: ocs_list.h:374
static void ocs_list_add_head(ocs_list_t *list, void *item)
Add an item to the head of the list.
Definition: ocs_list.h:235
ocs_list_t freelist
Definition: ocs_pool.c:65
ocs_os_handle_t os
Definition: ocs_pool.c:63
#define ocs_list_foreach(list, item)
Iterate a linked list.
Definition: ocs_list.h:428
void * ocs_array_get(ocs_array_t *array, uint32_t idx)
Return reference to an element of an array object.
Definition: ocs_array.c:166
uint32_t ocs_pool_get_count(ocs_pool_t *pool)
Return memory pool item count.
Definition: ocs_pool.c:291