Skip to content

Commit

Permalink
Merge PR #268 (Revert "Remove built-in dlmalloc") into max-next
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneia committed Jun 19, 2024
2 parents 08d37a0 + e5519b2 commit 5f8a376
Showing 1 changed file with 1 addition and 143 deletions.
144 changes: 1 addition & 143 deletions lib/luajit/src/lj_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,141 +75,21 @@


/* Determine system-specific block allocation method. */
#if LJ_TARGET_WINDOWS

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define LJ_ALLOC_VIRTUALALLOC 1

#if LJ_64 && !LJ_GC64
#define LJ_ALLOC_NTAVM 1
#endif

#else

#include <errno.h>
/* If this include fails, then rebuild with: -DLUAJIT_USE_SYSMALLOC */
#include <sys/mman.h>

#define LJ_ALLOC_MMAP 1

#if LJ_64

#define LJ_ALLOC_MMAP_PROBE 1

#if LJ_GC64
#define LJ_ALLOC_MBITS 47 /* 128 TB in LJ_GC64 mode. */
#elif LJ_TARGET_X64 && LJ_HASJIT
/* Due to limitations in the x64 compiler backend. */
#define LJ_ALLOC_MBITS 31 /* 2 GB on x64 with !LJ_GC64. */
#else
#define LJ_ALLOC_MBITS 32 /* 4 GB on other archs with !LJ_GC64. */
#endif

#endif

#if LJ_64 && !LJ_GC64 && defined(MAP_32BIT)
#define LJ_ALLOC_MMAP32 1
#endif

#if LJ_TARGET_LINUX
#define LJ_ALLOC_MREMAP 1
#endif

#endif


#if LJ_ALLOC_VIRTUALALLOC

#if LJ_ALLOC_NTAVM
/* Undocumented, but hey, that's what we all love so much about Windows. */
typedef long (*PNTAVM)(HANDLE handle, void **addr, ULONG_PTR zbits,
size_t *size, ULONG alloctype, ULONG prot);
static PNTAVM ntavm;

/* Number of top bits of the lower 32 bits of an address that must be zero.
** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB.
*/
#define NTAVM_ZEROBITS 1

static void init_mmap(void)
{
ntavm = (PNTAVM)GetProcAddress(GetModuleHandleA("ntdll.dll"),
"NtAllocateVirtualMemory");
}
#define INIT_MMAP() init_mmap()

/* Win64 32 bit MMAP via NtAllocateVirtualMemory. */
static void *mmap_plain(size_t size)
{
DWORD olderr = GetLastError();
void *ptr = NULL;
long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size,
MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
SetLastError(olderr);
return st == 0 ? ptr : MFAIL;
}

/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
static void *direct_mmap(size_t size)
{
DWORD olderr = GetLastError();
void *ptr = NULL;
long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size,
MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE);
SetLastError(olderr);
return st == 0 ? ptr : MFAIL;
}

#else

/* Win32 MMAP via VirtualAlloc */
static void *mmap_plain(size_t size)
{
DWORD olderr = GetLastError();
void *ptr = LJ_WIN_VALLOC(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
SetLastError(olderr);
return ptr ? ptr : MFAIL;
}

/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
static void *direct_mmap(size_t size)
{
DWORD olderr = GetLastError();
void *ptr = LJ_WIN_VALLOC(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN,
PAGE_READWRITE);
SetLastError(olderr);
return ptr ? ptr : MFAIL;
}

#endif

#define CALL_MMAP(prng, size) mmap_plain(size)
#define DIRECT_MMAP(prng, size) direct_mmap(size)

/* This function supports releasing coalesed segments */
static int CALL_MUNMAP(void *ptr, size_t size)
{
DWORD olderr = GetLastError();
MEMORY_BASIC_INFORMATION minfo;
char *cptr = (char *)ptr;
while (size) {
if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0)
return -1;
if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr ||
minfo.State != MEM_COMMIT || minfo.RegionSize > size)
return -1;
if (VirtualFree(cptr, 0, MEM_RELEASE) == 0)
return -1;
cptr += minfo.RegionSize;
size -= minfo.RegionSize;
}
SetLastError(olderr);
return 0;
}

#elif LJ_ALLOC_MMAP
#if LJ_ALLOC_MMAP

#define MMAP_PROT (PROT_READ|PROT_WRITE)
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
Expand Down Expand Up @@ -330,20 +210,6 @@ static void *mmap_plain(size_t size)
#define CALL_MMAP(prng, size) mmap_plain(size)
#endif

#if LJ_64 && !LJ_GC64 && ((defined(__FreeBSD__) && __FreeBSD__ < 10) || defined(__FreeBSD_kernel__)) && !LJ_TARGET_PS4 && !LJ_TARGET_PS5

#include <sys/resource.h>

static void init_mmap(void)
{
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = 0x10000;
setrlimit(RLIMIT_DATA, &rlim); /* Ignore result. May fail later. */
}
#define INIT_MMAP() init_mmap()

#endif

static int CALL_MUNMAP(void *ptr, size_t size)
{
int olderr = errno;
Expand All @@ -365,12 +231,8 @@ static void *CALL_MREMAP_(void *ptr, size_t osz, size_t nsz, int flags)
#define CALL_MREMAP(addr, osz, nsz, mv) CALL_MREMAP_((addr), (osz), (nsz), (mv))
#define CALL_MREMAP_NOMOVE 0
#define CALL_MREMAP_MAYMOVE 1
#if LJ_64 && (!LJ_GC64 || LJ_TARGET_ARM64)
#define CALL_MREMAP_MV CALL_MREMAP_NOMOVE
#else
#define CALL_MREMAP_MV CALL_MREMAP_MAYMOVE
#endif
#endif

#endif

Expand Down Expand Up @@ -557,11 +419,7 @@ typedef struct malloc_state *mstate;
(((S) + (DEFAULT_GRANULARITY - SIZE_T_ONE))\
& ~(DEFAULT_GRANULARITY - SIZE_T_ONE))

#if LJ_TARGET_WINDOWS
#define mmap_align(S) granularity_align(S)
#else
#define mmap_align(S) page_align(S)
#endif

/* True if segment S holds address A */
#define segment_holds(S, A)\
Expand Down

0 comments on commit 5f8a376

Please sign in to comment.