1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Split libmaxsi memory.cpp into multiple files.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-26 21:02:17 +02:00
parent 297b7259af
commit 4556036e08
10 changed files with 314 additions and 139 deletions

View file

@ -75,7 +75,11 @@ getline.o \
heap.o \
integer.o \
mbtowc.o \
memory.o \
memchr.o \
memcmp.o \
memcpy.o \
memmove.o \
memset.o \
op-new.o \
readparamstring.o \
rewind.o \
@ -143,6 +147,7 @@ ftruncate.o \
getc.o \
getcwd.o \
getdtablesize.o \
getpagesize.o \
getpid.o \
getppid.o \
gettermmode.o \
@ -152,6 +157,7 @@ isatty.o \
kernelinfo.o \
localeconv.o \
lseek.o \
memstat.o \
mkdir.o \
mktemp.o \
on_exit.o \
@ -163,6 +169,7 @@ random.o \
readdirents.o \
read.o \
rmdir.o \
sbrk.o \
scanf.o \
setjmp.o \
setlocale.o \

33
libmaxsi/getpagesize.cpp Normal file
View file

@ -0,0 +1,33 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
getpagesize.cpp
Returns the size of virtual memory pages.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL0(size_t, sys_getpagesize, SYSCALL_GET_PAGE_SIZE);
extern "C" size_t getpagesize(void)
{
return sys_getpagesize();
}

36
libmaxsi/memchr.cpp Normal file
View file

@ -0,0 +1,36 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memchr.cpp
Scans memory for a character.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
extern "C" void* memchr(const void* s, int c, size_t size)
{
const uint8_t* buf = (const uint8_t*) s;
for ( size_t i = 0; i < size; i++ )
{
if ( buf[i] == c ) { return (void*) (buf + i); }
}
return NULL;
}

37
libmaxsi/memcmp.cpp Normal file
View file

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memcmp.cpp
Compares two memory regions.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
extern "C" int memcmp(const void* a, const void* b, size_t size)
{
const uint8_t* buf1 = (const uint8_t*) a;
const uint8_t* buf2 = (const uint8_t*) b;
for ( size_t i = 0; i < size; i++ )
{
if ( buf1[i] != buf2[i] ) { return (int)(buf1[i]) - (int)(buf2[i]); }
}
return 0;
}

61
libmaxsi/memcpy.cpp Normal file
View file

@ -0,0 +1,61 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memcpy.cpp
Copy memory between non-overlapping regions.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
static void* memcpy_aligned(unsigned long* dest,
const unsigned long* src,
size_t length)
{
size_t numcopies = length / sizeof(unsigned long);
for ( size_t i = 0; i < numcopies; i++ )
dest[i] = src[i];
return dest;
}
static inline bool IsWordAligned(uintptr_t addr)
{
const size_t WORDSIZE = sizeof(unsigned long);
return (addr / WORDSIZE * WORDSIZE) == addr;
}
extern "C" void* memcpy(void* destptr, const void* srcptr, size_t length)
{
if ( IsWordAligned((uintptr_t) destptr) &&
IsWordAligned((uintptr_t) srcptr) &&
IsWordAligned(length) )
{
unsigned long* dest = (unsigned long*) destptr;
const unsigned long* src = (const unsigned long*) srcptr;
return memcpy_aligned(dest, src, length);
}
uint8_t* dest = (uint8_t*) destptr;
const uint8_t* src = (const uint8_t*) srcptr;
for ( size_t i = 0; i < length; i += sizeof(uint8_t) )
{
dest[i] = src[i];
}
return dest;
}

38
libmaxsi/memmove.cpp Normal file
View file

@ -0,0 +1,38 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memmove.cpp
Copy memory between potentionally overlapping regions.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
// TODO: This is a hacky implementation!
extern "C" void* memmove(void* _dest, const void* _src, size_t n)
{
uint8_t* dest = (uint8_t*) _dest;
const uint8_t* src = (const uint8_t*) _src;
uint8_t* tmp = new uint8_t[n];
memcpy(tmp, src, n);
memcpy(dest, tmp, n);
delete[] tmp;
return _dest;
}

View file

