]> git.cworth.org Git - apitrace/blob - os_posix.cpp
Handle vertex attriv locations correctly.
[apitrace] / os_posix.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010-2011 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
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <unistd.h>
32 #include <sys/time.h>
33 #include <pthread.h>
34
35 #ifdef __APPLE__
36 #include <mach-o/dyld.h>
37 #endif
38
39 #include "os.hpp"
40
41
42 namespace OS {
43
44
45 static pthread_mutex_t 
46 mutex = PTHREAD_MUTEX_INITIALIZER;
47
48
49 void
50 AcquireMutex(void)
51 {
52     pthread_mutex_lock(&mutex);
53 }
54
55
56 void
57 ReleaseMutex(void)
58 {
59     pthread_mutex_unlock(&mutex);
60 }
61
62
63 bool
64 GetProcessName(char *str, size_t size)
65 {
66     char szProcessPath[PATH_MAX + 1];
67     char *lpProcessName;
68
69     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
70 #ifdef __APPLE__
71     uint32_t len = sizeof szProcessPath;
72     if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
73         *str = 0;
74         return false;
75     }
76 #else
77     ssize_t len;
78     len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
79     if (len == -1) {
80         snprintf(str, size, "%i", (int)getpid());
81         return true;
82     }
83 #endif
84     szProcessPath[len] = 0;
85
86     lpProcessName = strrchr(szProcessPath, '/');
87     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
88
89     strncpy(str, lpProcessName, size);
90     if (size)
91         str[size - 1] = 0;
92
93     return true;
94 }
95
96 bool
97 GetCurrentDir(char *str, size_t size)
98 {
99     char *ret;
100     ret = getcwd(str, size);
101     str[size - 1] = 0;
102     return ret ? true : false;
103 }
104
105 void
106 DebugMessage(const char *format, ...)
107 {
108     va_list ap;
109     va_start(ap, format);
110     fflush(stdout);
111     vfprintf(stderr, format, ap);
112     va_end(ap);
113 }
114
115 long long GetTime(void)
116 {
117     struct timeval tv;
118     gettimeofday(&tv, NULL);
119     return tv.tv_usec + tv.tv_sec*1000000LL;
120 }
121
122 void
123 Abort(void)
124 {
125     exit(0);
126 }
127
128
129 } /* namespace OS */
130