xref: /illumos-gate/usr/src/lib/udapl/libdat/include/dat_osd.h (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 #include <dat/udat.h>
47 
48 #include <assert.h>
49 #include <ctype.h>
50 #include <dlfcn.h>
51 #include <errno.h>
52 #include <pthread.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <syslog.h>
58 #include <unistd.h>
59 #include <sys/time.h>
60 
61 #ifdef	__cplusplus
62 extern "C" {
63 #endif
64 
65 /*
66  *
67  * Debugging
68  *
69  */
70 
71 #define	dat_os_assert(expr)	assert(expr)
72 
73 typedef int 			DAT_OS_DBG_TYPE_VAL;
74 
75 typedef enum
76 {
77     DAT_OS_DBG_TYPE_ERROR 		= 0x1,
78     DAT_OS_DBG_TYPE_GENERIC 		= 0x2,
79     DAT_OS_DBG_TYPE_SR  		= 0x4,
80     DAT_OS_DBG_TYPE_DR  		= 0x8,
81     DAT_OS_DBG_TYPE_PROVIDER_API 	= 0x10,
82     DAT_OS_DBG_TYPE_CONSUMER_API 	= 0x20,
83     DAT_OS_DBG_TYPE_ALL 		= 0xff
84 } DAT_OS_DBG_TYPE;
85 
86 extern void
87 dat_os_dbg_init(void);
88 
89 extern void
90 dat_os_dbg_print(
91 	DAT_OS_DBG_TYPE_VAL		type,
92 	const char			*fmt,
93 	...);
94 
95 
96 /*
97  *
98  * Utility Functions
99  *
100  */
101 
102 #define	DAT_ERROR(Type, SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | \
103 					SubType))
104 
105 typedef size_t 			DAT_OS_SIZE;
106 typedef void * 			DAT_OS_LIBRARY_HANDLE;
107 
108 extern DAT_RETURN
109 dat_os_library_load(
110     const char 			*library_path,
111     DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr);
112 
113 extern DAT_RETURN
114 dat_os_library_unload(
115     const DAT_OS_LIBRARY_HANDLE library_handle);
116 
117 /*
118  * void *dat_os_library_sym(DAT_OS_LIBRARY_HANDLE library_handle, char *sym)
119  */
120 #define	dat_os_library_sym(libhndl, sym)	dlsym((libhndl), (sym))
121 
122 /* char *dat_os_getenv(const char *name) */
123 #define	dat_os_getenv(name)	getenv((name))
124 
125 /* long int dat_os_strtol(const char *nptr, char **endptr, int base) */
126 #define	dat_os_strtol(nptr, endptr, base)	strtol((nptr), (endptr), (base))
127 
128 /* DAT_OS_SIZE dat_os_strlen(const char *s) */
129 #define	dat_os_strlen(s)	strlen((s))
130 
131 /* int dat_os_strncmp(const char *s1, const char *s2, DAT_OS_SIZE n) */
132 #define	dat_os_strncmp(s1, s2, n)	strncmp((s1), (s2), (n))
133 
134 /* void * dat_os_strncpy(char *dest, const char *src, DAT_OS_SIZE len) */
135 #define	dat_os_strncpy(dest, src, len)	strncpy((dest), (src), (len))
136 
137 /* DAT_BOOLEAN dat_os_isblank(int c) */
138 #define	dat_os_isblank(c)	((DAT_BOOLEAN)((' ' == (c)) || ('\t' == (c))) \
139 					? DAT_TRUE : DAT_FALSE)
140 
141 
142 /* DAT_BOOLEAN dat_os_isdigit(int c) */
143 #define	dat_os_isdigit(c)	((DAT_BOOLEAN)(isdigit((c)) ? DAT_TRUE : \
144 					DAT_FALSE))
145 
146 /* void dat_os_usleep(unsigned long usec) */
147 #define	dat_os_usleep(usec)	usleep((usec))
148 
149 /*
150  *
151  * Memory Functions
152  *
153  */
154 
155 /* void *dat_os_alloc(int size) */
156 #define	dat_os_alloc(size)	malloc((size))
157 
158 /* void dat_os_free(void *ptr, int size) */
159 #define	dat_os_free(ptr, size)	free((ptr))
160 
161 /* void *dat_os_memset(void *loc, int c, DAT_OS_SIZE size) */
162 #define	dat_os_memset(loc, c, size)	memset((loc), (c), (size))
163 
164 /*
165  *
166  * File I/O
167  *
168  */
169 
170 typedef FILE 			DAT_OS_FILE;
171 typedef fpos_t			DAT_OS_FILE_POS;
172 
173 /*
174  * DAT_OS_FILE *dat_os_fopen(const char	*path)
175  * always open files in read only mode
176  */
177 #define	dat_os_fopen(path)	((DAT_OS_FILE *)fopen((path), "rF"))
178 
179 
180 /* DAT_RETURN dat_os_fgetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
181 #define	dat_os_fgetpos(file, pos)	((DAT_RETURN)(			\
182 			(0 == fgetpos((file), (pos))) ? DAT_SUCCESS :	\
183 			DAT_INTERNAL_ERROR))
184 
185 /* DAT_RETURN dat_os_fsetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
186 #define	dat_os_fsetpos(file, pos)	((DAT_RETURN)(			\
187 			(0 == fsetpos((file), (pos))) ? DAT_SUCCESS :	\
188 			DAT_INTERNAL_ERROR))
189 
190 /*
191  * dat_os_fgetc() returns EOF on error or end of file.
192  * int dat_os_fgetc(DAT_OS_FILE *file)
193  */
194 #define	dat_os_fgetc(file)	fgetc((file))
195 
196 
197 /* int dat_os_fputc(DAT_OS_FILE *file, int c) */
198 #define	dat_os_fputc(file, c)	fputc((c), (file))
199 
200 /* int dat_os_fungetc(DAT_OS_FILE *file) */
201 #define	dat_os_fungetc(file)	fseek((file), -1, SEEK_CUR)
202 
203 /*
204  * dat_os_fread returns the number of bytes read from the file.
205  * DAT_OS_SIZE dat_os_fread(DAT_OS_FILE *file, char *buf, DAT_OS_SIZE len)
206  */
207 #define	dat_os_fread(file, buf, len)	fread((buf), sizeof (char),	\
208 						(len), (file))
209 
210 /* DAT_RETURN dat_os_fclose(DAT_OS_FILE *file) */
211 #define	dat_os_fclose(file)	((0 == fclose(file)) ? DAT_SUCCESS :	\
212 					DAT_INTERNAL_ERROR)
213 
214 /*
215  *
216  * Locks
217  *
218  */
219 
220 typedef pthread_mutex_t 	DAT_OS_LOCK;
221 
222 
223 /* lock functions */
224 /*
225  * DAT_RETURN dat_os_lock_init(IN DAT_OS_LOCK *m)
226  */
227 #define	dat_os_lock_init(m)	((0 == pthread_mutex_init((m), NULL)) ?	\
228 					DAT_SUCCESS : DAT_INTERNAL_ERROR)
229 
230 /* DAT_RETURN dat_os_lock(IN DAT_OS_LOCK *m) */
231 #define	dat_os_lock(m)		((DAT_RETURN)(				\
232 				(0 == pthread_mutex_lock((m))) ?	\
233 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
234 
235 /* DAT_RETURN dat_os_unlock(IN DAT_OS_LOCK *m) */
236 #define	dat_os_unlock(m)	((DAT_RETURN)(				\
237 				(0 == pthread_mutex_unlock((m))) ?	\
238 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
239 
240 /* DAT_RETURN dat_os_lock_destroy(IN DAT_OS_LOCK *m) */
241 #define	dat_os_lock_destroy(m)	((DAT_RETURN)(				\
242 				(0 == pthread_mutex_destroy((m))) ?	\
243 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
244 
245 /*
246  * Simple macro to verify a handle is bad. Conditions:
247  * - pointer is NULL
248  * - pointer is not word aligned
249  */
250 #define	DAT_BAD_HANDLE(h) (((h) == NULL) || ((unsigned long)(h) & 3))
251 
252 #ifdef	__cplusplus
253 }
254 #endif
255 
256 #endif	/* _DAT_OSD_H_ */
257