2 * Copyright (c) 2004-2006 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;
68 cl_qcpool_init(IN cl_qcpool_t * const p_pool,
69 IN const size_t min_size,
70 IN const size_t max_size,
71 IN const size_t grow_size,
72 IN const size_t * const component_sizes,
73 IN const uint32_t num_components,
74 IN cl_pfn_qcpool_init_t pfn_initializer OPTIONAL,
75 IN cl_pfn_qcpool_dtor_t pfn_destructor OPTIONAL,
76 IN const void *const context)
82 /* Must have a minimum of 1 component. */
83 CL_ASSERT(num_components);
84 /* A component size array is required. */
85 CL_ASSERT(component_sizes);
87 * If no initializer is provided, the first component must be large
88 * enough to hold a pool item.
90 CL_ASSERT(pfn_initializer ||
91 (component_sizes[0] >= sizeof(cl_pool_item_t)));
93 cl_qcpool_construct(p_pool);
95 if (num_components > 1 && !pfn_initializer)
96 return (CL_INVALID_SETTING);
98 if (max_size && max_size < min_size)
99 return (CL_INVALID_SETTING);
102 * Allocate the array of component sizes and component pointers all
105 p_pool->component_sizes = (size_t *) malloc((sizeof(size_t) +
109 if (!p_pool->component_sizes)
110 return (CL_INSUFFICIENT_MEMORY);
112 memset(p_pool->component_sizes, 0,
113 (sizeof(size_t) + sizeof(void *)) * num_components);
115 /* Calculate the pointer to the array of pointers, used for callbacks. */
116 p_pool->p_components =
117 (void **)(p_pool->component_sizes + num_components);
119 /* Copy the user's sizes into our array for future use. */
120 memcpy(p_pool->component_sizes, component_sizes,
121 sizeof(component_sizes[0]) * num_components);
123 /* Store the number of components per object. */
124 p_pool->num_components = num_components;
126 /* Round up and store the size of the components. */
127 for (i = 0; i < num_components; i++) {
129 * We roundup each component size so that all components
130 * are aligned on a natural boundary.
132 p_pool->component_sizes[i] =
133 ROUNDUP(p_pool->component_sizes[i], sizeof(uintn_t));
136 p_pool->max_objects = max_size ? max_size : ~(size_t) 0;
137 p_pool->grow_size = grow_size;
139 /* Store callback function pointers. */
140 p_pool->pfn_init = pfn_initializer; /* may be NULL */
141 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
142 p_pool->context = context;
144 cl_qlist_init(&p_pool->alloc_list);
146 cl_qlist_init(&p_pool->free_list);
149 * We are now initialized. We change the initialized flag before
150 * growing since the grow function asserts that we are initialized.
152 p_pool->state = CL_INITIALIZED;
154 /* Allocate the minimum number of objects as requested. */
158 status = cl_qcpool_grow(p_pool, min_size);
159 /* Trap for error and cleanup if necessary. */
160 if (status != CL_SUCCESS)
161 cl_qcpool_destroy(p_pool);
166 void cl_qcpool_destroy(IN cl_qcpool_t * const p_pool)
168 /* CL_ASSERT that a non-NULL pointer was provided. */
170 /* CL_ASSERT that we are in a valid state (not uninitialized memory). */
171 CL_ASSERT(cl_is_state_valid(p_pool->state));
173 if (p_pool->state == CL_INITIALIZED) {
175 * Assert if the user hasn't put everything back in the pool
176 * before destroying it
177 * if they haven't, then most likely they are still using memory
178 * that will be freed, and the destructor will not be called!
181 /* but we do not want "free" version to assert on this one */
182 CL_ASSERT(cl_qcpool_count(p_pool) == p_pool->num_objects);
184 /* call the user's destructor for each object in the pool */
185 if (p_pool->pfn_dtor) {
186 while (!cl_is_qlist_empty(&p_pool->free_list)) {
187 p_pool->pfn_dtor((cl_pool_item_t *)
188 cl_qlist_remove_head(&p_pool->
190 (void *)p_pool->context);
193 cl_qlist_remove_all(&p_pool->free_list);
196 /* Free all allocated memory blocks. */
197 while (!cl_is_qlist_empty(&p_pool->alloc_list))
198 free(cl_qlist_remove_head(&p_pool->alloc_list));
200 if (p_pool->component_sizes) {
201 free(p_pool->component_sizes);
202 p_pool->component_sizes = NULL;
206 p_pool->state = CL_UNINITIALIZED;
209 cl_status_t cl_qcpool_grow(IN cl_qcpool_t * const p_pool, IN size_t obj_count)
211 cl_status_t status = CL_SUCCESS;
213 cl_pool_item_t *p_pool_item;
218 CL_ASSERT(p_pool->state == CL_INITIALIZED);
219 CL_ASSERT(obj_count);
221 /* Validate that growth is possible. */
222 if (p_pool->num_objects == p_pool->max_objects)
223 return (CL_INSUFFICIENT_MEMORY);
225 /* Cap the growth to the desired maximum. */
226 if (obj_count > (p_pool->max_objects - p_pool->num_objects))
227 obj_count = p_pool->max_objects - p_pool->num_objects;
229 /* Calculate the size of an object. */
231 for (i = 0; i < p_pool->num_components; i++)
232 obj_size += p_pool->component_sizes[i];
234 /* Allocate the buffer for the new objects. */
235 p_objects = (uint8_t *)
236 malloc(sizeof(cl_list_item_t) + (obj_size * obj_count));
238 /* Make sure the allocation succeeded. */
240 return (CL_INSUFFICIENT_MEMORY);
243 sizeof(cl_list_item_t) + (obj_size * obj_count));
245 /* Insert the allocation in our list. */
246 cl_qlist_insert_tail(&p_pool->alloc_list, (cl_list_item_t *) p_objects);
247 p_objects += sizeof(cl_list_item_t);
249 /* initialize the new elements and add them to the free list */
250 while (obj_count--) {
251 /* Setup the array of components for the current object. */
252 p_pool->p_components[0] = p_objects;
253 for (i = 1; i < p_pool->num_components; i++) {
254 /* Calculate the pointer to the next component. */
255 p_pool->p_components[i] =
256 (uint8_t *) p_pool->p_components[i - 1] +
257 p_pool->component_sizes[i - 1];
261 * call the user's initializer
264 if (p_pool->pfn_init) {
266 status = p_pool->pfn_init(p_pool->p_components,
267 p_pool->num_components,
268 (void *)p_pool->context,
270 if (status != CL_SUCCESS) {
272 * User initialization failed
273 * we may have only grown the pool by some partial amount
274 * Invoke the destructor for the object that failed
277 if (p_pool->pfn_dtor)
278 p_pool->pfn_dtor(p_pool_item,
282 /* Return the user's status. */
285 CL_ASSERT(p_pool_item);
288 * If no initializer is provided, assume that the pool item
289 * is stored at the beginning of the first component.
292 (cl_pool_item_t *) p_pool->p_components[0];
297 * Set the pool item's pool pointer to this pool so that we can
298 * check that items get returned to the correct pool.
300 p_pool_item->p_pool = p_pool;
303 /* Insert the new item in the free list, traping for failure. */
304 cl_qlist_insert_head(&p_pool->free_list,
305 &p_pool_item->list_item);
307 p_pool->num_objects++;
309 /* move the pointer to the next item */
310 p_objects += obj_size;
316 cl_pool_item_t *cl_qcpool_get(IN cl_qcpool_t * const p_pool)
318 cl_list_item_t *p_list_item;
321 CL_ASSERT(p_pool->state == CL_INITIALIZED);
323 if (cl_is_qlist_empty(&p_pool->free_list)) {
325 * No object is available.
326 * Return NULL if the user does not want automatic growth.
328 if (!p_pool->grow_size)
331 /* We ran out of elements. Get more */
332 cl_qcpool_grow(p_pool, p_pool->grow_size);
334 * We may not have gotten everything we wanted but we might have
337 if (cl_is_qlist_empty(&p_pool->free_list))
341 p_list_item = cl_qlist_remove_head(&p_pool->free_list);
342 /* OK, at this point we have an object */
343 CL_ASSERT(p_list_item != cl_qlist_end(&p_pool->free_list));
344 return ((cl_pool_item_t *) p_list_item);
347 cl_pool_item_t *cl_qcpool_get_tail(IN cl_qcpool_t * const p_pool)
349 cl_list_item_t *p_list_item;
352 CL_ASSERT(p_pool->state == CL_INITIALIZED);
354 if (cl_is_qlist_empty(&p_pool->free_list)) {
356 * No object is available.
357 * Return NULL if the user does not want automatic growth.
359 if (!p_pool->grow_size)
362 /* We ran out of elements. Get more */
363 cl_qcpool_grow(p_pool, p_pool->grow_size);
365 * We may not have gotten everything we wanted but we might have
368 if (cl_is_qlist_empty(&p_pool->free_list))
372 p_list_item = cl_qlist_remove_tail(&p_pool->free_list);
373 /* OK, at this point we have an object */
374 CL_ASSERT(p_list_item != cl_qlist_end(&p_pool->free_list));
375 return ((cl_pool_item_t *) p_list_item);
379 * IMPLEMENTATION OF QUICK GROW POOL
383 * Callback to translate quick composite to quick grow pool
384 * initializer callback.
387 __cl_qpool_init_cb(IN void **const p_comp_array,
388 IN const uint32_t num_components,
389 IN void *const context,
390 OUT cl_pool_item_t ** const pp_pool_item)
392 cl_qpool_t *p_pool = (cl_qpool_t *) context;
395 CL_ASSERT(p_pool->pfn_init);
396 CL_ASSERT(num_components == 1);
398 UNUSED_PARAM(num_components);
400 return (p_pool->pfn_init(p_comp_array[0], (void *)p_pool->context,
405 * Callback to translate quick composite to quick grow pool
406 * destructor callback.
409 __cl_qpool_dtor_cb(IN const cl_pool_item_t * const p_pool_item,
410 IN void *const context)
412 cl_qpool_t *p_pool = (cl_qpool_t *) context;
415 CL_ASSERT(p_pool->pfn_dtor);
417 p_pool->pfn_dtor(p_pool_item, (void *)p_pool->context);
420 void cl_qpool_construct(IN cl_qpool_t * const p_pool)
422 memset(p_pool, 0, sizeof(cl_qpool_t));
424 cl_qcpool_construct(&p_pool->qcpool);
428 cl_qpool_init(IN cl_qpool_t * const p_pool,
429 IN const size_t min_size,
430 IN const size_t max_size,
431 IN const size_t grow_size,
432 IN const size_t object_size,
433 IN cl_pfn_qpool_init_t pfn_initializer OPTIONAL,
434 IN cl_pfn_qpool_dtor_t pfn_destructor OPTIONAL,
435 IN const void *const context)
441 p_pool->pfn_init = pfn_initializer; /* may be NULL */
442 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
443 p_pool->context = context;
445 status = cl_qcpool_init(&p_pool->qcpool, min_size, max_size, grow_size,
447 pfn_initializer ? __cl_qpool_init_cb : NULL,
448 pfn_destructor ? __cl_qpool_dtor_cb : NULL,
455 * IMPLEMENTATION OF COMPOSITE POOL
459 * Callback to translate quick composite to compsite pool
460 * initializer callback.
463 __cl_cpool_init_cb(IN void **const p_comp_array,
464 IN const uint32_t num_components,
465 IN void *const context,
466 OUT cl_pool_item_t ** const pp_pool_item)
468 cl_cpool_t *p_pool = (cl_cpool_t *) context;
469 cl_pool_obj_t *p_pool_obj;
470 cl_status_t status = CL_SUCCESS;
475 * Set our pointer to the list item, which is stored at the beginning of
476 * the first component.
478 p_pool_obj = (cl_pool_obj_t *) p_comp_array[0];
479 /* Set the pool item pointer for the caller. */
480 *pp_pool_item = &p_pool_obj->pool_item;
482 /* Calculate the pointer to the user's first component. */
483 p_comp_array[0] = ((uint8_t *) p_comp_array[0]) + sizeof(cl_pool_obj_t);
486 * Set the object pointer in the pool object to point to the first of the
489 p_pool_obj->p_object = p_comp_array[0];
491 /* Invoke the user's constructor callback. */
492 if (p_pool->pfn_init) {
493 status = p_pool->pfn_init(p_comp_array, num_components,
494 (void *)p_pool->context);
501 * Callback to translate quick composite to composite pool
502 * destructor callback.
505 __cl_cpool_dtor_cb(IN const cl_pool_item_t * const p_pool_item,
506 IN void *const context)
508 cl_cpool_t *p_pool = (cl_cpool_t *) context;
511 CL_ASSERT(p_pool->pfn_dtor);
512 CL_ASSERT(((cl_pool_obj_t *) p_pool_item)->p_object);
514 /* Invoke the user's destructor callback. */
515 p_pool->pfn_dtor((void *)((cl_pool_obj_t *) p_pool_item)->p_object,
516 (void *)p_pool->context);
519 void cl_cpool_construct(IN cl_cpool_t * const p_pool)
523 memset(p_pool, 0, sizeof(cl_cpool_t));
525 cl_qcpool_construct(&p_pool->qcpool);
529 cl_cpool_init(IN cl_cpool_t * const p_pool,
530 IN const size_t min_size,
531 IN const size_t max_size,
532 IN const size_t grow_size,
533 IN size_t * const component_sizes,
534 IN const uint32_t num_components,
535 IN cl_pfn_cpool_init_t pfn_initializer OPTIONAL,
536 IN cl_pfn_cpool_dtor_t pfn_destructor OPTIONAL,
537 IN const void *const context)
542 CL_ASSERT(num_components);
543 CL_ASSERT(component_sizes);
545 /* Add the size of the pool object to the first component. */
546 component_sizes[0] += sizeof(cl_pool_obj_t);
548 /* Store callback function pointers. */
549 p_pool->pfn_init = pfn_initializer; /* may be NULL */
550 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
551 p_pool->context = context;
553 status = cl_qcpool_init(&p_pool->qcpool, min_size, max_size, grow_size,
554 component_sizes, num_components,
556 pfn_destructor ? __cl_cpool_dtor_cb : NULL,
559 /* Restore the original value of the first component. */
560 component_sizes[0] -= sizeof(cl_pool_obj_t);
566 * IMPLEMENTATION OF GROW POOL
570 * Callback to translate quick composite to grow pool constructor callback.
573 __cl_pool_init_cb(IN void **const pp_obj,
574 IN const uint32_t count,
575 IN void *const context,
576 OUT cl_pool_item_t ** const pp_pool_item)
578 cl_pool_t *p_pool = (cl_pool_t *) context;
579 cl_pool_obj_t *p_pool_obj;
580 cl_status_t status = CL_SUCCESS;
584 CL_ASSERT(count == 1);
589 * Set our pointer to the list item, which is stored at the beginning of
590 * the first component.
592 p_pool_obj = (cl_pool_obj_t *) * pp_obj;
593 *pp_pool_item = &p_pool_obj->pool_item;
595 /* Calculate the pointer to the user's first component. */
596 *pp_obj = ((uint8_t *) * pp_obj) + sizeof(cl_pool_obj_t);
599 * Set the object pointer in the pool item to point to the first of the
602 p_pool_obj->p_object = *pp_obj;
604 /* Invoke the user's constructor callback. */
605 if (p_pool->pfn_init)
606 status = p_pool->pfn_init(*pp_obj, (void *)p_pool->context);
612 * Callback to translate quick composite to grow pool destructor callback.
615 __cl_pool_dtor_cb(IN const cl_pool_item_t * const p_pool_item,
616 IN void *const context)
618 cl_pool_t *p_pool = (cl_pool_t *) context;
621 CL_ASSERT(p_pool->pfn_dtor);
622 CL_ASSERT(((cl_pool_obj_t *) p_pool_item)->p_object);
624 /* Invoke the user's destructor callback. */
625 p_pool->pfn_dtor((void *)((cl_pool_obj_t *) p_pool_item)->p_object,
626 (void *)p_pool->context);
629 void cl_pool_construct(IN cl_pool_t * const p_pool)
633 memset(p_pool, 0, sizeof(cl_pool_t));
635 cl_qcpool_construct(&p_pool->qcpool);
639 cl_pool_init(IN cl_pool_t * const p_pool,
640 IN const size_t min_size,
641 IN const size_t max_size,
642 IN const size_t grow_size,
643 IN const size_t object_size,
644 IN cl_pfn_pool_init_t pfn_initializer OPTIONAL,
645 IN cl_pfn_pool_dtor_t pfn_destructor OPTIONAL,
646 IN const void *const context)
653 /* Add the size of the list item to the first component. */
654 total_size = object_size + sizeof(cl_pool_obj_t);
656 /* Store callback function pointers. */
657 p_pool->pfn_init = pfn_initializer; /* may be NULL */
658 p_pool->pfn_dtor = pfn_destructor; /* may be NULL */
659 p_pool->context = context;
662 * We need an initializer in all cases for quick composite pool, since
663 * the user pointer must be manipulated to hide the prefixed cl_pool_obj_t.
665 status = cl_qcpool_init(&p_pool->qcpool, min_size, max_size, grow_size,
666 &total_size, 1, __cl_pool_init_cb,
667 pfn_destructor ? __cl_pool_dtor_cb : NULL,