Commit f6e2c2d7 authored by Swann Perarnau's avatar Swann Perarnau

[refactor] improve bitmap create/destroy

Implement #31 for bitmap.
Extra init/fini function for API consistency, but not needed.
Checkpatch another header.
parent 9059049c
......@@ -14,6 +14,7 @@
# files
include/aml.h
include/aml/area/linux.h
include/aml/utils/bitmap.h
include/aml/utils/error.h
src/aml.c
src/area/area.c
......
......@@ -25,24 +25,6 @@ struct aml_bitmap {
unsigned long mask[AML_BITMAP_SIZE];
};
/* Not needed, here for consistency */
#define AML_BITMAP_DECL(name) struct aml_bitmap name
#define AML_BITMAP_ALLOCSIZE (sizeof(struct aml_bitmap))
/**
* On heap allocation of new empty bitmap.
**/
struct aml_bitmap *aml_bitmap_create(void);
/**
* On heap allocation of a copy of an existing bitmap.
**/
struct aml_bitmap *aml_bitmap_dup(const struct aml_bitmap *src);
/**
* Free on heap allocated bitmap.
**/
void aml_bitmap_destroy(struct aml_bitmap *bitmap);
/**
* Copy bitmap content.
**/
......@@ -60,10 +42,10 @@ void aml_bitmap_fill(struct aml_bitmap *bitmap);
/**
* Check whether a bit in bitmap is set.
* Returns -1 if i is greater than bitmap length,
* Returns -1 if i is greater than bitmap length,
* 0 if bit is not set else a positive value..
**/
int aml_bitmap_isset(const struct aml_bitmap *bitmap, const unsigned i);
int aml_bitmap_isset(const struct aml_bitmap *bitmap, const unsigned int i);
/**
* Check whether a bit in bitmap is empty.
......@@ -86,26 +68,28 @@ int aml_bitmap_isequal(const struct aml_bitmap *a, const struct aml_bitmap *b);
* Set a bit in bitmap.
* Returns -1 if i is greater than bitmap length, else 0.
**/
int aml_bitmap_set(struct aml_bitmap *bitmap, const unsigned i);
int aml_bitmap_set(struct aml_bitmap *bitmap, const unsigned int i);
/**
* Clear a bit in bitmap.
* Returns -1 if i is greater than bitmap length, else 0.
**/
int aml_bitmap_clear(struct aml_bitmap *bitmap, const unsigned i);
int aml_bitmap_clear(struct aml_bitmap *bitmap, const unsigned int i);
/**
* Set a range [[i, ii]] of bits in bitmap.
* Returns -1 if i or ii is greater than bitmap length, else 0.
**/
int aml_bitmap_set_range(struct aml_bitmap *bitmap, const unsigned i, const unsigned ii);
int aml_bitmap_set_range(struct aml_bitmap *bitmap,
const unsigned int i, const unsigned int ii);
/**
* Clear a range [[i, ii]] of bits in bitmap.
* Returns -1 if i or ii is greater than bitmap length, else 0.
**/
int aml_bitmap_clear_range(struct aml_bitmap *bitmap, const unsigned i, const unsigned ii);
int aml_bitmap_clear_range(struct aml_bitmap *bitmap,
const unsigned int i, const unsigned int ii);
/**
* Count the number of bits set in bitmap.
**/
......@@ -115,20 +99,22 @@ unsigned long aml_bitmap_nset(const struct aml_bitmap *bitmap);
* Copy a unsigned long array used as a bitmap into an actual bitmap.
* Takes the array and its max set bit as input.
**/
void aml_bitmap_copy_from_ulong(struct aml_bitmap *bitmap, const unsigned long *, size_t);
void aml_bitmap_copy_from_ulong(struct aml_bitmap *bitmap,
const unsigned long *src, size_t size);
/**
* Copy a bitmap into an unsigned long array.
* Takes the array and its max set bit as input.
**/
void aml_bitmap_copy_to_ulong(const struct aml_bitmap *bitmap, unsigned long *, size_t);
void aml_bitmap_copy_to_ulong(const struct aml_bitmap *bitmap,
unsigned long *dst, size_t size);
/**
* Get index of the first bit set in bitmap.
* Return -1 if no bit is set.
**/
int aml_bitmap_last(const struct aml_bitmap *bitmap);
/**
* Get index of the last bit set in bitmap.
* Return -1 if no bit is set.
......@@ -143,13 +129,56 @@ int aml_bitmap_first(const struct aml_bitmap *bitmap);
* - an integer string to set one bit,
* - a comma separated list of integers string of bits to set.
**/
int aml_bitmap_from_string(struct aml_bitmap *bitmap, const char * bitmap_str);
int aml_bitmap_from_string(struct aml_bitmap *bitmap, const char *bitmap_str);
/**
* Bitmap conversion to string. Output strings are 1,0 wrapped in brackets [].
* Returns 0 if conversion went right, else -1.
**/
char * aml_bitmap_to_string(const struct aml_bitmap *bitmap);
char *aml_bitmap_to_string(const struct aml_bitmap *bitmap);
/*******************************************************************************
* create/destroy and others
*******************************************************************************/
/* Not needed, here for consistency */
#define AML_BITMAP_DECL(name) struct aml_bitmap name
#define AML_BITMAP_ALLOCSIZE (sizeof(struct aml_bitmap))
/**
* Allocate a new empty (all zero) struct aml_bitmap.
* @param[out] map pointer to an uninitialized struct aml_bitmap pointer to
* receive the new bitmap.
* @return On success, return 0 and map points to the new bitmap.
* @return On failrure, sets map to NULL and returns one of the AML error codes:
* - AML_ENOMEM if there wasn't enough memory available.
**/
int aml_bitmap_create(struct aml_bitmap **map);
/**
* Initialize (zero a struct aml_bitmap). Not necessary on stack allocated
* bitmaps.
* @return 0 on success (always).
**/
int aml_bitmap_init(struct aml_bitmap *map);
/**
* Finalize a struct aml_bitmap. This is an empty function.
**/
void aml_bitmap_fini(struct aml_bitmap *map);
/**
* Destroy (finalize and free resources) for a struct aml_bitmap created by
* aml_bitmap_create.
*
* @param map is NULL after this call.
**/
void aml_bitmap_destroy(struct aml_bitmap **map);
/**
* On heap allocation of a copy of an existing bitmap.
**/
int aml_bitmap_dup(struct aml_bitmap **dst, const struct aml_bitmap *src);
#endif //AML_BITMAP_H
......@@ -16,14 +16,9 @@
#define AML_BITMAP_NTH(i) ((i) / AML_BITMAP_NBITS)
#define AML_BITMAP_ITH(i) (((i) % AML_BITMAP_NBITS))
struct aml_bitmap * aml_bitmap_create(void)
{
struct aml_bitmap *b = malloc(sizeof(struct aml_bitmap));
if(b == NULL)
return NULL;
aml_bitmap_zero(b);
return b;
}
/*******************************************************************************
* General operators
*******************************************************************************/
void aml_bitmap_copy(struct aml_bitmap *dst, const struct aml_bitmap *src)
{
......@@ -58,19 +53,6 @@ void aml_bitmap_copy_to_ulong(const struct aml_bitmap *dst,
src[AML_BITMAP_NTH(i)] |= (1UL << AML_BITMAP_ITH(i));
}
struct aml_bitmap *aml_bitmap_dup(const struct aml_bitmap *a)
{
struct aml_bitmap *b = aml_bitmap_create();
if(b == NULL)
return NULL;
aml_bitmap_copy(b, a);
return b;
}
void aml_bitmap_destroy(struct aml_bitmap *bitmap)
{
free(bitmap);
}
void aml_bitmap_zero(struct aml_bitmap *bitmap)
{
......@@ -319,3 +301,67 @@ aml_bitmap_from_string(struct aml_bitmap *bitmap, const char * bitmap_str){
return 0;
}
/*******************************************************************************
* create/destroy and others
*******************************************************************************/
int aml_bitmap_create(struct aml_bitmap **map)
{
struct aml_bitmap *b = malloc(sizeof(struct aml_bitmap));
if(b == NULL)
{
*map = NULL;
return -AML_ENOMEM;
}
aml_bitmap_zero(b);
*map = b;
return 0;
}
/**
* Initialize (zero a struct aml_bitmap). Not necessary on stack allocated
* bitmaps.
* @return 0 on success (always).
**/
int aml_bitmap_init(struct aml_bitmap *map)
{
aml_bitmap_zero(map);
return 0;
}
/**
* Finalize a struct aml_bitmap. This is an empty function.
**/
void aml_bitmap_fini(struct aml_bitmap *map)
{
}
/**
* Destroy (finalize and free resources) for a struct aml_bitmap created by
* aml_bitmap_create.
*
* @param map is NULL after this call.
**/
void aml_bitmap_destroy(struct aml_bitmap **map)
{
free(*map);
*map = NULL;
}
/**
* On heap allocation of a copy of an existing bitmap.
**/
int aml_bitmap_dup(struct aml_bitmap **dst, const struct aml_bitmap *src)
{
struct aml_bitmap *ret;
int err;
err = aml_bitmap_create(&ret);
if(err) {
*dst = NULL;
return err;
}
aml_bitmap_copy(ret, src);
*dst = ret;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment