1
1
- Add a few more error checks
- Add a few more thing to conform to style guidelines
- int->size_t fixes
- remove some outdated error constants, use standardized ones
- make ompi_bitmap_size be inline

This commit was SVN r3223.
Этот коммит содержится в:
Jeff Squyres 2004-10-19 23:53:55 +00:00
родитель 52cfe186c2
Коммит f8b780ab22
2 изменённых файлов: 74 добавлений и 76 удалений

Просмотреть файл

@ -2,23 +2,21 @@
* $HEADER$ * $HEADER$
*/ */
#ifdef HAVE_CONFIG_H
#include "ompi_config.h" #include "ompi_config.h"
#endif
#include <stdio.h>
#include "include/constants.h"
#include "class/ompi_bitmap.h" #include "class/ompi_bitmap.h"
#define SIZE_OF_CHAR (sizeof(char) * 8) #define SIZE_OF_CHAR (sizeof(char) * 8)
static void ompi_bitmap_construct(ompi_bitmap_t *bm); static void ompi_bitmap_construct(ompi_bitmap_t *bm);
static void ompi_bitmap_destruct(ompi_bitmap_t *bm); static void ompi_bitmap_destruct(ompi_bitmap_t *bm);
ompi_class_t ompi_bitmap_t_class = { OBJ_CLASS_INSTANCE(ompi_bitmap_t, ompi_object_t,
"ompi_bitmap_t", ompi_bitmap_construct, ompi_bitmap_destruct);
OBJ_CLASS(ompi_object_t),
(ompi_construct_t)ompi_bitmap_construct,
(ompi_construct_t)ompi_bitmap_destruct
};
static void static void
@ -33,40 +31,44 @@ ompi_bitmap_construct(ompi_bitmap_t *bm)
static void static void
ompi_bitmap_destruct(ompi_bitmap_t *bm) ompi_bitmap_destruct(ompi_bitmap_t *bm)
{ {
if (NULL != bm->bitmap) {
free(bm->bitmap);
}
} }
int int
ompi_bitmap_init(ompi_bitmap_t *bm, size_t size) ompi_bitmap_init(ompi_bitmap_t *bm, size_t size)
{ {
size_t actual_size; size_t actual_size;
if (((int)size <= 0) || (NULL == bm)) if (((int)size <= 0) || (NULL == bm)) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
bm->legal_numbits = size; bm->legal_numbits = size;
actual_size = size / SIZE_OF_CHAR; actual_size = size / SIZE_OF_CHAR;
actual_size += (size % SIZE_OF_CHAR == 0) ? 0 : 1; actual_size += (size % SIZE_OF_CHAR == 0) ? 0 : 1;
bm->bitmap = (unsigned char *) malloc(actual_size); bm->bitmap = (unsigned char *) malloc(actual_size);
if (NULL == bm->bitmap) if (NULL == bm->bitmap) {
return OMPI_ERR_SYSRESOURCE; return OMPI_ERR_OUT_OF_RESOURCE;
}
bm->array_size = actual_size; bm->array_size = actual_size;
ompi_bitmap_clear_all_bits(bm); ompi_bitmap_clear_all_bits(bm);
return 0; return OMPI_SUCCESS;
} }
int int
ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit) ompi_bitmap_set_bit(ompi_bitmap_t *bm, size_t bit)
{ {
size_t index, offset, new_size; size_t index, offset, new_size, i;
int i;
if ((bit < 0) || (NULL == bm)) if ((bit < 0) || (NULL == bm)) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
index = bit / SIZE_OF_CHAR; index = bit / SIZE_OF_CHAR;
offset = bit % SIZE_OF_CHAR; offset = bit % SIZE_OF_CHAR;
@ -82,7 +84,7 @@ ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit)
bm->bitmap = (unsigned char *) realloc(bm->bitmap, new_size); bm->bitmap = (unsigned char *) realloc(bm->bitmap, new_size);
if (NULL == bm->bitmap) { if (NULL == bm->bitmap) {
return OMPI_ERR_SYSRESOURCE; return OMPI_ERR_OUT_OF_RESOURCE;
} }
/* zero out the new elements */ /* zero out the new elements */
@ -98,123 +100,118 @@ ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit)
/* Now set the bit */ /* Now set the bit */
bm->bitmap[index] |= (1 << offset); bm->bitmap[index] |= (1 << offset);
return 0; return OMPI_SUCCESS;
} }
int int
ompi_bitmap_clear_bit(ompi_bitmap_t *bm, int bit) ompi_bitmap_clear_bit(ompi_bitmap_t *bm, size_t bit)
{ {
size_t index, offset; size_t index, offset;
if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
index = bit / SIZE_OF_CHAR; index = bit / SIZE_OF_CHAR;
offset = bit % SIZE_OF_CHAR; offset = bit % SIZE_OF_CHAR;
if (index >= bm->array_size) { if (index >= bm->array_size) {
return OMPI_INVALID_BIT; return OMPI_ERR_BAD_PARAM;
} }
bm->bitmap[index] &= ~(1 << offset); bm->bitmap[index] &= ~(1 << offset);
return 0; return OMPI_SUCCESS;
} }
int int
ompi_bitmap_is_set_bit(ompi_bitmap_t *bm, int bit) ompi_bitmap_is_set_bit(ompi_bitmap_t *bm, size_t bit)
{ {
size_t index, offset; size_t index, offset;
if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
index = bit / SIZE_OF_CHAR; index = bit / SIZE_OF_CHAR;
offset = bit % SIZE_OF_CHAR; offset = bit % SIZE_OF_CHAR;
if (index >= bm->array_size) { if (index >= bm->array_size) {
return OMPI_INVALID_BIT; return OMPI_ERR_BAD_PARAM;
} }
if (0 != (bm->bitmap[index] & (1 << offset))) if (0 != (bm->bitmap[index] & (1 << offset))) {
return 1; return (int) true;
}
return 0; return (int) false;
} }
int int
ompi_bitmap_clear_all_bits(ompi_bitmap_t *bm) ompi_bitmap_clear_all_bits(ompi_bitmap_t *bm)
{ {
if (NULL == bm) if (NULL == bm) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
memset(bm->bitmap, 0, bm->array_size); memset(bm->bitmap, 0, bm->array_size);
return 0; return OMPI_SUCCESS;
} }
int int
ompi_bitmap_set_all_bits(ompi_bitmap_t *bm) ompi_bitmap_set_all_bits(ompi_bitmap_t *bm)
{ {
int i; size_t i;
if (NULL == bm) if (NULL == bm) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
for (i = 0; i < bm->array_size; ++i) { for (i = 0; i < bm->array_size; ++i) {
bm->bitmap[i] = ~((char) 0); bm->bitmap[i] = ~((char) 0);
} }
return 0; return OMPI_SUCCESS;
} }
#include <stdio.h>
int int
ompi_bitmap_find_and_set_first_unset_bit(ompi_bitmap_t *bm) ompi_bitmap_find_and_set_first_unset_bit(ompi_bitmap_t *bm, size_t *position)
{ {
size_t i = 0; size_t i = 0;
int position = 0;
unsigned char temp; unsigned char temp;
unsigned char all_ones = 0xff; unsigned char all_ones = 0xff;
if (NULL == bm) if (NULL == bm) {
return OMPI_ERR_ARG; return OMPI_ERR_BAD_PARAM;
}
/* Neglect all which dont have an unset bit */ /* Neglect all which dont have an unset bit */
*position = 0;
while((i < bm->array_size) && (bm->bitmap[i] == all_ones)) { while((i < bm->array_size) && (bm->bitmap[i] == all_ones)) {
++i; ++i;
} }
if (i == bm->array_size) { if (i == bm->array_size) {
/* increase the bitmap size then */ /* increase the bitmap size then */
position = bm->array_size * SIZE_OF_CHAR; *position = bm->array_size * SIZE_OF_CHAR;
ompi_bitmap_set_bit(bm, position); ompi_bitmap_set_bit(bm, *position);
return position; return OMPI_SUCCESS;
} }
/* This one has an unset bit, find its bit number */ /* This one has an unset bit, find its bit number */
temp = bm->bitmap[i]; temp = bm->bitmap[i];
while (temp & 0x1) { while (temp & 0x1) {
++position; ++(*position);
temp >>= 1; temp >>= 1;
} }
/* Now set the bit number */ /* Now set the bit number */
bm->bitmap[i] |= (bm->bitmap[i] + 1); bm->bitmap[i] |= (bm->bitmap[i] + 1);
position += i * SIZE_OF_CHAR; (*position) += i * SIZE_OF_CHAR;
return position; return OMPI_SUCCESS;
}
size_t
ompi_bitmap_size(ompi_bitmap_t *bm)
{
if (NULL == bm)
return OMPI_ERR_ARG;
return bm->legal_numbits;
} }

