File: dat_osd.h

package info (click to toggle)
dapl 2.0.19-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,300 kB
  • sloc: ansic: 39,217; sh: 10,475; makefile: 529
file content (415 lines) | stat: -rw-r--r-- 9,001 bytes parent folder | download | duplicates (5)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
 *
 * This Software is licensed under one of the following licenses:
 *
 * 1) under the terms of the "Common Public License 1.0" a copy of which is
 *    in the file LICENSE.txt in the root directory. The license is also
 *    available from the Open Source Initiative, see
 *    http://www.opensource.org/licenses/cpl.php.
 *
 * 2) under the terms of the "The BSD License" a copy of which is in the file
 *    LICENSE2.txt in the root directory. The license is also available from
 *    the Open Source Initiative, see
 *    http://www.opensource.org/licenses/bsd-license.php.
 *
 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a 
 *    copy of which is in the file LICENSE3.txt in the root directory. The 
 *    license is also available from the Open Source Initiative, see
 *    http://www.opensource.org/licenses/gpl-license.php.
 *
 * Licensee has the right to choose one of the above licenses.
 *
 * Redistributions of source code must retain the above copyright
 * notice and one of the license notices.
 *
 * Redistributions in binary form must reproduce both the above copyright
 * notice, one of the license notices in the documentation
 * and/or other materials provided with the distribution.
 */

/**********************************************************************
 *
 * HEADER: dat_osd.h
 *
 * PURPOSE: Operating System Dependent layer
 * Description:
 *	Provide OS dependent data structures & functions with
 *	a canonical DAT interface. Designed to be portable
 *	and hide OS specific quirks of common functions.
 *
 * $Id: dat_osd.h,v 1.18 2005/03/24 05:58:36 jlentini Exp $
 **********************************************************************/

#ifndef _DAT_OSD_H_
#define _DAT_OSD_H_

/*
 * This file is defined for Linux systems only, including it on any
 * other build will cause an error
 */
#ifndef __linux__
#error "UNDEFINED OS TYPE"
#endif /* __linux__ */

#include <dat2/udat.h>

#include <assert.h>
#include <ctype.h>
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#ifndef STATIC
#define STATIC static
#endif /* STATIC */

#ifndef INLINE
#define INLINE __inline__
#endif /* INLINE */


/*********************************************************************
 *                                                                   *
 * Debuging                                                          *
 *                                                                   *
 *********************************************************************/

#define dat_os_assert(expr)	assert(expr)

typedef int 			DAT_OS_DBG_TYPE_VAL;

typedef enum
{
    DAT_OS_DBG_TYPE_ERROR 		= 0x1,
    DAT_OS_DBG_TYPE_GENERIC 		= 0x2,
    DAT_OS_DBG_TYPE_SR  		= 0x4,
    DAT_OS_DBG_TYPE_DR  		= 0x8,
    DAT_OS_DBG_TYPE_PROVIDER_API 	= 0x10,
    DAT_OS_DBG_TYPE_CONSUMER_API 	= 0x20,
    DAT_OS_DBG_TYPE_ALL 		= 0xff
} DAT_OS_DBG_TYPE;

extern void
dat_os_dbg_init ( void );

extern void
dat_os_dbg_print (
    DAT_OS_DBG_TYPE_VAL		type,
    const char *		fmt,
    ...);


/*********************************************************************
 *                                                                   *
 * Utility Functions                                                 *
 *                                                                   *
 *********************************************************************/

#define DAT_ERROR(Type, SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | SubType))
#define dat_os_library_error() dlerror()

typedef size_t 			DAT_OS_SIZE;
typedef void * 			DAT_OS_LIBRARY_HANDLE;

extern DAT_RETURN
dat_os_library_load (
    const char 			*library_path,
    DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr );

STATIC INLINE void *
dat_os_library_sym (
    DAT_OS_LIBRARY_HANDLE library_handle,
    char *sym)
{
    return dlsym (library_handle, sym);
}

extern DAT_RETURN
dat_os_library_unload (
    const DAT_OS_LIBRARY_HANDLE library_handle );

STATIC INLINE char *
dat_os_getenv (
    const char *name)
{
    return getenv (name);
}

STATIC INLINE long int
dat_os_strtol (
    const char *nptr,
    char **endptr,
    int base)
{
    return strtol (nptr, endptr, base);
}

STATIC INLINE DAT_OS_SIZE
dat_os_strlen (
    const char *s )
{
    return strlen (s);
}

STATIC INLINE int
dat_os_strncmp (
    const char *s1,
    const char *s2,
    DAT_OS_SIZE n)
{
    return strncmp (s1, s2, n);
}

STATIC INLINE void *
dat_os_strncpy (
    char *dest,
    const char *src,
    DAT_OS_SIZE len)
{
    return strncpy (dest, src, len);
}

