- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# /space/home/slher/rel/Beken/v6_4_5/macsw/modules/common/src/co_pool.c

Go to the documentation of this file.

    00001
    00020 /*
    00021  * INCLUDE FILES
    00022  ****************************************************************************************
    00023  */
    00024 // for NULL
    00025 #include <stddef.h>
    00026 #include "co_pool.h"
    00027 // for assertions
    00028 #include "dbg_assert.h"
    00029
    00030 /*
    00031  * FUNCTION DEFINITIONS
    00032  ****************************************************************************************
    00033  */
    00034 void co_pool_init(struct co_pool *pool,
    00035                   struct co_pool_hdr *pool_hdr,
    00036                   void *elements,
    00037                   uint32_t elem_size,
    00038                   uint32_t elem_cnt)
    00039 {
    00040     uint32_t i;
    00041
    00042     // sanity check: check that elements are aligned on word
    00043     ASSERT_ERR((((uint32_t)elements) & 3) == 0);
    00044     ASSERT_ERR((elem_size & 3) == 0);
    00045
    00046
    00047     // build the linked list of elementary descriptors and make them point to the
    00048     // associated pool elements.
    00049     for (i = 0; i < elem_cnt; i++)
    00050     {
    00051         pool_hdr[i].next = &(pool_hdr[i + 1]);
    00052
    00053         // initialize the pointer to the element
    00054         pool_hdr[i].element = (void*)((uint32_t)elements + (i * elem_size));
    00055     }
    00056
    00057     // the last element in the list points to NULL
    00058     pool_hdr[elem_cnt - 1].next = NULL;
    00059
    00060     // initialize the pool
    00061     pool->first_ptr = pool_hdr;
    00062     pool->freecnt = elem_cnt;
    00063 }
    00064
    00065
    00066
    00067 struct co_pool_hdr *co_pool_alloc(struct co_pool *pool,
    00068                                   uint32_t nbelem)
    00069 {
    00070     uint32_t i;
    00071     struct co_pool_hdr *desclist_ptr, *currentdesc_ptr, *nextdesc_ptr;
    00072
    00073     // sanity check
    00074     ASSERT_ERR(nbelem != 0);
    00075
    00076     // if there aren't enough free elements in the pool, return NULL
    00077     if (pool->freecnt < nbelem)
    00078     {
    00079         return NULL;
    00080     }
    00081     // in the other cases, the function returns a pointer to the list of descriptors that
    00082     // have been allocated.
    00083     desclist_ptr = pool->first_ptr;
    00084     nextdesc_ptr = pool->first_ptr;
    00085     currentdesc_ptr = pool->first_ptr;
    00086
    00087     // jump over nbelem elements of the pool
    00088     for (i = 0; i < nbelem; i++)
    00089     {
    00090         currentdesc_ptr = nextdesc_ptr;
    00091         nextdesc_ptr = currentdesc_ptr->next ;
    00092     }
    00093
    00094     // Update the free list pointer value as well as the remaining number of element in
    00095     // the pool
    00096     pool->first_ptr = currentdesc_ptr->next;
    00097     pool->freecnt -= nbelem;
    00098
    00099     // The last element in the returned list points to NULL
    00100     currentdesc_ptr->next = NULL;
    00101
    00102     return desclist_ptr;
    00103 }
    00104
    00105
    00106 void co_pool_free(struct co_pool *pool,
    00107                   struct co_pool_hdr *elements,
    00108                   uint32_t nbelem)
    00109 {
    00110     uint32_t i;
    00111     struct co_pool_hdr *descfreelist_ptr, *currentdesc_ptr, *lastdesc_ptr;
    00112
    00113     // sanity check: passed parameters are OK
    00114     ASSERT_ERR(nbelem != 0);
    00115     ASSERT_ERR(elements);
    00116
    00117     descfreelist_ptr = pool->first_ptr;
    00118     lastdesc_ptr = elements;
    00119
    00120     pool->first_ptr = elements;
    00121     pool->freecnt += nbelem;
    00122
    00123     // move to the last element of the freed list
    00124     for (i = 0; i < (nbelem - 1); i++)
    00125     {
    00126         currentdesc_ptr = lastdesc_ptr;
    00127         lastdesc_ptr = currentdesc_ptr->next ;
    00128     }
    00129
    00130     // Hook the list of released descriptor onto the free list of descriptor.
    00131     lastdesc_ptr->next = descfreelist_ptr;
    00132 }
    00133