2 * Copyright (c) 2004, 2005 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 * Declaration of the composite pool.
39 * The composite pool managers a pool of composite objects. A composite object is an object
40 * that is made of multiple sub objects.
41 * The pool can grow to meet demand, limited only by system memory.
44 #ifndef _CL_COMP_POOL_H_
45 #define _CL_COMP_POOL_H_
47 #include <complib/cl_qcomppool.h>
50 # define BEGIN_C_DECLS extern "C" {
51 # define END_C_DECLS }
52 #else /* !__cplusplus */
53 # define BEGIN_C_DECLS
55 #endif /* __cplusplus */
58 /****h* Component Library/Composite Pool
63 * The Composite Pool provides a self-contained and self-sustaining pool of
64 * user defined composite objects.
66 * A composite object is an object that is composed of one or more
67 * sub-objects, each of which needs to be treated separately for
68 * initialization. Objects can be retrieved from the pool as long as there
69 * is memory in the system.
71 * To aid in object oriented design, the composite pool provides the user
72 * the ability to specify callbacks that are invoked for each object for
73 * construction, initialization, and destruction. Constructor and destructor
74 * callback functions may not fail.
76 * A composite pool does not return memory to the system as the user returns
77 * objects to the pool. The only method of returning memory to the system is
78 * to destroy the pool.
80 * The composite pool functions operates on a cl_cpool_t structure which
81 * should be treated as opaque and should be manipulated only through the
89 * cl_pfn_cpool_init_t, cl_pfn_cpool_dtor_t
91 * Initialization/Destruction:
92 * cl_cpool_construct, cl_cpool_init, cl_cpool_destroy
95 * cl_cpool_get, cl_cpool_put, cl_cpool_grow
98 * cl_is_cpool_inited, cl_cpool_count
100 /****d* Component Library: Composite Pool/cl_pfn_cpool_init_t
102 * cl_pfn_cpool_init_t
105 * The cl_pfn_cpool_init_t function type defines the prototype for
106 * functions used as initializers for objects being allocated by a
112 (*cl_pfn_cpool_init_t) (IN void **const p_comp_array,
113 IN const uint32_t num_components, IN void *context);
117 * [in] Pointer to an object to initialize.
120 * [in] Context provided in a call to cl_cpool_init.
123 * Return CL_SUCCESS to indicates that initialization of the object
124 * was successful and that initialization of further objects may continue.
126 * Other cl_status_t values will be returned by cl_cpool_init
130 * This function type is provided as function prototype reference for
131 * the function provided by the user as an optional parameter to the
132 * cl_cpool_init function.
134 * The initializer is invoked once per allocated object, allowing the user
135 * to chain components to form a composite object and perform any necessary
136 * initialization. Returning a status other than CL_SUCCESS aborts a grow
137 * operation, initiated either through cl_cpool_init or cl_cpool_grow, and
138 * causes the initiating function to fail. Any non-CL_SUCCESS status will
139 * be returned by the function that initiated the grow operation.
141 * All memory for the requested number of components is pre-allocated.
143 * When later performing a cl_cpool_get call, the return value is a pointer
144 * to the first component.
147 * Composite Pool, cl_cpool_init, cl_cpool_grow
150 /****d* Component Library: Composite Pool/cl_pfn_cpool_dtor_t
152 * cl_pfn_cpool_dtor_t
155 * The cl_pfn_cpool_dtor_t function type defines the prototype for
156 * functions used as destructor for objects being deallocated by a
162 (*cl_pfn_cpool_dtor_t) (IN void *const p_object, IN void *context);
166 * [in] Pointer to an object to destruct.
169 * [in] Context provided in the call to cl_cpool_init.
172 * This function does not return a value.
175 * This function type is provided as function prototype reference for
176 * the function provided by the user as an optional parameter to the
177 * cl_cpool_init function.
179 * The destructor is invoked once per allocated object, allowing the user
180 * to perform any necessary cleanup. Users should not attempt to deallocate
181 * the memory for the composite object, as the composite pool manages
182 * object allocation and deallocation.
185 * Composite Pool, cl_cpool_init
188 /****s* Component Library: Composite Pool/cl_cpool_t
193 * Composite pool structure.
195 * The cl_cpool_t structure should be treated as opaque and should be
196 * manipulated only through the provided functions.
200 typedef struct _cl_cpool {
202 cl_pfn_cpool_init_t pfn_init;
203 cl_pfn_cpool_dtor_t pfn_dtor;
209 * Quick composite pool that manages all objects.
212 * Pointer to the user's initializer callback, used by the pool
213 * to translate the quick composite pool's initializer callback to
214 * a composite pool initializer callback.
217 * Pointer to the user's destructor callback, used by the pool
218 * to translate the quick composite pool's destructor callback to
219 * a composite pool destructor callback.
222 * User's provided context for callback functions, used by the pool
223 * to when invoking callbacks.
229 /****f* Component Library: Composite Pool/cl_cpool_construct
234 * The cl_cpool_construct function constructs a composite pool.
238 void cl_cpool_construct(IN cl_cpool_t * const p_pool);
242 * [in] Pointer to a cl_cpool_t structure whose state to initialize.
245 * This function does not return a value.
248 * Allows calling cl_pool_init, cl_cpool_destroy, cl_is_cpool_inited.
250 * Calling cl_cpool_construct is a prerequisite to calling any other
251 * composite pool function except cl_cpool_init.
254 * Composite Pool, cl_cpool_init, cl_cpool_destroy, cl_is_cpool_inited
257 /****f* Component Library: Composite Pool/cl_is_cpool_inited
262 * The cl_is_cpool_inited function returns whether a composite pool was
263 * successfully initialized.
267 static inline boolean_t cl_is_cpool_inited(IN const cl_cpool_t * const p_pool)
269 /* CL_ASSERT that a non-null pointer is provided. */
271 return (cl_is_qcpool_inited(&p_pool->qcpool));
277 * [in] Pointer to a cl_cpool_t structure whose initialization state
281 * TRUE if the composite pool was initialized successfully.
286 * Allows checking the state of a composite pool to determine if invoking
287 * member functions is appropriate.
293 /****f* Component Library: Composite Pool/cl_cpool_init
298 * The cl_cpool_init function initializes a composite pool for use.
303 cl_cpool_init(IN cl_cpool_t * const p_pool,
304 IN const size_t min_size,
305 IN const size_t max_size,
306 IN const size_t grow_size,
307 IN size_t * const component_sizes,
308 IN const uint32_t num_components,
309 IN cl_pfn_cpool_init_t pfn_initializer OPTIONAL,
310 IN cl_pfn_cpool_dtor_t pfn_destructor OPTIONAL,
311 IN const void *const context);
315 * [in] Pointer to a cl_cpool_t structure to initialize.
318 * [in] Minimum number of objects that the pool should support. All
319 * necessary allocations to allow storing the minimum number of items
320 * are performed at initialization time, and all necessary callbacks
321 * successfully invoked.
324 * [in] Maximum number of objects to which the pool is allowed to grow.
325 * A value of zero specifies no maximum.
328 * [in] Number of objects to allocate when incrementally growing the pool.
329 * A value of zero disables automatic growth.
332 * [in] Pointer to the first entry in an array of sizes describing,
333 * in order, the sizes of the components that make up a composite object.
336 * [in] Number of components that make up a composite object.
339 * [in] Initialization callback to invoke for every new object when
340 * growing the pool. This parameter may be NULL only if the objects
341 * stored in the composite pool consist of only one component.
342 * See the cl_pfn_cpool_init function type declaration for details
343 * about the callback function.
346 * [in] Destructor callback to invoke for every object before memory for
347 * that object is freed. This parameter is optional and may be NULL.
348 * See the cl_pfn_cpool_dtor function type declaration for details
349 * about the callback function.
352 * [in] Value to pass to the callback functions to provide context.
355 * CL_SUCCESS if the composite pool was initialized successfully.
357 * CL_INSUFFICIENT_MEMORY if there was not enough memory to initialize the
360 * CL_INVALID_SETTING if a NULL constructor was provided for composite objects
361 * consisting of more than one component. Also returns CL_INVALID_SETTING if
362 * the maximum size is non-zero and less than the minimum size.
364 * Other cl_status_t value returned by optional initialization callback function
365 * specified by the pfn_initializer parameter.
368 * cl_cpool_init initializes, and if necessary, grows the pool to
369 * the capacity desired.
372 * Composite Pool, cl_cpool_construct, cl_cpool_destroy,
373 * cl_cpool_get, cl_cpool_put, cl_cpool_grow,
374 * cl_cpool_count, cl_pfn_cpool_ctor_t, cl_pfn_cpool_init_t,
375 * cl_pfn_cpool_dtor_t
378 /****f* Component Library: Composite Pool/cl_cpool_destroy
383 * The cl_cpool_destroy function destroys a composite pool.
387 static inline void cl_cpool_destroy(IN cl_cpool_t * const p_pool)
391 cl_qcpool_destroy(&p_pool->qcpool);
397 * [in] Pointer to a cl_cpool_t structure to destroy.
400 * This function does not return a value.
403 * All memory allocated for composite objects is freed. The destructor
404 * callback, if any, will be invoked for every allocated object. Further
405 * operations on the composite pool should not be attempted after
406 * cl_cpool_destroy is invoked.
408 * This function should only be called after a call to cl_cpool_construct.
410 * In a debug build, cl_cpool_destroy asserts that all objects are in
414 * Composite Pool, cl_cpool_construct, cl_cpool_init
417 /****f* Component Library: Composite Pool/cl_cpool_count
422 * The cl_cpool_count function returns the number of available objects
423 * in a composite pool.
427 static inline size_t cl_cpool_count(IN cl_cpool_t * const p_pool)
430 return (cl_qcpool_count(&p_pool->qcpool));
436 * [in] Pointer to a cl_cpool_t structure for which the number of
437 * available objects is requested.
440 * Returns the number of objects available in the specified
447 /****f* Component Library: Composite Pool/cl_cpool_get
452 * The cl_cpool_get function retrieves an object from a
457 static inline void *cl_cpool_get(IN cl_cpool_t * const p_pool)
459 cl_pool_obj_t *p_pool_obj;
463 p_pool_obj = (cl_pool_obj_t *) cl_qcpool_get(&p_pool->qcpool);
467 CL_ASSERT(p_pool_obj->p_object);
468 return ((void *)p_pool_obj->p_object);
474 * [in] Pointer to a cl_cpool_t structure from which to retrieve
478 * Returns a pointer to the first component of a composite object.
480 * Returns NULL if the pool is empty and can not be grown automatically.
483 * cl_cpool_get returns the object at the head of the pool. If the pool is
484 * empty, it is automatically grown to accommodate this request unless the
485 * grow_size parameter passed to the cl_cpool_init function was zero.
488 * Composite Pool, cl_cpool_get_tail, cl_cpool_put, cl_cpool_grow,
492 /****f* Component Library: Composite Pool/cl_cpool_put
497 * The cl_cpool_put function returns an object to a composite pool.
502 cl_cpool_put(IN cl_cpool_t * const p_pool, IN void *const p_object)
504 cl_pool_obj_t *p_pool_obj;
509 /* Calculate the offset to the list object representing this object. */
510 p_pool_obj = (cl_pool_obj_t *)
511 (((uint8_t *) p_object) - sizeof(cl_pool_obj_t));
513 /* good sanity check */
514 CL_ASSERT(p_pool_obj->p_object == p_object);
516 cl_qcpool_put(&p_pool->qcpool, &p_pool_obj->pool_item);
522 * [in] Pointer to a cl_cpool_t structure to which to return
526 * [in] Pointer to the first component of an object to return to the pool.
529 * This function does not return a value.
532 * cl_cpool_put places the returned object at the head of the pool.
534 * The object specified by the p_object parameter must have been
535 * retrieved from the pool by a previous call to cl_cpool_get.
538 * Composite Pool, cl_cpool_put_tail, cl_cpool_get
541 /****f* Component Library: Composite Pool/cl_cpool_grow
546 * The cl_cpool_grow function grows a composite pool by
547 * the specified number of objects.
551 static inline cl_status_t
552 cl_cpool_grow(IN cl_cpool_t * const p_pool, IN const uint32_t obj_count)
555 return (cl_qcpool_grow(&p_pool->qcpool, obj_count));
561 * [in] Pointer to a cl_cpool_t structure whose capacity to grow.
564 * [in] Number of objects by which to grow the pool.
567 * CL_SUCCESS if the composite pool grew successfully.
569 * CL_INSUFFICIENT_MEMORY if there was not enough memory to grow the
572 * cl_status_t value returned by optional initialization callback function
573 * specified by the pfn_initializer parameter passed to the
574 * cl_cpool_init function.
577 * It is not necessary to call cl_cpool_grow if the pool is
578 * configured to grow automatically.
585 #endif /* _CL_COMP_POOL_H_ */