2019-01-21 15:00:04 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-09-27 17:33:48 +02:00
|
|
|
// OS on Kaleid //
|
2019-01-21 15:00:04 +01:00
|
|
|
// //
|
2019-02-16 23:36:33 +01:00
|
|
|
// Desc: mem*() family //
|
2019-01-21 15:00:04 +01:00
|
|
|
// //
|
2019-02-16 23:36:33 +01:00
|
|
|
// //
|
2020-02-06 14:23:26 +01:00
|
|
|
// Copyright © 2018-2020 The OS/K Team //
|
2019-02-16 23:36:33 +01:00
|
|
|
// //
|
|
|
|
// This file is part of OS/K. //
|
|
|
|
// //
|
|
|
|
// OS/K is free software: you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or //
|
|
|
|
// any later version. //
|
|
|
|
// //
|
|
|
|
// OS/K is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
2019-01-21 15:00:04 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2019-05-13 23:22:27 +02:00
|
|
|
#include <libc.h>
|
|
|
|
#include <ex/malloc.h>
|
2019-01-21 15:00:04 +01:00
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
/* DO NOT compile with strict aliasing on */
|
|
|
|
|
2019-03-18 17:25:44 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
// Memory allocation //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
void *malloc(size_t n)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
error_t rc;
|
|
|
|
|
2019-05-15 16:39:20 +02:00
|
|
|
#ifndef _KALEID_KERNEL
|
2019-04-06 07:53:58 +02:00
|
|
|
rc = KalAllocMemory(&ptr, n);
|
2019-05-15 16:39:20 +02:00
|
|
|
#else
|
2019-05-15 16:39:36 +02:00
|
|
|
rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0);
|
2019-05-15 16:39:20 +02:00
|
|
|
#endif
|
2019-03-25 17:33:51 +01:00
|
|
|
if (rc > 0) seterrno(rc);
|
2019-03-18 17:25:44 +01:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:21:07 +01:00
|
|
|
void *memalign(size_t n, size_t align)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
error_t rc;
|
|
|
|
|
|
|
|
#ifndef _KALEID_KERNEL
|
|
|
|
rc = KalAllocMemoryEx(&ptr, n, 0, align);
|
|
|
|
#else
|
|
|
|
rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, align);
|
|
|
|
#endif
|
|
|
|
if (rc > 0) seterrno(rc);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:33:51 +01:00
|
|
|
void *calloc(size_t n, size_t m)
|
|
|
|
{
|
2019-04-06 07:53:58 +02:00
|
|
|
void *ptr;
|
|
|
|
error_t rc;
|
|
|
|
|
|
|
|
rc = KalAllocMemoryEx(&ptr, n * m, M_ZEROED, 0);
|
|
|
|
if (rc > 0) seterrno(rc);
|
|
|
|
|
|
|
|
return ptr;
|
2019-03-25 17:33:51 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 17:25:44 +01:00
|
|
|
void free(void *ptr)
|
|
|
|
{
|
|
|
|
error_t rc = KalFreeMemory(ptr);
|
|
|
|
(void)rc;
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:00:04 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
// memset() family //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set "bytes"-many bytes starting from ptr to val
|
|
|
|
//
|
|
|
|
void *memset(void *ptr, int val, size_t bytes)
|
|
|
|
{
|
|
|
|
uchar *uptr = (uchar *)ptr;
|
|
|
|
|
|
|
|
// Deal with bytes before start of the first aligned qword
|
2019-02-06 14:07:38 +01:00
|
|
|
while (((ulong)uptr % alignof(QWORD)) > 0 && bytes--) {
|
2019-01-21 15:00:04 +01:00
|
|
|
*uptr++ = (uchar)val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we're qword-aligned
|
2019-02-06 14:07:38 +01:00
|
|
|
if (bytes > sizeof(QWORD)) {
|
2019-01-21 15:00:04 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
ulong *uqptr = (ulong *)ptr;
|
|
|
|
|
|
|
|
// Moving fast, qword by qword
|
2019-02-06 14:07:38 +01:00
|
|
|
while (bytes > sizeof(QWORD)) {
|
|
|
|
bytes -= sizeof(QWORD);
|
2019-01-21 15:00:04 +01:00
|
|
|
*uqptr++ = uval;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr = (uchar *)(ulong)uqptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with the few remaining bytes
|
|
|
|
while (bytes--) *uptr++ = (uchar)val;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2019-05-13 23:22:27 +02:00
|
|
|
#if 0
|
2019-01-21 15:00:04 +01:00
|
|
|
//
|
|
|
|
// Set "words"-many words starting from ptr to val
|
|
|
|
//
|
|
|
|
void *memsetw(void *ptr, int val, size_t words)
|
|
|
|
{
|
|
|
|
ushort *uptr = (ushort *)ptr;
|
|
|
|
|
2019-02-06 14:07:38 +01:00
|
|
|
// Check whether we can we do this a word-aligned way
|
|
|
|
if unlikely (((ulong)uptr % alignof(WORD)) > 0) {
|
2019-01-21 15:00:04 +01:00
|
|
|
// We can't, so we write word by word all the way up
|
|
|
|
while (words--) *uptr++ = (ushort)val;
|
|
|
|
return uptr;
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:07:38 +01:00
|
|
|
while (((ulong)uptr % alignof(QWORD)) > 0 && words--) {
|
2019-01-21 15:00:04 +01:00
|
|
|
*uptr++ = (ushort)val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (words > QWORDS_TO_WORDS(1)) {
|
|
|
|
const ulong uval = ((ulong)val << 48) | ((ulong)val << 32)
|
|
|
|
| ((ulong)val << 16) | ((ulong)val);
|
|
|
|
|
|
|
|
ulong *uqptr = (ulong *)uptr;
|
|
|
|
|
|
|
|
while (words > QWORDS_TO_WORDS(1)) {
|
|
|
|
words -= QWORDS_TO_WORDS(1);
|
|
|
|
*uqptr++ = uval;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr = (ushort *)(ulong)uqptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (words--) *uptr++ = (ushort)val;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
2019-05-13 23:22:27 +02:00
|
|
|
#endif
|
2019-01-21 15:00:04 +01:00
|
|
|
|
|
|
|
// Set "dwords"-many dwords starting from ptr to val
|
|
|
|
// XXX unimplemented
|
|
|
|
void *memsetd(void *ptr, int val, size_t dwords)
|
|
|
|
{
|
|
|
|
(void)val;
|
|
|
|
(void)dwords;
|
2019-03-25 17:33:51 +01:00
|
|
|
seterrno(ENOSYS);
|
2019-01-21 15:00:04 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set "qwords"-many qwords starting from ptr to val
|
|
|
|
void *memsetq(void *ptr, long val, size_t qwords)
|
|
|
|
{
|
|
|
|
ulong *uptr = (ulong *)ptr;
|
|
|
|
|
|
|
|
// There's no need to check for alignment
|
|
|
|
while (qwords--) *uptr++ = (ulong)val;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------//
|
|
|
|
// Other mem*() functions //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set "bytes"-many bytes starting from ptr to 0
|
|
|
|
//
|
|
|
|
void *memzero(void *ptr, size_t bytes)
|
|
|
|
{
|
2019-05-13 23:22:27 +02:00
|
|
|
return memset(ptr, 0, bytes);
|
2019-01-21 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copy "bytes"-many bytes of src to dst
|
|
|
|
// Does not deal with overlapping blocks (memmove's job)
|
|
|
|
//
|
|
|
|
void *memcpy(void *restrict dst, const void *restrict src, size_t bytes)
|
|
|
|
{
|
|
|
|
const ulong *usrc = (const ulong *)src;
|
|
|
|
ulong *udst = (ulong *)dst;
|
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
assert(!"Don't use this, use memmove()");
|
2019-04-23 11:32:45 +02:00
|
|
|
assert((dst + bytes < src || src + bytes < dst) && "memcpy() overlap; use memmove()");
|
|
|
|
|
2019-01-21 15:00:04 +01:00
|
|
|
if unlikely (bytes == 0) return dst;
|
|
|
|
|
|
|
|
// Can align both src and dst at once at once?
|
2019-02-06 14:07:38 +01:00
|
|
|
if unlikely ((ulong)src % alignof(WORD) == 1
|
|
|
|
&& (ulong)dst % alignof(WORD) == 1) {
|
2019-01-21 15:00:04 +01:00
|
|
|
const uchar *ubsrc = (const uchar *)usrc;
|
|
|
|
uchar *ubdst = (uchar *)udst;
|
|
|
|
|
2019-02-06 14:07:38 +01:00
|
|
|
// Yes we can, we're guaranteed to be word-aligned after that
|
2019-01-21 15:00:04 +01:00
|
|
|
*ubdst++ = *ubsrc++;
|
|
|
|
bytes--;
|
|
|
|
|
|
|
|
udst = (ulong *)ubdst;
|
|
|
|
usrc = (ulong *)ubsrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ushort *uwsrc = (const ushort *)usrc;
|
|
|
|
ushort *uwdst = (ushort *)udst;
|
|
|
|
|
|
|
|
// Align either dst or src for qword access
|
2019-02-06 14:07:38 +01:00
|
|
|
while ((ulong)dst % alignof(QWORD) > 0
|
|
|
|
&& (ulong)src % alignof(QWORD) > 0
|
|
|
|
&& bytes > sizeof(WORD)) {
|
2019-01-21 15:00:04 +01:00
|
|
|
|
2019-02-06 14:07:38 +01:00
|
|
|
bytes -= sizeof(WORD);
|
2019-01-21 15:00:04 +01:00
|
|
|
*uwdst++ = *uwsrc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
udst = (ulong *)uwdst;
|
|
|
|
usrc = (ulong *)uwsrc;
|
|
|
|
|
2019-02-06 14:07:38 +01:00
|
|
|
// Copy fast
|
|
|
|
while (bytes > sizeof(QWORD)) {
|
|
|
|
bytes -= sizeof(QWORD);
|
2019-01-21 15:00:04 +01:00
|
|
|
*udst++ = *usrc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uchar *ubsrc = (const uchar *)usrc;
|
|
|
|
ushort *ubdst = (ushort *)udst;
|
|
|
|
|
|
|
|
// Deal with the few bytes left
|
|
|
|
while (bytes--) *ubdst ++ = *ubsrc++;
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Move memory from src to dest, even if they overlap
|
|
|
|
//
|
|
|
|
void *memmove(void *dst, const void *src, size_t bytes)
|
|
|
|
{
|
|
|
|
const uchar *usrc = src;
|
|
|
|
uchar *udst = dst;
|
|
|
|
|
2019-04-23 11:32:45 +02:00
|
|
|
// Can we safely go forward?
|
2019-01-21 15:00:04 +01:00
|
|
|
if (udst < usrc) {
|
2019-04-23 11:32:45 +02:00
|
|
|
while (bytes--) *udst++ = *usrc++;
|
2019-01-21 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// No, so we go backwards
|
2019-04-23 11:32:45 +02:00
|
|
|
else {
|
|
|
|
const uchar *usrc_end = usrc + bytes - 1;
|
|
|
|
uchar *udst_end = udst + bytes - 1;
|
|
|
|
while (bytes--) *udst_end-- = *usrc_end--;
|
|
|
|
}
|
2019-01-21 15:00:04 +01:00
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Compare memory areas
|
|
|
|
//
|
|
|
|
int memcmp(const void *ptr1, const void *ptr2, size_t bytes)
|
|
|
|
{
|
|
|
|
const uchar *uptr1 = ptr1;
|
|
|
|
const uchar *uptr2 = ptr2;
|
|
|
|
|
|
|
|
while (bytes--) {
|
|
|
|
if (*uptr1++ != *uptr2++) {
|
|
|
|
return uptr1[-1] < uptr2[-1] ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|