Skip to content

Commit 55f6a05

Browse files
committed
Experimental MMAP emulation
1 parent 1fc9c13 commit 55f6a05

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

mintlib/malloc.c

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#define HAVE_MMAP 0
2-
#define HAVE_MORECORE 1
3-
#define MORECORE_CONTIGUOUS 0
4-
#define MORECORE_CANNOT_TRIM 1
1+
#define HAVE_MMAP 1
2+
#define LACKS_SYS_MMAN_H 1
3+
#define MMAP_CLEARS 0
4+
#define HAVE_MORECORE 0
55
#define NO_MALLOC_STATS 1
6-
#define LACKS_TIME_H /* time(0) calls malloc... */
6+
#define LACKS_TIME_H 1 /* time(0) calls malloc... */
7+
#define DEFAULT_GRANULARITY 0
8+
#define MALLOC_ALIGNMENT 16
79

810
/*
911
Copyright 2023 Doug Lea
@@ -1653,7 +1655,7 @@ unsigned char _BitScanReverse(unsigned long *index, unsigned long mask);
16531655

16541656
#if HAVE_MMAP
16551657

1656-
#ifndef WIN32
1658+
#if !defined(WIN32) && !defined(__MINT__)
16571659
#define MUNMAP_DEFAULT(a, s) munmap((a), (s))
16581660
#define MMAP_PROT (PROT_READ|PROT_WRITE)
16591661
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
@@ -1677,7 +1679,7 @@ static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
16771679

16781680
#define DIRECT_MMAP_DEFAULT(s) MMAP_DEFAULT(s)
16791681

1680-
#else /* WIN32 */
1682+
#elif defined(WIN32)
16811683

16821684
/* Win32 MMAP via VirtualAlloc */
16831685
static FORCEINLINE void* win32mmap(size_t size) {
@@ -1713,6 +1715,71 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) {
17131715
#define MMAP_DEFAULT(s) win32mmap(s)
17141716
#define MUNMAP_DEFAULT(a, s) win32munmap((a), (s))
17151717
#define DIRECT_MMAP_DEFAULT(s) win32direct_mmap(s)
1718+
1719+
#else /* __MINT__ */
1720+
1721+
#include <mint/osbind.h>
1722+
1723+
#define MAX_POOL_ENTRIES 256
1724+
static int next_os_pool;
1725+
static struct {
1726+
void* ptr;
1727+
size_t size;
1728+
} os_pools[MAX_POOL_ENTRIES];
1729+
1730+
/* Atari MMAP via Malloc */
1731+
static FORCEINLINE void* atarimmap(size_t size) {
1732+
void* ptr;
1733+
1734+
if (next_os_pool == MAX_POOL_ENTRIES)
1735+
return MFAIL;
1736+
1737+
ptr = (void*)Malloc(size);
1738+
1739+
if (ptr != 0) {
1740+
os_pools[next_os_pool].ptr = ptr;
1741+
os_pools[next_os_pool].size = size;
1742+
next_os_pool++;
1743+
return ptr;
1744+
}
1745+
1746+
return MFAIL;
1747+
}
1748+
1749+
/* This function supports releasing coalesed segments */
1750+
static FORCEINLINE int atarimunmap(void* ptr, size_t size) {
1751+
char* cptr = (char*)ptr;
1752+
while (size) {
1753+
int found = -1;
1754+
1755+
for (int i = 0; i < next_os_pool; ++i) {
1756+
if (os_pools[i].ptr == cptr) {
1757+
if (os_pools[i].size <= size)
1758+
found = i;
1759+
break;
1760+
}
1761+
}
1762+
1763+
if (found != -1) {
1764+
cptr += os_pools[found].size;
1765+
size -= os_pools[found].size;
1766+
1767+
if (--next_os_pool > 0)
1768+
os_pools[found] = os_pools[next_os_pool];
1769+
1770+
Mfree(cptr);
1771+
}
1772+
else
1773+
return -1;
1774+
}
1775+
1776+
return 0;
1777+
}
1778+
1779+
#define MMAP_DEFAULT(s) atarimmap(s)
1780+
#define MUNMAP_DEFAULT(a, s) atarimunmap((a), (s))
1781+
#define DIRECT_MMAP_DEFAULT(s) MMAP_DEFAULT(s)
1782+
17161783
#endif /* WIN32 */
17171784
#endif /* HAVE_MMAP */
17181785

@@ -4169,8 +4236,7 @@ static void* sys_alloc(mstate m, size_t nb) {
41694236
char* end = CMFAIL;
41704237
ACQUIRE_MALLOC_GLOBAL_LOCK();
41714238
br = (char*)(CALL_MORECORE(asize));
4172-
/* end = (char*)(CALL_MORECORE(0)); */
4173-
end = br + asize;
4239+
end = (char*)(CALL_MORECORE(0));
41744240
RELEASE_MALLOC_GLOBAL_LOCK();
41754241
if (br != CMFAIL && end != CMFAIL && br < end) {
41764242
size_t ssize = end - br;

0 commit comments

Comments
 (0)