diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 70c4dbd..8d85d9c 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -9,62 +9,15 @@ #include "common/memory.h" -// to be moved -#define QWORD_SIZE 8 -#define QWORD_ALIGN 8 - -// -// Set "qwords"-many aligned qwords starting from ptr to val -// -static inline void *memsetq(void *ptr, ullong uval, size_t qwords) -{ - size_t n; - ullong *uptr = (ullong *)ptr; - - // aligned memory write - for (n = 0; n < qwords; n++) { - *(uptr + n) = uval; - } - - return ptr; -} - // // Set "bytes"-many bytes starting from ptr to val -// This is most certainely overkill, but I enjoyed doing this -// Alignment stuff barely matters on modern processors -// This may actually be slower than the naive way // void *memset(void *ptr, register int val, register size_t bytes) { - register uchar *uptr = (uchar *)ptr; - const size_t qwords = bytes/QWORD_SIZE; + uchar uval = val & 0xFF; + uchar *uptr = (uchar *)ptr; - // get rid of everything after the first byte - val = val & 0xFF; - - // deal with bytes before start of the first aligned qword - while (((ullong)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); - - memsetq(uptr, uval, qwords); - - uptr += qwords * QWORD_SIZE; - bytes %= QWORD_SIZE; - } - - // deal with what's left - while (bytes--) { - *uptr++ = (uchar)val; - } + while (bytes--) *uptr++ = uval; return ptr; } @@ -74,6 +27,11 @@ void *memset(void *ptr, register int val, register size_t bytes) // void *memzero(void *ptr, size_t bytes) { - return memset(ptr, 0, bytes); + uchar *uptr = (uchar *)ptr; + + while (bytes--) *uptr++ = 0; + + return ptr; } + diff --git a/src/kaleid/common/sub/memory.c b/src/kaleid/common/sub/memory.c new file mode 100644 index 0000000..8a4585a --- /dev/null +++ b/src/kaleid/common/sub/memory.c @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: mem*() functions - sub-optimal version // +//----------------------------------------------------------------------------// + +#include "common/memory.h" + +// to be moved +#define QWORD_SIZE 8 +#define QWORD_ALIGN 8 + +// +// Set "qwords"-many aligned qwords starting from ptr to val +// +static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +{ + size_t n; + ullong *uptr = (ullong *)ptr; + + // aligned memory write + for (n = 0; n < qwords; n++) { + *(uptr + n) = uval; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to val +// This is most certainely overkill, but I enjoyed doing this +// Alignment stuff barely matters on modern processors +// +void *memset(void *ptr, register int val, register size_t bytes) +{ + register uchar *uptr = (uchar *)ptr; + const size_t qwords = bytes/QWORD_SIZE; + + // get rid of everything after the first byte + val = val & 0xFF; + + // deal with bytes before start of the first aligned qword + while (((ullong)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); + + memsetq(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE; + bytes %= QWORD_SIZE; + } + + // deal with what's left + while (bytes--) { + *uptr++ = (uchar)val; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + return memset(ptr, 0, bytes); +} + diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index bef4749..127d07b 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -21,35 +21,32 @@ int main(int argc, char *argv[]) (void)argc; (void)argv; - assert(sizeof(char) == 1); - assert(sizeof(short) == 2); - assert(sizeof(int) == 4); - assert(sizeof(long) == 8); - assert(sizeof(long long) == 8); - // please don't mind how weird this file is // I remove tests of "simple" functions after // I'm done with testing that function // so a lot of test variables remain in case // I need them for a future test - const char *test1 = "test string\n"; - const size_t size1 = strlen(test1); - char *test2 = malloc(size1); - char *test3 = malloc(size1); + //const char *test1 = "test string\n"; + //const size_t size1 = strlen(test1); + //char *test2 = malloc(size1); + //char *test3 = malloc(size1); - memset(test2, 'x', size1); + //const size_t sizex = 10000000; + //void *xxx = malloc(sizex); + //printf("%p\n",xxx); + //memset(xxx, 'x', sizex); - size_t it; - for (it = 0; it < size1; it++) { - char c = *(test2 + it); + /*size_t it; + for (it = 0; it < sizex; it++) { + char c = *(xxx + it); printf("%c", (c ? c : '-')); } - puts(""); + puts("");*/ - free(test2); - free(test3); + //free(test2); + //free(test3); return 0; }