From e9683297bf480f9590b0e5796f4520fb430e2e03 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Tue, 1 May 2012 17:42:36 +0200 Subject: Now using Doug Lea's malloc for userland too. And improved stability. --- src/common/Makefile | 2 +- src/common/include/sched.h | 15 +++++++++++++++ src/common/include/stdlib.h | 14 -------------- src/common/include/stdlib_common.h | 14 ++++++++++++++ src/common/sched.c | 26 ++++++++++++++++++++++++++ src/common/string.c | 7 ++++--- 6 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 src/common/include/sched.h delete mode 100644 src/common/include/stdlib.h create mode 100644 src/common/include/stdlib_common.h create mode 100644 src/common/sched.c (limited to 'src/common') diff --git a/src/common/Makefile b/src/common/Makefile index ef0f6b1..8698100 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -1,5 +1,5 @@ Out = _common.o -Obj = string.o +Obj = string.o sched.o ExtObj = include $(SrcPath)/common.make diff --git a/src/common/include/sched.h b/src/common/include/sched.h new file mode 100644 index 0000000..0f8f8f4 --- /dev/null +++ b/src/common/include/sched.h @@ -0,0 +1,15 @@ +#ifndef DEF_MUTEX_H +#define DEF_MUTEX_H + +#include + +#define MUTEX_LOCKED 1 +#define MUTEX_UNLOCKED 0 + +//A mutex is just an uint32_t + +int mutex_lock(uint32_t* mutex); //wait for mutex to be free +int mutex_lockE(uint32_t* mutex); //lock mutex only if free, returns !0 if locked, 0 if was busy +void mutex_unlock(uint32_t* mutex); + +#endif diff --git a/src/common/include/stdlib.h b/src/common/include/stdlib.h deleted file mode 100644 index 2c5e6dd..0000000 --- a/src/common/include/stdlib.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _DEF_STDLIB_H -#define _DEF_STDLIB_H - -#include - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) - -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); - -#endif - diff --git a/src/common/include/stdlib_common.h b/src/common/include/stdlib_common.h new file mode 100644 index 0000000..80c188f --- /dev/null +++ b/src/common/include/stdlib_common.h @@ -0,0 +1,14 @@ +#ifndef _DEF_STDLIB_H +#define _DEF_STDLIB_H + +#include + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +void *memcpy(void *dest, const void *src, int count); +void *memset(void *dest, int val, int count); +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); + +#endif + diff --git a/src/common/sched.c b/src/common/sched.c new file mode 100644 index 0000000..29ea8a8 --- /dev/null +++ b/src/common/sched.c @@ -0,0 +1,26 @@ +#include + +/* Internal use only. This function is atomic, meaning it cannot be interrupted by a system task switch. */ +static uint32_t atomic_exchange(uint32_t* ptr, uint32_t newval) { + uint32_t r; + asm volatile("xchg (%%ecx), %%eax" : "=a"(r) : "c"(ptr), "a"(newval)); + return r; +} + +int mutex_lock(uint32_t* mutex) { + while (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) { + schedule(); + } + return 0; +} + +int mutex_lockE(uint32_t* mutex) { + if (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) { + return 0; + } + return 1; +} + +void mutex_unlock(uint32_t* mutex) { + *mutex = MUTEX_UNLOCKED; +} diff --git a/src/common/string.c b/src/common/string.c index b2ec309..120fd5d 100644 --- a/src/common/string.c +++ b/src/common/string.c @@ -1,5 +1,5 @@ #include -#include +#include int strlen(const char *str) { int i = 0; @@ -63,10 +63,11 @@ void *memcpy(void *vd, const void *vs, int count) { return vd; } -uint8_t *memset(uint8_t *dest, uint8_t val, int count) { +void *memset(void *dest, int val, int count) { + uint8_t *dest_c = (uint8_t*)dest; int i; for (i = 0; i < count; i++) { - dest[i] = val; + dest_c[i] = val; } return dest; } -- cgit v1.2.3