aboutsummaryrefslogtreecommitdiff
path: root/src/core/defs.h
blob: 92ebdb8645df87e25161429815c4bf806a2724ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Copyright 2016 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#ifndef CORE_DEFS_H
#define CORE_DEFS_H

#include <stdint.h>

// C compilers may get unhappy when named arguments are not used.  While
// there are things like __attribute__((unused)) which are arguably
// superior, support for such are not universal.
#define NNI_ARG_UNUSED(x)    ((void) x);

// These types are common but have names shared with user space.
typedef struct nng_socket	nni_sock;
typedef struct nng_endpoint	nni_ep;
typedef struct nng_pipe		nni_pipe;
typedef struct nng_msg		nni_msg;
typedef struct nng_sockaddr	nni_sockaddr;

// These are our own names.
typedef struct nni_tran		nni_tran;
typedef struct nni_tran_ep	nni_tran_ep;
typedef struct nni_tran_pipe	nni_tran_pipe;

typedef struct nni_proto_pipe	nni_proto_pipe;
typedef struct nni_proto	nni_proto;


typedef int			nni_signal;     // Turnstile/wakeup channel.
typedef uint64_t		nni_time;       // Absolute time (usec).
typedef int64_t			nni_duration;   // Relative time (usec).

// Used by transports for scatter gather I/O.
typedef struct {
	void *	iov_buf;
	size_t	iov_len;
} nni_iov;

// Some default timing things.
#define NNI_TIME_NEVER		((nni_time) -1)
#define NNI_TIME_ZERO		((nni_time) 0)
#define NNI_SECOND		(1000000)

// Structure allocation conveniences.
#define NNI_ALLOC_STRUCT(s)	nni_alloc(sizeof (*s))
#define NNI_FREE_STRUCT(s)	nni_free((s), sizeof (*s))

#define NNI_PUT16(ptr, u)				      \
	do {						      \
		(ptr)[0] = (uint8_t) (((uint16_t) (u)) >> 8); \
		(ptr)[1] = (uint8_t) ((uint16_t) (u));	      \
	}						      \
	while (0)

#define NNI_PUT32(ptr, u)				       \
	do {						       \
		(ptr)[0] = (uint8_t) (((uint32_t) (u)) >> 24); \
		(ptr)[1] = (uint8_t) (((uint32_t) (u)) >> 16); \
		(ptr)[2] = (uint8_t) (((uint32_t) (u)) >> 8);  \
		(ptr)[3] = (uint8_t) ((uint32_t) (u));	       \
	}						       \
	while (0)

#define NNI_PUT64(ptr, u)				       \
	do {						       \
		(ptr)[0] = (uint8_t) (((uint64_t) (u)) >> 56); \
		(ptr)[1] = (uint8_t) (((uint64_t) (u)) >> 48); \
		(ptr)[2] = (uint8_t) (((uint64_t) (u)) >> 40); \
		(ptr)[3] = (uint8_t) (((uint64_t) (u)) >> 32); \
		(ptr)[4] = (uint8_t) (((uint64_t) (u)) >> 24); \
		(ptr)[5] = (uint8_t) (((uint64_t) (u)) >> 16); \
		(ptr)[6] = (uint8_t) (((uint64_t) (u)) >> 8);  \
		(ptr)[7] = (uint8_t) ((uint64_t) (u));	       \
	}						       \
	while (0)

#define NNI_GET16(ptr, v)			       \
	v = (((uint32_t) ((uint8_t) (ptr)[0])) << 8) + \
	    (((uint32_t) (uint8_t) (ptr)[1]))

#define NNI_GET32(ptr, v)				\
	v = (((uint32_t) ((uint8_t) (ptr)[0])) << 24) +	\
	    (((uint32_t) ((uint8_t) (ptr)[1])) << 16) +	\
	    (((uint32_t) ((uint8_t) (ptr)[2])) << 8) +	\
	    (((uint32_t) (uint8_t) (ptr)[3]))

#define NNI_GET64(ptr, v)				\
	v = (((uint64_t) ((uint8_t) (ptr)[0])) << 56) +	\
	    (((uint64_t) ((uint8_t) (ptr)[1])) << 48) +	\
	    (((uint64_t) ((uint8_t) (ptr)[2])) << 40) +	\
	    (((uint64_t) ((uint8_t) (ptr)[3])) << 32) +	\
	    (((uint64_t) ((uint8_t) (ptr)[4])) << 24) +	\
	    (((uint64_t) ((uint8_t) (ptr)[5])) << 16) +	\
	    (((uint64_t) ((uint8_t) (ptr)[6])) << 8) +	\
	    (((uint64_t) (uint8_t) (ptr)[7]))

// A few assorted other items.
#define NNI_FLAG_IPV4ONLY    1

#endif  // CORE_DEFS_H