diff --git a/libpolyml/Makefile.am b/libpolyml/Makefile.am index b2ecd7bc..d6814a4c 100644 --- a/libpolyml/Makefile.am +++ b/libpolyml/Makefile.am @@ -1,164 +1,163 @@ AUTOMAKE_OPTIONS=foreign moduledir = @moduledir@ AM_CPPFLAGS = $(CFLAGS) $(OSFLAG) $(GIT_VERSION) -Wall -DMODULEDIR=\"$(moduledir)\" AM_CFLAGS = $(CFLAGS) $(OSFLAG) $(GIT_VERSION) -Wall -fno-strict-aliasing AM_ASFLAGS = $(OSFLAG) AM_CCASFLAGS = $(OSFLAG) lib_LTLIBRARIES = libpolyml.la libpolyml_la_LDFLAGS = -version-info 11:0:0 DIST_SUBDIRS = libffi if INTERNAL_LIBFFI SUBDIRS = libffi libpolyml_la_LIBADD = libffi/libffi_convenience.la endif if NO_UNDEFINED # Force all references to be defined to build the DLL. libpolyml_la_LDFLAGS += -no-undefined endif # Select the architecture-specific modules if ARCHI386 ARCHSOURCE = x86_dep.cpp x86assembly_gas32.S else if ARCHINTERPRET ARCHSOURCE = interpret.cpp else if ARCHINTERPRET64 ARCHSOURCE = interpret.cpp else if ARCHX86_64 ARCHSOURCE = x86_dep.cpp x86assembly_gas64.S else if ARCHX8632IN64 ARCHSOURCE = x86_dep.cpp x86assembly_gas64.S else endif endif endif endif endif # Select the object-format-specific modules if EXPPECOFF EXPORTSOURCE = pecoffexport.cpp else if EXPELF EXPORTSOURCE = elfexport.cpp else if EXPMACHO EXPORTSOURCE = machoexport.cpp endif endif endif if NATIVE_WINDOWS -OSSOURCE = winstartup.cpp winbasicio.cpp winguiconsole.cpp windows_specific.cpp +OSSOURCE = winstartup.cpp winbasicio.cpp winguiconsole.cpp windows_specific.cpp osmemwin.cpp else -OSSOURCE = basicio.cpp unix_specific.cpp +OSSOURCE = basicio.cpp unix_specific.cpp osmemunix.cpp endif noinst_HEADERS = \ arb.h \ basicio.h \ bitmap.h \ check_objects.h \ diagnostics.h \ elfexport.h \ errors.h \ exporter.h \ gc.h \ gctaskfarm.h \ globals.h \ heapsizing.h \ int_opcodes.h \ io_internal.h \ locking.h \ machine_dep.h \ machoexport.h \ memmgr.h \ mpoly.h \ network.h \ noreturn.h \ objsize.h \ osmem.h \ os_specific.h \ pecoffexport.h \ pexport.h \ PolyControl.h \ poly_specific.h \ polyffi.h \ polystring.h \ process_env.h \ processes.h \ profiling.h \ realconv.h \ reals.h \ rts_module.h \ rtsentry.h \ run_time.h \ savestate.h \ save_vec.h \ scanaddrs.h \ sharedata.h \ sighandler.h \ statistics.h \ sys.h \ timing.h \ version.h \ winguiconsole.h \ winstartup.h \ xcall_numbers.h \ xwindows.h libpolyml_la_SOURCES = \ arb.cpp \ bitmap.cpp \ check_objects.cpp \ diagnostics.cpp \ errors.cpp \ exporter.cpp \ gc.cpp \ gc_check_weak_ref.cpp \ gc_copy_phase.cpp \ gc_mark_phase.cpp \ gc_share_phase.cpp \ gc_update_phase.cpp \ gctaskfarm.cpp \ heapsizing.cpp \ locking.cpp \ memmgr.cpp \ mpoly.cpp \ network.cpp \ objsize.cpp \ - osmem.cpp \ pexport.cpp \ poly_specific.cpp \ polyffi.cpp \ polystring.cpp \ process_env.cpp \ processes.cpp \ profiling.cpp \ quick_gc.cpp \ realconv.cpp \ reals.cpp \ rts_module.cpp \ rtsentry.cpp \ run_time.cpp \ save_vec.cpp \ savestate.cpp \ scanaddrs.cpp \ sharedata.cpp \ sighandler.cpp \ statistics.cpp \ timing.cpp \ xwindows.cpp \ $(ARCHSOURCE) $(EXPORTSOURCE) $(OSSOURCE) pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = polyml.pc diff --git a/libpolyml/PolyLib.vcxproj b/libpolyml/PolyLib.vcxproj index 541c7a8f..93443823 100644 --- a/libpolyml/PolyLib.vcxproj +++ b/libpolyml/PolyLib.vcxproj @@ -1,891 +1,891 @@  Debug32in64 Win32 Debug32in64 x64 Debug Win32 Int32in64Debug Win32 Int32in64Debug x64 Int32In64Release Win32 Int32In64Release x64 IntDebug Win32 IntDebug x64 IntRelease Win32 IntRelease x64 Release32in64 Win32 Release32in64 x64 Release Win32 Debug x64 Release x64 {0BA5D5B5-F85B-4C49-8A27-67186FA68922} PolyLib 10.0 DynamicLibrary true v142 Unicode DynamicLibrary true v142 Unicode DynamicLibrary true v142 Unicode DynamicLibrary true v142 Unicode DynamicLibrary false v142 true Unicode DynamicLibrary false v142 true Unicode DynamicLibrary false v142 true Unicode DynamicLibrary false v142 true Unicode DynamicLibrary true v142 Unicode DynamicLibrary true v142 Unicode DynamicLibrary true v142 Unicode DynamicLibrary true v142 Unicode DynamicLibrary false v142 true Unicode DynamicLibrary false v142 true Unicode DynamicLibrary false v142 true Unicode DynamicLibrary false v142 true Unicode .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll .dll Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) POLYML32IN64;_CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 Disabled true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) POLYML32IN64;_CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreadedDebug true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) POLYML32IN64;_CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) _CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false Level3 MaxSpeed true true true ..;libffi\include;libffi\msvc32include;libffi\src\x86;%(AdditionalIncludeDirectories) POLYML32IN64;_CRT_SECURE_NO_WARNINGS;POLYLIB_EXPORTS;HOSTARCHITECTURE_X86_64;LONG_LONG_MAX=_I64_MAX;%(PreprocessorDefinitions) MultiThreaded true true true ws2_32.lib;%(AdditionalDependencies) Windows 6.0 false true true true true true true true true true true true true true true true true + true true false false true true false false true true false false true true false false - true true true true true true true true true true true true true true true true true true true true true true true true true true true true false false true true false false true true true true true true Document cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml /nologo /DWINDOWS /Fo $(IntDir)%(Filename).obj /c /coff "$(IntDir)%(Filename).asm" $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj true true true true true true true true true true true true Document cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER /DPOLYML32IN64 "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER /DPOLYML32IN64 "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm cl /nologo /EP /I. /D_MSC_VER "%(FullPath)" > $(IntDir)%(Filename).asm ml64 /nologo /Fo $(IntDir)%(Filename).obj /c $(IntDir)%(Filename).asm $(IntDir)%(Filename).obj $(IntDir)%(Filename).obj {6d86bc6f-e74e-40c5-9881-f8bb606bca78} \ No newline at end of file diff --git a/libpolyml/osmem.h b/libpolyml/osmem.h index dc9b301e..734cfb4b 100644 --- a/libpolyml/osmem.h +++ b/libpolyml/osmem.h @@ -1,111 +1,105 @@ /* Title: osomem.h - Interface to OS memory management Copyright (c) 2006, 2017-18, 2020 David C.J. Matthews This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library 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 this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef OS_MEM_H_INCLUDED #define OS_MEM_H_INCLUDED // We need size_t so include these two here. #ifdef HAVE_STRING_H #include #endif #ifdef HAVE_STDLIB_H #include #endif #ifdef POLYML32IN64 #include "bitmap.h" #endif #include "locking.h" // This class provides access to the memory management provided by the // operating system. It would be nice if we could always use malloc and // free for this but we need to have execute permission on the code // objects. class OSMem { public: OSMem(); - ~OSMem() {} + virtual ~OSMem(); enum _MemUsage { UsageData, // Data or code in the interpreted version UsageStack, // Stack UsageExecutableCode // Code in the native code versions. }; bool Initialise(enum _MemUsage usage, size_t space = 0, void** pBase = 0); // Allocate space and return a pointer to it. The size is the minimum // size requested in bytes and it is updated with the actual space allocated. // Returns NULL if it cannot allocate the space. void *AllocateDataArea(size_t& bytes); // Release the space previously allocated. This must free the whole of // the segment. The space must be the size actually allocated. bool FreeDataArea(void* p, size_t space); // Enable/disable writing. This must apply to the whole of a segment. // Only for data areas. bool EnableWrite(bool enable, void* p, size_t space); // Allocate code area. Some systems will not allow both write and execute permissions // on the same page. On those systems we have to allocate two regions of shared memory, // one with read+execute permission and the other with read+write. void *AllocateCodeArea(size_t& bytes, void*& shadowArea); // Free the allocated areas. bool FreeCodeArea(void* codeAddr, void* dataAddr, size_t space); // Remove write access. This is used after the permanent code area has been created // either from importing a portable export file or copying the area in 32-in-64. bool DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space); protected: size_t pageSize; enum _MemUsage memUsage; #ifndef _WIN32 // If we need to use dual areas because WRITE+EXECUTE permission is not allowed. int shadowFd; PLock allocLock; size_t allocPtr; #endif #ifdef POLYML32IN64 - size_t PageSize(); - void* ReserveHeap(size_t space); - bool UnreserveHeap(void* baseAddr, size_t space); - void* CommitPages(void* baseAddr, size_t space, bool allowExecute); - bool UncommitPages(void* baseAddr, size_t space); - Bitmap pageMap; uintptr_t lastAllocated; char* memBase; PLock bitmapLock; #endif }; #endif diff --git a/libpolyml/osmem.cpp b/libpolyml/osmemunix.cpp similarity index 60% rename from libpolyml/osmem.cpp rename to libpolyml/osmemunix.cpp index 4433cea8..bdca8c4f 100644 --- a/libpolyml/osmem.cpp +++ b/libpolyml/osmemunix.cpp @@ -1,570 +1,389 @@ /* - Title: osomem.cpp - Interface to OS memory management + Title: osomem.cpp - Interface to OS memory management - Unix version Copyright (c) 2006, 2017-18, 2020 David C.J. Matthews This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library 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 this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include "config.h" -#elif defined(_WIN32) -#include "winconfig.h" #else #error "No configuration file" #endif #ifdef HAVE_SYS_TYPES_H #include #endif #ifdef HAVE_SYS_MMAN_H #include #endif #ifdef HAVE_ASSERT_H #include #define ASSERT(x) assert(x) #else #define ASSERT(x) #endif #include "osmem.h" #include "bitmap.h" #include "locking.h" // Linux prefers MAP_ANONYMOUS to MAP_ANON #ifndef MAP_ANON #ifdef MAP_ANONYMOUS #define MAP_ANON MAP_ANONYMOUS #endif #endif +// Assume that mmap is supported. If it isn't we can't run. -#ifdef POLYML32IN64 +#ifdef HAVE_UNISTD_H +#include +#endif + +#ifdef HAVE_SYS_PARAM_H +#include +#endif + +#ifdef HAVE_ERRNO_H +#include +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#include "polystring.h" // For TempCString + +// How do we get the page size? +#ifndef HAVE_GETPAGESIZE +#ifdef _SC_PAGESIZE +#define getpagesize() sysconf(_SC_PAGESIZE) +#else +// If this fails we're stuck +#define getpagesize() PAGESIZE +#endif +#endif + +#ifdef SOLARIS +#define FIXTYPE (caddr_t) +#else +#define FIXTYPE +#endif + +// Open a temporary file, unlink it and return the file descriptor. +static int openTmpFile(const char* dirName) +{ + const char* template_subdir = "/mlMapXXXXXX"; + TempString buff((char*)malloc(strlen(dirName) + strlen(template_subdir) + 1)); + if (buff == 0) return -1; // Unable to allocate + strcpy(buff, dirName); + strcat(buff, template_subdir); + int fd = mkstemp(buff); + if (fd == -1) return -1; + unlink(buff); + return fd; +} + +#ifdef POLYML32IN64 OSMem::OSMem() { memBase = 0; } -bool OSMem::Initialise(enum _MemUsage usage, size_t space /* = 0 */, void **pBase /* = 0 */) +OSMem::~OSMem() +{ +} + +bool OSMem::Initialise(enum _MemUsage usage, size_t space /* = 0 */, void** pBase /* = 0 */) { memUsage = usage; - pageSize = PageSize(); - memBase = (char*)ReserveHeap(space); - if (memBase == 0) - return 0; + pageSize = getpagesize(); + + memBase = (char*)mmap(0, space, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); + if (memBase == 0) return 0; + // We need the heap to be such that the top 32-bits are non-zero. + if ((uintptr_t)memBase < ((uintptr_t)1 << 32)) + { + // Allocate again. + void* newSpace = mmap(0, space, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); + munmap(FIXTYPE memBase, space); // Free the old area that isn't suitable. + // Return what we got, or zero if it failed. + memBase = (char*)newSpace; + } if (pBase != 0) *pBase = memBase; // Create a bitmap with a bit for each page. if (!pageMap.Create(space / pageSize)) return false; lastAllocated = space / pageSize; // Beyond the last page in the area // Set the last bit in the area so that we don't use it. // This is effectively a work-around for a problem with the heap. // If we have a zero-sized cell at the end of the memory its address is // going to be zero. This causes problems with forwarding pointers. // There may be better ways of doing this. pageMap.SetBit(space / pageSize - 1); return true; } -void *OSMem::AllocateDataArea(size_t& space) +void* OSMem::AllocateDataArea(size_t& space) { - char *baseAddr; + char* baseAddr; { PLocker l(&bitmapLock); uintptr_t pages = (space + pageSize - 1) / pageSize; // Round up to an integral number of pages. space = pages * pageSize; // Find some space while (pageMap.TestBit(lastAllocated - 1)) // Skip the wholly allocated area. lastAllocated--; uintptr_t free = pageMap.FindFree(0, lastAllocated, pages); if (free == lastAllocated) return 0; // Can't find the space. pageMap.SetBits(free, pages); // TODO: Do we need to zero this? It may have previously been set. baseAddr = memBase + free * pageSize; } - return CommitPages(baseAddr, space, false); + int prot = PROT_READ | PROT_WRITE; + if (allowExecute) prot |= PROT_EXEC; + if (mmap(baseAddr, space, prot, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == MAP_FAILED) + return 0; + msync(baseAddr, space, MS_SYNC | MS_INVALIDATE); + return baseAddr; } -bool OSMem::FreeDataArea(void *p, size_t space) +bool OSMem::FreeDataArea(void* p, size_t space) { - char *addr = (char*)p; + char* addr = (char*)p; uintptr_t offset = (addr - memBase) / pageSize; - if (!UncommitPages(p, space)) + // Remap the pages as new entries. This should remove the old versions. + if (mmap(p, space, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == MAP_FAILED) return false; + msync(p, space, MS_SYNC | MS_INVALIDATE); uintptr_t pages = space / pageSize; { PLocker l(&bitmapLock); pageMap.ClearBits(offset, pages); if (offset + pages > lastAllocated) // We allocate from the top down. lastAllocated = offset + pages; } return true; } -void * OSMem::AllocateCodeArea(size_t& space, void*& shadowArea) +void* OSMem::AllocateCodeArea(size_t& space, void*& shadowArea) { char* baseAddr; { PLocker l(&bitmapLock); uintptr_t pages = (space + pageSize - 1) / pageSize; // Round up to an integral number of pages. space = pages * pageSize; // Find some space while (pageMap.TestBit(lastAllocated - 1)) // Skip the wholly allocated area. lastAllocated--; uintptr_t free = pageMap.FindFree(0, lastAllocated, pages); if (free == lastAllocated) return 0; // Can't find the space. pageMap.SetBits(free, pages); // TODO: Do we need to zero this? It may have previously been set. baseAddr = memBase + free * pageSize; } - void *dataArea = CommitPages(baseAddr, space, memUsage == UsageExecutableCode); - shadowArea = dataArea; - return dataArea; + + int prot = PROT_READ | PROT_WRITE; + if (memUsage == UsageExecutableCode) prot |= PROT_EXEC; + if (mmap(baseAddr, space, prot, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == MAP_FAILED) + return 0; + msync(baseAddr, space, MS_SYNC | MS_INVALIDATE); + shadowArea = baseAddr; + return baseAddr; } bool OSMem::FreeCodeArea(void* codeAddr, void* dataAddr, size_t space) { ASSERT(codeAddr == dataAddr); char* addr = (char*)codeAddr; uintptr_t offset = (addr - memBase) / pageSize; - if (!UncommitPages(codeAddr, space)) - return false; + mmap(codeAddr, space, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0); + msync(codeAddr, space, MS_SYNC | MS_INVALIDATE); uintptr_t pages = space / pageSize; { PLocker l(&bitmapLock); pageMap.ClearBits(offset, pages); if (offset + pages > lastAllocated) // We allocate from the top down. lastAllocated = offset + pages; } return true; } -#endif - -#if (defined(HAVE_MMAP) && defined(MAP_ANON)) -// We don't use autoconf's test for mmap here because that tests for -// file mapping. Instead the test simply tests for the presence of an mmap -// function. -// We also insist that the OS supports MAP_ANON or MAP_ANONYMOUS. Older -// versions of Solaris required the use of /dev/zero instead. We don't -// support that. - -#ifdef HAVE_UNISTD_H -#include -#endif - -#ifdef HAVE_SYS_PARAM_H -#include -#endif - -#ifdef HAVE_ERRNO_H -#include -#endif - -#ifdef HAVE_STDLIB_H -#include -#endif - -#include "polystring.h" // For TempCString - -// How do we get the page size? -#ifndef HAVE_GETPAGESIZE -#ifdef _SC_PAGESIZE -#define getpagesize() sysconf(_SC_PAGESIZE) -#else -// If this fails we're stuck -#define getpagesize() PAGESIZE -#endif -#endif - -#ifdef SOLARIS -#define FIXTYPE (caddr_t) -#else -#define FIXTYPE -#endif - -#ifdef POLYML32IN64 -// Unix-specific implementation of the subsidiary functions. - -size_t OSMem::PageSize() -{ - return getpagesize(); -} - -void *OSMem::ReserveHeap(size_t space) -{ - return mmap(0, space, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); -} - -bool OSMem::UnreserveHeap(void *p, size_t space) -{ - return munmap(FIXTYPE p, space) == 0; -} - -void *OSMem::CommitPages(void *baseAddr, size_t space, bool allowExecute) -{ - int prot = PROT_READ | PROT_WRITE; - if (allowExecute) prot |= PROT_EXEC; - if (mmap(baseAddr, space, prot, MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0) == MAP_FAILED) - return 0; - msync(baseAddr, space, MS_SYNC|MS_INVALIDATE); - - return baseAddr; -} - -bool OSMem::UncommitPages(void *p, size_t space) -{ - // Remap the pages as new entries. This should remove the old versions. - if (mmap(p, space, PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0) == MAP_FAILED) - return false; - msync(p, space, MS_SYNC|MS_INVALIDATE); - return true; -} - bool OSMem::EnableWrite(bool enable, void* p, size_t space) { int res = mprotect(FIXTYPE p, space, enable ? PROT_READ|PROT_WRITE: PROT_READ); return res != -1; } bool OSMem::DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space) { int prot = PROT_READ; if (needExecute) prot |= PROT_EXEC; int res = mprotect(FIXTYPE codeAddr, space, prot); return res != -1; } #else + +// Native address versions + OSMem::OSMem() { allocPtr = 0; shadowFd = -1; } -// Open a temporary file, unlink it and return the file descriptor. -static int openTmpFile(const char *dirName) +OSMem::OSMem() { - const char *template_subdir = "/mlMapXXXXXX"; - TempString buff((char *)malloc(strlen(dirName) + strlen(template_subdir) + 1)); - if (buff == 0) return -1; // Unable to allocate - strcpy(buff, dirName); - strcat(buff, template_subdir); - int fd = mkstemp(buff); - if (fd == -1) return -1; - unlink(buff); - return fd; + if (shadowFd != -1) close(shadowFd); } bool OSMem::Initialise(enum _MemUsage usage, size_t space /* = 0 */, void **pBase /* = 0 */) { memUsage = usage; pageSize = getpagesize(); if (usage != UsageExecutableCode) return true; // Can we allocate memory with write+execute? int fd = -1; // This value is required by FreeBSD. Linux doesn't care void *test = mmap(0, pageSize, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, fd, 0); if (test != MAP_FAILED) { // Don't require shadow area munmap(FIXTYPE test, pageSize); return true; } if (errno != ENOTSUP && errno != EACCES) // Fails with ENOTSUPP on OpenBSD and EACCES in SELinux. return false; // Check that read-write works. test = mmap(0, pageSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, fd, 0); if (test == MAP_FAILED) return false; // There's a problem. munmap(FIXTYPE test, pageSize); // Need to create a file descriptor for mapping. char *tmpDir = getenv("TMPDIR"); if (tmpDir != NULL) { shadowFd = openTmpFile(tmpDir); if (shadowFd != -1) return true; } #ifdef P_tmpdir shadowFd = openTmpFile(P_tmpdir); if (shadowFd != -1) return true; #endif shadowFd = openTmpFile("/tmp"); if (shadowFd != -1) return true; shadowFd = openTmpFile("/var/tmp"); if (shadowFd != -1) return true; return false; } // Allocate space and return a pointer to it. The size is the minimum // size requested and it is updated with the actual space allocated. // Returns NULL if it cannot allocate the space. void *OSMem::AllocateDataArea(size_t &space) { // Round up to an integral number of pages. space = (space + pageSize-1) & ~(pageSize-1); int fd = -1; // This value is required by FreeBSD. Linux doesn't care int flags = MAP_PRIVATE | MAP_ANON; #ifdef MAP_STACK if (memUsage == UsageStack) flags |= MAP_STACK; // OpenBSD seems to require this #endif void *result = mmap(0, space, PROT_READ|PROT_WRITE, flags, fd, 0); // Convert MAP_FAILED (-1) into NULL if (result == MAP_FAILED) return 0; return result; } // Release the space previously allocated. This must free the whole of // the segment. The space must be the size actually allocated. bool OSMem::FreeDataArea(void *p, size_t space) { return munmap(FIXTYPE p, space) == 0; } bool OSMem::EnableWrite(bool enable, void* p, size_t space) { int res = mprotect(FIXTYPE p, space, enable ? PROT_READ|PROT_WRITE: PROT_READ); return res != -1; } void *OSMem::AllocateCodeArea(size_t &space, void*& shadowArea) { // Round up to an integral number of pages. space = (space + pageSize-1) & ~(pageSize-1); if (shadowFd == -1) { int fd = -1; // This value is required by FreeBSD. Linux doesn't care int prot = PROT_READ | PROT_WRITE; if (memUsage == UsageExecutableCode) prot |= PROT_EXEC; void *result = mmap(0, space, prot, MAP_PRIVATE|MAP_ANON, fd, 0); // Convert MAP_FAILED (-1) into NULL if (result == MAP_FAILED) return 0; shadowArea = result; return result; } // Have to use dual areas. size_t allocAt; { PLocker lock(&allocLock); allocAt = allocPtr; allocPtr += space; } if (ftruncate(shadowFd, allocAt + space) == -1) return 0; void *readExec = mmap(0, space, PROT_READ|PROT_EXEC, MAP_SHARED, shadowFd, allocAt); if (readExec == MAP_FAILED) return 0; void *readWrite = mmap(0, space, PROT_READ|PROT_WRITE, MAP_SHARED, shadowFd, allocAt); if (readWrite == MAP_FAILED) { munmap(FIXTYPE readExec, space); return 0; } shadowArea = readWrite; return readExec; } bool OSMem::FreeCodeArea(void *codeArea, void *dataArea, size_t space) { bool freeCode = munmap(FIXTYPE codeArea, space) == 0; if (codeArea == dataArea) return freeCode; return (munmap(FIXTYPE dataArea, space) == 0) & freeCode; } bool OSMem::DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space) { int prot = PROT_READ; if (memUsage == UsageExecutableCode) prot |= PROT_EXEC; int res = mprotect(FIXTYPE codeAddr, space, prot); return res != -1; } #endif - -#elif defined(_WIN32) -// Use Windows memory management. -#include - -#ifdef POLYML32IN64 - -// Windows-specific implementations of the subsidiary functions. -size_t OSMem::PageSize() -{ - // Get the page size and round up to that multiple. - SYSTEM_INFO sysInfo; - GetSystemInfo(&sysInfo); - // Get the page size. Put it in a size_t variable otherwise the rounding - // up of "space" may go wrong on 64-bits. - return sysInfo.dwPageSize; -} - -void *OSMem::ReserveHeap(size_t space) -{ - void *memBase = VirtualAlloc(0, space, MEM_RESERVE, PAGE_NOACCESS); - if (memBase == 0) return 0; - // We need the heap to be such that the top 32-bits are non-zero. - if ((uintptr_t)memBase >= ((uintptr_t)1 << 32)) - return memBase; - // Allocate again. - void *newSpace = ReserveHeap(space); - UnreserveHeap(memBase, space); // Free the old area that isn't suitable. - // Return what we got, or zero if it failed. - return newSpace; -} - -bool OSMem::UnreserveHeap(void *p, size_t space) -{ - return VirtualFree(p, 0, MEM_RELEASE) == TRUE; -} - -void *OSMem::CommitPages(void *baseAddr, size_t space, bool allowExecute) -{ - return VirtualAlloc(baseAddr, space, MEM_COMMIT, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE); -} - -bool OSMem::UncommitPages(void *baseAddr, size_t space) -{ - return VirtualFree(baseAddr, space, MEM_DECOMMIT) == TRUE; -} - -bool OSMem::EnableWrite(bool enable, void* p, size_t space) -{ - DWORD oldProtect; - return VirtualProtect(p, space, enable ? PAGE_READWRITE : PAGE_READONLY, &oldProtect) == TRUE; -} - -bool OSMem::DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space) -{ - ASSERT(codeAddr == dataAddr); - DWORD oldProtect; - return VirtualProtect(codeAddr, space, - memUsage == UsageExecutableCode ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldProtect) == TRUE; -} - -#else - -OSMem::OSMem() -{ -} - -bool OSMem::Initialise(enum _MemUsage usage, size_t space /* = 0 */, void **pBase /* = 0 */) -{ - memUsage = usage; - // Get the page size and round up to that multiple. - SYSTEM_INFO sysInfo; - GetSystemInfo(&sysInfo); - // Get the page size. Put it in a size_t variable otherwise the rounding - // up of "space" may go wrong on 64-bits. - pageSize = sysInfo.dwPageSize; - return true; -} - -// Allocate space and return a pointer to it. The size is the minimum -// size requested and it is updated with the actual space allocated. -// Returns NULL if it cannot allocate the space. -void *OSMem::AllocateDataArea(size_t &space) -{ - space = (space + pageSize - 1) & ~(pageSize - 1); - DWORD options = MEM_RESERVE | MEM_COMMIT; - return VirtualAlloc(0, space, options, PAGE_READWRITE); -} - -// Release the space previously allocated. This must free the whole of -// the segment. The space must be the size actually allocated. -bool OSMem::FreeDataArea(void *p, size_t space) -{ - return VirtualFree(p, 0, MEM_RELEASE) == TRUE; -} - -// Adjust the permissions on a segment. This must apply to the -// whole of a segment. -bool OSMem::EnableWrite(bool enable, void* p, size_t space) -{ - DWORD oldProtect; - return VirtualProtect(p, space, enable ? PAGE_READWRITE: PAGE_READONLY, &oldProtect) == TRUE; -} - -void* OSMem::AllocateCodeArea(size_t& space, void*& shadowArea) -{ - space = (space + pageSize - 1) & ~(pageSize - 1); - DWORD options = MEM_RESERVE | MEM_COMMIT; - void * dataAddr = VirtualAlloc(0, space, options, - memUsage == UsageExecutableCode ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE); - shadowArea = dataAddr; - return dataAddr; -} - -bool OSMem::FreeCodeArea(void* codeAddr, void* dataAddr, size_t space) -{ - ASSERT(codeAddr == dataAddr); - return VirtualFree(codeAddr, 0, MEM_RELEASE) == TRUE; -} - -bool OSMem::DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space) -{ - ASSERT(codeAddr == dataAddr); - DWORD oldProtect; - return VirtualProtect(codeAddr, space, - memUsage == UsageExecutableCode ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldProtect) == TRUE; -} - -#endif - -#else - -#ifdef HAVE_STDLIB_H -#include -#endif - -#ifdef HAVE_MALLOC_H -#include -#endif - -#ifdef POLYML32IN64 -#error "32 bit in 64-bits requires either mmap or VirtualAlloc" -#endif - -// Use calloc to allocate the memory. Using calloc ensures the memory is -// zeroed and is compatible with the other allocators. -void *OSMem::Allocate(size_t &bytes, unsigned permissions) -{ - return calloc(bytes, 1); -} - -bool OSMem::Free(void *p, size_t/*space*/) -{ - free(p); - return true; -} - -// We can't do this if we don't have mprotect. -bool OSMem::SetPermissions(void *p, size_t space, unsigned permissions) -{ - return true; // Let's hope this is all right. -} - -#endif diff --git a/libpolyml/osmemwin.cpp b/libpolyml/osmemwin.cpp new file mode 100644 index 00000000..c3e2d805 --- /dev/null +++ b/libpolyml/osmemwin.cpp @@ -0,0 +1,254 @@ +/* + Title: osomem.cpp - Interface to OS memory management - Windows version + + Copyright (c) 2006, 2017-18, 2020 David C.J. Matthews + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library 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 this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#ifndef _WIN32 +#error "This is for Windows only" +#endif + +#include "winconfig.h" + +#ifdef HAVE_ASSERT_H +#include +#define ASSERT(x) assert(x) +#else +#define ASSERT(x) +#endif + +#include "osmem.h" +#include "bitmap.h" +#include "locking.h" + +// Use Windows memory management. +#include + +#ifdef POLYML32IN64 +OSMem::OSMem() +{ + memBase = 0; +} + +OSMem::~OSMem() +{ +} + +bool OSMem::Initialise(enum _MemUsage usage, size_t space /* = 0 */, void** pBase /* = 0 */) +{ + memUsage = usage; + // Get the page size and round up to that multiple. + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + // Get the page size. Put it in a size_t variable otherwise the rounding + // up of "space" may go wrong on 64-bits. + pageSize = sysInfo.dwPageSize; + + memBase = (char*)VirtualAlloc(0, space, MEM_RESERVE, PAGE_NOACCESS); + if (memBase == 0) return 0; + // We need the heap to be such that the top 32-bits are non-zero. + if ((uintptr_t)memBase < ((uintptr_t)1 << 32)) + { + // Allocate again. + void* newSpace = VirtualAlloc(0, space, MEM_RESERVE, PAGE_NOACCESS); + VirtualFree(memBase, 0, MEM_RELEASE); // Free the old area that isn't suitable. + // Return what we got, or zero if it failed. + memBase = (char*)newSpace; + } + + if (pBase != 0) *pBase = memBase; + + // Create a bitmap with a bit for each page. + if (!pageMap.Create(space / pageSize)) + return false; + lastAllocated = space / pageSize; // Beyond the last page in the area + // Set the last bit in the area so that we don't use it. + // This is effectively a work-around for a problem with the heap. + // If we have a zero-sized cell at the end of the memory its address is + // going to be zero. This causes problems with forwarding pointers. + // There may be better ways of doing this. + pageMap.SetBit(space / pageSize - 1); + return true; +} + +void* OSMem::AllocateDataArea(size_t& space) +{ + char* baseAddr; + { + PLocker l(&bitmapLock); + uintptr_t pages = (space + pageSize - 1) / pageSize; + // Round up to an integral number of pages. + space = pages * pageSize; + // Find some space + while (pageMap.TestBit(lastAllocated - 1)) // Skip the wholly allocated area. + lastAllocated--; + uintptr_t free = pageMap.FindFree(0, lastAllocated, pages); + if (free == lastAllocated) + return 0; // Can't find the space. + pageMap.SetBits(free, pages); + // TODO: Do we need to zero this? It may have previously been set. + baseAddr = memBase + free * pageSize; + } + return VirtualAlloc(baseAddr, space, MEM_COMMIT, PAGE_READWRITE); +} + +bool OSMem::FreeDataArea(void* p, size_t space) +{ + char* addr = (char*)p; + uintptr_t offset = (addr - memBase) / pageSize; + if (!VirtualFree(p, space, MEM_DECOMMIT)) + return false; + uintptr_t pages = space / pageSize; + { + PLocker l(&bitmapLock); + pageMap.ClearBits(offset, pages); + if (offset + pages > lastAllocated) // We allocate from the top down. + lastAllocated = offset + pages; + } + return true; +} + +void* OSMem::AllocateCodeArea(size_t& space, void*& shadowArea) +{ + char* baseAddr; + { + PLocker l(&bitmapLock); + uintptr_t pages = (space + pageSize - 1) / pageSize; + // Round up to an integral number of pages. + space = pages * pageSize; + // Find some space + while (pageMap.TestBit(lastAllocated - 1)) // Skip the wholly allocated area. + lastAllocated--; + uintptr_t free = pageMap.FindFree(0, lastAllocated, pages); + if (free == lastAllocated) + return 0; // Can't find the space. + pageMap.SetBits(free, pages); + // TODO: Do we need to zero this? It may have previously been set. + baseAddr = memBase + free * pageSize; + } + + void* dataArea = + VirtualAlloc(baseAddr, space, MEM_COMMIT, memUsage == UsageExecutableCode ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE); + shadowArea = dataArea; + return dataArea; +} + +bool OSMem::FreeCodeArea(void* codeAddr, void* dataAddr, size_t space) +{ + ASSERT(codeAddr == dataAddr); + char* addr = (char*)codeAddr; + uintptr_t offset = (addr - memBase) / pageSize; + if (! VirtualFree(codeAddr, space, MEM_DECOMMIT)) + return false; + uintptr_t pages = space / pageSize; + { + PLocker l(&bitmapLock); + pageMap.ClearBits(offset, pages); + if (offset + pages > lastAllocated) // We allocate from the top down. + lastAllocated = offset + pages; + } + return true; +} + +bool OSMem::EnableWrite(bool enable, void* p, size_t space) +{ + DWORD oldProtect; + return VirtualProtect(p, space, enable ? PAGE_READWRITE : PAGE_READONLY, &oldProtect) == TRUE; +} + +bool OSMem::DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space) +{ + ASSERT(codeAddr == dataAddr); + DWORD oldProtect; + return VirtualProtect(codeAddr, space, + memUsage == UsageExecutableCode ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldProtect) == TRUE; +} + +#else + +// Native address versions +OSMem::OSMem() +{ +} + +OSMem::~OSMem() +{ +} + +bool OSMem::Initialise(enum _MemUsage usage, size_t space /* = 0 */, void **pBase /* = 0 */) +{ + memUsage = usage; + // Get the page size and round up to that multiple. + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + // Get the page size. Put it in a size_t variable otherwise the rounding + // up of "space" may go wrong on 64-bits. + pageSize = sysInfo.dwPageSize; + return true; +} + +// Allocate space and return a pointer to it. The size is the minimum +// size requested and it is updated with the actual space allocated. +// Returns NULL if it cannot allocate the space. +void *OSMem::AllocateDataArea(size_t &space) +{ + space = (space + pageSize - 1) & ~(pageSize - 1); + DWORD options = MEM_RESERVE | MEM_COMMIT; + return VirtualAlloc(0, space, options, PAGE_READWRITE); +} + +// Release the space previously allocated. This must free the whole of +// the segment. The space must be the size actually allocated. +bool OSMem::FreeDataArea(void *p, size_t space) +{ + return VirtualFree(p, 0, MEM_RELEASE) == TRUE; +} + +// Adjust the permissions on a segment. This must apply to the +// whole of a segment. +bool OSMem::EnableWrite(bool enable, void* p, size_t space) +{ + DWORD oldProtect; + return VirtualProtect(p, space, enable ? PAGE_READWRITE: PAGE_READONLY, &oldProtect) == TRUE; +} + +void* OSMem::AllocateCodeArea(size_t& space, void*& shadowArea) +{ + space = (space + pageSize - 1) & ~(pageSize - 1); + DWORD options = MEM_RESERVE | MEM_COMMIT; + void * dataAddr = VirtualAlloc(0, space, options, + memUsage == UsageExecutableCode ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE); + shadowArea = dataAddr; + return dataAddr; +} + +bool OSMem::FreeCodeArea(void* codeAddr, void* dataAddr, size_t space) +{ + ASSERT(codeAddr == dataAddr); + return VirtualFree(codeAddr, 0, MEM_RELEASE) == TRUE; +} + +bool OSMem::DisableWriteForCode(void* codeAddr, void* dataAddr, size_t space) +{ + ASSERT(codeAddr == dataAddr); + DWORD oldProtect; + return VirtualProtect(codeAddr, space, + memUsage == UsageExecutableCode ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldProtect) == TRUE; +} + +#endif +