From f8b780ab226bb75a0ef349816603b537d03a1f67 Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Tue, 19 Oct 2004 23:53:55 +0000 Subject: [PATCH] - Convert to OBJ interface - 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. --- src/class/ompi_bitmap.c | 121 ++++++++++++++++++++-------------------- src/class/ompi_bitmap.h | 29 +++++----- 2 files changed, 74 insertions(+), 76 deletions(-) diff --git a/src/class/ompi_bitmap.c b/src/class/ompi_bitmap.c index 58ef27246a..d50763422f 100644 --- a/src/class/ompi_bitmap.c +++ b/src/class/ompi_bitmap.c @@ -2,23 +2,21 @@ * $HEADER$ */ -#ifdef HAVE_CONFIG_H #include "ompi_config.h" -#endif +#include + +#include "include/constants.h" #include "class/ompi_bitmap.h" + #define SIZE_OF_CHAR (sizeof(char) * 8) static void ompi_bitmap_construct(ompi_bitmap_t *bm); static void ompi_bitmap_destruct(ompi_bitmap_t *bm); -ompi_class_t ompi_bitmap_t_class = { - "ompi_bitmap_t", - OBJ_CLASS(ompi_object_t), - (ompi_construct_t)ompi_bitmap_construct, - (ompi_construct_t)ompi_bitmap_destruct -}; +OBJ_CLASS_INSTANCE(ompi_bitmap_t, ompi_object_t, + ompi_bitmap_construct, ompi_bitmap_destruct); static void @@ -33,40 +31,44 @@ ompi_bitmap_construct(ompi_bitmap_t *bm) static void ompi_bitmap_destruct(ompi_bitmap_t *bm) { + if (NULL != bm->bitmap) { + free(bm->bitmap); + } } int ompi_bitmap_init(ompi_bitmap_t *bm, size_t size) { - size_t actual_size; - if (((int)size <= 0) || (NULL == bm)) - return OMPI_ERR_ARG; + if (((int)size <= 0) || (NULL == bm)) { + return OMPI_ERR_BAD_PARAM; + } bm->legal_numbits = size; 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) - return OMPI_ERR_SYSRESOURCE; + if (NULL == bm->bitmap) { + return OMPI_ERR_OUT_OF_RESOURCE; + } bm->array_size = actual_size; ompi_bitmap_clear_all_bits(bm); - return 0; + return OMPI_SUCCESS; } 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; - int i; + size_t index, offset, new_size, i; - if ((bit < 0) || (NULL == bm)) - return OMPI_ERR_ARG; + if ((bit < 0) || (NULL == bm)) { + return OMPI_ERR_BAD_PARAM; + } index = 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); if (NULL == bm->bitmap) { - return OMPI_ERR_SYSRESOURCE; + return OMPI_ERR_OUT_OF_RESOURCE; } /* zero out the new elements */ @@ -98,123 +100,118 @@ ompi_bitmap_set_bit(ompi_bitmap_t *bm, int bit) /* Now set the bit */ bm->bitmap[index] |= (1 << offset); - return 0; + return OMPI_SUCCESS; } 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; - if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) - return OMPI_ERR_ARG; + if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) { + return OMPI_ERR_BAD_PARAM; + } index = bit / SIZE_OF_CHAR; offset = bit % SIZE_OF_CHAR; if (index >= bm->array_size) { - return OMPI_INVALID_BIT; + return OMPI_ERR_BAD_PARAM; } bm->bitmap[index] &= ~(1 << offset); - return 0; + return OMPI_SUCCESS; } 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; - if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) - return OMPI_ERR_ARG; - + if ((bit < 0) || (bit > bm->legal_numbits - 1) || (NULL == bm)) { + return OMPI_ERR_BAD_PARAM; + } index = bit / SIZE_OF_CHAR; offset = bit % SIZE_OF_CHAR; if (index >= bm->array_size) { - return OMPI_INVALID_BIT; + return OMPI_ERR_BAD_PARAM; } - if (0 != (bm->bitmap[index] & (1 << offset))) - return 1; + if (0 != (bm->bitmap[index] & (1 << offset))) { + return (int) true; + } - return 0; + return (int) false; } int ompi_bitmap_clear_all_bits(ompi_bitmap_t *bm) { - if (NULL == bm) - return OMPI_ERR_ARG; + if (NULL == bm) { + return OMPI_ERR_BAD_PARAM; + } memset(bm->bitmap, 0, bm->array_size); - return 0; + return OMPI_SUCCESS; } int ompi_bitmap_set_all_bits(ompi_bitmap_t *bm) { - int i; + size_t i; - if (NULL == bm) - return OMPI_ERR_ARG; + if (NULL == bm) { + return OMPI_ERR_BAD_PARAM; + } for (i = 0; i < bm->array_size; ++i) { bm->bitmap[i] = ~((char) 0); } - return 0; + return OMPI_SUCCESS; } -#include + 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; - int position = 0; unsigned char temp; unsigned char all_ones = 0xff; - if (NULL == bm) - return OMPI_ERR_ARG; + if (NULL == bm) { + return OMPI_ERR_BAD_PARAM; + } /* Neglect all which dont have an unset bit */ + *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; - ompi_bitmap_set_bit(bm, position); - return position; + *position = bm->array_size * SIZE_OF_CHAR; + ompi_bitmap_set_bit(bm, *position); + return OMPI_SUCCESS; } /* This one has an unset bit, find its bit number */ temp = bm->bitmap[i]; while (temp & 0x1) { - ++position; + ++(*position); temp >>= 1; } /* Now set the bit number */ bm->bitmap[i] |= (bm->bitmap[i] + 1); - position += i * SIZE_OF_CHAR; - return position; -} - - -size_t -ompi_bitmap_size(ompi_bitmap_t *bm) -{ - if (NULL == bm) - return OMPI_ERR_ARG; - - return bm->legal_numbits; + (*position) += i * SIZE_OF_CHAR; + return OMPI_SUCCESS; } diff --git a/src/class/ompi_bitmap.h b/src/class/ompi_bitmap.h index 87ac69bd18..4e128c206e 100644 --- a/src/class/ompi_bitmap.h +++ b/src/class/ompi_bitmap.h @@ -24,14 +24,6 @@ #include "include/types.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 { ompi_object_t super; /**< Subclass of ompi_object_t */ 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; +OBJ_CLASS_DECLARATION(ompi_bitmap_t); + + #if defined(c_plusplus) || defined(__cplusplus) extern "C" { #endif @@ -71,7 +66,7 @@ int ompi_bitmap_init (ompi_bitmap_t *bm, size_t size); * @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 * */ -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 * */ -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 * * @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 * */ -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) }