aboutsummaryrefslogtreecommitdiffstats
path: root/o3000_portable.h
blob: 4e7da6aa63fbb622013dd7b605562ea62d42b383 (plain)
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
/**
* @file			o3000_portable.h
* @brief		O-3000 definitions to make portable platform-independent code
* @author		Patrick Roth - roth@stettbacher.ch
* @author		Christian Jaeggi - jaeggi@stettbacher.ch
* @version		1.0
* @date			2016-03-01
* @copyright	2012-2016 Stettbacher Signal Processing AG
* 
* @remarks
*
* <PRE>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* </PRE>
*
*/


#ifndef _O3000_PORTABLE_H
#define _O3000_PORTABLE_H


/* stdint.h is not available on older MSVC */
#if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))

typedef unsigned __int8   uint8_t;
typedef unsigned __int16  uint16_t;
typedef unsigned __int32  uint32_t;
typedef unsigned __int64  uint64_t;

#else

#include <stdint.h>

#endif // defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))



/*
 * Calling conventions
 * The System V ABI is one of the major ABIs in use today and is virtually universal among Unix
 * systems. It is the calling convention used by toolchains such as i686-elf-gcc and x86_64-elf-gcc. 
 * 
 * Windows builds often use "project files" and are less transparent and more ad hoc than Makefiles.
 * As an example, the driver crashes by calling a callback previously register via .NET. Therfore 
 * on MSVC, the calling ABI is defined. On other system (UNIX), the macros below are left
 * empty.
 * 
 * _WIN32 is always defined for Win32 or Win64 applications.
 */
#ifndef _WIN32

#define __cdecl    	/* nothing */
#define __stdcall  	/* nothing */
#define __fastcall 	/* nothing */

#else

#include <windows.h>

#endif // _WIN32


/*
 * aligned malloc functiions
 */
#ifndef _WIN32

#define ALIGNED_ALLOC(align, size)	aligned_alloc(align, size)
#define ALIGNED_FREE(buf)			free(buf)

#else

#include <malloc.h>
#define ALIGNED_ALLOC(align, size)	_aligned_malloc(size, align)
#define ALIGNED_FREE(buf)			_aligned_free(buf)

#endif // _WIN32


/*
 * structure packing macros
 */
#if defined(_MSC_VER)

#define __func__					__FUNCTION__
#define __packed__					__packed

#else

/* define nothing for __func__ because it's used as default macro at source code */
#define __packed__ 					__attribute__ ((packed))

#endif // defined(_MSC_VER)


/*
 * mutexes
 * 
 * copied from libusb project see files os/threads_windows.* and os/threads_poxix.*
 */
#if defined(_MSC_VER)

#define O3000_MUTEX_INITIALIZER		0L
typedef LONG						o3000_mutex_static_t;
static inline void o3000_mutex_static_lock(o3000_mutex_static_t *mutex) {
	while (InterlockedExchange(mutex, 1L) == 1L)
		SleepEx(0, TRUE);
}
static inline void o3000_mutex_static_unlock(o3000_mutex_static_t *mutex) {
	InterlockedExchange(mutex, 0L);
}

#else

#include <pthread.h>

#define O3000_MUTEX_INITIALIZER		PTHREAD_MUTEX_INITIALIZER
typedef pthread_mutex_t				o3000_mutex_static_t;
static inline void o3000_mutex_static_lock(o3000_mutex_static_t *mutex) {
	(void)pthread_mutex_lock(mutex);
}
static inline void o3000_mutex_static_unlock(o3000_mutex_static_t *mutex) {
	(void)pthread_mutex_unlock(mutex);
}

#endif // defined(_MSC_VER)


#endif // _O3000_PORTABLE_H