-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.h
167 lines (134 loc) · 4.64 KB
/
util.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
/* 1010 PCI/PCIe Audio Driver
Copyright (c) Eugene Gavrilov, 2002-2016
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
typedef unsigned long dword;
typedef unsigned short word;
typedef unsigned char byte;
#if !defined(InterlockedOr)
FORCEINLINE LONG InterlockedAnd (
__inout __drv_interlocked LONG volatile *Target,
__in LONG Set
)
{
LONG i;
LONG j;
j = *Target;
do {
i = j;
j = InterlockedCompareExchange(Target,
i & Set,
i);
} while (i != j);
return j;
}
FORCEINLINE LONG InterlockedOr (
__inout __drv_interlocked LONG volatile *Target,
__in LONG Set
)
{
LONG i;
LONG j;
j = *Target;
do {
i = j;
j = InterlockedCompareExchange(Target,
i | Set,
i);
} while (i != j);
return j;
}
#endif
extern "C" NTKERNELAPI PHYSICAL_ADDRESS MmGetPhysicalAddress (__in PVOID BaseAddress);
#pragma code_seg()
struct memory_handle_t
{
memory_handle_t() { size=0; addr=NULL; physical=0; };
size_t size;
void *addr; // virtual
dword physical; // physical (we are using 32-bit, since E-DSP can only address 2Gb of RAM anyway)
static size_t pool_size;
__int64 get_physical(size_t offset) { if(!addr) return 0; PHYSICAL_ADDRESS tmp=MmGetPhysicalAddress(((byte *)addr)+offset); return tmp.QuadPart; };
int alloc_contiguous(size_t size);
void free_contiguous(void);
void clear(void) { memset(addr,0,size); };
};
typedef KLOCK_QUEUE_HANDLE lock_instance;
#pragma code_seg()
class lock
{
KSPIN_LOCK spin_lock;
public:
inline void init(void) { KeInitializeSpinLock(&spin_lock); }
inline void acquire(lock_instance *lock_handle) { KeAcquireInStackQueuedSpinLock(&spin_lock,lock_handle); }
inline void release(lock_instance *lock_handle) { KeReleaseInStackQueuedSpinLock(lock_handle); }
inline void acquire_at_dpc(lock_instance *lock_handle) { KeAcquireInStackQueuedSpinLockAtDpcLevel(&spin_lock,lock_handle); }
inline void release_at_dpc(lock_instance *lock_handle) { KeReleaseInStackQueuedSpinLockFromDpcLevel(lock_handle); }
};
#pragma code_seg()
class mutex
{
KMUTEX kmutex;
public:
void init(void) { KeInitializeMutex(&kmutex,0); }
void release(void) { KeReleaseMutex(&kmutex,FALSE); }
bool acquire(void) { return !!KeWaitForSingleObject(&kmutex,Executive,KernelMode,FALSE,NULL); }
};
#pragma code_seg()
class atomic
{
volatile LONG value;
public:
atomic() { value=0; };
inline bool test_and_reset(LONG bit) { return !!InterlockedBitTestAndReset(&value,bit); };
inline void _or(LONG mask) { InterlockedOr(&value,mask); };
inline void _and(LONG mask) { InterlockedAnd(&value,mask); };
inline LONG get(void) { return value; };
};
#if DBG
// timer
#pragma code_seg()
class kTimer
{
private:
LARGE_INTEGER res;
LARGE_INTEGER prev;
int disabled;
int cnt;
public:
kTimer();
void enable(void) { disabled=0; }
void disable(void) { disabled=1; }
void print(int resolution,char *txt);
void warn(int min_,int max_,char *warn);
ULONGLONG time(void);
ULONGLONG ping(void);
};
#else
// timer
#pragma code_seg()
class kTimer
{
public:
kTimer() {};
inline void enable(void) {};
inline void disable(void) {};
inline void print(int ,char *) { ; }
inline void warn(int ,int ,char *) { ; }
inline ULONGLONG time(void) { return 0; }
inline ULONGLONG ping(void) { return 0; }
};
#endif