Debian Codegraph @

dat/udat/linux/dat_osd.h

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