2004-03-17 02:19:10 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* All rights reserved.
|
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-03-17 02:19:10 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
#include "ompi_config.h"
|
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "include/constants.h"
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "class/ompi_bitmap.h"
|
2004-03-17 02:19:10 +03:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
|
2004-03-17 02:19:10 +03:00
|
|
|
#define SIZE_OF_CHAR (sizeof(char) * 8)
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
static void ompi_bitmap_construct(ompi_bitmap_t *bm);
|
|
|
|
static void ompi_bitmap_destruct(ompi_bitmap_t *bm);
|
2004-03-17 02:19:10 +03:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
OBJ_CLASS_INSTANCE(ompi_bitmap_t, ompi_object_t,
|
|
|
|
ompi_bitmap_construct, ompi_bitmap_destruct);
|
2004-03-17 02:19:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_bitmap_construct(ompi_bitmap_t *bm)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-04-12 01:15:09 +04:00
|
|
|
bm->legal_numbits = 0;
|
2004-03-17 02:19:10 +03:00
|
|
|
bm->array_size = 0;
|
|
|
|
bm->bitmap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_bitmap_destruct(ompi_bitmap_t *bm)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-20 03:53:55 +04:00
|
|
|
if (NULL != bm->bitmap) {
|
|
|
|
free(bm->bitmap);
|
|
|
|
}
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-10-23 23:24:00 +04:00
|
|
|
ompi_bitmap_init(ompi_bitmap_t *bm, int size)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-23 23:24:00 +04:00
|
|
|
int actual_size;
|
2004-04-12 01:15:09 +04:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
if ((size <= 0) || (size > OMPI_FORTRAN_HANDLE_MAX) || (NULL == bm)) {
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
bm->legal_numbits = size;
|
|
|
|
actual_size = size / SIZE_OF_CHAR;
|
2004-03-17 02:19:10 +03:00
|
|
|
|
|
|
|
actual_size += (size % SIZE_OF_CHAR == 0) ? 0 : 1;
|
2004-04-12 01:15:09 +04:00
|
|
|
bm->bitmap = (unsigned char *) malloc(actual_size);
|
2004-10-20 03:53:55 +04:00
|
|
|
if (NULL == bm->bitmap) {
|
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2004-03-17 02:19:10 +03:00
|
|
|
|
|
|
|
bm->array_size = actual_size;
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_bitmap_clear_all_bits(bm);
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-10-23 23:24:00 +04:00
|
|
|
ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-23 23:24:00 +04:00
|
|
|
int index, offset, new_size, i;
|
|
|
|
size_t new_size_large;
|
2004-03-17 02:19:10 +03:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
if ((bit < 0) || (bit > OMPI_FORTRAN_HANDLE_MAX) || (NULL == bm)) {
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
index = bit / SIZE_OF_CHAR;
|
|
|
|
offset = bit % SIZE_OF_CHAR;
|
2004-03-17 02:19:10 +03:00
|
|
|
|
|
|
|
if (index >= bm->array_size) {
|
2004-10-23 23:24:00 +04:00
|
|
|
|
|
|
|
/* If we're already full, return "No vacancy!" */
|
|
|
|
|
|
|
|
if (bm->array_size >= OMPI_FORTRAN_HANDLE_MAX) {
|
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
2004-03-17 02:19:10 +03:00
|
|
|
/* We need to allocate more space for the bitmap, since we are
|
|
|
|
out of range. We dont throw any error here, because this is
|
|
|
|
valid and we simply expand the bitmap */
|
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
new_size_large = (index / bm->array_size + 1 ) * bm->array_size;
|
|
|
|
|
|
|
|
/* Check to be sure that we still have less than
|
|
|
|
OMPI_FORTRAN_HANDLE_MAX bits */
|
|
|
|
|
|
|
|
if (new_size_large > OMPI_FORTRAN_HANDLE_MAX) {
|
|
|
|
new_size_large = OMPI_FORTRAN_HANDLE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that new_size is guaranteed to be <=
|
|
|
|
OMPI_FORTRAN_HANDLE_MAX, which is guaranteed to fit in a
|
|
|
|
[signed] int. */
|
|
|
|
|
|
|
|
new_size = (int) new_size_large;
|
|
|
|
|
2004-03-17 02:19:10 +03:00
|
|
|
/* New size is just a multiple of the original size to fit in
|
2004-10-23 23:24:00 +04:00
|
|
|
the index. */
|
2004-03-17 02:19:10 +03:00
|
|
|
|
2004-10-23 23:24:00 +04:00
|
|
|
bm->bitmap = (unsigned char *) realloc(bm->bitmap, (int) new_size);
|
2004-03-17 02:19:10 +03:00
|
|
|
if (NULL == bm->bitmap) {
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* zero out the new elements */
|
|
|
|
for (i = bm->array_size; i < new_size; ++i) {
|
|
|
|
bm->bitmap[i] = 0;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
/* Update the array_size */
|
|
|
|
bm->array_size = new_size;
|
|
|
|
bm->legal_numbits = bit + 1;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
/* Now set the bit */
|
2004-03-17 02:19:10 +03:00
|
|
|
bm->bitmap[index] |= (1 << offset);
|
2004-04-12 01:15:09 +04:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-10-23 23:24:00 +04:00
|
|
|
ompi_bitmap_clear_bit(ompi_bitmap_t *bm, int bit)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-23 23:24:00 +04:00
|
|
|
int index, offset;
|
2004-04-12 01:15:09 +04:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
index = bit / SIZE_OF_CHAR;
|
2004-03-17 02:19:10 +03:00
|
|
|
offset = bit % SIZE_OF_CHAR;
|
|
|
|
|
|
|
|
if (index >= bm->array_size) {
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_ERR_BAD_PARAM;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bm->bitmap[index] &= ~(1 << offset);
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-10-23 23:24:00 +04:00
|
|
|
ompi_bitmap_is_set_bit(ompi_bitmap_t *bm, int bit)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-23 23:24:00 +04:00
|
|
|
int index, offset;
|
2004-03-17 02:19:10 +03:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
index = bit / SIZE_OF_CHAR;
|
|
|
|
offset = bit % SIZE_OF_CHAR;
|
2004-03-17 02:19:10 +03:00
|
|
|
|
|
|
|
if (index >= bm->array_size) {
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_ERR_BAD_PARAM;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
if (0 != (bm->bitmap[index] & (1 << offset))) {
|
|
|
|
return (int) true;
|
|
|
|
}
|
2004-03-17 02:19:10 +03:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
return (int) false;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-12 01:15:09 +04:00
|
|
|
int
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_bitmap_clear_all_bits(ompi_bitmap_t *bm)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-20 03:53:55 +04:00
|
|
|
if (NULL == bm) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
2004-03-17 02:19:10 +03:00
|
|
|
memset(bm->bitmap, 0, bm->array_size);
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-12 01:15:09 +04:00
|
|
|
int
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_bitmap_set_all_bits(ompi_bitmap_t *bm)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-23 23:24:00 +04:00
|
|
|
int i;
|
2004-04-12 01:15:09 +04:00
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
if (NULL == bm) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
2004-03-17 02:19:10 +03:00
|
|
|
for (i = 0; i < bm->array_size; ++i) {
|
|
|
|
bm->bitmap[i] = ~((char) 0);
|
|
|
|
}
|
2004-10-20 03:53:55 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
|
2004-03-17 02:19:10 +03:00
|
|
|
int
|
2004-10-23 23:24:00 +04:00
|
|
|
ompi_bitmap_find_and_set_first_unset_bit(ompi_bitmap_t *bm, int *position)
|
2004-03-17 02:19:10 +03:00
|
|
|
{
|
2004-10-23 23:24:00 +04:00
|
|
|
int i = 0;
|
2004-04-12 01:15:09 +04:00
|
|
|
unsigned char temp;
|
|
|
|
unsigned char all_ones = 0xff;
|
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
if (NULL == bm) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
/* Neglect all which dont have an unset bit */
|
2004-10-20 03:53:55 +04:00
|
|
|
*position = 0;
|
2004-04-12 01:15:09 +04:00
|
|
|
while((i < bm->array_size) && (bm->bitmap[i] == all_ones)) {
|
|
|
|
++i;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|
2004-04-12 01:15:09 +04:00
|
|
|
|
|
|
|
if (i == bm->array_size) {
|
|
|
|
/* increase the bitmap size then */
|
2004-10-20 03:53:55 +04:00
|
|
|
*position = bm->array_size * SIZE_OF_CHAR;
|
2004-10-23 23:24:00 +04:00
|
|
|
return ompi_bitmap_set_bit(bm, *position);
|
2004-04-12 01:15:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This one has an unset bit, find its bit number */
|
|
|
|
|
|
|
|
temp = bm->bitmap[i];
|
|
|
|
while (temp & 0x1) {
|
2004-10-20 03:53:55 +04:00
|
|
|
++(*position);
|
2004-04-12 01:15:09 +04:00
|
|
|
temp >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now set the bit number */
|
|
|
|
bm->bitmap[i] |= (bm->bitmap[i] + 1);
|
|
|
|
|
2004-10-20 03:53:55 +04:00
|
|
|
(*position) += i * SIZE_OF_CHAR;
|
|
|
|
return OMPI_SUCCESS;
|
2004-03-17 02:19:10 +03:00
|
|
|
}
|