STATIC INLINE DAT_BOOLEAN
dat_os_isblank (
    int c)
{
    if ( (' ' == c) || ('\t' == c) )
    {
	return DAT_TRUE;
    }
    else
    {
	return DAT_FALSE;
    }
}

STATIC INLINE DAT_BOOLEAN
dat_os_isdigit (
    int c)
{
    if ( isdigit (c) )
    {
	return DAT_TRUE;
    }
    else
    {
	return DAT_FALSE;
    }
}

STATIC INLINE void
dat_os_usleep (
    unsigned long usec)
{
    usleep (usec);
}


/*********************************************************************
 *                                                                   *
 * Memory Functions                                                  *
 *                                                                   *
 *********************************************************************/

STATIC INLINE void *
dat_os_alloc (
    int size)
{
    return malloc (size);
}

STATIC INLINE void
dat_os_free (
    void *ptr,
    int size)
{
    free (ptr);
}

STATIC INLINE void *
dat_os_memset (void *loc, int c, DAT_OS_SIZE size)
{
    return memset (loc, c, size);
}


/*********************************************************************
 *                                                                   *
 * File I/O                                                          *
 *                                                                   *
 *********************************************************************/

typedef FILE 			DAT_OS_FILE;
typedef fpos_t                  DAT_OS_FILE_POS;


STATIC INLINE DAT_OS_FILE *
dat_os_fopen (
    const char * path)
{
    /* always open files in read only mode*/
    return fopen (path, "r");
}

STATIC INLINE DAT_RETURN
dat_os_fgetpos (
    DAT_OS_FILE *file,
    DAT_OS_FILE_POS *pos)
{
    if ( 0 == fgetpos (file, pos) )
    {
	return DAT_SUCCESS;
    }
    else
    {
	return DAT_INTERNAL_ERROR;
    }
}

STATIC INLINE DAT_RETURN
dat_os_fsetpos (
    DAT_OS_FILE *file,
    DAT_OS_FILE_POS *pos)
{
    if ( 0 == fsetpos (file, pos) )
    {
	return DAT_SUCCESS;
    }
    else
    {
	return DAT_INTERNAL_ERROR;
    }
}

/* dat_os_fgetc() returns EOF on error or end of file. */
STATIC INLINE int
dat_os_fgetc (
    DAT_OS_FILE *file)
{
    return fgetc (file);
}

/* dat_os_ungetc() returns EOF on error or char 'c'.
 * Push char 'c' back into specified stream for subsequent read.
 */
STATIC INLINE int
dat_os_ungetc (
    DAT_OS_FILE *file, int c)
{
    return ungetc(c, file);
}

/* dat_os_fgetc() returns EOF on error or end of file. */
STATIC INLINE int
dat_os_fputc (
    DAT_OS_FILE *file, int c)
{
    return fputc (c, file);
}

/* dat_os_fread returns the number of bytes read from the file. */
STATIC INLINE DAT_OS_SIZE
dat_os_fread (
    DAT_OS_FILE *file,
    char *buf,
    DAT_OS_SIZE len)
{
    return fread (buf, sizeof (char), len, file);
}

STATIC INLINE DAT_RETURN
dat_os_fclose (
    DAT_OS_FILE *file)
{
    if ( 0 == fclose (file) )
    {
	return DAT_SUCCESS;
    }
    else
    {
	return DAT_INTERNAL_ERROR;
    }
}


/*********************************************************************
 *                                                                   *
 * Locks                                                             *
 *                                                                   *
 *********************************************************************/

typedef pthread_mutex_t 	DAT_OS_LOCK;


/* lock functions */
STATIC INLINE DAT_RETURN
dat_os_lock_init (
    IN	DAT_OS_LOCK *m)
{
    /* pthread_mutex_init always returns 0 */
    pthread_mutex_init (m, NULL);

    return DAT_SUCCESS;
}

STATIC INLINE DAT_RETURN
dat_os_lock (
    IN	DAT_OS_LOCK *m)
{
    if (0 == pthread_mutex_lock (m))
    {
	return DAT_SUCCESS;
    }
    else
    {
	return DAT_INTERNAL_ERROR;
    }
}

STATIC INLINE DAT_RETURN
dat_os_unlock (
    IN	DAT_OS_LOCK *m)
{
    if (0 == pthread_mutex_unlock (m))
    {
	return DAT_SUCCESS;
    }
    else
    {
	return DAT_INTERNAL_ERROR;
    }
}

STATIC INLINE DAT_RETURN
dat_os_lock_destroy (
    IN	DAT_OS_LOCK *m)
{
    if (0 == pthread_mutex_destroy (m))
    {
	return DAT_SUCCESS;
    }
    else
    {
	return DAT_INTERNAL_ERROR;
    }
}


#endif /*  _DAT_OSD_H_ */

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 8
 * End:
 */