xref: /titanic_44/usr/src/lib/udapl/libdat/include/dat_osd.h (revision 9dd0f810214fdc8e1af881a9a5c4b6927629ff9e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
23  */
24 
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  *
32  * HEADER: dat_osd.h
33  *
34  * PURPOSE: Operating System Dependent layer
35  * Description:
36  *	Provide OS dependent data structures & functions with
37  *	a canonical DAT interface. Designed to be portable
38  *	and hide OS specific quirks of common functions.
39  *
40  * $Id: dat_osd.h,v 1.14 2003/07/31 14:04:19 jlentini Exp $
41  */
42 
43 #ifndef _DAT_OSD_H_
44 #define	_DAT_OSD_H_
45 
46 #pragma ident	"%Z%%M%	%I%	%E% SMI"
47 
48 #include <dat/udat.h>
49 
50 #include <assert.h>
51 #include <ctype.h>
52 #include <dlfcn.h>
53 #include <errno.h>
54 #include <pthread.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <syslog.h>
60 #include <unistd.h>
61 #include <sys/time.h>
62 
63 #ifdef	__cplusplus
64 extern "C" {
65 #endif
66 
67 /*
68  *
69  * Debugging
70  *
71  */
72 
73 #define	dat_os_assert(expr)	assert(expr)
74 
75 typedef int 			DAT_OS_DBG_TYPE_VAL;
76 
77 typedef enum
78 {
79     DAT_OS_DBG_TYPE_ERROR 		= 0x1,
80     DAT_OS_DBG_TYPE_GENERIC 		= 0x2,
81     DAT_OS_DBG_TYPE_SR  		= 0x4,
82     DAT_OS_DBG_TYPE_DR  		= 0x8,
83     DAT_OS_DBG_TYPE_PROVIDER_API 	= 0x10,
84     DAT_OS_DBG_TYPE_CONSUMER_API 	= 0x20,
85     DAT_OS_DBG_TYPE_ALL 		= 0xff
86 } DAT_OS_DBG_TYPE;
87 
88 extern void
89 dat_os_dbg_init(void);
90 
91 extern void
92 dat_os_dbg_print(
93 	DAT_OS_DBG_TYPE_VAL		type,
94 	const char			*fmt,
95 	...);
96 
97 
98 /*
99  *
100  * Utility Functions
101  *
102  */
103 
104 #define	DAT_ERROR(Type, SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | \
105 					SubType))
106 
107 typedef size_t 			DAT_OS_SIZE;
108 typedef void * 			DAT_OS_LIBRARY_HANDLE;
109 
110 extern DAT_RETURN
111 dat_os_library_load(
112     const char 			*library_path,
113     DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr);
114 
115 extern DAT_RETURN
116 dat_os_library_unload(
117     const DAT_OS_LIBRARY_HANDLE library_handle);
118 
119 /*
120  * void *dat_os_library_sym(DAT_OS_LIBRARY_HANDLE library_handle, char *sym)
121  */
122 #define	dat_os_library_sym(libhndl, sym)	dlsym((libhndl), (sym))
123 
124 /* char *dat_os_getenv(const char *name) */
125 #define	dat_os_getenv(name)	getenv((name))
126 
127 /* long int dat_os_strtol(const char *nptr, char **endptr, int base) */
128 #define	dat_os_strtol(nptr, endptr, base)	strtol((nptr), (endptr), (base))
129 
130 /* DAT_OS_SIZE dat_os_strlen(const char *s) */
131 #define	dat_os_strlen(s)	strlen((s))
132 
133 /* int dat_os_strncmp(const char *s1, const char *s2, DAT_OS_SIZE n) */
134 #define	dat_os_strncmp(s1, s2, n)	strncmp((s1), (s2), (n))
135 
136 /* void * dat_os_strncpy(char *dest, const char *src, DAT_OS_SIZE len) */
137 #define	dat_os_strncpy(dest, src, len)	strncpy((dest), (src), (len))
138 
139 /* DAT_BOOLEAN dat_os_isblank(int c) */
140 #define	dat_os_isblank(c)	((DAT_BOOLEAN)((' ' == (c)) || ('\t' == (c))) \
141 					? DAT_TRUE : DAT_FALSE)
142 
143 
144 /* DAT_BOOLEAN dat_os_isdigit(int c) */
145 #define	dat_os_isdigit(c)	((DAT_BOOLEAN)(isdigit((c)) ? DAT_TRUE : \
146 					DAT_FALSE))
147 
148 /* void dat_os_usleep(unsigned long usec) */
149 #define	dat_os_usleep(usec)	usleep((usec))
150 
151 /*
152  *
153  * Memory Functions
154  *
155  */
156 
157 /* void *dat_os_alloc(int size) */
158 #define	dat_os_alloc(size)	malloc((size))
159 
160 /* void dat_os_free(void *ptr, int size) */
161 #define	dat_os_free(ptr, size)	free((ptr))
162 
163 /* void *dat_os_memset(void *loc, int c, DAT_OS_SIZE size) */
164 #define	dat_os_memset(loc, c, size)	memset((loc), (c), (size))
165 
166 /*
167  *
168  * File I/O
169  *
170  */
171 
172 typedef FILE 			DAT_OS_FILE;
173 typedef fpos_t			DAT_OS_FILE_POS;
174 
175 /*
176  * DAT_OS_FILE *dat_os_fopen(const char	*path)
177  * always open files in read only mode
178  */
179 #define	dat_os_fopen(path)	((DAT_OS_FILE *)fopen((path), "rF"))
180 
181 
182 /* DAT_RETURN dat_os_fgetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
183 #define	dat_os_fgetpos(file, pos)	((DAT_RETURN)(			\
184 			(0 == fgetpos((file), (pos))) ? DAT_SUCCESS :	\
185 			DAT_INTERNAL_ERROR))
186 
187 /* DAT_RETURN dat_os_fsetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
188 #define	dat_os_fsetpos(file, pos)	((DAT_RETURN)(			\
189 			(0 == fsetpos((file), (pos))) ? DAT_SUCCESS :	\
190 			DAT_INTERNAL_ERROR))
191 
192 /*
193  * dat_os_fgetc() returns EOF on error or end of file.
194  * int dat_os_fgetc(DAT_OS_FILE *file)
195  */
196 #define	dat_os_fgetc(file)	fgetc((file))
197 
198 
199 /* int dat_os_fputc(DAT_OS_FILE *file, int c) */
200 #define	dat_os_fputc(file, c)	fputc((c), (file))
201 
202 /* int dat_os_fungetc(DAT_OS_FILE *file) */
203 #define	dat_os_fungetc(file)	fseek((file), -1, SEEK_CUR)
204 
205 /*
206  * dat_os_fread returns the number of bytes read from the file.
207  * DAT_OS_SIZE dat_os_fread(DAT_OS_FILE *file, char *buf, DAT_OS_SIZE len)
208  */
209 #define	dat_os_fread(file, buf, len)	fread((buf), sizeof (char),	\
210 						(len), (file))
211 
212 /* DAT_RETURN dat_os_fclose(DAT_OS_FILE *file) */
213 #define	dat_os_fclose(file)	((0 == fclose(file)) ? DAT_SUCCESS :	\
214 					DAT_INTERNAL_ERROR)
215 
216 /*
217  *
218  * Locks
219  *
220  */
221 
222 typedef pthread_mutex_t 	DAT_OS_LOCK;
223 
224 
225 /* lock functions */
226 /*
227  * DAT_RETURN dat_os_lock_init(IN DAT_OS_LOCK *m)
228  */
229 #define	dat_os_lock_init(m)	((0 == pthread_mutex_init((m), NULL)) ?	\
230 					DAT_SUCCESS : DAT_INTERNAL_ERROR)
231 
232 /* DAT_RETURN dat_os_lock(IN DAT_OS_LOCK *m) */
233 #define	dat_os_lock(m)		((DAT_RETURN)(				\
234 				(0 == pthread_mutex_lock((m))) ?	\
235 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
236 
237 /* DAT_RETURN dat_os_unlock(IN DAT_OS_LOCK *m) */
238 #define	dat_os_unlock(m)	((DAT_RETURN)(				\
239 				(0 == pthread_mutex_unlock((m))) ?	\
240 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
241 
242 /* DAT_RETURN dat_os_lock_destroy(IN DAT_OS_LOCK *m) */
243 #define	dat_os_lock_destroy(m)	((DAT_RETURN)(				\
244 				(0 == pthread_mutex_destroy((m))) ?	\
245 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
246 
247 /*
248  * Simple macro to verify a handle is bad. Conditions:
249  * - pointer is NULL
250  * - pointer is not word aligned
251  */
252 #define	DAT_BAD_HANDLE(h) (((h) == NULL) || ((unsigned long)(h) & 3))
253 
254 #ifdef	__cplusplus
255 }
256 #endif
257 
258 #endif	/* _DAT_OSD_H_ */
259