q_shwin.c 4.9 KB

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