diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index 2c70043e..48ff24e6 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -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 \ diff --git a/libmaxsi/getpagesize.cpp b/libmaxsi/getpagesize.cpp new file mode 100644 index 00000000..3d52fa99 --- /dev/null +++ b/libmaxsi/getpagesize.cpp @@ -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 . + + getpagesize.cpp + Returns the size of virtual memory pages. + +*******************************************************************************/ + +#include +#include + +DEFN_SYSCALL0(size_t, sys_getpagesize, SYSCALL_GET_PAGE_SIZE); + +extern "C" size_t getpagesize(void) +{ + return sys_getpagesize(); +} diff --git a/libmaxsi/memchr.cpp b/libmaxsi/memchr.cpp new file mode 100644 index 00000000..031c5954 --- /dev/null +++ b/libmaxsi/memchr.cpp @@ -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 . + + memchr.cpp + Scans memory for a character. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libmaxsi/memcmp.cpp b/libmaxsi/memcmp.cpp new file mode 100644 index 00000000..ae23baea --- /dev/null +++ b/libmaxsi/memcmp.cpp @@ -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 . + + memcmp.cpp + Compares two memory regions. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libmaxsi/memcpy.cpp b/libmaxsi/memcpy.cpp new file mode 100644 index 00000000..e372c21c --- /dev/null +++ b/libmaxsi/memcpy.cpp @@ -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 . + + memcpy.cpp + Copy memory between non-overlapping regions. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libmaxsi/memmove.cpp b/libmaxsi/memmove.cpp new file mode 100644 index 00000000..c4008849 --- /dev/null +++ b/libmaxsi/memmove.cpp @@ -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 . + + memmove.cpp + Copy memory between potentionally overlapping regions. + +*******************************************************************************/ + +#include +#include + +// 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; +} diff --git a/libmaxsi/memory.cpp b/libmaxsi/memory.cpp deleted file mode 100644 index be13c75c..00000000 --- a/libmaxsi/memory.cpp +++ /dev/null @@ -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 . - - memory.cpp - Useful functions for manipulating and handling memory. - -******************************************************************************/ - -#ifndef SORTIX_KERNEL -#include -#endif -#include -#include - -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; - } - } -} diff --git a/libmaxsi/memset.cpp b/libmaxsi/memset.cpp new file mode 100644 index 00000000..304dc954 --- /dev/null +++ b/libmaxsi/memset.cpp @@ -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 . + + memset.cpp + Initializes a region of memory to a byte value. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libmaxsi/memstat.cpp b/libmaxsi/memstat.cpp new file mode 100644 index 00000000..e0e15177 --- /dev/null +++ b/libmaxsi/memstat.cpp @@ -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 . + + memstat.cpp + Retrieves memory statistics. + +*******************************************************************************/ + +#include +#include + +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); +} diff --git a/libmaxsi/sbrk.cpp b/libmaxsi/sbrk.cpp new file mode 100644 index 00000000..ce32a549 --- /dev/null +++ b/libmaxsi/sbrk.cpp @@ -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 . + + sbrk.cpp + Controls the size of the data segment. + +*******************************************************************************/ + +#include +#include +#include + +DEFN_SYSCALL1(void*, sys_sbrk, SYSCALL_SBRK, intptr_t); + +extern "C" void* sbrk(intptr_t increment) +{ + return sys_sbrk(increment); +}