blob: fe8a37795773a152a75282852c19e8efd19e8918 [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
Harald Welteec0e2172010-07-20 00:03:44 +02002 * Copyright (c) 2003, 2004, 2007 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Harald Welte92c45f32010-06-12 18:59:38 +02004 * Redistribution and modifications are permitted subject to BSD license.
5 */
6/*
7 * Miscellaneous system-dependent types.
8 */
9#ifndef _ASN_SYSTEM_H_
10#define _ASN_SYSTEM_H_
11
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
16#include <stdio.h> /* For snprintf(3) */
17#include <stdlib.h> /* For *alloc(3) */
18#include <string.h> /* For memcpy(3) */
19#include <sys/types.h> /* For size_t */
Harald Welteec0e2172010-07-20 00:03:44 +020020#include <limits.h> /* For LONG_MAX */
Harald Welte92c45f32010-06-12 18:59:38 +020021#include <stdarg.h> /* For va_start */
22#include <stddef.h> /* for offsetof and ptrdiff_t */
23
Harald Weltea37b06d2015-12-18 15:37:17 +010024extern int asn_debug;
25
Harald Welte41b85d52015-08-31 08:56:53 +020026#ifdef _WIN32
Harald Welte92c45f32010-06-12 18:59:38 +020027
28#include <malloc.h>
Harald Welte92c45f32010-06-12 18:59:38 +020029#define snprintf _snprintf
30#define vsnprintf _vsnprintf
31
Harald Welteec0e2172010-07-20 00:03:44 +020032/* To avoid linking with ws2_32.lib, here's the definition of ntohl() */
33#define sys_ntohl(l) ((((l) << 24) & 0xff000000) \
Harald Welte41b85d52015-08-31 08:56:53 +020034 | (((l) << 8) & 0xff0000) \
35 | (((l) >> 8) & 0xff00) \
36 | ((l >> 24) & 0xff))
Harald Welteec0e2172010-07-20 00:03:44 +020037
Harald Welte92c45f32010-06-12 18:59:38 +020038#ifdef _MSC_VER /* MSVS.Net */
39#ifndef __cplusplus
40#define inline __inline
41#endif
Harald Welteec0e2172010-07-20 00:03:44 +020042#ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */
Harald Welte92c45f32010-06-12 18:59:38 +020043#define ssize_t SSIZE_T
44typedef char int8_t;
45typedef short int16_t;
46typedef int int32_t;
47typedef unsigned char uint8_t;
48typedef unsigned short uint16_t;
49typedef unsigned int uint32_t;
Harald Welteec0e2172010-07-20 00:03:44 +020050#endif /* ASSUMESTDTYPES */
Harald Welte92c45f32010-06-12 18:59:38 +020051#define WIN32_LEAN_AND_MEAN
52#include <windows.h>
53#include <float.h>
54#define isnan _isnan
55#define finite _finite
56#define copysign _copysign
57#define ilogb _logb
Harald Welteec0e2172010-07-20 00:03:44 +020058#else /* !_MSC_VER */
59#include <stdint.h>
Harald Welte92c45f32010-06-12 18:59:38 +020060#endif /* _MSC_VER */
61
Harald Welte41b85d52015-08-31 08:56:53 +020062#else /* !_WIN32 */
Harald Welte92c45f32010-06-12 18:59:38 +020063
64#if defined(__vxworks)
65#include <types/vxTypes.h>
66#else /* !defined(__vxworks) */
67
68#include <inttypes.h> /* C99 specifies this file */
69/*
70 * 1. Earlier FreeBSD version didn't have <stdint.h>,
71 * but <inttypes.h> was present.
72 * 2. Sun Solaris requires <alloca.h> for alloca(3),
73 * but does not have <stdint.h>.
74 */
75#if (!defined(__FreeBSD__) || !defined(_SYS_INTTYPES_H_))
76#if defined(sun)
77#include <alloca.h> /* For alloca(3) */
78#include <ieeefp.h> /* for finite(3) */
79#elif defined(__hpux)
80#ifdef __GNUC__
81#include <alloca.h> /* For alloca(3) */
82#else /* !__GNUC__ */
83#define inline
84#endif /* __GNUC__ */
85#else
86#include <stdint.h> /* SUSv2+ and C99 specify this file, for uintXX_t */
87#endif /* defined(sun) */
88#endif
89
Harald Welteec0e2172010-07-20 00:03:44 +020090#include <netinet/in.h> /* for ntohl() */
91#define sys_ntohl(foo) ntohl(foo)
92
Harald Welte92c45f32010-06-12 18:59:38 +020093#endif /* defined(__vxworks) */
94
Harald Welte41b85d52015-08-31 08:56:53 +020095#endif /* _WIN32 */
Harald Welte92c45f32010-06-12 18:59:38 +020096
97#if __GNUC__ >= 3
98#ifndef GCC_PRINTFLIKE
99#define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
100#endif
Harald Welteec0e2172010-07-20 00:03:44 +0200101#ifndef GCC_NOTUSED
102#define GCC_NOTUSED __attribute__((unused))
103#endif
Harald Welte92c45f32010-06-12 18:59:38 +0200104#else
105#ifndef GCC_PRINTFLIKE
106#define GCC_PRINTFLIKE(fmt,var) /* nothing */
107#endif
Harald Welteec0e2172010-07-20 00:03:44 +0200108#ifndef GCC_NOTUSED
109#define GCC_NOTUSED
Harald Welte92c45f32010-06-12 18:59:38 +0200110#endif
Harald Welteec0e2172010-07-20 00:03:44 +0200111#endif
112
113/* Figure out if thread safety is requested */
114#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))
115#define ASN_THREAD_SAFE
116#endif /* Thread safety */
Harald Welte92c45f32010-06-12 18:59:38 +0200117
118#ifndef offsetof /* If not defined by <stddef.h> */
119#define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0))
120#endif /* offsetof */
121
122#ifndef MIN /* Suitable for comparing primitive types (integers) */
123#if defined(__GNUC__)
124#define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \
125 ((_a)<(_b)?(_a):(_b)); })
126#else /* !__GNUC__ */
127#define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */
128#endif /* __GNUC__ */
129#endif /* MIN */
130
131#endif /* _ASN_SYSTEM_H_ */