]> git.cworth.org Git - apitrace/blob - os_win32.cpp
No need to filter functions that don't exist in GLEW.
[apitrace] / os_win32.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include <windows.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "os.hpp"
31 #include "trace_write.hpp"
32
33
34 namespace OS {
35
36
37 /* 
38  * Trick from http://locklessinc.com/articles/pthreads_on_windows/
39  */
40 static CRITICAL_SECTION
41 CriticalSection = {
42     (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
43 };
44
45
46 void
47 AcquireMutex(void)
48 {
49     EnterCriticalSection(&CriticalSection); 
50 }
51
52
53 void
54 ReleaseMutex(void)
55 {
56     LeaveCriticalSection(&CriticalSection); 
57 }
58
59
60 bool
61 GetProcessName(char *str, size_t size)
62 {
63     char szProcessPath[PATH_MAX];
64     char *lpProcessName;
65     char *lpProcessExt;
66
67     GetModuleFileNameA(NULL, szProcessPath, sizeof(szProcessPath)/sizeof(szProcessPath[0]));
68
69     lpProcessName = strrchr(szProcessPath, '\\');
70     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
71
72     lpProcessExt = strrchr(lpProcessName, '.');
73     if (lpProcessExt) {
74         *lpProcessExt = '\0';
75     }
76
77     strncpy(str, lpProcessName, size);
78
79     return true;
80 }
81
82 bool
83 GetCurrentDir(char *str, size_t size)
84 {
85     DWORD ret;
86     ret = GetCurrentDirectoryA(size, str);
87     str[size - 1] = 0;
88     return ret == 0 ? false : true;
89 }
90
91 void
92 DebugMessage(const char *format, ...)
93 {
94    char buf[4096];
95
96    va_list ap;
97    va_start(ap, format);
98    fflush(stdout);
99    vsnprintf(buf, sizeof buf, format, ap);
100    va_end(ap);
101
102    OutputDebugStringA(buf);
103    if (!IsDebuggerPresent()) {
104       fflush(stdout);
105       fputs(buf, stderr);
106       fflush(stderr);
107    }
108 }
109
110 void
111 Abort(void)
112 {
113 #ifndef NDEBUG
114     DebugBreak();
115 #else
116     ExitProcess(0);
117 #endif
118 }
119
120
121
122 } /* namespace OS */
123
124
125 #if 0
126 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
127     switch(fdwReason) {
128     case DLL_PROCESS_ATTACH:
129     case DLL_THREAD_ATTACH:
130         return TRUE;
131     case DLL_THREAD_DETACH:
132         return TRUE;
133     case DLL_PROCESS_DETACH:
134         Trace::Close();
135         return TRUE;
136     }
137     (void)hinstDLL;
138     (void)lpvReserved;
139     return TRUE;
140 }
141 #endif