<commit_before>#include "Inline/BasicTypes.h"
#include "Runtime.h"
#include "Platform/Platform.h"
#include "RuntimePrivate.h"

namespace Runtime
{
	// Global lists of memories; used to query whether an address is reserved by one of them.
	std::vector<MemoryInstance*> memories;

	static Uptr getPlatformPagesPerWebAssemblyPageLog2()
	{
		errorUnless(Platform::getPageSizeLog2() <= IR::numBytesPerPageLog2);
		return IR::numBytesPerPageLog2 - Platform::getPageSizeLog2();
	}

	U8* allocateVirtualPagesAligned(Uptr numBytes,Uptr alignmentBytes,U8*& outUnalignedBaseAddress,Uptr& outUnalignedNumPlatformPages)
	{
		const Uptr numAllocatedVirtualPages = numBytes >> Platform::getPageSizeLog2();
		const Uptr alignmentPages = alignmentBytes >> Platform::getPageSizeLog2();
		outUnalignedNumPlatformPages = numAllocatedVirtualPages + alignmentPages;
		outUnalignedBaseAddress = Platform::allocateVirtualPages(outUnalignedNumPlatformPages);
		if(!outUnalignedBaseAddress) { outUnalignedNumPlatformPages = 0; return nullptr; }
		else { return (U8*)((Uptr)(outUnalignedBaseAddress + alignmentBytes - 1) & ~(alignmentBytes - 1)); }
	}

	MemoryInstance* createMemory(MemoryType type)
	{
		MemoryInstance* memory = new MemoryInstance(type);

		// On a 64-bit runtime, allocate 8GB of address space for the memory.
		// This allows eliding bounds checks on memory accesses, since a 32-bit index + 32-bit offset will always be within the reserved address-space.
		// On a 32-bit runtime, allocate 1GB.
		const Uptr memoryMaxBytes = HAS_64BIT_ADDRESS_SPACE ? 8ull*1024*1024*1024 : 0x40000000;
		
		// On a 64 bit runtime, align the instance memory base to a 4GB boundary, so the lower 32-bits will all be zero. Maybe it will allow better code generation?
		// Note that this reserves a full extra 4GB, but only uses (4GB-1 page) for alignment, so there will always be a guard page at the end to
		// protect against unaligned loads/stores that straddle the end of the address-space.
		const Uptr alignmentBytes = HAS_64BIT_ADDRESS_SPACE ? 4ull*1024*1024*1024 : ((Uptr)1 << Platform::getPageSizeLog2());
		memory->baseAddress = allocateVirtualPagesAligned(memoryMaxBytes,alignmentBytes,memory->reservedBaseAddress,memory->reservedNumPlatformPages);
		memory->endOffset = memoryMaxBytes;
		if(!memory->baseAddress) { delete memory; return nullptr; }<commit_msg>Allocate 512MB of address-space instead of 1GB for memory objects on 32-bit. This allows the test scripts to have more than one around at a time without exhausting 2GB of user address-space.<commit_after>#include "Inline/BasicTypes.h"
#include "Runtime.h"
#include "Platform/Platform.h"
#include "RuntimePrivate.h"

namespace Runtime
{
	// Global lists of memories; used to query whether an address is reserved by one of them.
	std::vector<MemoryInstance*> memories;

	static Uptr getPlatformPagesPerWebAssemblyPageLog2()
	{
		errorUnless(Platform::getPageSizeLog2() <= IR::numBytesPerPageLog2);
		return IR::numBytesPerPageLog2 - Platform::getPageSizeLog2();
	}

	U8* allocateVirtualPagesAligned(Uptr numBytes,Uptr alignmentBytes,U8*& outUnalignedBaseAddress,Uptr& outUnalignedNumPlatformPages)
	{
		const Uptr numAllocatedVirtualPages = numBytes >> Platform::getPageSizeLog2();
		const Uptr alignmentPages = alignmentBytes >> Platform::getPageSizeLog2();
		outUnalignedNumPlatformPages = numAllocatedVirtualPages + alignmentPages;
		outUnalignedBaseAddress = Platform::allocateVirtualPages(outUnalignedNumPlatformPages);
		if(!outUnalignedBaseAddress) { outUnalignedNumPlatformPages = 0; return nullptr; }
		else { return (U8*)((Uptr)(outUnalignedBaseAddress + alignmentBytes - 1) & ~(alignmentBytes - 1)); }
	}

	MemoryInstance* createMemory(MemoryType type)
	{
		MemoryInstance* memory = new MemoryInstance(type);

		// On a 64-bit runtime, allocate 8GB of address space for the memory.
		// This allows eliding bounds checks on memory accesses, since a 32-bit index + 32-bit offset will always be within the reserved address-space.
		// On a 32-bit runtime, allocate 512MB.
		const Uptr memoryMaxBytes = HAS_64BIT_ADDRESS_SPACE ? 8ull*1024*1024*1024 : 0x20000000;
		
		// On a 64 bit runtime, align the instance memory base to a 4GB boundary, so the lower 32-bits will all be zero. Maybe it will allow better code generation?
		// Note that this reserves a full extra 4GB, but only uses (4GB-1 page) for alignment, so there will always be a guard page at the end to
		// protect against unaligned loads/stores that straddle the end of the address-space.
		const Uptr alignmentBytes = HAS_64BIT_ADDRESS_SPACE ? 4ull*1024*1024*1024 : ((Uptr)1 << Platform::getPageSizeLog2());
		memory->baseAddress = allocateVirtualPagesAligned(memoryMaxBytes,alignmentBytes,memory->reservedBaseAddress,memory->reservedNumPlatformPages);
		memory->endOffset = memoryMaxBytes;
		if(!memory->baseAddress) { delete memory; return nullptr; }
{'fraction_non_alphanumeric': 0.08896650914320937, 'fraction_numerical': 0.031436202999794535, 'mean_word_length': 5.641200545702592, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5821315', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1387, 'n_words': 439}