Program Listing for File mpool.h

Return to documentation for file (rcsw/multithread/mpool.h)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcsw/multithread/csem.h"
#include "rcsw/multithread/mutex.h"
#include "rcsw/ds/ds.h"
#include "rcsw/ds/llist.h"

/*******************************************************************************
 * Structure Definitions
 ******************************************************************************/
struct mpool_params {
  dptr_t *meta;

  dptr_t *elements;

  size_t elt_size;

  size_t max_elts;

  uint32_t flags;
};

struct mpool {
  dptr_t           *elements;

  struct llist_node *nodes;

  struct llist      free;

  struct llist      alloc;

  int               *refs;

  size_t            elt_size;

  size_t            max_elts;

  struct csem       slots_avail;

  struct mutex      mutex;

  uint32_t          flags;
};

/*******************************************************************************
 * API Functions
 ******************************************************************************/
BEGIN_C_DECLS

static inline size_t  mpool_meta_space(size_t max_elts) {
    /* x2 for free and alloc lists */
    return 2 * llist_meta_space(max_elts);
}

static inline size_t  mpool_element_space(size_t max_elts, size_t elt_size) {
    return ds_elt_space_with_meta(max_elts, elt_size);
}


static inline bool_t mpool_isfull(const struct mpool* const pool) {
    RCSW_FPC_NV(false, NULL != pool);
    return llist_isfull(&pool->alloc);
}

static inline bool_t mpool_isempty(const struct mpool* const pool) {
    RCSW_FPC_NV(false, NULL != pool);
    return llist_isempty(&pool->alloc);
}

static inline size_t mpool_size(const struct mpool* const pool) {
    RCSW_FPC_NV(0, NULL != pool);
    return llist_size(&pool->alloc);
}

static inline size_t mpool_capacity(const struct mpool* const pool) {
    RCSW_FPC_NV(0, NULL != pool);
    return pool->max_elts;
}


RCSW_API struct mpool*mpool_init(struct mpool * pool_in,
                                 const struct mpool_params * params) RCSW_WUR;

RCSW_API void mpool_destroy(struct mpool * the_pool);

RCSW_API void *mpool_req(struct mpool * the_pool);

RCSW_API status_t mpool_timedreq(struct mpool * the_pool,
                        const struct timespec* to,
                        void** chunk);

RCSW_API status_t mpool_release(struct mpool * the_pool, void * ptr);

RCSW_API status_t mpool_ref_add(struct mpool * the_pool, const void * ptr);

RCSW_API status_t mpool_ref_remove(struct mpool * the_pool,
                          const void * ptr);

RCSW_API int mpool_ref_query(struct mpool * the_pool, const void* ptr);

RCSW_API size_t mpool_ref_count(struct mpool * the_pool, const void* ptr);

END_C_DECLS