2013-09-29 00:21:18 +04:00
|
|
|
/* include/c/portable.h
|
|
|
|
**
|
|
|
|
** This file is in the public domain.
|
|
|
|
*/
|
|
|
|
/** Must be compiled on gcc with C99 support.
|
|
|
|
**/
|
|
|
|
# ifndef __GNUC__
|
|
|
|
# error "port me"
|
|
|
|
# endif
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
|
|
|
|
|
|
|
|
/** System include files.
|
|
|
|
***
|
|
|
|
*** Do not put include files that are only used in the
|
|
|
|
*** porting layer here. Include them directly in the
|
|
|
|
*** C file.
|
|
|
|
**/
|
2014-11-05 03:29:08 +03:00
|
|
|
# if defined(U3_OS_linux)
|
2014-11-29 23:36:33 +03:00
|
|
|
# include <inttypes.h>
|
2013-09-29 00:21:18 +04:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <stdarg.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <stdint.h>
|
|
|
|
# include <assert.h>
|
2014-03-14 21:47:17 +04:00
|
|
|
# include <byteswap.h>
|
2013-09-29 00:21:18 +04:00
|
|
|
# include <setjmp.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/resource.h>
|
|
|
|
# include <sys/mman.h>
|
|
|
|
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_osx)
|
2014-11-29 23:36:33 +03:00
|
|
|
# include <inttypes.h>
|
2013-09-29 00:21:18 +04:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <stdarg.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <stdint.h>
|
|
|
|
# include <assert.h>
|
|
|
|
# include <setjmp.h>
|
|
|
|
# include <signal.h>
|
2014-03-14 21:47:17 +04:00
|
|
|
# include <machine/endian.h>
|
|
|
|
# include <machine/byte_order.h>
|
2013-09-29 00:21:18 +04:00
|
|
|
# include <stdio.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/resource.h>
|
|
|
|
# include <sys/mman.h>
|
|
|
|
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_bsd)
|
2014-11-29 23:36:33 +03:00
|
|
|
# include <inttypes.h>
|
2013-10-02 18:44:22 +04:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <stdarg.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <stdint.h>
|
|
|
|
# include <assert.h>
|
|
|
|
# include <machine/endian.h>
|
|
|
|
# include <setjmp.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/resource.h>
|
|
|
|
# include <sys/mman.h>
|
|
|
|
|
|
|
|
# else
|
|
|
|
#error "port: headers"
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
# endif
|
|
|
|
|
|
|
|
/** Address space layout.
|
|
|
|
**/
|
2014-11-05 03:29:08 +03:00
|
|
|
# if defined(U3_OS_linux)
|
2014-12-03 05:06:06 +03:00
|
|
|
# define U3_OS_LoomBase 0x36000000
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB
|
|
|
|
# elif defined(U3_OS_osx)
|
2013-09-29 00:21:18 +04:00
|
|
|
# ifdef __LP64__
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBase 0x200000000
|
2013-09-29 00:21:18 +04:00
|
|
|
# else
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBase 0x4000000
|
2013-09-29 00:21:18 +04:00
|
|
|
# endif
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB
|
|
|
|
# elif defined(U3_OS_bsd)
|
2013-10-02 18:44:22 +04:00
|
|
|
# ifdef __LP64__
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBase 0x200000000
|
2013-10-02 18:44:22 +04:00
|
|
|
# else
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBase 0x4000000
|
2013-10-02 18:44:22 +04:00
|
|
|
# endif
|
2014-11-05 03:29:08 +03:00
|
|
|
# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB
|
2013-10-02 18:44:22 +04:00
|
|
|
# else
|
|
|
|
# error "port: LoomBase"
|
2013-09-29 00:21:18 +04:00
|
|
|
# endif
|
|
|
|
|
|
|
|
/** Global variable control.
|
|
|
|
***
|
|
|
|
*** To instantiate globals, #define c3_global as extern.
|
|
|
|
**/
|
|
|
|
# ifndef c3_global
|
|
|
|
# define c3_global
|
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
|
|
/** External, OS-independent library dependencies.
|
|
|
|
**/
|
|
|
|
/* The GMP (GNU arbitrary-precision arithmetic) library.
|
|
|
|
** (Tested with version 4.0.1.)
|
|
|
|
*/
|
|
|
|
# include <gmp.h>
|
|
|
|
|
|
|
|
|
|
|
|
/** Private C "extensions."
|
|
|
|
***
|
|
|
|
*** Except for these and main(), any function, macro, or structure
|
2014-11-05 03:29:08 +03:00
|
|
|
*** names must be prefixed either by u3_/U3_ (for public names),
|
2013-09-29 00:21:18 +04:00
|
|
|
*** or _ (for static and other file-local names).
|
|
|
|
**/
|
|
|
|
/* Endianness.
|
|
|
|
*/
|
|
|
|
# define c3_endian_little 0
|
|
|
|
# define c3_endian_big 1
|
|
|
|
|
2014-11-05 03:29:08 +03:00
|
|
|
# ifdef U3_OS_ENDIAN_little
|
2013-09-29 00:21:18 +04:00
|
|
|
# define c3_endian c3_endian_little
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_ENDIAN_big)
|
2013-09-29 00:21:18 +04:00
|
|
|
# define c3_endian c3_endian_big
|
|
|
|
# else
|
2014-11-05 03:29:08 +03:00
|
|
|
# error "port: U3_OS_ENDIAN"
|
2013-09-29 00:21:18 +04:00
|
|
|
# endif
|
|
|
|
|
|
|
|
/* Byte swapping.
|
|
|
|
*/
|
2014-11-05 03:29:08 +03:00
|
|
|
# if defined(U3_OS_linux) || defined(U3_OS_bsd)
|
2013-09-29 00:21:18 +04:00
|
|
|
# define c3_bswap_16(x) bswap_16(x)
|
|
|
|
# define c3_bswap_32(x) bswap_32(x)
|
|
|
|
# define c3_bswap_64(x) bswap_64(x)
|
|
|
|
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_osx)
|
2013-09-29 00:21:18 +04:00
|
|
|
# define c3_bswap_16(x) NXSwapShort(x)
|
|
|
|
# define c3_bswap_32(x) NXSwapInt(x)
|
|
|
|
# define c3_bswap_64(x) NXSwapLongLong(x)
|
2013-10-02 18:44:22 +04:00
|
|
|
# else
|
|
|
|
# error "port: byte swap"
|
2013-09-29 00:21:18 +04:00
|
|
|
# endif
|
|
|
|
|
2014-03-08 06:56:15 +04:00
|
|
|
/* Sync
|
|
|
|
*/
|
2015-06-19 06:52:38 +03:00
|
|
|
# if defined(U3_OS_linux)
|
2014-03-08 06:56:15 +04:00
|
|
|
# define c3_sync(fd) (fdatasync(fd))
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_osx)
|
2014-03-08 06:56:15 +04:00
|
|
|
# define c3_sync(fd) (fcntl(fd, F_FULLFSYNC, 0))
|
2015-06-19 06:52:38 +03:00
|
|
|
# elif defined(U3_OS_bsd)
|
|
|
|
# define c3_sync(fd) (fsync(fd))
|
2014-03-08 06:56:15 +04:00
|
|
|
# else
|
|
|
|
# error "port: sync"
|
|
|
|
# endif
|
|
|
|
|
2014-03-25 19:41:09 +04:00
|
|
|
/* Purge
|
|
|
|
*/
|
2014-11-05 03:29:08 +03:00
|
|
|
# if defined(U3_OS_linux)
|
2014-03-25 19:41:09 +04:00
|
|
|
# include <stdio_ext.h>
|
|
|
|
# define c3_fpurge __fpurge
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_bsd) || defined(U3_OS_osx)
|
2014-03-25 19:41:09 +04:00
|
|
|
# define c3_fpurge fpurge
|
|
|
|
# else
|
|
|
|
# error "port: fpurge"
|
|
|
|
# endif
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
/* Stat struct
|
|
|
|
*/
|
2014-11-05 03:29:08 +03:00
|
|
|
# if defined(U3_OS_linux)
|
2014-09-11 04:01:32 +04:00
|
|
|
# define c3_stat_mtime(dp) (u3_time_t_in_ts((dp)->st_mtime))
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_osx)
|
2014-09-11 04:01:32 +04:00
|
|
|
# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtimespec)))
|
2014-01-27 23:09:56 +04:00
|
|
|
# define lseek64 lseek
|
2014-11-05 03:29:08 +03:00
|
|
|
# elif defined(U3_OS_bsd)
|
2014-09-11 04:01:32 +04:00
|
|
|
# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtim)))
|
2014-01-30 22:37:58 +04:00
|
|
|
# define lseek64 lseek
|
2013-10-02 18:44:22 +04:00
|
|
|
# else
|
|
|
|
# error "port: timeconvert"
|
2013-09-29 00:21:18 +04:00
|
|
|
# endif
|
2015-03-19 01:48:29 +03:00
|
|
|
|
|
|
|
/* Entropy
|
|
|
|
*/
|
|
|
|
# if defined(U3_OS_bsd) && defined(__OpenBSD__)
|
2016-08-12 21:58:52 +03:00
|
|
|
# define c3_rand(rd) (getentropy((void*)rd, 64) == 0 ? \
|
2015-03-19 01:48:29 +03:00
|
|
|
(void)0 : c3_assert(!"ent"))
|
|
|
|
# else
|
|
|
|
# define c3_rand u3_sist_rand
|
|
|
|
# endif
|