forked from NVIDIA/Q2RTX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hunk.c
97 lines (76 loc) · 2.91 KB
/
hunk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "shared/shared.h"
#include "system/hunk.h"
#include <sys/mman.h>
#include <errno.h>
void Hunk_Begin(memhunk_t *hunk, size_t maxsize)
{
void *buf;
if (maxsize > SIZE_MAX - 4095)
Com_Error(ERR_FATAL, "%s: size > SIZE_MAX", __func__);
// reserve a huge chunk of memory, but don't commit any yet
hunk->cursize = 0;
hunk->maxsize = (maxsize + 4095) & ~4095;
buf = mmap(NULL, hunk->maxsize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (buf == NULL || buf == (void *)-1)
Com_Error(ERR_FATAL, "%s: unable to reserve %"PRIz" bytes: %s",
__func__, hunk->maxsize, strerror(errno));
hunk->base = buf;
hunk->mapped = hunk->maxsize;
}
void *Hunk_Alloc(memhunk_t *hunk, size_t size)
{
void *buf;
if (size > SIZE_MAX - 63)
Com_Error(ERR_FATAL, "%s: size > SIZE_MAX", __func__);
// round to cacheline
size = (size + 63) & ~63;
if (hunk->cursize > hunk->maxsize)
Com_Error(ERR_FATAL, "%s: cursize > maxsize", __func__);
if (size > hunk->maxsize - hunk->cursize)
Com_Error(ERR_FATAL, "%s: couldn't allocate %"PRIz" bytes", __func__, size);
buf = (byte *)hunk->base + hunk->cursize;
hunk->cursize += size;
return buf;
}
void Hunk_End(memhunk_t *hunk)
{
size_t newsize;
if (hunk->cursize > hunk->maxsize)
Com_Error(ERR_FATAL, "%s: cursize > maxsize", __func__);
newsize = (hunk->cursize + 4095) & ~4095;
if (newsize < hunk->maxsize) {
#if (defined __linux__) && (defined _GNU_SOURCE)
void *buf = mremap(hunk->base, hunk->maxsize, newsize, 0);
#else
void *unmap_base = (byte *)hunk->base + newsize;
size_t unmap_len = hunk->maxsize - newsize;
void *buf = munmap(unmap_base, unmap_len) + (byte *)hunk->base;
#endif
if (buf != hunk->base)
Com_Error(ERR_FATAL, "%s: could not remap virtual block: %s",
__func__, strerror(errno));
}
hunk->mapped = newsize;
}
void Hunk_Free(memhunk_t *hunk)
{
if (hunk->base && munmap(hunk->base, hunk->mapped))
Com_Error(ERR_FATAL, "%s: munmap failed: %s",
__func__, strerror(errno));
memset(hunk, 0, sizeof(*hunk));
}