Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Consider the case of windows context, update xbt_context_new() and xbt_context_destro...
[simgrid.git] / src / portable.h
1 /* $Id$ */
2
3 /* portable -- header loading to write portable code                         */
4 /* loads much more stuff than sysdep.h since the latter is in public interface*/
5
6 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                   */
7
8 /* This program is free software; you can redistribute it and/or modify it
9   * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #ifndef GRAS_PORTABLE_H
12 #define GRAS_PORTABLE_H
13
14 /* 
15  * win32 or win64 (__WIN32 is defined for win32 and win64 applications, __TOS_WIN__ is defined by xlC). 
16 */ 
17 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__TOS_WIN__)
18 # include "win32/config.h"
19 #  include <windows.h>
20 #else
21 #  include "gras_config.h"
22 #endif
23
24 #include <stdarg.h>
25
26 #ifdef HAVE_ERRNO_H
27 #  include <errno.h>
28 #endif
29
30 #ifdef HAVE_UNISTD_H
31 #  include <unistd.h>
32 #endif
33
34 /****
35  **** Networking 
36  ****/
37
38
39 #ifdef HAVE_SYS_SOCKET_H
40 #  include <sys/socket.h>
41 #  include <netinet/in.h>   /* sometimes required for #include <arpa/inet.h> */
42 #  include <netinet/tcp.h>  /* TCP_NODELAY */
43 #  include <netdb.h>        /* getprotobyname() */
44 #  include <arpa/inet.h>    /* inet_ntoa() */
45 #  include <sys/types.h>    /* sometimes required for fd_set */
46 # endif
47
48
49 #ifndef HAVE_WINSOCK_H
50 #       define tcp_read( s, buf, len)   read( s, buf, len )
51 #       define tcp_write( s, buf, len)  write( s, buf, len )
52 #       define sock_errno        errno
53 #       define sock_errstr(err)  strerror(err)
54
55 #       ifdef SHUT_RDWR
56 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
57 #       else
58 #               define tcp_close( s )   close( s )
59 #       endif
60 #endif /* windows or unix ? */
61
62 /****
63  **** File handling
64  ****/
65
66 #include <fcntl.h>
67
68 #ifdef HAVE_SYS_STAT_H
69 #  include <sys/stat.h>
70 #endif
71
72 #ifndef O_BINARY
73 #  define O_BINARY 0
74 #endif
75
76 /****
77  **** Time handling
78  ****/
79
80 #ifdef TIME_WITH_SYS_TIME
81 # include <sys/time.h>
82 # include <time.h>
83 #else
84 # if HAVE_SYS_TIME_H
85 #  include <sys/time.h>
86 # else
87 #  include <time.h>
88 # endif
89 #endif
90
91 #ifdef _WIN32
92 #define sleep _sleep /* else defined in stdlib.h */
93 #endif
94
95 /****
96  **** Contexts
97  ****/
98
99 #ifdef USE_UCONTEXT
100 # ifndef S_SPLINT_S /* This header drives splint into the wall */
101 #   include <ucontext.h>
102 # endif 
103 #endif
104
105 #ifdef _WIN32
106 #  include "xbt/context_win32.h" /* Manual reimplementation for prehistoric platforms */
107 #endif
108
109 /****
110  **** Signals
111  ****/
112 #ifdef HAVE_SIGNAL_H
113 # include <signal.h>
114 #endif
115
116 /****
117  **** string handling (parts from http://www.ijs.si/software/snprintf/)
118  ****/
119
120 /* prototype of C99 functions */
121 #ifdef HAVE_SNPRINTF
122 #include <stdio.h>
123 #else
124 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
125 extern int vsnprintf(char *, size_t, const char *, va_list);
126 #endif
127
128 /* use internal functions when OS provided ones are borken */
129 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
130 extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
131 extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
132 #define snprintf  portable_snprintf
133 #define vsnprintf portable_vsnprintf
134 #endif
135
136 /* prototype of GNU functions  */
137 extern int asprintf  (char **ptr, const char *fmt, /*args*/ ...);
138 extern int vasprintf (char **ptr, const char *fmt, va_list ap);
139 extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
140 extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
141
142 /*
143  * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
144  * (without adding an extra check on working platforms :)
145  */
146
147 #ifdef PRINTF_NULL_WORKING
148 #  define PRINTF_STR(a) (a)
149 #else
150 #  define PRINTF_STR(a) (a)?:"(null)"
151 #endif
152
153 /*
154  * What we need to extract the backtrace in exception handling code
155  */
156 #ifdef HAVE_EXECINFO_H
157 #  include <execinfo.h>
158 #endif
159
160 /****
161  **** Some debugging functions. Can't we find a better place for this??
162  ****/
163 void hexa_print(const char*name, unsigned char *data, int size);
164 const char *hexa_str(unsigned char *data, int size, int downside);
165
166 /* Windows __declspec(). */
167 #if defined (_XBT_USE_DECLSPEC) /* using export/import technique */
168
169 #    ifndef _XBT_EXPORT_DECLSPEC
170 #        define _XBT_EXPORT_DECLSPEC
171 #    endif
172
173 #    ifndef _XBT_IMPORT_DECLSPEC
174 #        define _XBT_IMPORT_DECLSPEC
175 #    endif
176
177 #    if defined (_XBT_DESIGNATED_DLL) /* this is a lib which will contain xbt exports */
178 #        define  _XBT_DECLSPEC        _XBT_EXPORT_DECLSPEC 
179 #    else
180 #        define  _XBT_DECLSPEC        _XBT_IMPORT_DECLSPEC   /* other modules, importing xbt exports */
181 #    endif
182
183 #else /* not using DLL export/import specifications */
184
185 #    define _XBT_DECLSPEC
186
187 #endif /* #if defined (_XBT_USE_DECLSPEC) */
188
189 #if !defined (_XBT_CALL)
190 #define _XBT_CALL
191 #endif
192
193 #endif /* GRAS_PORTABLE_H */