2 * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * Implementation of the grow pools. The grow pools manage a pool of objects.
39 * The pools can grow to meet demand, limited only by system memory.
45 #endif /* HAVE_CONFIG_H */
49 #include <complib/cl_qcomppool.h>
50 #include <complib/cl_comppool.h>
51 #include <complib/cl_qpool.h>
52 #include <complib/cl_pool.h>
53 #include <complib/cl_math.h>
56 * IMPLEMENTATION OF QUICK COMPOSITE POOL
58 void cl_qcpool_construct(IN cl_qcpool_t * const p_pool)
62 memset(p_pool, 0, sizeof(cl_qcpool_t));
64 p_pool->state = CL_UNINITIALIZED;
67 cl_status_t cl_qcpool_init(IN cl_qcpool_t * const p_pool,
68 IN const size_t min_size, IN const size_t max_size,
69 IN const size_t grow_size,
70 IN const size_t * const component_sizes,
71 IN const uint32_t num_components,
72 IN cl_pfn_qcpool_init_t pfn_initializer OPTIONAL,
73 IN cl_pfn_qcpool_dtor_t pfn_destructor OPTIONAL,
74 IN const void *const context)
80 /* Must have a minimum of 1 component. */
81 CL_ASSERT(num_components);
82 /* A component size array is required. */
83 CL_ASSERT(component_sizes);
85 * If no initializer is provided, the first component must be large
86 * enough to hold a pool item.
88 CL_ASSERT(pfn_initializer ||
89 (component_sizes[0] >= sizeof(cl_pool_item_t)));
91 cl_qcpool_construct(p_pool);
93 if (num_components > 1 && !pfn_initializer)
94 return (CL_INVALID_SETTING);
96 if (max_size && max_size < min_size)
97 return (CL_INVALID_SETTING);
100 * Allocate the array of component sizes and component pointers all
103 p_pool->component_sizes = (size_t *) malloc((sizeof(size_t) +
107 if (!p_pool->component_sizes)
108 return (CL_INSUFFICIENT_MEMORY);
110 memset(p_pool->component_sizes, 0,
111 (sizeof(size_t) + sizeof(void *)) * num_components);
113 /* Calculate the pointer to the array of pointers, used for callbacks. */
114 p_pool->p_components =
115 (void **)(p_pool->component_sizes + num_components);
117 /* Copy the user's sizes into our array for future use. */
118 memcpy(p_pool->component_sizes, component_sizes,
119 sizeof(component_sizes[0]) * num_components);
121 /* Store the number of components per object. */
122 p_pool->num_components = num_components;
124 /* Round up and store the size of the components. */
125 for (i = 0; i < num_components; i++) {
127 * We roundup each component size so that all components
128 * are aligned on a natural boundary.
130 p_pool->component_sizes[i] =
131 ROUNDUP(p_pool->component_sizes[i], sizeof(uintptr_t));
134 p_pool->max_objects = max_size ? max_size : ~(size_t) 0;
135 p_pool->grow_size = grow_size;
137 /* Store callback function pointers. */
138 p_pool->pfn_init = pfn_initializer; /* may be NULL */
139 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
140 p_pool->context = context;
142 cl_qlist_init(&p_pool->alloc_list);
144 cl_qlist_init(&p_pool->free_list);
147 * We are now initialized. We change the initialized flag before
148 * growing since the grow function asserts that we are initialized.
150 p_pool->state = CL_INITIALIZED;
152 /* Allocate the minimum number of objects as requested. */
156 status = cl_qcpool_grow(p_pool, min_size);
157 /* Trap for error and cleanup if necessary. */
158 if (status != CL_SUCCESS)
159 cl_qcpool_destroy(p_pool);
164 void cl_qcpool_destroy(IN cl_qcpool_t * const p_pool)
166 /* CL_ASSERT that a non-NULL pointer was provided. */
168 /* CL_ASSERT that we are in a valid state (not uninitialized memory). */
169 CL_ASSERT(cl_is_state_valid(p_pool->state));
171 if (p_pool->state == CL_INITIALIZED) {
173 * Assert if the user hasn't put everything back in the pool
174 * before destroying it
175 * if they haven't, then most likely they are still using memory
176 * that will be freed, and the destructor will not be called!
179 /* but we do not want "free" version to assert on this one */
180 CL_ASSERT(cl_qcpool_count(p_pool) == p_pool->num_objects);
182 /* call the user's destructor for each object in the pool */
183 if (p_pool->pfn_dtor) {
184 while (!cl_is_qlist_empty(&p_pool->free_list)) {
185 p_pool->pfn_dtor((cl_pool_item_t *)
186 cl_qlist_remove_head(&p_pool->
188 (void *)p_pool->context);
191 cl_qlist_remove_all(&p_pool->free_list);
194 /* Free all allocated memory blocks. */
195 while (!cl_is_qlist_empty(&p_pool->alloc_list))
196 free(cl_qlist_remove_head(&p_pool->alloc_list));
198 if (p_pool->component_sizes) {
199 free(p_pool->component_sizes);
200 p_pool->component_sizes = NULL;
204 p_pool->state = CL_UNINITIALIZED;
207 cl_status_t cl_qcpool_grow(IN cl_qcpool_t * const p_pool, IN size_t obj_count)
209 cl_status_t status = CL_SUCCESS;
211 cl_pool_item_t *p_pool_item;
216 CL_ASSERT(p_pool->state == CL_INITIALIZED);
217 CL_ASSERT(obj_count);
219 /* Validate that growth is possible. */
220 if (p_pool->num_objects == p_pool->max_objects)
221 return (CL_INSUFFICIENT_MEMORY);
223 /* Cap the growth to the desired maximum. */
224 if (obj_count > (p_pool->max_objects - p_pool->num_objects))
225 obj_count = p_pool->max_objects - p_pool->num_objects;
227 /* Calculate the size of an object. */
229 for (i = 0; i < p_pool->num_components; i++)
230 obj_size += p_pool->component_sizes[i];
232 /* Allocate the buffer for the new objects. */
233 p_objects = (uint8_t *)
234 malloc(sizeof(cl_list_item_t) + (obj_size * obj_count));
236 /* Make sure the allocation succeeded. */
238 return (CL_INSUFFICIENT_MEMORY);
241 sizeof(cl_list_item_t) + (obj_size * obj_count));
243 /* Insert the allocation in our list. */
244 cl_qlist_insert_tail(&p_pool->alloc_list, (cl_list_item_t *) p_objects);
245 p_objects += sizeof(cl_list_item_t);
247 /* initialize the new elements and add them to the free list */
248 while (obj_count--) {
249 /* Setup the array of components for the current object. */
250 p_pool->p_components[0] = p_objects;
251 for (i = 1; i < p_pool->num_components; i++) {
252 /* Calculate the pointer to the next component. */
253 p_pool->p_components[i] =
254 (uint8_t *) p_pool->p_components[i - 1] +
255 p_pool->component_sizes[i - 1];
259 * call the user's initializer
262 if (p_pool->pfn_init) {
264 status = p_pool->pfn_init(p_pool->p_components,
265 p_pool->num_components,
266 (void *)p_pool->context,
268 if (status != CL_SUCCESS) {
270 * User initialization failed
271 * we may have only grown the pool by some partial amount
272 * Invoke the destructor for the object that failed
275 if (p_pool->pfn_dtor)
276 p_pool->pfn_dtor(p_pool_item,
280 /* Return the user's status. */
283 CL_ASSERT(p_pool_item);
286 * If no initializer is provided, assume that the pool item
287 * is stored at the beginning of the first component.
290 (cl_pool_item_t *) p_pool->p_components[0];
295 * Set the pool item's pool pointer to this pool so that we can
296 * check that items get returned to the correct pool.
298 p_pool_item->p_pool = p_pool;
301 /* Insert the new item in the free list, traping for failure. */
302 cl_qlist_insert_head(&p_pool->free_list,
303 &p_pool_item->list_item);
305 p_pool->num_objects++;
307 /* move the pointer to the next item */
308 p_objects += obj_size;
314 cl_pool_item_t *cl_qcpool_get(IN cl_qcpool_t * const p_pool)
316 cl_list_item_t *p_list_item;
319 CL_ASSERT(p_pool->state == CL_INITIALIZED);
321 if (cl_is_qlist_empty(&p_pool->free_list)) {
323 * No object is available.
324 * Return NULL if the user does not want automatic growth.
326 if (!p_pool->grow_size)
329 /* We ran out of elements. Get more */
330 cl_qcpool_grow(p_pool, p_pool->grow_size);
332 * We may not have gotten everything we wanted but we might have
335 if (cl_is_qlist_empty(&p_pool->free_list))
339 p_list_item = cl_qlist_remove_head(&p_pool->free_list);
340 /* OK, at this point we have an object */
341 CL_ASSERT(p_list_item != cl_qlist_end(&p_pool->free_list));
342 return ((cl_pool_item_t *) p_list_item);
345 cl_pool_item_t *cl_qcpool_get_tail(IN cl_qcpool_t * const p_pool)
347 cl_list_item_t *p_list_item;
350 CL_ASSERT(p_pool->state == CL_INITIALIZED);
352 if (cl_is_qlist_empty(&p_pool->free_list)) {
354 * No object is available.
355 * Return NULL if the user does not want automatic growth.
357 if (!p_pool->grow_size)
360 /* We ran out of elements. Get more */
361 cl_qcpool_grow(p_pool, p_pool->grow_size);
363 * We may not have gotten everything we wanted but we might have
366 if (cl_is_qlist_empty(&p_pool->free_list))
370 p_list_item = cl_qlist_remove_tail(&p_pool->free_list);
371 /* OK, at this point we have an object */
372 CL_ASSERT(p_list_item != cl_qlist_end(&p_pool->free_list));
373 return ((cl_pool_item_t *) p_list_item);
377 * IMPLEMENTATION OF QUICK GROW POOL
381 * Callback to translate quick composite to quick grow pool
382 * initializer callback.
384 static cl_status_t __cl_qpool_init_cb(IN void **const p_comp_array,
385 IN const uint32_t num_components,
386 IN void *const context,
387 OUT cl_pool_item_t ** const pp_pool_item)
389 cl_qpool_t *p_pool = (cl_qpool_t *) context;
392 CL_ASSERT(p_pool->pfn_init);
393 CL_ASSERT(num_components == 1);
395 UNUSED_PARAM(num_components);
397 return (p_pool->pfn_init(p_comp_array[0], (void *)p_pool->context,
402 * Callback to translate quick composite to quick grow pool
403 * destructor callback.
405 static void __cl_qpool_dtor_cb(IN const cl_pool_item_t * const p_pool_item,
406 IN void *const context)
408 cl_qpool_t *p_pool = (cl_qpool_t *) context;
411 CL_ASSERT(p_pool->pfn_dtor);
413 p_pool->pfn_dtor(p_pool_item, (void *)p_pool->context);
416 void cl_qpool_construct(IN cl_qpool_t * const p_pool)
418 memset(p_pool, 0, sizeof(cl_qpool_t));
420 cl_qcpool_construct(&p_pool->qcpool);
423 cl_status_t cl_qpool_init(IN cl_qpool_t * const p_pool,
424 IN const size_t min_size, IN const size_t max_size,
425 IN const size_t grow_size,
426 IN const size_t object_size,
427 IN cl_pfn_qpool_init_t pfn_initializer OPTIONAL,
428 IN cl_pfn_qpool_dtor_t pfn_destructor OPTIONAL,
429 IN const void *const context)
435 p_pool->pfn_init = pfn_initializer; /* may be NULL */
436 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
437 p_pool->context = context;
439 status = cl_qcpool_init(&p_pool->qcpool, min_size, max_size, grow_size,
441 pfn_initializer ? __cl_qpool_init_cb : NULL,
442 pfn_destructor ? __cl_qpool_dtor_cb : NULL,
449 * IMPLEMENTATION OF COMPOSITE POOL
453 * Callback to translate quick composite to compsite pool
454 * initializer callback.
456 static cl_status_t __cl_cpool_init_cb(IN void **const p_comp_array,
457 IN const uint32_t num_components,
458 IN void *const context,
459 OUT cl_pool_item_t ** const pp_pool_item)
461 cl_cpool_t *p_pool = (cl_cpool_t *) context;
462 cl_pool_obj_t *p_pool_obj;
463 cl_status_t status = CL_SUCCESS;
468 * Set our pointer to the list item, which is stored at the beginning of
469 * the first component.
471 p_pool_obj = (cl_pool_obj_t *) p_comp_array[0];
472 /* Set the pool item pointer for the caller. */
473 *pp_pool_item = &p_pool_obj->pool_item;
475 /* Calculate the pointer to the user's first component. */
476 p_comp_array[0] = ((uint8_t *) p_comp_array[0]) + sizeof(cl_pool_obj_t);
479 * Set the object pointer in the pool object to point to the first of the
482 p_pool_obj->p_object = p_comp_array[0];
484 /* Invoke the user's constructor callback. */
485 if (p_pool->pfn_init) {
486 status = p_pool->pfn_init(p_comp_array, num_components,
487 (void *)p_pool->context);
494 * Callback to translate quick composite to composite pool
495 * destructor callback.
497 static void __cl_cpool_dtor_cb(IN const cl_pool_item_t * const p_pool_item,
498 IN void *const context)
500 cl_cpool_t *p_pool = (cl_cpool_t *) context;
503 CL_ASSERT(p_pool->pfn_dtor);
504 CL_ASSERT(((cl_pool_obj_t *) p_pool_item)->p_object);
506 /* Invoke the user's destructor callback. */
507 p_pool->pfn_dtor((void *)((cl_pool_obj_t *) p_pool_item)->p_object,
508 (void *)p_pool->context);
511 void cl_cpool_construct(IN cl_cpool_t * const p_pool)
515 memset(p_pool, 0, sizeof(cl_cpool_t));
517 cl_qcpool_construct(&p_pool->qcpool);
520 cl_status_t cl_cpool_init(IN cl_cpool_t * const p_pool,
521 IN const size_t min_size, IN const size_t max_size,
522 IN const size_t grow_size,
523 IN size_t * const component_sizes,
524 IN const uint32_t num_components,
525 IN cl_pfn_cpool_init_t pfn_initializer OPTIONAL,
526 IN cl_pfn_cpool_dtor_t pfn_destructor OPTIONAL,
527 IN const void *const context)
532 CL_ASSERT(num_components);
533 CL_ASSERT(component_sizes);
535 /* Add the size of the pool object to the first component. */
536 component_sizes[0] += sizeof(cl_pool_obj_t);
538 /* Store callback function pointers. */
539 p_pool->pfn_init = pfn_initializer; /* may be NULL */
540 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
541 p_pool->context = context;
543 status = cl_qcpool_init(&p_pool->qcpool, min_size, max_size, grow_size,
544 component_sizes, num_components,
546 pfn_destructor ? __cl_cpool_dtor_cb : NULL,
549 /* Restore the original value of the first component. */
550 component_sizes[0] -= sizeof(cl_pool_obj_t);
556 * IMPLEMENTATION OF GROW POOL
560 * Callback to translate quick composite to grow pool constructor callback.
562 static cl_status_t __cl_pool_init_cb(IN void **const pp_obj,
563 IN const uint32_t count,
564 IN void *const context,
565 OUT cl_pool_item_t ** const pp_pool_item)
567 cl_pool_t *p_pool = (cl_pool_t *) context;
568 cl_pool_obj_t *p_pool_obj;
569 cl_status_t status = CL_SUCCESS;
573 CL_ASSERT(count == 1);
578 * Set our pointer to the list item, which is stored at the beginning of
579 * the first component.
581 p_pool_obj = (cl_pool_obj_t *) * pp_obj;
582 *pp_pool_item = &p_pool_obj->pool_item;
584 /* Calculate the pointer to the user's first component. */
585 *pp_obj = ((uint8_t *) * pp_obj) + sizeof(cl_pool_obj_t);
588 * Set the object pointer in the pool item to point to the first of the
591 p_pool_obj->p_object = *pp_obj;
593 /* Invoke the user's constructor callback. */
594 if (p_pool->pfn_init)
595 status = p_pool->pfn_init(*pp_obj, (void *)p_pool->context);
601 * Callback to translate quick composite to grow pool destructor callback.
603 static void __cl_pool_dtor_cb(IN const cl_pool_item_t * const p_pool_item,
604 IN void *const context)
606 cl_pool_t *p_pool = (cl_pool_t *) context;
609 CL_ASSERT(p_pool->pfn_dtor);
610 CL_ASSERT(((cl_pool_obj_t *) p_pool_item)->p_object);
612 /* Invoke the user's destructor callback. */
613 p_pool->pfn_dtor((void *)((cl_pool_obj_t *) p_pool_item)->p_object,
614 (void *)p_pool->context);
617 void cl_pool_construct(IN cl_pool_t * const p_pool)
621 memset(p_pool, 0, sizeof(cl_pool_t));
623 cl_qcpool_construct(&p_pool->qcpool);
626 cl_status_t cl_pool_init(IN cl_pool_t * const p_pool, IN const size_t min_size,
627 IN const size_t max_size, IN const size_t grow_size,
628 IN const size_t object_size,
629 IN cl_pfn_pool_init_t pfn_initializer OPTIONAL,
630 IN cl_pfn_pool_dtor_t pfn_destructor OPTIONAL,
631 IN const void *const context)
638 /* Add the size of the list item to the first component. */
639 total_size = object_size + sizeof(cl_pool_obj_t);
641 /* Store callback function pointers. */
642 p_pool->pfn_init = pfn_initializer; /* may be NULL */
643 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
644 p_pool->context = context;
647 * We need an initializer in all cases for quick composite pool, since
648 * the user pointer must be manipulated to hide the prefixed cl_pool_obj_t.
650 status = cl_qcpool_init(&p_pool->qcpool, min_size, max_size, grow_size,
651 &total_size, 1, __cl_pool_init_cb,
652 pfn_destructor ? __cl_pool_dtor_cb : NULL,