|
|
1.1 root 1:
2: #include "../qcommon/qcommon.h"
3: #include "winquake.h"
4: #include <errno.h>
5: #include <fcntl.h>
6: #include <stdio.h>
7: #include <direct.h>
8: #include <io.h>
9: #include <conio.h>
10:
11: //===============================================================================
12:
13: int hunkcount;
14:
15:
16: byte *membase;
17: int hunkmaxsize;
18: int cursize;
19:
20: #define VIRTUAL_ALLOC
21:
22: void *Hunk_Begin (int maxsize)
23: {
24: // reserve a huge chunk of memory, but don't commit any yet
25: cursize = 0;
26: hunkmaxsize = maxsize;
27: #ifdef VIRTUAL_ALLOC
28: membase = VirtualAlloc (NULL, maxsize, MEM_RESERVE, PAGE_NOACCESS);
29: #else
30: membase = malloc (maxsize);
31: memset (membase, 0, maxsize);
32: #endif
33: if (!membase)
34: Sys_Error ("VirtualAlloc reserve failed");
35: return (void *)membase;
36: }
37:
38: void *Hunk_Alloc (int size)
39: {
40: void *buf;
41:
42: // round to cacheline
43: size = (size+31)&~31;
44:
45: #ifdef VIRTUAL_ALLOC
46: // commit pages as needed
47: // buf = VirtualAlloc (membase+cursize, size, MEM_COMMIT, PAGE_READWRITE);
48: buf = VirtualAlloc (membase, cursize+size, MEM_COMMIT, PAGE_READWRITE);
49: if (!buf)
50: {
51: FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buf, 0, NULL);
52: Sys_Error ("VirtualAlloc commit failed.\n%s", buf);
53: }
54: #endif
55: cursize += size;
56: if (cursize > hunkmaxsize)
57: Sys_Error ("Hunk_Alloc overflow");
58:
59: return (void *)(membase+cursize-size);
60: }
61:
62: int Hunk_End (void)
63: {
64:
65: // free the remaining unused virtual memory
66: #if 0
67: void *buf;
68:
69: // write protect it
70: buf = VirtualAlloc (membase, cursize, MEM_COMMIT, PAGE_READONLY);
71: if (!buf)
72: Sys_Error ("VirtualAlloc commit failed");
73: #endif
74:
75: hunkcount++;
76: //Com_Printf ("hunkcount: %i\n", hunkcount);
77: return cursize;
78: }
79:
80: void Hunk_Free (void *base)
81: {
82: if ( base )
83: #ifdef VIRTUAL_ALLOC
84: VirtualFree (base, 0, MEM_RELEASE);
85: #else
86: free (base);
87: #endif
88:
89: hunkcount--;
90: }
91:
92: //===============================================================================
93:
94:
95: /*
96: ================
97: Sys_Milliseconds
98: ================
99: */
100: int curtime;
101: int Sys_Milliseconds (void)
102: {
103: static int base;
104: static qboolean initialized = false;
105:
106: if (!initialized)
107: { // let base retain 16 bits of effectively random data
108: base = timeGetTime() & 0xffff0000;
109: initialized = true;
110: }
111: curtime = timeGetTime() - base;
112:
113: return curtime;
114: }
115:
116: void Sys_Mkdir (char *path)
117: {
118: _mkdir (path);
119: }
120:
121: //============================================
122:
123: char findbase[MAX_OSPATH];
124: char findpath[MAX_OSPATH];
125: int findhandle;
126:
127: static qboolean CompareAttributes( unsigned found, unsigned musthave, unsigned canthave )
128: {
129: if ( ( found & _A_RDONLY ) && ( canthave & SFF_RDONLY ) )
130: return false;
131: if ( ( found & _A_HIDDEN ) && ( canthave & SFF_HIDDEN ) )
132: return false;
133: if ( ( found & _A_SYSTEM ) && ( canthave & SFF_SYSTEM ) )
134: return false;
135: if ( ( found & _A_SUBDIR ) && ( canthave & SFF_SUBDIR ) )
136: return false;
137: if ( ( found & _A_ARCH ) && ( canthave & SFF_ARCH ) )
138: return false;
139:
140: if ( ( musthave & SFF_RDONLY ) && !( found & _A_RDONLY ) )
141: return false;
142: if ( ( musthave & SFF_HIDDEN ) && !( found & _A_HIDDEN ) )
143: return false;
144: if ( ( musthave & SFF_SYSTEM ) && !( found & _A_SYSTEM ) )
145: return false;
146: if ( ( musthave & SFF_SUBDIR ) && !( found & _A_SUBDIR ) )
147: return false;
148: if ( ( musthave & SFF_ARCH ) && !( found & _A_ARCH ) )
149: return false;
150:
151: return true;
152: }
153:
154: char *Sys_FindFirst (char *path, unsigned musthave, unsigned canthave )
155: {
156: struct _finddata_t findinfo;
157:
158: if (findhandle)
159: Sys_Error ("Sys_BeginFind without close");
160: findhandle = 0;
161:
162: COM_FilePath (path, findbase);
163: findhandle = _findfirst (path, &findinfo);
164: if (findhandle == -1)
165: return NULL;
166: if ( !CompareAttributes( findinfo.attrib, musthave, canthave ) )
167: return NULL;
168: Com_sprintf (findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
169: return findpath;
170: }
171:
172: char *Sys_FindNext ( unsigned musthave, unsigned canthave )
173: {
174: struct _finddata_t findinfo;
175:
176: if (findhandle == -1)
177: return NULL;
178: if (_findnext (findhandle, &findinfo) == -1)
179: return NULL;
180: if ( !CompareAttributes( findinfo.attrib, musthave, canthave ) )
181: return NULL;
182:
183: Com_sprintf (findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
184: return findpath;
185: }
186:
187: void Sys_FindClose (void)
188: {
189: if (findhandle != -1)
190: _findclose (findhandle);
191: findhandle = 0;
192: }
193:
194:
195: //============================================
196:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.