Jungo WinDriver  
Official Documentation
kpstdlib.h
Go to the documentation of this file.
1/* @JUNGO_COPYRIGHT@ */
2
3#ifndef _KPSTDLIB_H_
4#define _KPSTDLIB_H_
5
6#ifndef __KERNEL__
7 #define __KERNEL__
8#endif
9
10#if !defined(UNIX) && defined(LINUX)
11 #define UNIX
12#endif
13
14#if defined(UNIX)
15 #include "windrvr.h" // for use of KDBG DWORD parameter.
16#endif
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/* Spinlocks and interlocked operations */
23
25typedef struct _KP_SPINLOCK KP_SPINLOCK;
35
47
59
71
73typedef volatile int KP_INTERLOCKED;
74
86
98
110
122
136int kp_interlocked_add(KP_INTERLOCKED *target, int val);
137
149
162void kp_interlocked_set(KP_INTERLOCKED *target, int val);
163
179
180#if defined(WINNT) || defined(WIN32)
181 #if defined(_WIN64) && !defined(KERNEL_64BIT)
182 #define KERNEL_64BIT
183 #endif
184 typedef unsigned long ULONG;
185 typedef unsigned short USHORT;
186 typedef unsigned char UCHAR;
187 typedef long LONG;
188 typedef short SHORT;
189 typedef char CHAR;
190 typedef ULONG DWORD;
191 typedef DWORD *PDWORD;
192 typedef unsigned char *PBYTE;
193 typedef USHORT WORD;
194 typedef void *PVOID;
195 typedef char *PCHAR;
196 typedef PVOID HANDLE;
197 typedef ULONG BOOL;
198 #ifndef WINAPI
199 #define WINAPI
200 #endif
201#elif defined(UNIX)
202 #ifndef __cdecl
203 #define __cdecl
204 #endif
205#endif
206
207#if defined(WINNT) || defined(WIN32)
208 #define OS_needs_copy_from_user(fKernelMode) FALSE
209 #define COPY_FROM_USER(dst,src,n) memcpy(dst,src,n)
210 #define COPY_TO_USER(dst,src,n) memcpy(dst,src,n)
211#elif defined(LINUX)
212 #define OS_needs_copy_from_user(fKernelMode) (!fKernelMode)
213 #define COPY_FROM_USER(dst,src,n) LINUX_copy_from_user(dst,src,n)
214 #define COPY_TO_USER(dst,src,n) LINUX_copy_to_user(dst,src,n)
215#endif
216
239#define COPY_FROM_USER_OR_KERNEL(dst, src, n, fKernelMode) \
240{ \
241 if (OS_needs_copy_from_user(fKernelMode)) \
242 COPY_FROM_USER(dst, src, n); \
243 else \
244 memcpy(dst, src, n); \
245}
246
251#define COPY_TO_USER_OR_KERNEL(dst, src, n, fKernelMode) \
252{ \
253 if (OS_needs_copy_from_user(fKernelMode)) \
254 COPY_TO_USER(dst, src, n); \
255 else \
256 memcpy(dst, src, n); \
257}
258
259#ifndef FALSE
260 #define FALSE 0
261#endif
262
263#ifndef TRUE
264 #define TRUE 1
265#endif
266
267#ifndef NULL
268 #define NULL 0UL
269#endif
270
271int __cdecl KDBG(DWORD dwLevel, DWORD dwSection, const char *format, ...);
272
273#if defined(WIN32)
274 #if defined(KERNEL_64BIT)
275 #include <stdarg.h>
276 #else
277 #define _INTSIZEOF(n) ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1))
278 // Define varargs ANSI style
279 typedef char * va_list;
280 #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
281 #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
282 #define va_end(ap) ( ap = (va_list)0 )
283
284 #endif
285
286 int __cdecl _snprintf(char *buffer, unsigned long Limit, const char *format,
287 ...);
288 int __cdecl _vsnprintf(char *buffer, unsigned long Limit, const char
289 *format, va_list Next);
290#endif
291
292char* __cdecl strcpy(char *s1, const char *s2);
293void* __cdecl malloc(unsigned long size);
294void __cdecl free(void *buf);
295
296#if defined(LINUX)
297 #include <linux_wrappers.h>
298 #define memset LINUX_memset
299 #define strncmp LINUX_strncmp
300 #define strcpy LINUX_strcpy
301 #define strcmp LINUX_strcmp
302 #define strncpy LINUX_strncpy
303 #define strcat LINUX_strcat
304 #define strncat LINUX_strncat
305 #define strlen LINUX_strlen
306 #define memcpy LINUX_memcpy
307 #define memcmp LINUX_memcmp
308 #define sprintf LINUX_sprintf
309 #define vsprintf LINUX_vsprintf
310 #define snprintf LINUX_snprintf
311 #define vsnprintf LINUX_vsnprintf
312#elif defined(WINNT)
313 #if !defined size_t
314 #if defined(KERNEL_64BIT)
315 typedef unsigned __int64 size_t;
316 #else
317 typedef unsigned int size_t;
318 #endif
319 #endif
320 void* __cdecl memcpy(void *dest, const void *src, size_t count);
321 void* __cdecl memset(void *dest, int c, size_t count);
322#if !defined(_STRNCPY)
323 char* _strncpy(char* s1, const char* s2, size_t limit);
324#endif
325 #define snprintf _snprintf
326 #define vsnprintf _vsnprintf
327 #define strncpy _strncpy
328#endif
329
330#ifdef __cplusplus
331}
332#endif
333
334#endif /* _KPSTDLIB_H_ */
335
int kp_interlocked_decrement(KP_INTERLOCKED *target)
Decrements the value of a Kernel PlugIn interlocked counter by one.
int kp_interlocked_add(KP_INTERLOCKED *target, int val)
Adds a specified value to the current value of a Kernel PlugIn interlocked counter.
void kp_interlocked_set(KP_INTERLOCKED *target, int val)
Sets the value of a Kernel PlugIn interlocked counter to the specified value.
void kp_spinlock_uninit(KP_SPINLOCK *spinlock)
Uninitializes a Kernel PlugIn spinlock object.
void *__cdecl malloc(unsigned long size)
struct _KP_SPINLOCK KP_SPINLOCK
Kernel PlugIn spinlock object structure.
Definition: kpstdlib.h:25
int kp_interlocked_increment(KP_INTERLOCKED *target)
Increments the value of a Kernel PlugIn interlocked counter by one.
volatile int KP_INTERLOCKED
a Kernel PlugIn interlocked operations counter
Definition: kpstdlib.h:73
KP_SPINLOCK * kp_spinlock_init(void)
Initializes a new Kernel PlugIn spinlock object.
int __cdecl KDBG(DWORD dwLevel, DWORD dwSection, const char *format,...)
void kp_interlocked_uninit(KP_INTERLOCKED *target)
Uninitializes a Kernel PlugIn interlocked counter.
void kp_interlocked_init(KP_INTERLOCKED *target)
Initializes a Kernel PlugIn interlocked counter.
void __cdecl free(void *buf)
char *__cdecl strcpy(char *s1, const char *s2)
int kp_interlocked_exchange(KP_INTERLOCKED *target, int val)
Sets the value of a Kernel PlugIn interlocked counter to the specified value and returns the previous...
int kp_interlocked_read(KP_INTERLOCKED *target)
Reads to the value of a Kernel PlugIn interlocked counter.
void kp_spinlock_wait(KP_SPINLOCK *spinlock)
Waits on a Kernel PlugIn spinlock object.
void kp_spinlock_release(KP_SPINLOCK *spinlock)
Releases a Kernel PlugIn spinlock object.
unsigned short int WORD
Definition: windrvr.h:333