Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add context ucontext for windows.
[simgrid.git] / src / portable.h
1 /* portable -- header loading to write portable code                         */
2 /* loads much more stuff than sysdep.h since the latter is in public interface*/
3
4 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
5  * All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8   * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef GRAS_PORTABLE_H
11 #define GRAS_PORTABLE_H
12
13 #include "simgrid_config.h"
14 #include "xbt/misc.h"
15 /* 
16  * win32 or win64 (__WIN32 is defined for win32 and win64 applications, __TOS_WIN__ is defined by xlC). 
17 */
18 #ifdef _WIN32
19 # include "win32/config.h"
20 # include <windows.h>
21 #else
22 #  include "gras_config.h"
23 #endif
24
25 #include <stdarg.h>
26
27 #ifdef HAVE_ERRNO_H
28 #  include <errno.h>
29 #endif
30
31 #ifdef HAVE_UNISTD_H
32 #  include <unistd.h>
33 #endif
34
35 /****
36  **** Networking 
37  ****/
38
39
40 #ifdef HAVE_SYS_SOCKET_H
41 #  include <sys/socket.h>
42 #  include <netinet/in.h>       /* sometimes required for #include <arpa/inet.h> */
43 #  include <netinet/tcp.h>      /* TCP_NODELAY */
44 #  include <netdb.h>            /* getprotobyname() */
45 #  include <arpa/inet.h>        /* inet_ntoa() */
46 #  include <sys/types.h>        /* sometimes required for fd_set */
47 # endif
48
49
50 #ifndef HAVE_WINSOCK_H
51 #       define tcp_read( s, buf, len)   read( s, buf, len )
52 #       define tcp_write( s, buf, len)  write( s, buf, len )
53 #       define sock_errno        errno
54 #       define sock_errstr(err)  strerror(err)
55
56 #       ifdef SHUT_RDWR
57 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
58 #       else
59 #               define tcp_close( s )   close( s )
60 #       endif
61 #endif /* windows or unix ? */
62
63 /****
64  **** File handling
65  ****/
66
67 #include <fcntl.h>
68
69 #ifdef HAVE_SYS_STAT_H
70 #  include <sys/stat.h>
71 #endif
72
73 #ifndef O_BINARY
74 #  define O_BINARY 0
75 #endif
76
77 /****
78  **** Time handling
79  ****/
80
81 #ifdef TIME_WITH_SYS_TIME
82 # include <sys/time.h>
83 # include <time.h>
84 #else
85 # if HAVE_SYS_TIME_H
86 #  include <sys/time.h>
87 # else
88 #  include <time.h>
89 # endif
90 #endif
91
92 /****
93  **** Signals
94  ****/
95 #ifdef HAVE_SIGNAL_H
96 # include <signal.h>
97 #endif
98
99 /****
100  **** string handling (parts from http://www.ijs.si/software/snprintf/)
101  ****/
102
103 /* prototype of C99 functions */
104 #if defined(HAVE_SNPRINTF)
105 #include <stdio.h>
106 #else
107 XBT_PUBLIC(int) snprintf(char *, size_t, const char *, /*args */ ...);
108 XBT_PUBLIC(int) vsnprintf(char *, size_t, const char *, va_list);
109 #endif
110
111
112 /* use internal functions when OS provided ones are borken */
113 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
114      extern int portable_snprintf(char *str, size_t str_m, const char *fmt,
115                                   /*args */ ...);
116      extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt,
117                                    va_list ap);
118 #define snprintf  portable_snprintf
119 #define vsnprintf portable_vsnprintf
120 #endif
121
122 /* prototype of GNU functions  */
123 #if (defined(__GNUC__) && !defined(__cplusplus))
124      extern int asprintf(char **ptr, const char *fmt, /*args */ ...);
125      extern int vasprintf(char **ptr, const char *fmt, va_list ap);
126 #endif
127
128      extern int asnprintf(char **ptr, size_t str_m, const char *fmt,    /*args */
129                           ...);
130      extern int vasnprintf(char **ptr, size_t str_m, const char *fmt,
131                            va_list ap);
132
133 /*
134  * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
135  * (without adding an extra check on working platforms :)
136  */
137
138 #ifdef PRINTF_NULL_WORKING
139 #  define PRINTF_STR(a) (a)
140 #else
141 #  define PRINTF_STR(a) (a)?:"(null)"
142 #endif
143
144 /*
145  * What we need to extract the backtrace in exception handling code
146  */
147 #ifdef HAVE_EXECINFO_H
148 #  include <execinfo.h>
149 #endif
150
151 /****
152  **** Some debugging functions. Can't we find a better place for this??
153  ****/
154      void hexa_print(const char *name, unsigned char *data, int size);
155      const char *hexa_str(unsigned char *data, int size, int downside);
156
157 #endif /* GRAS_PORTABLE_H */