moved from mem
This commit was SVN r1279.
Этот коммит содержится в:
родитель
b1cb8b8cfa
Коммит
1491d25583
82
src/class/ompi_free_list.c
Обычный файл
82
src/class/ompi_free_list.c
Обычный файл
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* $HEADER$
|
||||||
|
*/
|
||||||
|
#include "ompi_config.h"
|
||||||
|
#include "class/ompi_free_list.h"
|
||||||
|
|
||||||
|
|
||||||
|
static void ompi_free_list_construct(ompi_free_list_t* fl);
|
||||||
|
static void ompi_free_list_destruct(ompi_free_list_t* fl);
|
||||||
|
|
||||||
|
|
||||||
|
ompi_class_t ompi_free_list_t_class = {
|
||||||
|
"ompi_free_list_t",
|
||||||
|
OBJ_CLASS(ompi_list_t),
|
||||||
|
(ompi_construct_t)ompi_free_list_construct,
|
||||||
|
(ompi_destruct_t)ompi_free_list_destruct
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void ompi_free_list_construct(ompi_free_list_t* fl)
|
||||||
|
{
|
||||||
|
OBJ_CONSTRUCT(&fl->fl_lock, ompi_mutex_t);
|
||||||
|
fl->fl_max_to_alloc = 0;
|
||||||
|
fl->fl_num_allocated = 0;
|
||||||
|
fl->fl_num_per_alloc = 0;
|
||||||
|
fl->fl_elem_size = 0;
|
||||||
|
fl->fl_elem_class = 0;
|
||||||
|
fl->fl_mpool = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ompi_free_list_destruct(ompi_free_list_t* fl)
|
||||||
|
{
|
||||||
|
OBJ_DESTRUCT(&fl->fl_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ompi_free_list_init(
|
||||||
|
ompi_free_list_t *flist,
|
||||||
|
size_t elem_size,
|
||||||
|
ompi_class_t* elem_class,
|
||||||
|
int num_elements_to_alloc,
|
||||||
|
int max_elements_to_alloc,
|
||||||
|
int num_elements_per_alloc,
|
||||||
|
mca_mpool_t* mpool)
|
||||||
|
{
|
||||||
|
flist->fl_elem_size = elem_size;
|
||||||
|
flist->fl_elem_class = elem_class;
|
||||||
|
flist->fl_max_to_alloc = max_elements_to_alloc;
|
||||||
|
flist->fl_num_allocated = 0;
|
||||||
|
flist->fl_num_per_alloc = num_elements_per_alloc;
|
||||||
|
flist->fl_mpool = mpool;
|
||||||
|
return ompi_free_list_grow(flist, num_elements_to_alloc);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ompi_free_list_grow(ompi_free_list_t* flist, size_t num_elements)
|
||||||
|
{
|
||||||
|
unsigned char* ptr;
|
||||||
|
size_t i;
|
||||||
|
if (flist->fl_max_to_alloc > 0 && flist->fl_num_allocated + num_elements > flist->fl_max_to_alloc)
|
||||||
|
return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
|
||||||
|
|
||||||
|
if (NULL != flist->fl_mpool)
|
||||||
|
ptr = (unsigned char*)flist->fl_mpool->mpool_alloc(flist->fl_mpool, num_elements * flist->fl_elem_size, 0);
|
||||||
|
else
|
||||||
|
ptr = malloc(num_elements * flist->fl_elem_size);
|
||||||
|
if(NULL == ptr)
|
||||||
|
return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
|
||||||
|
|
||||||
|
for(i=0; i<num_elements; i++) {
|
||||||
|
ompi_list_item_t* item = (ompi_list_item_t*)ptr;
|
||||||
|
if (NULL != flist->fl_elem_class) {
|
||||||
|
OBJ_CONSTRUCT_INTERNAL(item, flist->fl_elem_class);
|
||||||
|
}
|
||||||
|
ompi_list_append(&flist->super, item);
|
||||||
|
ptr += flist->fl_elem_size;
|
||||||
|
}
|
||||||
|
flist->fl_num_allocated += num_elements;
|
||||||
|
return OMPI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
68
src/class/ompi_free_list.h
Обычный файл
68
src/class/ompi_free_list.h
Обычный файл
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* $HEADER$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef OMPI_FREE_LIST_H
|
||||||
|
#define OMPI_FREE_LIST_H
|
||||||
|
|
||||||
|
#include "ompi_config.h"
|
||||||
|
#include "class/ompi_list.h"
|
||||||
|
#include "include/constants.h"
|
||||||
|
#include "mca/mpool/mpool.h"
|
||||||
|
|
||||||
|
extern ompi_class_t ompi_free_list_t_class;
|
||||||
|
struct mca_mem_pool_t;
|
||||||
|
|
||||||
|
|
||||||
|
struct ompi_free_list_t
|
||||||
|
{
|
||||||
|
ompi_list_t super;
|
||||||
|
int fl_max_to_alloc;
|
||||||
|
int fl_num_allocated;
|
||||||
|
int fl_num_per_alloc;
|
||||||
|
size_t fl_elem_size;
|
||||||
|
ompi_class_t* fl_elem_class;
|
||||||
|
mca_mpool_t* fl_mpool;
|
||||||
|
ompi_mutex_t fl_lock;
|
||||||
|
};
|
||||||
|
typedef struct ompi_free_list_t ompi_free_list_t;
|
||||||
|
|
||||||
|
|
||||||
|
int ompi_free_list_init(
|
||||||
|
ompi_free_list_t *flist,
|
||||||
|
size_t element_size,
|
||||||
|
ompi_class_t* element_class,
|
||||||
|
int num_elements_to_alloc,
|
||||||
|
int max_elements_to_alloc,
|
||||||
|
int num_elements_per_alloc,
|
||||||
|
mca_mpool_t*);
|
||||||
|
|
||||||
|
int ompi_free_list_grow(ompi_free_list_t* flist, size_t num_elements);
|
||||||
|
|
||||||
|
|
||||||
|
#define OMPI_FREE_LIST_GET(fl, item, rc) \
|
||||||
|
{ \
|
||||||
|
if(ompi_using_threads()) { \
|
||||||
|
ompi_mutex_lock(&((fl)->fl_lock)); \
|
||||||
|
item = ompi_list_remove_first(&((fl)->super)); \
|
||||||
|
if(NULL == item) { \
|
||||||
|
ompi_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
||||||
|
item = ompi_list_remove_first(&((fl)->super)); \
|
||||||
|
} \
|
||||||
|
ompi_mutex_unlock(&((fl)->fl_lock)); \
|
||||||
|
} else { \
|
||||||
|
item = ompi_list_remove_first(&((fl)->super)); \
|
||||||
|
if(NULL == item) { \
|
||||||
|
ompi_free_list_grow((fl), (fl)->fl_num_per_alloc); \
|
||||||
|
item = ompi_list_remove_first(&((fl)->super)); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
rc = (NULL == item) ? OMPI_ERR_TEMP_OUT_OF_RESOURCE : OMPI_SUCCESS; \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define OMPI_FREE_LIST_RETURN(fl, item) \
|
||||||
|
THREAD_SCOPED_LOCK(&((fl)->fl_lock), ompi_list_append(&((fl)->super), (item)));
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Загрузка…
x
Ссылка в новой задаче
Block a user