2004-06-15 23:46:26 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2006-04-20 23:53:45 +04:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-06-15 23:46:26 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-06-15 23:46:26 +04:00
|
|
|
#include "ompi_config.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "ompi/class/ompi_free_list.h"
|
|
|
|
#include "opal/sys/cache.h"
|
2006-03-16 20:27:54 +03:00
|
|
|
#include "opal/util/output.h"
|
2006-06-14 21:43:50 +04:00
|
|
|
#include "ompi/mca/mpool/mpool.h"
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
static inline size_t align_to(size_t val, size_t alignment);
|
|
|
|
static inline size_t align_to(size_t val, size_t alignment)
|
|
|
|
{
|
|
|
|
size_t mod;
|
|
|
|
|
|
|
|
if(0 == alignment)
|
|
|
|
return val;
|
|
|
|
|
|
|
|
mod = val % alignment;
|
|
|
|
|
|
|
|
if(mod)
|
|
|
|
val += (alignment - mod);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2004-06-15 23:46:26 +04:00
|
|
|
static void ompi_free_list_construct(ompi_free_list_t* fl);
|
|
|
|
static void ompi_free_list_destruct(ompi_free_list_t* fl);
|
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
OBJ_CLASS_INSTANCE(ompi_free_list_t, opal_atomic_lifo_t,
|
|
|
|
ompi_free_list_construct, ompi_free_list_destruct);
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2005-09-03 23:46:44 +04:00
|
|
|
struct ompi_free_list_memory_t {
|
|
|
|
opal_list_item_t super;
|
|
|
|
mca_mpool_base_registration_t *registration;
|
|
|
|
};
|
|
|
|
typedef struct ompi_free_list_memory_t ompi_free_list_memory_t;
|
|
|
|
static OBJ_CLASS_INSTANCE(ompi_free_list_memory_t,
|
|
|
|
opal_list_item_t,
|
|
|
|
NULL, NULL);
|
|
|
|
|
2006-06-12 20:44:00 +04:00
|
|
|
OBJ_CLASS_INSTANCE(ompi_free_list_item_t,
|
|
|
|
opal_list_item_t,
|
|
|
|
NULL, NULL);
|
2004-06-15 23:46:26 +04:00
|
|
|
|
|
|
|
static void ompi_free_list_construct(ompi_free_list_t* fl)
|
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CONSTRUCT(&fl->fl_lock, opal_mutex_t);
|
2005-09-02 20:00:42 +04:00
|
|
|
OBJ_CONSTRUCT(&fl->fl_condition, opal_condition_t);
|
2004-06-15 23:46:26 +04:00
|
|
|
fl->fl_max_to_alloc = 0;
|
|
|
|
fl->fl_num_allocated = 0;
|
|
|
|
fl->fl_num_per_alloc = 0;
|
2004-10-19 20:56:45 +04:00
|
|
|
fl->fl_num_waiting = 0;
|
2006-07-20 18:39:05 +04:00
|
|
|
fl->fl_elem_size = sizeof(ompi_free_list_item_t);
|
|
|
|
fl->fl_elem_class = OBJ_CLASS(ompi_free_list_item_t);
|
|
|
|
fl->fl_header_space = 0;
|
|
|
|
fl->fl_alignment = 0;
|
2004-06-15 23:46:26 +04:00
|
|
|
fl->fl_mpool = 0;
|
2005-09-03 23:46:44 +04:00
|
|
|
OBJ_CONSTRUCT(&(fl->fl_allocations), opal_list_t);
|
2004-06-15 23:46:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ompi_free_list_destruct(ompi_free_list_t* fl)
|
|
|
|
{
|
2005-09-03 23:46:44 +04:00
|
|
|
opal_list_item_t *item;
|
|
|
|
|
2006-03-17 21:46:48 +03:00
|
|
|
#if 0 && OMPI_ENABLE_DEBUG
|
2006-03-16 20:27:54 +03:00
|
|
|
if(opal_list_get_size(&fl->super) != fl->fl_num_allocated) {
|
|
|
|
opal_output(0, "ompi_free_list: %d allocated %d returned: %s:%d\n",
|
|
|
|
fl->fl_num_allocated, opal_list_get_size(&fl->super),
|
|
|
|
fl->super.super.cls_init_file_name, fl->super.super.cls_init_lineno);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-31 04:38:11 +04:00
|
|
|
if (NULL != fl->fl_mpool) {
|
2006-03-31 07:21:28 +04:00
|
|
|
ompi_free_list_memory_t *fl_mem;
|
|
|
|
|
2006-03-31 04:38:11 +04:00
|
|
|
while (NULL != (item = opal_list_remove_first(&(fl->fl_allocations)))) {
|
|
|
|
/* destruct the item (we constructed it), then free the memory chunk */
|
|
|
|
OBJ_DESTRUCT(item);
|
2006-03-31 07:21:28 +04:00
|
|
|
fl_mem = (ompi_free_list_memory_t*) item;
|
2005-09-03 23:46:44 +04:00
|
|
|
fl->fl_mpool->mpool_free(fl->fl_mpool, item, fl_mem->registration);
|
2006-03-31 04:38:11 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (NULL != (item = opal_list_remove_first(&(fl->fl_allocations)))) {
|
|
|
|
/* destruct the item (we constructed it), then free the memory chunk */
|
|
|
|
OBJ_DESTRUCT(item);
|
2005-09-03 23:46:44 +04:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJ_DESTRUCT(&fl->fl_allocations);
|
2005-09-02 20:00:42 +04:00
|
|
|
OBJ_DESTRUCT(&fl->fl_condition);
|
2004-06-15 23:46:26 +04:00
|
|
|
OBJ_DESTRUCT(&fl->fl_lock);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
int ompi_free_list_init_ex(
|
2004-06-15 23:46:26 +04:00
|
|
|
ompi_free_list_t *flist,
|
|
|
|
size_t elem_size,
|
2006-07-20 18:39:05 +04:00
|
|
|
size_t header_space,
|
|
|
|
size_t alignment,
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_class_t* elem_class,
|
2004-06-15 23:46:26 +04:00
|
|
|
int num_elements_to_alloc,
|
|
|
|
int max_elements_to_alloc,
|
|
|
|
int num_elements_per_alloc,
|
2004-08-02 04:24:22 +04:00
|
|
|
mca_mpool_base_module_t* mpool)
|
2004-06-15 23:46:26 +04:00
|
|
|
{
|
2006-07-20 18:39:05 +04:00
|
|
|
if(elem_size > flist->fl_elem_size)
|
|
|
|
flist->fl_elem_size = elem_size;
|
|
|
|
if(elem_class)
|
|
|
|
flist->fl_elem_class = elem_class;
|
2004-06-15 23:46:26 +04:00
|
|
|
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;
|
2006-07-20 18:39:05 +04:00
|
|
|
flist->fl_header_space = header_space;
|
|
|
|
flist->fl_alignment = alignment;
|
|
|
|
flist->fl_elem_size = align_to(flist->fl_elem_size, flist->fl_alignment);
|
2004-07-15 22:08:20 +04:00
|
|
|
if(num_elements_to_alloc)
|
|
|
|
return ompi_free_list_grow(flist, num_elements_to_alloc);
|
|
|
|
return OMPI_SUCCESS;
|
2004-06-15 23:46:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int ompi_free_list_grow(ompi_free_list_t* flist, size_t num_elements)
|
|
|
|
{
|
|
|
|
unsigned char* ptr;
|
2005-09-03 23:46:44 +04:00
|
|
|
ompi_free_list_memory_t *alloc_ptr;
|
2006-07-20 18:39:05 +04:00
|
|
|
size_t i, alloc_size;
|
2005-08-08 23:10:36 +04:00
|
|
|
mca_mpool_base_registration_t* user_out = NULL;
|
2005-05-24 02:06:50 +04:00
|
|
|
|
2005-10-27 21:48:40 +04:00
|
|
|
if (flist->fl_max_to_alloc > 0)
|
|
|
|
if (flist->fl_num_allocated + num_elements > flist->fl_max_to_alloc)
|
|
|
|
num_elements = flist->fl_max_to_alloc - flist->fl_num_allocated;
|
|
|
|
|
|
|
|
if (num_elements == 0)
|
2006-01-20 02:57:03 +03:00
|
|
|
return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
|
2004-06-15 23:46:26 +04:00
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
alloc_size = num_elements * flist->fl_elem_size +
|
|
|
|
sizeof(ompi_free_list_memory_t) + flist->fl_header_space +
|
|
|
|
flist->fl_alignment;
|
|
|
|
|
2004-06-15 23:46:26 +04:00
|
|
|
if (NULL != flist->fl_mpool)
|
2006-08-24 20:38:08 +04:00
|
|
|
alloc_ptr = (ompi_free_list_memory_t*)flist->fl_mpool->mpool_alloc(flist->fl_mpool,
|
|
|
|
alloc_size, 0, 0, &user_out);
|
2004-06-15 23:46:26 +04:00
|
|
|
else
|
2006-08-24 20:38:08 +04:00
|
|
|
alloc_ptr = (ompi_free_list_memory_t*)malloc(alloc_size);
|
2006-07-20 18:39:05 +04:00
|
|
|
|
2005-09-03 23:46:44 +04:00
|
|
|
if(NULL == alloc_ptr)
|
2004-06-15 23:46:26 +04:00
|
|
|
return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
|
|
|
|
|
2005-09-03 23:46:44 +04:00
|
|
|
/* make the alloc_ptr a list item, save the chunk in the allocations list, and
|
|
|
|
have ptr point to memory right after the list item structure */
|
|
|
|
OBJ_CONSTRUCT(alloc_ptr, ompi_free_list_memory_t);
|
|
|
|
opal_list_append(&(flist->fl_allocations), (opal_list_item_t*) alloc_ptr);
|
2006-06-22 18:07:14 +04:00
|
|
|
|
|
|
|
alloc_ptr->registration = user_out;
|
|
|
|
|
2005-09-03 23:46:44 +04:00
|
|
|
ptr = (unsigned char*) alloc_ptr + sizeof(ompi_free_list_memory_t);
|
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
ptr = (unsigned char*)(align_to((size_t)ptr + flist->fl_header_space,
|
|
|
|
flist->fl_alignment) - flist->fl_header_space);
|
2006-01-20 02:57:03 +03:00
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
for(i=0; i<num_elements; i++) {
|
|
|
|
ompi_free_list_item_t* item = (ompi_free_list_item_t*)ptr;
|
|
|
|
item->user_data = user_out;
|
2006-01-20 02:57:03 +03:00
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
OBJ_CONSTRUCT_INTERNAL(item, flist->fl_elem_class);
|
2006-04-13 03:27:38 +04:00
|
|
|
|
2006-07-20 18:39:05 +04:00
|
|
|
opal_atomic_lifo_push(&(flist->super), &(item->super));
|
|
|
|
ptr += flist->fl_elem_size;
|
2004-06-15 23:46:26 +04:00
|
|
|
}
|
2006-07-20 18:39:05 +04:00
|
|
|
|
2004-06-15 23:46:26 +04:00
|
|
|
flist->fl_num_allocated += num_elements;
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
2006-04-20 23:53:45 +04:00
|
|
|
|
2006-10-10 18:47:51 +04:00
|
|
|
/**
|
|
|
|
* This function resize the free_list to contain at least the specified
|
|
|
|
* number of elements. We do not create all of them in the same memory
|
|
|
|
* segment. Instead we will several time the fl_num_per_alloc elements
|
|
|
|
* until we reach the required number of the maximum allowed by the
|
|
|
|
* initialization.
|
|
|
|
*/
|
2006-10-07 01:13:49 +04:00
|
|
|
int
|
|
|
|
ompi_free_list_resize(ompi_free_list_t* flist, size_t size)
|
|
|
|
{
|
2006-10-10 18:47:51 +04:00
|
|
|
ssize_t inc_num;
|
2006-10-07 01:13:49 +04:00
|
|
|
int ret = OMPI_SUCCESS;
|
|
|
|
|
|
|
|
if (flist->fl_num_allocated > size) {
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
2006-10-10 18:47:51 +04:00
|
|
|
OPAL_THREAD_LOCK(&((flist)->fl_lock));
|
2006-10-20 23:33:55 +04:00
|
|
|
inc_num = (ssize_t)size - (ssize_t)flist->fl_num_allocated;
|
2006-10-10 18:47:51 +04:00
|
|
|
while( inc_num > 0 ) {
|
|
|
|
ret = ompi_free_list_grow(flist, flist->fl_num_per_alloc);
|
|
|
|
if( OMPI_SUCCESS != ret ) break;
|
2006-10-20 23:33:55 +04:00
|
|
|
inc_num = (ssize_t)size - (ssize_t)flist->fl_num_allocated;
|
2006-10-10 18:47:51 +04:00
|
|
|
}
|
|
|
|
OPAL_THREAD_UNLOCK(&((flist)->fl_lock));
|
2006-10-07 01:13:49 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|