Debian Codegraph @

dat/udat/windows/dat_osd.h

raw
1 /*
2 * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
3 *
4 * This Software is licensed under either one of the following two licenses:
5 *
6 * 1) under the terms of the "Common Public License 1.0" a copy of which is
7 * in the file LICENSE.txt in the root directory. The license is also
8 * available from the Open Source Initiative, see
9 * http://www.opensource.org/licenses/cpl.php.
10 * OR
11 *
12 * 2) under the terms of the "The BSD License" a copy of which is in the file
13 * LICENSE2.txt in the root directory. The license is also available from
14 * the Open Source Initiative, see
15 * http://www.opensource.org/licenses/bsd-license.php.
16 *
17 * Licensee has the right to choose either one of the above two licenses.
18 *
19 * Redistributions of source code must retain both the above copyright
20 * notice and either one of the license notices.
21 *
22 * Redistributions in binary form must reproduce both the above copyright
23 * notice, either one of the license notices in the documentation
24 * and/or other materials provided with the distribution.
25 */
26
27 /**********************************************************************
28 *
29 * HEADER: dat_osd.h
30 *
31 * PURPOSE: Operating System Dependent layer
32 * Description:
33 * Provide OS dependent data structures & functions with
34 * a canonical DAPL interface. Designed to be portable
35 * and hide OS specific quirks of common functions.
36 *
37 * $Id: dat_osd.h 33 2005-07-11 19:51:17Z ftillier $
38 **********************************************************************/
39
40 #ifndef _DAT_OSD_H_
41 #define _DAT_OSD_H_
42
43 /*
44 * This file is defined for Windows systems only, including it on any
45 * other build will cause an error
46 */
47 #if !defined(WIN32) && !defined(WIN64)
48 #error "UNDEFINED OS TYPE"
49 #endif /* WIN32/64 */
50
51 #include <dat2/udat.h>
52 #include <dat2/dat_registry.h>
53
54 #include <windows.h>
55 #include <assert.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stdarg.h>
60 #include <_errno.h>
61
62 #ifndef STATIC
63 #define STATIC static
64 #endif /* STATIC */
65
66 #ifndef INLINE
67 #define INLINE __inline
68 #endif /* INLINE */
69
70 #ifndef __inline__
71 #define __inline__ __inline
72 #endif /* __inline */
73
74 /*********************************************************************
75 * *
76 * Debuging *
77 * *
78 *********************************************************************/
79
80 #define dat_os_assert(expr) assert(expr)
81
82 typedef int DAT_OS_DBG_TYPE_VAL;
83
84 typedef enum
85 {
86 DAT_OS_DBG_TYPE_ERROR = 0x1,
87 DAT_OS_DBG_TYPE_GENERIC = 0x2,
88 DAT_OS_DBG_TYPE_SR = 0x4,
89 DAT_OS_DBG_TYPE_DR = 0x8,
90 DAT_OS_DBG_TYPE_PROVIDER_API = 0x10,
91 DAT_OS_DBG_TYPE_CONSUMER_API = 0x20,
92 DAT_OS_DBG_TYPE_ALL = 0xff
93 } DAT_OS_DBG_TYPE_TYPE;
94
95 extern void
96 dat_os_dbg_init ( void );
97
98 extern void
99 dat_os_dbg_print (
100 DAT_OS_DBG_TYPE_VAL type,
101 const char * fmt,
102 ...);
103
104
105 /*********************************************************************
106 * *
107 * Utility Functions *
108 * *
109 *********************************************************************/
110
111 #define DAT_ERROR(Type,SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | SubType))
112
113 typedef size_t DAT_OS_SIZE;
114 typedef HMODULE DAT_OS_LIBRARY_HANDLE;
115
116 #ifdef INLINE_LIB_LOAD
117
118 STATIC INLINE DAT_RETURN
119 dat_os_library_load (
120 const char *library_path,
121 DAT_OS_LIBRARY_HANDLE *library_handle_ptr)
122 {
123 DAT_OS_LIBRARY_HANDLE library_handle;
124
125 if ( NULL != (library_handle = LoadLibrary(library_path)) )
126 {
127 if ( NULL != library_handle_ptr )
128 {
129 *library_handle_ptr = library_handle;
130 }
131 return DAT_SUCCESS;
132 }
133 else
134 {
135 dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR,
136 "DAT: library load failure\n");
137 return DAT_INTERNAL_ERROR;
138 }
139 }
140 #else
141
142 DAT_RETURN
143 dat_os_library_load (
144 const char *library_path,
145 DAT_OS_LIBRARY_HANDLE *library_handle_ptr);
146
147 #endif
148
149 extern char *dat_os_library_error(void);
150
151 #ifdef WIN32_LEAN_AND_MEAN
152 #define dat_os_library_sym GetProcAddress
153 #else
154 STATIC INLINE
155 dat_os_library_sym (
156 DAT_OS_LIBRARY_HANDLE library_handle,
157 const char *sym)
158 {
159 return GetProcAddress(library_handle, (LPCSTR)sym);
160 }
161 #endif /* WIN32_LEAN_AND_MEAN */
162
163 STATIC INLINE DAT_RETURN
164 dat_os_library_unload (
165 const DAT_OS_LIBRARY_HANDLE library_handle)
166 {
167 if ( 0 == FreeLibrary(library_handle) )
168 {
169 return DAT_INTERNAL_ERROR;
170 }
171 else
172 {
173 return DAT_SUCCESS;
174 }
175 }
176
177 STATIC INLINE char *
178 dat_os_getenv (
179 const char *name)
180 {
181 return getenv(name);
182 }
183
184 STATIC INLINE long int
185 dat_os_strtol (
186 const char *nptr,
187 char **endptr,
188 int base)
189 {
190 return strtol(nptr, endptr, base);
191 }
192
193 STATIC INLINE DAT_OS_SIZE
194 dat_os_strlen (
195 const char *s )
196 {
197 return strlen(s);
198 }
199
200 STATIC INLINE int
201 dat_os_strncmp (
202 const char *s1,
203 const char *s2,
204 DAT_OS_SIZE n)
205 {
206 return strncmp(s1, s2, n);
207 }
208
209 STATIC INLINE void *
210 dat_os_strncpy (
211 char *dest,
212 const char *src,
213 DAT_OS_SIZE len)
214 {
215 return strncpy (dest, src, len);
216 }
217
218 STATIC INLINE DAT_BOOLEAN
219 dat_os_isblank(
220 int c)
221 {
222 if ( (' ' == c) || ('\t' == c) ) { return DAT_TRUE; }
223 else { return DAT_FALSE; }
224 }
225
226 STATIC INLINE DAT_BOOLEAN
227 dat_os_isdigit(
228 int c)
229 {
230 if ( isdigit(c) ) { return DAT_TRUE; }
231 else { return DAT_FALSE; }
232 }
233
234 STATIC INLINE void
235 dat_os_usleep(
236 unsigned long usec)
237 {
238 Sleep(usec/1000);
239 }
240
241
242 /*********************************************************************
243 * *
244 * Memory Functions *
245 * *
246 *********************************************************************/
247
248 extern HANDLE heap;
249
250 STATIC INLINE void *
251 dat_os_alloc (
252 int size)
253 {
254 return HeapAlloc(heap, 0, size);
255 }
256
257 STATIC INLINE void
258 dat_os_free (
259 void *ptr,
260 int size)
261 {
262 UNREFERENCED_PARAMETER(size);
263 HeapFree(heap, 0, ptr);
264 }
265
266 STATIC INLINE void *
267 dat_os_memset (void *loc, int c, DAT_OS_SIZE size)
268 {
269 return memset (loc, c, size);
270 }
271
272
273 /*********************************************************************
274 * *
275 * File I/O *
276 * *
277 *********************************************************************/
278
279 typedef FILE DAT_OS_FILE;
280 typedef fpos_t DAT_OS_FILE_POS;
281
282
283 STATIC INLINE DAT_OS_FILE *
284 dat_os_fopen (
285 const char * path)
286 {
287 /* always open files in read only mode*/
288 return fopen(path, "r");
289 }
290
291 STATIC INLINE DAT_RETURN
292 dat_os_fgetpos (
293 DAT_OS_FILE *file,
294 DAT_OS_FILE_POS *pos)
295 {
296 if ( 0 == fgetpos(file, pos) )
297 {
298 return DAT_SUCCESS;
299 }
300 else
301 {
302 return DAT_INTERNAL_ERROR;
303 }
304 }
305
306 STATIC INLINE DAT_RETURN
307 dat_os_fsetpos (
308 DAT_OS_FILE *file,
309 DAT_OS_FILE_POS *pos)
310 {
311 if ( 0 == fsetpos(file, pos) )
312 {
313 return DAT_SUCCESS;
314 }
315 else
316 {
317 return DAT_INTERNAL_ERROR;
318 }
319 }
320
321 /* dat_os_fgetc() returns EOF on error or end of file. */
322 STATIC INLINE int
323 dat_os_fgetc (
324 DAT_OS_FILE *file)
325 {
326 return fgetc(file);
327 }
328
329 /* dat_os_ungetc() returns EOF on error or char 'c'.
330 * Push char 'c' back into specified stream for subsequent read.
331 */
332 STATIC INLINE int
333 dat_os_ungetc (
334 DAT_OS_FILE *file, int c)
335 {
336 return ungetc(c, file);
337 }
338
339 /* dat_os_fputc() returns EOF on error or char 'c'. */
340 STATIC INLINE int
341 dat_os_fputc (
342 DAT_OS_FILE *file, int c)
343 {
344 return fputc(c, file);
345 }
346
347 /* dat_os_fread returns the number of bytes read from the file. */
348 STATIC INLINE DAT_OS_SIZE
349 dat_os_fread (
350 DAT_OS_FILE *file,
351 char *buf,
352 DAT_OS_SIZE len)
353 {
354 return fread(buf, sizeof(char), len, file);
355 }
356
357 STATIC INLINE DAT_RETURN
358 dat_os_fclose (
359 DAT_OS_FILE *file)
360 {
361 if ( 0 == fclose(file) )
362 {
363 return DAT_SUCCESS;
364 }
365 else
366 {
367 return DAT_INTERNAL_ERROR;
368 }
369 }
370
371
372 /*********************************************************************
373 * *
374 * Locks *
375 * *
376 *********************************************************************/
377
378 typedef HANDLE DAT_OS_LOCK;
379
380 /* lock functions */
381 STATIC INLINE DAT_RETURN
382 dat_os_lock_init (
383 IN DAT_OS_LOCK *m)
384 {
385 *m = CreateMutex (0, FALSE, 0);
386 if (*(HANDLE *)m == NULL)
387 {
388 return DAT_INTERNAL_ERROR;
389 }
390 return DAT_SUCCESS;
391 }
392
393 STATIC INLINE DAT_RETURN
394 dat_os_lock (
395 IN DAT_OS_LOCK *m)
396 {
397 WaitForSingleObject(*m, INFINITE);
398
399 return DAT_SUCCESS;
400 }
401
402 STATIC INLINE DAT_RETURN
403 dat_os_unlock (
404 IN DAT_OS_LOCK *m)
405 {
406 ReleaseMutex (*m);
407
408 return DAT_SUCCESS;
409 }
410
411 STATIC INLINE DAT_RETURN
412 dat_os_lock_destroy (
413 IN DAT_OS_LOCK *m)
414 {
415 CloseHandle (*m);
416
417 return DAT_SUCCESS;
418 }
419
420
421 #endif /* _DAT_OSD_H_ */
422
423 /*
424 * Local variables:
425 * c-indent-level: 4
426 * c-basic-offset: 4
427 * tab-width: 8
428 * End:
429 */
430