@ -1,138 +0,0 @@
/******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memory.cpp
Useful functions for manipulating and handling memory.
******************************************************************************/
#ifndef SORTIX_KERNEL
#include <sys/syscall.h>
#endif
#include <stdint.h>
#include <string.h>
namespace Maxsi
{
namespace Memory
{
#ifndef SORTIX_KERNEL
DEFN_SYSCALL2(int, SysMemStat, SYSCALL_MEMSTAT, size_t*, size_t*);
DEFN_SYSCALL1(void*, SysSbrk, SYSCALL_SBRK, intptr_t);
DEFN_SYSCALL0(size_t, SysGetPageSize, SYSCALL_GET_PAGE_SIZE);
extern "C" int memstat(size_t* memused, size_t* memtotal)
{
return SysMemStat(memused, memtotal);
}
extern "C" void* sbrk(intptr_t increment)
{
return SysSbrk(increment);
}
extern "C" size_t getpagesize(void)
{
return SysGetPageSize();
}
// TODO: This is a hacky implementation!
extern "C" void* memmove(void* _dest, const void* _src, size_t n)
{
uint8_t* dest = (uint8_t*) _dest;
const uint8_t* src = (const uint8_t*) _src;
uint8_t* tmp = new uint8_t[n];
memcpy(tmp, src, n);
memcpy(dest, tmp, n);
delete[] tmp;
return _dest;
}
#endif
extern "C" void* memcpy_aligned(unsigned long* dest,
const unsigned long* src,
size_t length)
{
size_t numcopies = length / sizeof(unsigned long);
for ( size_t i = 0; i < numcopies; i++ )
{
dest[i] = src[i];
}
return dest;
}
static inline bool IsWordAligned(uintptr_t addr)
{
const size_t WORDSIZE = sizeof(unsigned long);
return (addr / WORDSIZE * WORDSIZE) == addr;
}
extern "C" void* memcpy(void* destptr, const void* srcptr, size_t length)
{
if ( IsWordAligned((uintptr_t) destptr) &&
IsWordAligned((uintptr_t) srcptr) &&
IsWordAligned(length) )
{
unsigned long* dest = (unsigned long*) destptr;
const unsigned long* src = (const unsigned long*) srcptr;
return memcpy_aligned(dest, src, length);
}
uint8_t* dest = (uint8_t*) destptr;
const uint8_t* src = (const uint8_t*) srcptr;
for ( size_t i = 0; i < length; i += sizeof(uint8_t) )
{
dest[i] = src[i];
}
return dest;
}
extern "C" void* memset(void* Dest, int Value, size_t Length)
{
uint8_t* D = (uint8_t*) Dest;
for ( size_t I = 0; I < Length; I++ )
{
D[I] = Value & 0xFF;
}
return Dest;
}
extern "C" int memcmp(const void* a, const void* b, size_t size)
{
const uint8_t* buf1 = (const uint8_t*) a;
const uint8_t* buf2 = (const uint8_t*) b;
for ( size_t i = 0; i < size; i++ )
{
if ( buf1[i] != buf2[i] ) { return (int)(buf1[i]) - (int)(buf2[i]); }
}
return 0;
}
extern "C" void* memchr(const void* s, int c, size_t size)
{
const uint8_t* buf = (const uint8_t*) s;
for ( size_t i = 0; i < size; i++ )
{
if ( buf[i] == c ) { return (void*) (buf + i); }
}
return NULL;
}
}
}

34
libmaxsi/memset.cpp Normal file
View file

@ -0,0 +1,34 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memset.cpp
Initializes a region of memory to a byte value.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
extern "C" void* memset(void* destptr, int value, size_t length)
{
uint8_t* dest = (uint8_t*) destptr;
for ( size_t i = 0; i < length; i++ )
dest[i] = value & 0xFF;
return destptr;
}

33
libmaxsi/memstat.cpp Normal file
View file

@ -0,0 +1,33 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
memstat.cpp
Retrieves memory statistics.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL2(int, sys_memstat, SYSCALL_MEMSTAT, size_t*, size_t*);
extern "C" int memstat(size_t* memused, size_t* memtotal)
{
return sys_memstat(memused, memtotal);
}

34
libmaxsi/sbrk.cpp Normal file
View file

@ -0,0 +1,34 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
sbrk.cpp
Controls the size of the data segment.
*******************************************************************************/
#include <sys/syscall.h>
#include <stdint.h>
#include <unistd.h>
DEFN_SYSCALL1(void*, sys_sbrk, SYSCALL_SBRK, intptr_t);
extern "C" void* sbrk(intptr_t increment)
{
return sys_sbrk(increment);
}