Skip to content

Commit b0d6347

Browse files
committed
Engine: Fix OOM caused by regions not being unmapped
1 parent 15795fd commit b0d6347

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/linux/q_shlinux.c

+24-22
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,36 @@ void *Hunk_Alloc (int size)
7676
return buf;
7777
}
7878

79+
80+
// FIXME: 64-bit????
81+
#define PAGE_SIZE 4096
82+
7983
int Hunk_End (void)
8084
{
8185
byte *n;
8286

83-
#if defined(__FreeBSD__)
84-
size_t old_size = maxhunksize;
85-
size_t new_size = curhunksize + sizeof(int);
86-
void * unmap_base;
87-
size_t unmap_len;
88-
89-
new_size = round_page(new_size);
90-
old_size = round_page(old_size);
91-
if (new_size > old_size)
92-
n = 0; /* error */
93-
else if (new_size < old_size)
94-
{
95-
unmap_base = (caddr_t)(membase + new_size);
96-
unmap_len = old_size - new_size;
97-
n = munmap(unmap_base, unmap_len) + membase;
98-
}
87+
#if defined(__FreeBSD__) || defined(__serenity__)
88+
size_t old_size = maxhunksize;
89+
size_t new_size = curhunksize + sizeof(int);
90+
void * unmap_base;
91+
size_t unmap_len;
92+
93+
new_size = ((new_size + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)));
94+
old_size = ((old_size + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)));
95+
if (new_size > old_size)
96+
n = 0; /* error */
97+
else if (new_size < old_size)
98+
{
99+
unmap_base = (caddr_t)(membase + new_size);
100+
unmap_len = old_size - new_size;
101+
n = munmap(unmap_base, unmap_len) + membase;
102+
}
99103
#endif
100-
// HACK: This code crashes Quake2 on launch in Serenity.
101-
// It works just fine without it, but this should _definitely_ be implemented eventually.
102-
#if defined(__linux__) || defined(__serenity__)
103-
//n = mremap(membase, maxhunksize, curhunksize + sizeof(int), 0);
104+
#if defined(__linux__)
105+
n = mremap(membase, maxhunksize, curhunksize + sizeof(int), 0);
104106
#endif
105-
//if (n != membase)
106-
//Sys_Error("Hunk_End: Could not remap virtual block (%d)", errno);
107+
if (n != membase)
108+
Sys_Error("Hunk_End: Could not remap virtual block (%d)", errno);
107109
*((int *)membase) = curhunksize + sizeof(int);
108110

109111
return curhunksize;

0 commit comments

Comments
 (0)