Quake II RTX doxygen
1.0 dev
hunk.c
Go to the documentation of this file.
1
/*
2
Copyright (C) 1997-2001 Id Software, Inc.
3
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License along
15
with this program; if not, write to the Free Software Foundation, Inc.,
16
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
*/
18
19
#include "shared/shared.h"
20
#include "system/hunk.h"
21
#include <windows.h>
22
23
void
Hunk_Begin
(memhunk_t *hunk,
size_t
maxsize)
24
{
25
if
(maxsize > SIZE_MAX - 4095)
26
Com_Error
(ERR_FATAL,
"%s: size > SIZE_MAX"
, __func__);
27
28
// reserve a huge chunk of memory, but don't commit any yet
29
hunk->cursize = 0;
30
hunk->maxsize = (maxsize + 4095) & ~4095;
31
hunk->base = VirtualAlloc(NULL, hunk->maxsize, MEM_RESERVE, PAGE_NOACCESS);
32
if
(!hunk->base)
33
Com_Error
(ERR_FATAL,
34
"VirtualAlloc reserve %"
PRIz
" bytes failed with error %lu"
,
35
hunk->maxsize, GetLastError());
36
}
37
38
void
*
Hunk_Alloc
(memhunk_t *hunk,
size_t
size)
39
{
40
void
*buf;
41
42
if
(size > SIZE_MAX - 63)
43
Com_Error
(ERR_FATAL,
"%s: size > SIZE_MAX"
, __func__);
44
45
// round to cacheline
46
size = (size + 63) & ~63;
47
48
if
(hunk->cursize > hunk->maxsize)
49
Com_Error
(ERR_FATAL,
"%s: cursize > maxsize"
, __func__);
50
51
if
(size > hunk->maxsize - hunk->cursize)
52
Com_Error
(ERR_FATAL,
"%s: couldn't allocate %"
PRIz
" bytes"
, __func__, size);
53
54
hunk->cursize += size;
55
56
// commit pages as needed
57
buf = VirtualAlloc(hunk->base, hunk->cursize, MEM_COMMIT, PAGE_READWRITE);
58
if
(!buf)
59
Com_Error
(ERR_FATAL,
60
"VirtualAlloc commit %"
PRIz
" bytes failed with error %lu"
,
61
hunk->cursize, GetLastError());
62
63
return
(
byte
*)hunk->base + hunk->cursize - size;
64
}
65
66
void
Hunk_End
(memhunk_t *hunk)
67
{
68
if
(hunk->cursize > hunk->maxsize)
69
Com_Error
(ERR_FATAL,
"%s: cursize > maxsize"
, __func__);
70
71
// for statistics
72
hunk->mapped = (hunk->cursize + 4095) & ~4095;
73
}
74
75
void
Hunk_Free
(memhunk_t *hunk)
76
{
77
if
(hunk->base && !VirtualFree(hunk->base, 0, MEM_RELEASE))
78
Com_Error
(ERR_FATAL,
"VirtualFree failed with error %lu"
, GetLastError());
79
80
memset(hunk, 0,
sizeof
(*hunk));
81
}
Hunk_Begin
void Hunk_Begin(memhunk_t *hunk, size_t maxsize)
Definition:
hunk.c:23
Hunk_Alloc
void * Hunk_Alloc(memhunk_t *hunk, size_t size)
Definition:
hunk.c:38
Hunk_Free
void Hunk_Free(memhunk_t *hunk)
Definition:
hunk.c:75
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition:
g_main.c:258
Hunk_End
void Hunk_End(memhunk_t *hunk)
Definition:
hunk.c:66
src
windows
hunk.c
Generated by
1.8.17