Просмотреть файл

@ -24,14 +24,6 @@
#include "include/types.h" #include "include/types.h"
#include "class/ompi_object.h" #include "class/ompi_object.h"
/* VPS: Just to compile right now, has to move later on */
#define OMPI_ERR_SYSRESOURCE -1
#define OMPI_INVALID_BIT -1
#define OMPI_ERR_ARG -2
extern ompi_class_t ompi_bitmap_t_class;
struct ompi_bitmap_t { struct ompi_bitmap_t {
ompi_object_t super; /**< Subclass of ompi_object_t */ ompi_object_t super; /**< Subclass of ompi_object_t */
unsigned char *bitmap; /**< The actual bitmap array of characters */ unsigned char *bitmap; /**< The actual bitmap array of characters */
@ -44,6 +36,9 @@ struct ompi_bitmap_t {
typedef struct ompi_bitmap_t ompi_bitmap_t; typedef struct ompi_bitmap_t ompi_bitmap_t;
OBJ_CLASS_DECLARATION(ompi_bitmap_t);
#if defined(c_plusplus) || defined(__cplusplus) #if defined(c_plusplus) || defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
@ -71,7 +66,7 @@ int ompi_bitmap_init (ompi_bitmap_t *bm, size_t size);
* @return OMPI error code or success * @return OMPI error code or success
* *
*/ */
int ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit); int ompi_bitmap_set_bit(ompi_bitmap_t *bm, size_t bit);
/** /**
@ -83,7 +78,7 @@ int ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit);
* @return OMPI error code if the bit is out of range, else success * @return OMPI error code if the bit is out of range, else success
* *
*/ */
int ompi_bitmap_clear_bit(ompi_bitmap_t *bm, int bit); int ompi_bitmap_clear_bit(ompi_bitmap_t *bm, size_t bit);
/** /**
@ -96,16 +91,19 @@ int ompi_bitmap_clear_bit(ompi_bitmap_t *bm, int bit);
* 0 if the bit is not set * 0 if the bit is not set
* *
*/ */
int ompi_bitmap_is_set_bit(ompi_bitmap_t *bm, int bit); int ompi_bitmap_is_set_bit(ompi_bitmap_t *bm, size_t bit);
/** /**
* Find the first clear bit in the bitmap and set it * Find the first clear bit in the bitmap and set it
* *
* @param bitmap The input bitmap (IN) * @param bitmap The input bitmap (IN)
* @return bit number The bit number of the first unset bit * @param position Position of the first clear bit (OUT)
* @return err OMPI_SUCCESS on success
*/ */
int ompi_bitmap_find_and_set_first_unset_bit(ompi_bitmap_t *bm); int ompi_bitmap_find_and_set_first_unset_bit(ompi_bitmap_t *bm,
size_t *position);
/** /**
@ -135,7 +133,10 @@ int ompi_bitmap_set_all_bits(ompi_bitmap_t *bm);
* @return OMPI error code if bm is NULL * @return OMPI error code if bm is NULL
* *
*/ */
size_t ompi_bitmap_size (ompi_bitmap_t *bm); static inline size_t ompi_bitmap_size(ompi_bitmap_t *bm)
{
return (NULL == bm) ? 0 : bm->legal_numbits;
}
#if defined(c_plusplus) || defined(__cplusplus) #if defined(c_plusplus) || defined(__cplusplus)
} }