diff options
Diffstat (limited to 'src/kernel/lib')
-rw-r--r-- | src/kernel/lib/bitset.c | 35 | ||||
-rw-r--r-- | src/kernel/lib/bitset.h | 19 | ||||
-rw-r--r-- | src/kernel/lib/stdlib.c | 39 | ||||
-rw-r--r-- | src/kernel/lib/stdlib.h | 12 | ||||
-rw-r--r-- | src/kernel/lib/types.h | 15 |
5 files changed, 120 insertions, 0 deletions
diff --git a/src/kernel/lib/bitset.c b/src/kernel/lib/bitset.c new file mode 100644 index 0000000..a6d334b --- /dev/null +++ b/src/kernel/lib/bitset.c @@ -0,0 +1,35 @@ +#include "bitset.h" + +void bitset_set(struct bitset* t, uint32_t num) { + uint32_t idx = INDEX_FROM_BIT(num); + uint32_t off = OFFSET_FROM_BIT(num); + t->bits[idx] |= (0x1 << off); +} + +void bitset_clear(struct bitset* t, uint32_t num) { + uint32_t idx = INDEX_FROM_BIT(num); + uint32_t off = OFFSET_FROM_BIT(num); + t->bits[idx] &= ~(0x1 << off); +} + +uint32_t bitset_test(struct bitset* t, uint32_t num) { + uint32_t idx = INDEX_FROM_BIT(num); + uint32_t off = OFFSET_FROM_BIT(num); + return (t->bits[idx] & (0x1 << off)); +} + +uint32_t bitset_firstFree(struct bitset* t) { + uint32_t i, j; + for (i = 0; i < INDEX_FROM_BIT(t->size); i++) { + if (t->bits[i] != 0xFFFFFFFF) { + for (j = 0; j < 32; j++) { + uint32_t toTest = 0x1 << j; + if (!(t->bits[i] & toTest)) { + return i*4*8+j; + } + } + } + } + return (uint32_t) - 1; +} + diff --git a/src/kernel/lib/bitset.h b/src/kernel/lib/bitset.h new file mode 100644 index 0000000..fe9e8c2 --- /dev/null +++ b/src/kernel/lib/bitset.h @@ -0,0 +1,19 @@ +#ifndef DEF_BITSET_H +#define DEF_BITSET_H + +#include <types.h> + +#define INDEX_FROM_BIT(a) (a/(8*4)) +#define OFFSET_FROM_BIT(a) (a%(8*4)) + +struct bitset { + uint32_t *bits; + uint32_t size; +}; + +void bitset_set(struct bitset* t, uint32_t num); +void bitset_clear(struct bitset* t, uint32_t num); +uint32_t bitset_test(struct bitset* t, uint32_t num); +uint32_t bitset_firstFree(struct bitset* t); + +#endif diff --git a/src/kernel/lib/stdlib.c b/src/kernel/lib/stdlib.c new file mode 100644 index 0000000..c5245e7 --- /dev/null +++ b/src/kernel/lib/stdlib.c @@ -0,0 +1,39 @@ +#include "stdlib.h" + +void *memcpy(void *vd, const void *vs, int count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + uint32_t f = count % 4, n = count / 4, i; + const uint32_t* s = (uint32_t*)src; + uint32_t* d = (uint32_t*)dest; + for (i = 0; i < n; i++) { + d[i] = s[i]; + } + if (f != 0) { + for (i = count - f; i < count; i++) { + dest[i] = src[i]; + } + } + return vd; +} + +uint8_t *memset(uint8_t *dest, uint8_t val, int count) { + int i; + for (i = 0; i < count; i++) { + dest[i] = val; + } + return dest; +} + +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { + int i; + for (i = 0; i < count; i++) { + dest[i] = val; + } + return dest; +} + +int strlen(const char *str) { + int i = 0; + while (str[i++]); + return i; +} diff --git a/src/kernel/lib/stdlib.h b/src/kernel/lib/stdlib.h new file mode 100644 index 0000000..704c410 --- /dev/null +++ b/src/kernel/lib/stdlib.h @@ -0,0 +1,12 @@ +#ifndef DEF_STDLIB_H +#define DEF_STDLIB_H + +#include <types.h> + +void *memcpy(void *dest, const void *src, int count); +uint8_t *memset(uint8_t *dest, uint8_t val, int count); +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); +int strlen(const char *str); + +#endif + diff --git a/src/kernel/lib/types.h b/src/kernel/lib/types.h new file mode 100644 index 0000000..b7b19ab --- /dev/null +++ b/src/kernel/lib/types.h @@ -0,0 +1,15 @@ +#ifndef DEF_TYPES_H +#define DEF_TYPES_H + +typedef unsigned long long uint64_t; +typedef unsigned int uint32_t; +typedef unsigned short uint16_t; +typedef unsigned char uint8_t; +typedef long long int64_t; +typedef int int32_t; +typedef short int16_t; +typedef char int8_t; + +typedef unsigned int size_t; + +#endif |