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