This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
vsx_platform.h
207 lines (183 loc) · 7.13 KB
/
vsx_platform.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Useful information about the current OS / compiler and compiler / OS-specific hacks
#pragma once
// unused macro
#define VSX_UNUSED(arg) (void)arg
// inlining macro
#ifdef VSX_OPTIMIZATIONS
#define VSX_ALWAYS_INLINE __attribute__((always_inline))
#else
#define VSX_ALWAYS_INLINE
#endif
#define PLATFORM_WINDOWS 0
#define PLATFORM_LINUX 1
#define PLATFORM_MACINTOSH 2
#define PLATFORM_WM6 3
#define PLATFORM_ANDROID 4
#define PLATFORM_SOMETHINGELSE 1000
#define PLATFORM_FAMILY_WINDOWS 0
#define PLATFORM_FAMILY_UNIX 1
#define PLATFORM_FAMILY_OTHER 2
#define COMPILER_GCC 1
#define COMPILER_MINGW 2
#define COMPILER_VISUAL_STUDIO 4
#ifndef PLATFORM
#if defined(WIN32) || defined(WIN64) || defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#ifdef __GNUC__
#define COMPILER COMPILER_MINGW + COMPILER_GCC
#endif
#ifdef _MSC_VER
#define COMPILER COMPILER_VISUAL_STUDIO
#endif
#define PLATFORM PLATFORM_WINDOWS
#define PLATFORM_NAME "Windows"
#define PLATFORM_FAMILY PLATFORM_FAMILY_WINDOWS
#define PLATFORM_SHARED_FILES vsx_string<>("share/")
#define PLATFORM_DLL_SUFFIX ".dll"
#ifdef PLATFORM_SHARED_FILES_STL
#define PLATFORM_SHARED_FILES_STLSTRING std::string("share/")
#endif
#define DIRECTORY_SEPARATOR "/"
#define DIRECTORY_SEPARATOR_CHAR '/'
#define VSXU_PLUGIN_LOCATION vsx_string<>("plugins/")
#elif defined(__APPLE__) || defined(__MACH__)
#define PLATFORM PLATFORM_MACINTOSH
#define PLATFORM_NAME "Macintosh"
#define PLATFORM_FAMILY PLATFORM_FAMILY_UNIX
#define PLATFORM_SHARED_FILES vsx_string<>("")
#define PLATFORM_DLL_SUFFIX ".dylib"
#ifdef PLATFORM_SHARED_FILES_STL
#define PLATFORM_SHARED_FILES_STLSTRING std::string("")
#endif
#define DIRECTORY_SEPARATOR "/"
#define DIRECTORY_SEPARATOR_CHAR '/'
#define VSXU_PLUGIN_LOCATION vsx_string<>("plugins/")
#elif defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
#define COMPILER COMPILER_GCC
#define PLATFORM PLATFORM_LINUX
#define PLATFORM_NAME "GNU / Linux"
#define PLATFORM_FAMILY PLATFORM_FAMILY_UNIX
#define PLATFORM_SHARED_FILES vsx_string<>(get_exec_path().c_str()) + "/../share/vsxu/"
#define VSXU_PLUGIN_LOCATION vsx_string<>(get_exec_path().c_str()) + "/../lib/vsxu/plugins"
#define PLATFORM_DLL_SUFFIX ".so"
#ifdef PLATFORM_SHARED_FILES_STL
#define PLATFORM_SHARED_FILES_STLSTRING get_exec_path() + std::string("/../share/vsxu/")
#endif
#define DIRECTORY_SEPARATOR "/"
#define DIRECTORY_SEPARATOR_CHAR '/'
#else
#define PLATFORM PLATFORM_SOMETHINGELSE
#define PLATFORM_NAME "Something Else"
#define PLATFORM_FAMILY PLATFORM_FAMILY_OTHER
#define PLATFORM_SHARED_FILES ""
#define DIRECTORY_SEPARATOR "/"
#define DIRECTORY_SEPARATOR_CHAR '/'
#define VSXU_PLUGIN_LOCATION vsx_string<>("plugins/")
#endif
/*
How many bits is this system?
*/
// Windows
#if (defined(_WIN64) || defined(WIN64))
#define PLATFORM_BITS 64
// Macintosh
#elif (defined(__LP64__) || defined(_LP64) || defined(__ppc64__))
#define PLATFORM_BITS 64
// Linux
#elif (defined(__x86_64__) || defined(__64BIT__) || (__WORDSIZE == 64))
#define PLATFORM_BITS 64
#else
#define PLATFORM_BITS 32
#endif
#ifdef vsx_string
#undef vsx_string
#endif
#endif
// packing
#if COMPILER == COMPILER_GCC
#define VSX_PACK_BEGIN
#define VSX_PACK_END __attribute__((__packed__));
#define VSX_MEMORY_BARRIER asm volatile("": : :"memory")
#endif
#if COMPILER == COMPILER_VISUAL_STUDIO
#define __PRETTY_FUNCTION__ L""
#define VSX_PACK_BEGIN __pragma( pack(push, 1) )
#define VSX_PACK_END ; __pragma( pack(pop) )
#define VSX_MEMORY_BARRIER MemoryBarrier()
#endif
// aligned malloc
#if COMPILER == COMPILER_VISUAL_STUDIO
#include <malloc.h>
#define vsx_aligned_malloc(n) _aligned_malloc(n, 64)
#define vsx_aligned_realloc(pointer, n) _aligned_realloc(pointer, n, 64)
#define vsx_aligned_free(pointer) _aligned_free(pointer)
#endif
#if PLATFORM_FAMILY == PLATFORM_FAMILY_UNIX
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <string>
#include <cstring>
inline void* vsx_aligned_malloc(size_t n)
{
void* r;
int f = posix_memalign(&r, 64, n);
(void)f;
return r;
}
#define vsx_aligned_realloc(pointer, n) realloc(pointer, n)
#define vsx_aligned_free(pointer) free(pointer)
inline std::string get_exec_path()
{
int dest_len = 2048;
char path[2048];
std::string result;
/*If we are in linux */
if (readlink ("/proc/self/exe", path, dest_len) != -1)
{
dirname (path);
strcat (path, "/");
result = path;
}
else
{
/* else Try the PATH. */
char* systemPath = getenv ("PATH");
char* candidateDir = NULL;
char* baseName = NULL;
if (systemPath != NULL)
{
dest_len--;
systemPath = strdup (systemPath);
for (candidateDir = strtok (systemPath, ":"); candidateDir != NULL; candidateDir = strtok (NULL, ":"))
{
strncpy (path, candidateDir, dest_len);
strncat (path, "/", dest_len);
strncat (path, baseName, dest_len);
if (access(path, F_OK) == 0)
{
free (systemPath);
dirname (path);
strcat (path, "/");
result = path;
break;
}
}
free(systemPath);
dest_len++;
}
}
if(result != "")
return result;
/* again someone has use execve: we dont knowe the executable name; we surrender and give instead current path */
if (getcwd (path, dest_len - 1) == NULL)
return std::string();
strcat (path, "/");
result = path;
return result;
}
#endif
// wchar string hack
#define WIDE2(x) L ##x
#define WIDE1(x) WIDE2(x)