f566805563e8cecda036e79cb749774faab78ca4
[cacao.git] / src / vmcore / system.h
1 /* src/vmcore/system.h - system (OS) functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _VMCORE_SYSTEM_H
27 #define _VMCORE_SYSTEM_H
28
29 #include "config.h"
30
31 /* NOTE: In this file we check for all system headers, because we wrap
32    all system calls into inline functions for better portability. */
33
34 #if defined(HAVE_DIRENT_H)
35 # include <dirent.h>
36 #endif
37
38 #if defined(HAVE_FCNTL_H)
39 # include <fcntl.h>
40 #endif
41
42 #if defined(WITH_JRE_LAYOUT)
43 # if defined(HAVE_LIBGEN_H)
44 #  include <libgen.h>
45 # endif
46 #endif
47
48 #if defined(HAVE_STDINT_H)
49 # include <stdint.h>
50 #endif
51
52 #if defined(HAVE_STDIO_H)
53 # include <stdio.h>
54 #endif
55
56 #if defined(HAVE_STDLIB_H)
57 # include <stdlib.h>
58 #endif
59
60 #if defined(HAVE_STRING_H)
61 # include <string.h>
62 #endif
63
64 #if defined(HAVE_UNISTD_H)
65 # include <unistd.h>
66 #endif
67
68 #if defined(HAVE_SYS_MMAN_H)
69 # include <sys/mman.h>
70 #endif
71
72 #if defined(HAVE_SYS_SOCKET_H)
73 # include <sys/socket.h>
74 #endif
75
76 #if defined(HAVE_SYS_STAT_H)
77 # include <sys/stat.h>
78 #endif
79
80 #if defined(HAVE_SYS_TYPES_H)
81 # include <sys/types.h>
82 #endif
83
84
85 /* inline functions ***********************************************************/
86
87 inline static int system_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
88 {
89 #if defined(HAVE_ACCEPT)
90         return accept(sockfd, addr, addrlen);
91 #else
92 # error accept not available
93 #endif
94 }
95
96 inline static int system_access(const char *pathname, int mode)
97 {
98 #if defined(HAVE_ACCESS)
99         return access(pathname, mode);
100 #else
101 # error access not available
102 #endif
103 }
104
105 inline static void *system_calloc(size_t nmemb, size_t size)
106 {
107 #if defined(HAVE_CALLOC)
108         return calloc(nmemb, size);
109 #else
110 # error calloc not available
111 #endif
112 }
113
114 inline static int system_close(int fd)
115 {
116 #if defined(HAVE_CLOSE)
117         return close(fd);
118 #else
119 # error close not available
120 #endif
121 }
122
123 inline static int system_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
124 {
125 #if defined(HAVE_CONNECT)
126         return connect(sockfd, serv_addr, addrlen);
127 #else
128 # error connect not available
129 #endif
130 }
131
132 #if defined(WITH_JRE_LAYOUT)
133 inline static char *system_dirname(char *path)
134 {
135 #if defined(HAVE_DIRNAME)
136         return dirname(path);
137 #else
138 # error dirname not available
139 #endif
140 }
141 #endif
142
143 inline static FILE *system_fopen(const char *path, const char *mode)
144 {
145 #if defined(HAVE_FOPEN)
146         return fopen(path, mode);
147 #else
148 # error fopen not available
149 #endif
150 }
151
152 inline static int system_fclose(FILE *fp)
153 {
154 #if defined(HAVE_FCLOSE)
155         return fclose(fp);
156 #else
157 # error fclose not available
158 #endif
159 }
160
161 inline static size_t system_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
162 {
163 #if defined(HAVE_FREAD)
164         return fread(ptr, size, nmemb, stream);
165 #else
166 # error fread not available
167 #endif
168 }
169
170 inline static void system_free(void *ptr)
171 {
172 #if defined(HAVE_FREE)
173         free(ptr);
174 #else
175 # error free not available
176 #endif
177 }
178
179 inline static int system_fsync(int fd)
180 {
181 #if defined(HAVE_FSYNC)
182         return fsync(fd);
183 #else
184 # error fsync not available
185 #endif
186 }
187
188 inline static int system_ftruncate(int fd, off_t length)
189 {
190 #if defined(HAVE_FTRUNCATE)
191         return ftruncate(fd, length);
192 #else
193 # error ftruncate not available
194 #endif
195 }
196
197 inline static int system_gethostname(char *name, size_t len)
198 {
199 #if defined(HAVE_GETHOSTNAME)
200         return gethostname(name, len);
201 #else
202 # error gethostname not available
203 #endif
204 }
205
206 inline static int system_getpagesize(void)
207 {
208 #if defined(HAVE_GETPAGESIZE)
209         return getpagesize();
210 #else
211 # error getpagesize not available
212 #endif
213 }
214
215 inline static int system_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
216 {
217 #if defined(HAVE_GETSOCKNAME)
218         return getsockname(s, name, namelen);
219 #else
220 # error getsockname not available
221 #endif
222 }
223
224 inline static int system_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
225 {
226 #if defined(HAVE_GETSOCKOPT)
227         return getsockopt(s, level, optname, optval, optlen);
228 #else
229 # error getsockopt not available
230 #endif
231 }
232
233 inline static int system_listen(int sockfd, int backlog)
234 {
235 #if defined(HAVE_LISTEN)
236         return listen(sockfd, backlog);
237 #else
238 # error listen not available
239 #endif
240 }
241
242 inline static off_t system_lseek(int fildes, off_t offset, int whence)
243 {
244 #if defined(HAVE_LSEEK)
245         return lseek(fildes, offset, whence);
246 #else
247 # error lseek not available
248 #endif
249 }
250
251 inline static void *system_malloc(size_t size)
252 {
253 #if defined(HAVE_MALLOC)
254         return malloc(size);
255 #else
256 # error malloc not available
257 #endif
258 }
259
260 inline static void *system_memcpy(void *dest, const void *src, size_t n)
261 {
262 #if defined(HAVE_MEMCPY)
263         return memcpy(dest, src, n);
264 #else
265 # error memcpy not available
266 #endif
267 }
268
269 inline static void *system_memset(void *s, int c, size_t n)
270 {
271 #if defined(HAVE_MEMSET)
272         return memset(s, c, n);
273 #else
274 # error memset not available
275 #endif
276 }
277
278 inline static int system_mprotect(void *addr, size_t len, int prot)
279 {
280 #if defined(HAVE_MPROTECT)
281         return mprotect(addr, len, prot);
282 #else
283 # error mprotect not available
284 #endif
285 }
286
287 inline static int system_open(const char *pathname, int flags, mode_t mode)
288 {
289 #if defined(HAVE_OPEN)
290         return open(pathname, flags, mode);
291 #else
292 # error open not available
293 #endif
294 }
295
296 inline static ssize_t system_read(int fd, void *buf, size_t count)
297 {
298 #if defined(HAVE_READ)
299         return read(fd, buf, count);
300 #else
301 # error read not available
302 #endif
303 }
304
305 inline static void *system_realloc(void *ptr, size_t size)
306 {
307 #if defined(HAVE_REALLOC)
308         return realloc(ptr, size);
309 #else
310 # error realloc not available
311 #endif
312 }
313
314 inline static int system_scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
315 {
316 #if defined(HAVE_SCANDIR)
317         return scandir(dir, namelist, filter, compar);
318 #else
319 # error scandir not available
320 #endif
321 }
322
323 inline static int system_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
324 {
325 #if defined(HAVE_SETSOCKOPT)
326         return setsockopt(s, level, optname, optval, optlen);
327 #else
328 # error setsockopt not available
329 #endif
330 }
331
332 inline static int system_shutdown(int s, int how)
333 {
334 #if defined(HAVE_SHUTDOWN)
335         return shutdown(s, how);
336 #else
337 # error shutdown not available
338 #endif
339 }
340
341 inline static int system_socket(int domain, int type, int protocol)
342 {
343 #if defined(HAVE_SOCKET)
344         return socket(domain, type, protocol);
345 #else
346 # error socket not available
347 #endif
348 }
349
350 inline static int system_stat(const char *path, struct stat *buf)
351 {
352 #if defined(HAVE_STAT)
353         return stat(path, buf);
354 #else
355 # error stat not available
356 #endif
357 }
358
359 inline static char *system_strdup(const char *s)
360 {
361 #if defined(HAVE_STRDUP)
362         return strdup(s);
363 #else
364 # error strdup not available
365 #endif
366 }
367
368 inline static size_t system_strlen(const char *s)
369 {
370 #if defined(HAVE_STRLEN)
371         return strlen(s);
372 #else
373 # error strlen not available
374 #endif
375 }
376
377 inline static ssize_t system_write(int fd, const void *buf, size_t count)
378 {
379 #if defined(HAVE_WRITE)
380         return write(fd, buf, count);
381 #else
382 # error write not available
383 #endif
384 }
385
386
387 /* function prototypes ********************************************************/
388
389 void *system_mmap_anonymous(void *addr, size_t len, int prot, int flags);
390 int   system_processors_online(void);
391
392 #endif /* _VMCORE_SYSTEM_H */
393
394
395 /*
396  * These are local overrides for various environment variables in Emacs.
397  * Please do not remove this and leave it at the end of the file, where
398  * Emacs will automagically detect them.
399  * ---------------------------------------------------------------------
400  * Local variables:
401  * mode: c
402  * indent-tabs-mode: t
403  * c-basic-offset: 4
404  * tab-width: 4
405  * End:
406  * vim:noexpandtab:sw=4:ts=4:
407  */