2005-05-24 17:39:15 +04: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 (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "orte_config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "include/orte_constants.h"
|
|
|
|
#include "mca/errmgr/errmgr.h"
|
|
|
|
|
|
|
|
#include "class/orte_bitmap.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define SIZE_OF_CHAR (sizeof(char) * 8)
|
|
|
|
|
|
|
|
static void orte_bitmap_construct(orte_bitmap_t *bm);
|
|
|
|
static void orte_bitmap_destruct(orte_bitmap_t *bm);
|
|
|
|
|
2005-07-03 20:06:07 +04:00
|
|
|
OBJ_CLASS_INSTANCE(orte_bitmap_t, opal_object_t,
|
2005-05-24 17:39:15 +04:00
|
|
|
orte_bitmap_construct, orte_bitmap_destruct);
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
orte_bitmap_construct(orte_bitmap_t *bm)
|
|
|
|
{
|
|
|
|
bm->legal_numbits = 0;
|
|
|
|
bm->array_size = 0;
|
|
|
|
bm->bitmap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
orte_bitmap_destruct(orte_bitmap_t *bm)
|
|
|
|
{
|
|
|
|
if (NULL != bm->bitmap) {
|
|
|
|
free(bm->bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_init(orte_bitmap_t *bm, size_t size)
|
|
|
|
{
|
|
|
|
size_t actual_size;
|
|
|
|
|
|
|
|
if (NULL == bm) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual_size = size / SIZE_OF_CHAR;
|
|
|
|
|
|
|
|
actual_size += (size % SIZE_OF_CHAR == 0) ? 0 : 1;
|
|
|
|
bm->bitmap = (unsigned char *) malloc(actual_size);
|
|
|
|
if (NULL == bm->bitmap) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
|
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bm->array_size = actual_size;
|
2005-06-08 22:32:53 +04:00
|
|
|
bm->legal_numbits = SIZE_OF_CHAR*actual_size;
|
2005-05-24 17:39:15 +04:00
|
|
|
orte_bitmap_clear_all_bits(bm);
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-06-08 22:32:53 +04:00
|
|
|
orte_bitmap_resize(orte_bitmap_t *bm, size_t bit)
|
2005-05-24 17:39:15 +04:00
|
|
|
{
|
2005-06-08 19:48:38 +04:00
|
|
|
size_t index, new_size, i;
|
2005-05-24 17:39:15 +04:00
|
|
|
|
|
|
|
index = bit / SIZE_OF_CHAR;
|
2005-06-08 19:48:38 +04:00
|
|
|
index += (bit % SIZE_OF_CHAR == 0) ? 0 : 1;
|
2005-05-24 17:39:15 +04:00
|
|
|
|
|
|
|
if (index >= bm->array_size) {
|
|
|
|
|
2005-06-08 19:48:38 +04: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 */
|
2005-05-24 17:39:15 +04:00
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
new_size = (index / bm->array_size + 1 ) * bm->array_size;
|
2005-05-24 17:39:15 +04:00
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
/* New size is just a multiple of the original size to fit in
|
|
|
|
the index. */
|
2005-05-24 17:39:15 +04:00
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
bm->bitmap = (unsigned char *) realloc(bm->bitmap, new_size);
|
|
|
|
if (NULL == bm->bitmap) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
|
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2005-05-24 17:39:15 +04:00
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
/* zero out the new elements */
|
|
|
|
for (i = bm->array_size; i < new_size; ++i) {
|
|
|
|
bm->bitmap[i] = 0;
|
|
|
|
}
|
2005-05-24 17:39:15 +04:00
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
/* Update the array_size */
|
|
|
|
bm->array_size = new_size;
|
2005-06-08 22:32:53 +04:00
|
|
|
bm->legal_numbits = new_size*SIZE_OF_CHAR;
|
2005-05-24 17:39:15 +04:00
|
|
|
}
|
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_set_bit(orte_bitmap_t *bm, size_t bit)
|
|
|
|
{
|
|
|
|
size_t index, offset;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (NULL == bm) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure the bitmap covers the requested bit */
|
2005-06-08 22:32:53 +04:00
|
|
|
if (ORTE_SUCCESS != (rc = orte_bitmap_resize(bm, bit))) {
|
2005-06-08 19:48:38 +04:00
|
|
|
ORTE_ERROR_LOG(rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = bit / SIZE_OF_CHAR;
|
|
|
|
offset = bit % SIZE_OF_CHAR;
|
|
|
|
|
2005-05-24 17:39:15 +04:00
|
|
|
/* Now set the bit */
|
|
|
|
bm->bitmap[index] |= (1 << offset);
|
|
|
|
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_clear_bit(orte_bitmap_t *bm, size_t bit)
|
|
|
|
{
|
|
|
|
size_t index, offset;
|
2005-06-08 19:48:38 +04:00
|
|
|
int rc;
|
2005-05-24 17:39:15 +04:00
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
if (NULL == bm) {
|
2005-05-24 17:39:15 +04:00
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
/* make sure the bitmap covers the requested bit */
|
2005-06-08 22:32:53 +04:00
|
|
|
if (ORTE_SUCCESS != (rc = orte_bitmap_resize(bm, bit))) {
|
2005-06-08 19:48:38 +04:00
|
|
|
ORTE_ERROR_LOG(rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2005-05-24 17:39:15 +04:00
|
|
|
index = bit / SIZE_OF_CHAR;
|
|
|
|
offset = bit % SIZE_OF_CHAR;
|
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
/* now clear the bit */
|
2005-05-24 17:39:15 +04:00
|
|
|
bm->bitmap[index] &= ~(1 << offset);
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_is_set_bit(orte_bitmap_t *bm, size_t bit)
|
|
|
|
{
|
|
|
|
size_t index, offset;
|
|
|
|
|
|
|
|
if ((bit > bm->legal_numbits - 1) || (NULL == bm)) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = bit / SIZE_OF_CHAR;
|
|
|
|
offset = bit % SIZE_OF_CHAR;
|
|
|
|
|
|
|
|
if (index >= bm->array_size) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != (bm->bitmap[index] & (1 << offset))) {
|
|
|
|
return (int) true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_clear_all_bits(orte_bitmap_t *bm)
|
|
|
|
{
|
|
|
|
if (NULL == bm) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(bm->bitmap, 0, bm->array_size);
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_set_all_bits(orte_bitmap_t *bm)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (NULL == bm) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < bm->array_size; ++i) {
|
|
|
|
bm->bitmap[i] = ~((char) 0);
|
|
|
|
}
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_bitmap_find_and_set_first_unset_bit(orte_bitmap_t *bm, size_t *position)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
unsigned char temp;
|
|
|
|
unsigned char all_ones = 0xff;
|
|
|
|
|
2005-06-08 19:48:38 +04:00
|
|
|
if (NULL == bm || NULL == position) {
|
2005-05-24 17:39:15 +04:00
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_BAD_PARAM);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
2005-06-08 22:32:53 +04:00
|
|
|
/* Neglect all which don't have an unset bit */
|
2005-05-24 17:39:15 +04:00
|
|
|
*position = 0;
|
|
|
|
while((i < bm->array_size) && (bm->bitmap[i] == all_ones)) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == bm->array_size) {
|
|
|
|
/* increase the bitmap size then */
|
|
|
|
*position = bm->array_size * SIZE_OF_CHAR;
|
|
|
|
return orte_bitmap_set_bit(bm, *position);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This one has an unset bit, find its bit number */
|
|
|
|
|
|
|
|
temp = bm->bitmap[i];
|
|
|
|
while (temp & 0x1) {
|
|
|
|
++(*position);
|
|
|
|
temp >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now set the bit number */
|
|
|
|
bm->bitmap[i] |= (bm->bitmap[i] + 1);
|
|
|
|
|
|
|
|
(*position) += i * SIZE_OF_CHAR;
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|