Quake II RTX doxygen  1.0 dev
hunk.c File Reference
#include "shared/shared.h"
#include "system/hunk.h"
#include <windows.h>

Go to the source code of this file.

Functions

void Hunk_Begin (memhunk_t *hunk, size_t maxsize)
 
voidHunk_Alloc (memhunk_t *hunk, size_t size)
 
void Hunk_End (memhunk_t *hunk)
 
void Hunk_Free (memhunk_t *hunk)
 

Function Documentation

◆ Hunk_Alloc()

void* Hunk_Alloc ( memhunk_t *  hunk,
size_t  size 
)

Definition at line 38 of file hunk.c.

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 }

Referenced by GL_LoadWorld().

◆ Hunk_Begin()

void Hunk_Begin ( memhunk_t *  hunk,
size_t  maxsize 
)

Definition at line 23 of file hunk.c.

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 }

Referenced by BSP_Load(), GL_LoadWorld(), MOD_LoadMD2(), MOD_LoadMD2_GL(), MOD_LoadMD2_RTX(), and MOD_LoadSP2().

◆ Hunk_End()

void Hunk_End ( memhunk_t *  hunk)

Definition at line 66 of file hunk.c.

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 }

Referenced by BSP_Load(), GL_LoadWorld(), MOD_LoadMD2(), MOD_LoadMD2_GL(), MOD_LoadMD2_RTX(), and MOD_LoadSP2().

◆ Hunk_Free()

void Hunk_Free ( memhunk_t *  hunk)

Definition at line 75 of file hunk.c.

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 }

Referenced by BSP_Free(), BSP_Load(), GL_FreeWorld(), MOD_FreeAll(), MOD_FreeUnused(), MOD_LoadMD2(), MOD_LoadMD2_GL(), and MOD_LoadMD2_RTX().

Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258