mirror of
https://github.com/drasko/codezero.git
synced 2026-02-07 15:33:16 +01:00
Initial commit
This commit is contained in:
98
tasks/mm0/tests/idpool_test/bit.c
Normal file
98
tasks/mm0/tests/idpool_test/bit.c
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Bit manipulation functions.
|
||||
*
|
||||
* Copyright (C) 2007 Bahadir Balban
|
||||
*/
|
||||
#include "bit.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/* Emulation of ARM's CLZ (count leading zeroes) instruction */
|
||||
unsigned int __clz(unsigned int bitvector)
|
||||
{
|
||||
unsigned int x = 0;
|
||||
while((!(bitvector & ((unsigned)1 << 31))) && (x < 32)) {
|
||||
bitvector <<= 1;
|
||||
x++;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int find_and_set_first_free_bit(u32 *word, unsigned int limit)
|
||||
{
|
||||
int success = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0; i < limit; i++) {
|
||||
/* Find first unset bit */
|
||||
if (!(word[BITWISE_GETWORD(i)] & BITWISE_GETBIT(i))) {
|
||||
/* Set it */
|
||||
word[BITWISE_GETWORD(i)] |= BITWISE_GETBIT(i);
|
||||
success = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return bit just set */
|
||||
if (success)
|
||||
return i;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
int find_and_set_first_free_contig_bits(u32 *word, unsigned int limit,
|
||||
int nbits)
|
||||
{
|
||||
int i = 0, first = 0, last = 0, found = 0;
|
||||
|
||||
/* Can't allocate more than the limit */
|
||||
if (nbits > limit)
|
||||
return -1;
|
||||
|
||||
/* This is a state machine that checks n contiguous free bits. */
|
||||
while (i < limit) {
|
||||
first = i;
|
||||
last = i;
|
||||
while (!(word[BITWISE_GETWORD(last)] & BITWISE_GETBIT(last))) {
|
||||
last++;
|
||||
i++;
|
||||
if (last == first + nbits) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
if (i == limit)
|
||||
break;
|
||||
}
|
||||
if (found)
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
|
||||
/* If found, set the bits */
|
||||
if (found) {
|
||||
for (int x = first; x < first + nbits; x++)
|
||||
word[BITWISE_GETWORD(x)] |= BITWISE_GETBIT(x);
|
||||
return first;
|
||||
} else
|
||||
return -1;
|
||||
}
|
||||
|
||||
int check_and_clear_bit(u32 *word, int bit)
|
||||
{
|
||||
/* Check that bit was set */
|
||||
if (word[BITWISE_GETWORD(bit)] & BITWISE_GETBIT(bit)) {
|
||||
word[BITWISE_GETWORD(bit)] &= ~BITWISE_GETBIT(bit);
|
||||
return 0;
|
||||
} else {
|
||||
printf("Trying to clear already clear bit\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int check_and_clear_contig_bits(u32 *word, int first, int nbits)
|
||||
{
|
||||
for (int i = first; i < first + nbits; i++)
|
||||
if (check_and_clear_bit(word, i) < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
51
tasks/mm0/tests/idpool_test/bit.h
Normal file
51
tasks/mm0/tests/idpool_test/bit.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef __LIB_BIT_H__
|
||||
#define __LIB_BIT_H__
|
||||
|
||||
/* Minimum excess needed for word alignment */
|
||||
#define SZ_WORD sizeof(unsigned int)
|
||||
#define WORD_BITS 32
|
||||
#define WORD_BITS_LOG2 5
|
||||
#define BITWISE_GETWORD(x) ((x) >> WORD_BITS_LOG2) /* Divide by 32 */
|
||||
#define BITWISE_GETBIT(x) (1 << ((x) % WORD_BITS))
|
||||
|
||||
|
||||
typedef unsigned int u32;
|
||||
|
||||
unsigned int __clz(unsigned int bitvector);
|
||||
int find_and_set_first_free_bit(u32 *word, unsigned int lastbit);
|
||||
int check_and_clear_bit(u32 *word, int bit);
|
||||
|
||||
int check_and_clear_contig_bits(u32 *word, int first, int nbits);
|
||||
|
||||
int find_and_set_first_free_contig_bits(u32 *word, unsigned int limit,
|
||||
int nbits);
|
||||
/* Set */
|
||||
static inline void setbit(unsigned int *w, unsigned int flags)
|
||||
{
|
||||
*w |= flags;
|
||||
}
|
||||
|
||||
|
||||
/* Clear */
|
||||
static inline void clrbit(unsigned int *w, unsigned int flags)
|
||||
{
|
||||
*w &= ~flags;
|
||||
}
|
||||
|
||||
/* Test */
|
||||
static inline int tstbit(unsigned int *w, unsigned int flags)
|
||||
{
|
||||
return *w & flags;
|
||||
}
|
||||
|
||||
/* Test and clear */
|
||||
static inline int tstclr(unsigned int *w, unsigned int flags)
|
||||
{
|
||||
int res = tstbit(w, flags);
|
||||
|
||||
clrbit(w, flags);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif /* __LIB_BIT_H__ */
|
||||
63
tasks/mm0/tests/idpool_test/idpool.c
Normal file
63
tasks/mm0/tests/idpool_test/idpool.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Used for thread and space ids.
|
||||
*
|
||||
* Copyright (C) 2007 Bahadir Balban
|
||||
*/
|
||||
#include "idpool.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct id_pool *id_pool_new_init(int totalbits)
|
||||
{
|
||||
int nwords = BITWISE_GETWORD(totalbits);
|
||||
struct id_pool *new = calloc(1, (nwords * SZ_WORD)
|
||||
+ sizeof(struct id_pool));
|
||||
new->nwords = nwords;
|
||||
return new;
|
||||
}
|
||||
|
||||
int id_new(struct id_pool *pool)
|
||||
{
|
||||
int id = find_and_set_first_free_bit(pool->bitmap,
|
||||
pool->nwords * WORD_BITS);
|
||||
if (id < 0)
|
||||
printf("%s: Warning! New id alloc failed\n", __FUNCTION__);
|
||||
return id;
|
||||
}
|
||||
|
||||
/* This finds n contiguous free ids, allocates and returns the first one */
|
||||
int ids_new_contiguous(struct id_pool *pool, int numids)
|
||||
{
|
||||
printf("%s: Enter\n", __FUNCTION__);
|
||||
int id = find_and_set_first_free_contig_bits(pool->bitmap,
|
||||
pool->nwords *WORD_BITS,
|
||||
numids);
|
||||
if (id < 0)
|
||||
printf("%s: Warning! New id alloc failed\n", __FUNCTION__);
|
||||
return id;
|
||||
}
|
||||
|
||||
/* This deletes a list of contiguous ids given the first one and number of ids */
|
||||
int ids_del_contiguous(struct id_pool *pool, int first, int numids)
|
||||
{
|
||||
int ret;
|
||||
printf("bits: %d, first +nids: %d\n", pool->nwords *WORD_BITS, first+numids);
|
||||
if (pool->nwords * WORD_BITS < first + numids)
|
||||
return -1;
|
||||
if ((ret = check_and_clear_contig_bits(pool->bitmap, first, numids)))
|
||||
printf("Warning!!!\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int id_del(struct id_pool *pool, int id)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (pool->nwords * WORD_BITS < id)
|
||||
return -1;
|
||||
|
||||
if ((ret = check_and_clear_bit(pool->bitmap, id) < 0))
|
||||
printf("Warning!!!\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
17
tasks/mm0/tests/idpool_test/idpool.h
Normal file
17
tasks/mm0/tests/idpool_test/idpool.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef __MM0_IDPOOL_H__
|
||||
#define __MM0_IDPOOL_H__
|
||||
|
||||
#include "bit.h"
|
||||
|
||||
struct id_pool {
|
||||
int nwords;
|
||||
u32 bitmap[];
|
||||
};
|
||||
|
||||
struct id_pool *id_pool_new_init(int mapsize);
|
||||
int id_new(struct id_pool *pool);
|
||||
int id_del(struct id_pool *pool, int id);
|
||||
|
||||
int ids_new_contiguous(struct id_pool *pool, int numids);
|
||||
int ids_del_contiguous(struct id_pool *pool, int first, int numids);
|
||||
#endif /* __MM0_IDPOOL_H__ */
|
||||
58
tasks/mm0/tests/idpool_test/main.c
Normal file
58
tasks/mm0/tests/idpool_test/main.c
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
|
||||
|
||||
#include "bit.h"
|
||||
#include "idpool.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define CTOTAL 3
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct id_pool *pool = id_pool_new_init(64);
|
||||
int id_array[64];
|
||||
int first;
|
||||
|
||||
|
||||
if ((first = ids_new_contiguous(pool, 64)) < 0)
|
||||
printf("%d contig ids not allocated.\n", 64);
|
||||
else
|
||||
printf("%d contig ids allocated starting from %d\n", 64, first);
|
||||
|
||||
if (ids_del_contiguous(pool, 5, 60) == 0)
|
||||
printf("%d contig ids freed with success.\n", 64);
|
||||
else
|
||||
printf("%d-%d contig ids could not be freed\n", 1, 65);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct id_pool *pool = id_pool_new_init(64);
|
||||
int id_array[64];
|
||||
int first;
|
||||
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
id_array[i] = id_new(pool);
|
||||
printf("Allocated id: %d\n", id_array[i]);
|
||||
}
|
||||
if ((first = ids_new_contiguous(pool, CTOTAL)) < 0)
|
||||
printf("%d contig ids not allocated as expected.\n", CTOTAL);
|
||||
|
||||
printf("Now freeing id_array[30 - 32]\n");
|
||||
ids_del_contiguous(pool, id_array[30], 3);
|
||||
ids_del_contiguous(pool, id_array[35], 9);
|
||||
if ((first = ids_new_contiguous(pool, CTOTAL + 3)) < 0)
|
||||
printf("%d contig ids not allocated.\n", CTOTAL + 3);
|
||||
else
|
||||
printf("%d contig ids allocated starting from %d\n", CTOTAL + 3, first);
|
||||
|
||||
if ((first = ids_new_contiguous(pool, CTOTAL)) < 0)
|
||||
printf("Error: %d contig ids not allocated.\n", CTOTAL);
|
||||
else
|
||||
printf("%d contig ids allocated as expected starting from %d\n", CTOTAL, first);
|
||||
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user