2018-12-25 19:09:58 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
|
|
|
// Authors: spectral` //
|
|
|
|
// NeoX //
|
|
|
|
// //
|
|
|
|
// Desc: mem*() functions //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2018-12-31 10:49:08 +01:00
|
|
|
// XXX to be improved before being brought back
|
|
|
|
// to be tested with more alignment sizes
|
|
|
|
|
|
|
|
#include <kaleid/common/stdlib.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;
|
|
|
|
}
|
2018-12-25 19:09:58 +01:00
|
|
|
|
2018-12-28 18:52:55 +01:00
|
|
|
//
|
|
|
|
// Set "bytes"-many bytes starting from ptr to val
|
|
|
|
//
|
2018-12-28 20:50:28 +01:00
|
|
|
void *memset(void *ptr, int val, size_t bytes)
|
2018-12-28 18:52:55 +01:00
|
|
|
{
|
2018-12-28 20:49:43 +01:00
|
|
|
uchar *uptr = (uchar *)ptr;
|
2018-12-31 10:49:08 +01:00
|
|
|
const size_t qwords = bytes/QWORD_SIZE;
|
2018-12-28 18:52:55 +01:00
|
|
|
|
2018-12-31 10:49:08 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2018-12-28 18:52:55 +01:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set "bytes"-many bytes starting from ptr to 0
|
|
|
|
//
|
|
|
|
void *memzero(void *ptr, size_t bytes)
|
|
|
|
{
|
2018-12-31 10:49:08 +01:00
|
|
|
return memset(ptr, 0, bytes);
|
2018-12-28 18:52:55 +01:00
|
|
|
}
|
|
|
|
|