1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "qcommon/qcommon.h"
26 #include "winquake.h"
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <direct.h>
31 #include <io.h>
32 #include <conio.h>
33
34 //===============================================================================
35
36 int hunkcount;
37
38
39 byte *membase;
40 int hunkmaxsize;
41 int cursize;
42
43 #define VIRTUAL_ALLOC
44
Hunk_Begin(int maxsize)45 void *Hunk_Begin (int maxsize)
46 {
47 // reserve a huge chunk of memory, but don't commit any yet
48 cursize = 0;
49 hunkmaxsize = maxsize;
50 #ifdef VIRTUAL_ALLOC
51 membase = VirtualAlloc (NULL, maxsize, MEM_RESERVE|MEM_TOP_DOWN, PAGE_NOACCESS);
52 #else
53 membase = malloc (maxsize);
54 memset (membase, 0, maxsize);
55 #endif
56 if (!membase)
57 Sys_Error ("VirtualAlloc reserve failed");
58 return (void *)membase;
59 }
60
Hunk_Alloc(int size)61 void *Hunk_Alloc (int size)
62 {
63 void *buf;
64
65 // round to cacheline
66 size = (size+31)&~31;
67
68 #ifdef VIRTUAL_ALLOC
69 // commit pages as needed
70 // buf = VirtualAlloc (membase+cursize, size, MEM_COMMIT, PAGE_READWRITE);
71 buf = VirtualAlloc (membase, cursize+size, MEM_COMMIT, PAGE_READWRITE);
72 if (!buf)
73 {
74 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buf, 0, NULL);
75 Sys_Error ("VirtualAlloc commit failed.\n%s", buf);
76 }
77 #endif
78 cursize += size;
79 if (cursize > hunkmaxsize)
80 Sys_Error ("Hunk_Alloc overflow");
81
82 return (void *)(membase+cursize-size);
83 }
84
Hunk_End(void)85 int Hunk_End (void)
86 {
87
88 // free the remaining unused virtual memory
89 #if 0
90 void *buf;
91
92 // write protect it
93 buf = VirtualAlloc (membase, cursize, MEM_COMMIT, PAGE_READONLY);
94 if (!buf)
95 Sys_Error ("VirtualAlloc commit failed");
96 #endif
97
98 hunkcount++;
99 //Com_Printf ("hunkcount: %i\n", hunkcount);
100 return cursize;
101 }
102
Hunk_Free(void * base)103 void Hunk_Free (void *base)
104 {
105 if ( base )
106 #ifdef VIRTUAL_ALLOC
107 VirtualFree (base, 0, MEM_RELEASE);
108 #else
109 free (base);
110 #endif
111
112 hunkcount--;
113 }
114
115 //===============================================================================
116
117
118 /*
119 ================
120 Sys_Milliseconds
121 ================
122 */
123 int curtime;
Sys_Milliseconds(void)124 int Sys_Milliseconds (void)
125 {
126 static int base;
127 static qboolean initialized = false;
128 int timeofday;
129
130 if (!initialized)
131 { // let base retain 16 bits of effectively random data
132 base = timeGetTime() & 0xffff0000;
133 initialized = true;
134 }
135 timeofday = timeGetTime() - base;
136
137 return timeofday;
138 }
139
Sys_Mkdir(char * path)140 void Sys_Mkdir (char *path)
141 {
142 _mkdir (path);
143 }
144
145 //============================================
146
147 char findbase[MAX_OSPATH];
148 char findpath[MAX_OSPATH];
149 int findhandle;
150
CompareAttributes(unsigned found,unsigned musthave,unsigned canthave)151 static qboolean CompareAttributes( unsigned found, unsigned musthave, unsigned canthave )
152 {
153 if ( ( found & _A_RDONLY ) && ( canthave & SFF_RDONLY ) )
154 return false;
155 if ( ( found & _A_HIDDEN ) && ( canthave & SFF_HIDDEN ) )
156 return false;
157 if ( ( found & _A_SYSTEM ) && ( canthave & SFF_SYSTEM ) )
158 return false;
159 if ( ( found & _A_SUBDIR ) && ( canthave & SFF_SUBDIR ) )
160 return false;
161 if ( ( found & _A_ARCH ) && ( canthave & SFF_ARCH ) )
162 return false;
163
164 if ( ( musthave & SFF_RDONLY ) && !( found & _A_RDONLY ) )
165 return false;
166 if ( ( musthave & SFF_HIDDEN ) && !( found & _A_HIDDEN ) )
167 return false;
168 if ( ( musthave & SFF_SYSTEM ) && !( found & _A_SYSTEM ) )
169 return false;
170 if ( ( musthave & SFF_SUBDIR ) && !( found & _A_SUBDIR ) )
171 return false;
172 if ( ( musthave & SFF_ARCH ) && !( found & _A_ARCH ) )
173 return false;
174
175 return true;
176 }
177
Sys_FindFirst(char * path,unsigned musthave,unsigned canthave)178 char *Sys_FindFirst (char *path, unsigned musthave, unsigned canthave )
179 {
180 struct _finddata_t findinfo;
181
182 if (findhandle)
183 Sys_Error ("Sys_BeginFind without close");
184 findhandle = 0;
185
186 COM_FilePath (path, findbase);
187 findhandle = _findfirst (path, &findinfo);
188 if (findhandle == -1)
189 return NULL;
190 if ( !CompareAttributes( findinfo.attrib, musthave, canthave ) )
191 return NULL;
192 Com_sprintf (findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
193 return findpath;
194 }
195
Sys_FindNext(unsigned musthave,unsigned canthave)196 char *Sys_FindNext ( unsigned musthave, unsigned canthave )
197 {
198 struct _finddata_t findinfo;
199
200 if (findhandle == -1)
201 return NULL;
202 if (_findnext (findhandle, &findinfo) == -1)
203 return NULL;
204 if ( !CompareAttributes( findinfo.attrib, musthave, canthave ) )
205 return NULL;
206
207 Com_sprintf (findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
208 return findpath;
209 }
210
Sys_FindClose(void)211 void Sys_FindClose (void)
212 {
213 if (findhandle != -1)
214 _findclose (findhandle);
215 findhandle = 0;
216 }
217
218
219 //============================================
220
221