diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index 121a50f..db57a20 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -47,6 +47,10 @@ # define unlikely(x) (__builtin_expect((x), 0)) #endif +#ifndef alignof +# define alignof _Alignof +#endif + #ifndef INITOK # define INITOK ((unsigned int)0xCAFEBABE) #endif @@ -82,6 +86,14 @@ typedef void *va_list; // XXX limits +#define WORD_SIZE sizeof(ushort) +#define DWORD_SIZE sizeof(uint) +#define QWORD_SIZE sizeof(ulong) + +#define WORD_ALIGN alignof(ushort) +#define DWORD_ALIGN alignof(uint) +#define QWORD_ALIGN alignof(ulong) + //------------------------------------------// // VALUES FOR "status_t" // //------------------------------------------// @@ -129,12 +141,12 @@ const char *describe_status(status_t); #if !defined(_NO_DEBUG) && !defined(NDEBUG) // uses panic() in kernel, abort() in system -noreturn void ___assert_handler(const char *, const char *, int, const char *); +noreturn void _assert_handler(const char *, const char *, int, const char *); #define DosAssert(x) \ do { \ if unlikely(!(x)) \ - ___assert_handler(#x, __FILE__, __LINE__, __func__); \ + _assert_handler(#x, __FILE__, __LINE__, __func__); \ } while (FALSE); #define assert DosAssert diff --git a/src/kaleid/common/folder.desc b/src/kaleid/common/folder.desc index 124f60c..eaf5c24 100644 --- a/src/kaleid/common/folder.desc +++ b/src/kaleid/common/folder.desc @@ -22,10 +22,10 @@ This folder contains the following files: - Defines the macro "assert()". Currently any program wanting to use this macro has to implement its own handler: - noreturn void ___assert_handler(const char *cond, - const char *file, - int line, - const char *func) + noreturn void _assert_handler(const char *cond, + const char *file, + int line, + const char *func) but an overridable default handler will be furnished later. - stlib.h diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 7d40011..196cdcc 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -7,22 +7,19 @@ // Desc: mem*() functions // //----------------------------------------------------------------------------// -// XXX to be improved before being brought back -// to be tested with more alignment sizes - #include -// to be moved -#define QWORD_SIZE 8 -#define QWORD_ALIGN 8 +//------------------------------------------// +// memset() family // +//------------------------------------------// // // Set "qwords"-many aligned qwords starting from ptr to val // -static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +static inline void *_memset_internal(void *ptr, ulong uval, size_t qwords) { size_t n; - ullong *uptr = (ullong *)ptr; + ulong *uptr = (ulong *)ptr; // aligned memory write for (n = 0; n < qwords; n++) { @@ -44,18 +41,18 @@ void *memset(void *ptr, int val, size_t bytes) val = val & 0xFF; // deal with bytes before start of the first aligned qword - while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { + while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { *uptr++ = (uchar)val; } // move qword by qword if (qwords) { - const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) - | ((ullong)val << 40) | ((ullong)val << 32) - | ((ullong)val << 24) | ((ullong)val << 16) - | ((ullong)val << 8) | ((ullong)val); + const ulong uval = ((ulong)val << 56) | ((ulong)val << 48) + | ((ulong)val << 40) | ((ulong)val << 32) + | ((ulong)val << 24) | ((ulong)val << 16) + | ((ulong)val << 8) | ((ulong)val); - memsetq(uptr, uval, qwords); + _memset_internal(uptr, uval, qwords); uptr += qwords * QWORD_SIZE; bytes %= QWORD_SIZE; @@ -69,11 +66,75 @@ void *memset(void *ptr, int val, size_t bytes) return ptr; } +// +// Set "words"-many words starting from ptr to val +// +void *memsetw(void *ptr, int val, size_t words) +{ + ushort *uptr = (ushort *)ptr; + + // get rid of everything after the first word + val = val & 0xFFFF; + + // can we do this an aligned way? + if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { + // no, we can't align ourselves + while (words--) { + // do it the hard way + *uptr++ = (ushort)val; + } + // too bad '-' + return uptr; + } + + // deal with words before start of the first aligned qword + while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) { + *uptr++ = (ushort)val; + } + + const size_t qwords = (words * WORD_SIZE)/QWORD_SIZE; + + // move qword by qword + if (qwords) { + const ulong uval = ((ulong)val << 48) | ((ulong)val << 32) + | ((ulong)val << 16) | ((ulong)val); + + _memset_internal(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE / WORD_SIZE; + words %= QWORD_SIZE / WORD_SIZE; + } + + // deal with what's left + while (words--) { + *uptr++ = (ushort)val; + } + + return ptr; +} + +// +// Set "qwords"-many qwords starting from ptr to val +// +void *memsetq(void *ptr, long val, size_t qwords) +{ + return _memset_internal(ptr, (ulong)val, qwords); +} + // // Set "bytes"-many bytes starting from ptr to 0 // void *memzero(void *ptr, size_t bytes) { + // is direct aligned access possible? (is "unlikely" good here?) + if unlikely (bytes % QWORD_SIZE && (ulong)ptr % QWORD_ALIGN) { + return _memset_internal(ptr, (ulong)0, bytes/QWORD_SIZE); + } + + if unlikely (bytes % WORD_SIZE && (ulong)ptr % WORD_ALIGN) { + return memsetw(ptr, (int)0, bytes/WORD_SIZE); + } + return memset(ptr, 0, bytes); } diff --git a/src/kaleid/common/stdlib.h b/src/kaleid/common/stdlib.h index e267a4e..b4af1f3 100644 --- a/src/kaleid/common/stdlib.h +++ b/src/kaleid/common/stdlib.h @@ -8,8 +8,8 @@ //----------------------------------------------------------------------------// -#ifndef _KALCOMM_STRING_H -#define _KALCOMM_STRING_H +#ifndef _KALCOMM_STDLIB_H +#define _KALCOMM_STDLIB_H #ifndef _KALCOMM_COMMON_H # include @@ -21,12 +21,16 @@ #ifndef _OSK_SOURCE # define memcpy _osk_memcpy -# define memset _osk_memset # define memcmp _osk_memcmp # define memzero _osk_memzero #endif +#define memsetb memset void *memset(void *, int, size_t); +void *memsetw(void *, int, size_t); +void *memsetd(void *, int, size_t); +void *memsetq(void *, long, size_t); + void *memzero(void *, size_t); //------------------------------------------// diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 633cbf7..aaad786 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -21,7 +21,7 @@ const char *__panicmsg = NULL; // // Failed assert() handler // -noreturn void ___assert_handler(const char *msg, +noreturn void _assert_handler(const char *msg, const char *file, int line, const char *func) diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index b23e00a..cbce8cb 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -9,11 +9,9 @@ #include #include -#define KEEP_KALCOMM_TYPES_MINIMAL -#include -#include -#include +#define KEEP_KALCOMM_TYPES_MINIMAL +#include int main(int argc, char *argv[]) { @@ -31,37 +29,42 @@ int main(int argc, char *argv[]) //char *test2 = malloc(size1); //char *test3 = malloc(size1); - //const size_t sizex = 10000000; - //void *xxx = malloc(sizex); - //printf("%p\n",xxx); - //memset(xxx, 'x', sizex); + const size_t sizex = 130; + short *xxx = (short *)malloc(sizex * sizeof(short)); + //printf("%ld\n",(ulong)xxx%8); + //memzero(xxx, sizex); + memsetw(xxx, 300, sizex); - const char *str = "ceci est un string de test!"; - char *str2 = malloc((strlen(str) + 3) * sizeof(char)); - char *str3 = malloc((strlen(str) + 3) * sizeof(char)); + size_t it; + for (it = 0; it < sizex; it++) { + short s = *(xxx + it); + printf("%hd", s); + } + + puts(""); + + //free((void *)xxx); + + //const char *str = "ceci est un string de test!"; + //char *str2 = malloc((strlen(str) + 3) * sizeof(char)); + //char *str3 = malloc((strlen(str) + 3) * sizeof(char)); - strcpy(str2, str); + //strcpy(str2, str); //size_t s = strlen(str2); - strrev(str3,str2); - printf("%lu - %s\n", strlen(str3), str3); + //strrev(str3,str2); + //printf("%lu - %s\n", strlen(str3), str3); //str2[s] = '\n'; //str2[s+1] = '\0'; - strrev(str2,str3); - printf("%lu - %s\n", strlen(str2), str2); + //strrev(str2,str3); + //printf("%lu - %s\n", strlen(str2), str2); - free(str2); - free(str3); + //free(str2); + //free(str3); - /*size_t it; - for (it = 0; it < sizex; it++) { - char c = *(xxx + it); - printf("%c", (c ? c : '-')); - } - puts("");*/ //free(test2); //free(test3);