File: win32\q_shwin.c
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 #include "../qcommon/qcommon.h"
22 #include "winquake.h"
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <direct.h>
27 #include <io.h>
28 #include <conio.h>
29
30 //===============================================================================
31
32 int hunkcount;
33
34
35 byte *membase;
36 int hunkmaxsize;
37 int cursize;
38
39 #define VIRTUAL_ALLOC
40
41 void *Hunk_Begin (int maxsize)
42 {
43 // reserve a huge chunk of memory, but don't commit any yet
44 cursize = 0;
45 hunkmaxsize = maxsize;
46 #ifdef VIRTUAL_ALLOC
47 membase = VirtualAlloc (NULL, maxsize, MEM_RESERVE, PAGE_NOACCESS);
51 #endif
52 if (!membase)
53 Sys_Error ("VirtualAlloc reserve failed");
54 return (void *)membase;
55 }
56
57 void *Hunk_Alloc (int size)
58 {
59 void *buf;
60
61 // round to cacheline
62 size = (size+31)&~31;
63
64 #ifdef VIRTUAL_ALLOC
65 // commit pages as needed
66 // buf = VirtualAlloc (membase+cursize, size, MEM_COMMIT, PAGE_READWRITE);
67 buf = VirtualAlloc (membase, cursize+size, MEM_COMMIT, PAGE_READWRITE);
68 if (!buf)
69 {
70 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buf, 0, NULL);
71 Sys_Error ("VirtualAlloc commit failed.\n%s", buf);
72 }
73 #endif
74 cursize += size;
75 if (cursize > hunkmaxsize)
76 Sys_Error ("Hunk_Alloc overflow");
77
78 return (void *)(membase+cursize-size);
79 }
80
81 int Hunk_End (void)
82 {
83
84 // free the remaining unused virtual memory
85 #if 0
92 #endif
93
94 hunkcount++;
95 //Com_Printf ("hunkcount: %i\n", hunkcount);
96 return cursize;
97 }
98
99 void Hunk_Free (void *base)
100 {
101 if ( base )
102 #ifdef VIRTUAL_ALLOC
103 VirtualFree (base, 0, MEM_RELEASE);
106 #endif
107
108 hunkcount--;
109 }
110
111 //===============================================================================
112
113
114 /*
115 ================
116 Sys_Milliseconds
117 ================
118 */
119 int curtime;
120 int Sys_Milliseconds (void)
121 {
122 static int base;
123 static qboolean initialized = false;
124
125 if (!initialized)
126 { // let base retain 16 bits of effectively random data
127 base = timeGetTime() & 0xffff0000;
128 initialized = true;
129 }
130 curtime = timeGetTime() - base;
131
132 return curtime;
133 }
134
135 void Sys_Mkdir (char *path)
136 {
137 _mkdir (path);
138 }
139
140 //============================================
141
142 char findbase[MAX_OSPATH];
143 char findpath[MAX_OSPATH];
144 int findhandle;
145
146 static qboolean CompareAttributes( unsigned found, unsigned musthave, unsigned canthave )
147 {
148 if ( ( found & _A_RDONLY ) && ( canthave & SFF_RDONLY ) )
149 return false;
150 if ( ( found & _A_HIDDEN ) && ( canthave & SFF_HIDDEN ) )
151 return false;
152 if ( ( found & _A_SYSTEM ) && ( canthave & SFF_SYSTEM ) )
153 return false;
154 if ( ( found & _A_SUBDIR ) && ( canthave & SFF_SUBDIR ) )
155 return false;
156 if ( ( found & _A_ARCH ) && ( canthave & SFF_ARCH ) )
157 return false;
158
159 if ( ( musthave & SFF_RDONLY ) && !( found & _A_RDONLY ) )
160 return false;
161 if ( ( musthave & SFF_HIDDEN ) && !( found & _A_HIDDEN ) )
162 return false;
163 if ( ( musthave & SFF_SYSTEM ) && !( found & _A_SYSTEM ) )
164 return false;
165 if ( ( musthave & SFF_SUBDIR ) && !( found & _A_SUBDIR ) )
166 return false;
167 if ( ( musthave & SFF_ARCH ) && !( found & _A_ARCH ) )
168 return false;
169
170 return true;
171 }
172
173 char *Sys_FindFirst (char *path, unsigned musthave, unsigned canthave )
174 {
175 struct _finddata_t findinfo;
176
177 if (findhandle)
178 Sys_Error ("Sys_BeginFind without close");
179 findhandle = 0;
180
181 COM_FilePath (path, findbase);
182 findhandle = _findfirst (path, &findinfo);
183 if (findhandle == -1)
184 return NULL;
185 if ( !CompareAttributes( findinfo.attrib, musthave, canthave ) )
186 return NULL;
187 Com_sprintf (findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
188 return findpath;
189 }
190
191 char *Sys_FindNext ( unsigned musthave, unsigned canthave )
192 {
193 struct _finddata_t findinfo;
194
195 if (findhandle == -1)
196 return NULL;
197 if (_findnext (findhandle, &findinfo) == -1)
198 return NULL;
199 if ( !CompareAttributes( findinfo.attrib, musthave, canthave ) )
200 return NULL;
201
202 Com_sprintf (findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
203 return findpath;
204 }
205
206 void Sys_FindClose (void)
207 {
208 if (findhandle != -1)
209 _findclose (findhandle);
210 findhandle = 0;
211 }
212
213
214 //============================================
215
216