aboutsummaryrefslogtreecommitdiff
path: root/src/core/defs.h
blob: 32d6782ab638bd9fceeb032ecc837265ca12a961 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitoar.com>
//
// 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);

#ifndef NDEBUG
#define NNI_ASSERT(x) \
	if (!(x))     \
	nni_panic("%s: %d: assert err: %s", __FILE__, __LINE__, #x)
#else
#define NNI_ASSERT(x)
#endif

// Returns the size of an array in elements. (Convenience.)
#define NNI_NUM_ELEMENTS(x) ((unsigned) (sizeof(x) / sizeof((x)[0])))

// These types are common but have names shared with user space.
// Internal code should use these names when possible.
typedef nng_msg           nni_msg;
typedef nng_sockaddr      nni_sockaddr;
typedef nng_url           nni_url;
typedef nng_iov           nni_iov;
typedef nng_aio           nni_aio;
typedef struct nng_event  nni_event;
typedef struct nng_notify nni_notify;

// These are our own names.
typedef struct nni_socket   nni_sock;
typedef struct nni_ctx      nni_ctx;
typedef struct nni_dialer   nni_dialer;
typedef struct nni_listener nni_listener;
typedef struct nni_pipe     nni_pipe;

typedef struct nni_tran              nni_tran;
typedef struct nni_tran_dialer_ops   nni_tran_dialer_ops;
typedef struct nni_tran_listener_ops nni_tran_listener_ops;
typedef struct nni_tran_pipe_ops     nni_tran_pipe_ops;

typedef struct nni_proto_ctx_ops  nni_proto_ctx_ops;
typedef struct nni_proto_sock_ops nni_proto_sock_ops;
typedef struct nni_proto_pipe_ops nni_proto_pipe_ops;
typedef struct nni_proto          nni_proto;

typedef struct nni_plat_mtx nni_mtx;
typedef struct nni_plat_cv  nni_cv;
typedef struct nni_thr      nni_thr;
typedef void (*nni_thr_func)(void *);

typedef int      nni_signal;   // Wakeup channel.
typedef uint64_t nni_time;     // Abs. time (ms).
typedef int32_t  nni_duration; // Rel. time (ms).

typedef void (*nni_cb)(void *);

// Notify descriptor.
typedef struct {
	int sn_wfd; // written to in order to flag an event
	int sn_rfd; // read from in order to clear an event
	int sn_init;
} nni_notifyfd;

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

// Structure allocation conveniences.
#define NNI_ALLOC_STRUCT(s) nni_zalloc(sizeof(*s))
#define NNI_FREE_STRUCT(s) nni_free((s), sizeof(*s))
#define NNI_ALLOC_STRUCTS(s, n) nni_zalloc(sizeof(*s) * n)
#define NNI_FREE_STRUCTS(s, n) nni_free(s, sizeof(*s) * n)

#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 = (((uint16_t)((uint8_t)(ptr)[0])) << 8) + \
	    (((uint16_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]))

// This increments a pointer a fixed number of byte cells.
#define NNI_INCPTR(ptr, n) ((ptr) = (void *) ((char *) (ptr) + (n)))

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

// Types.  These are used to provide more structured access to options
// (and maybe later statistics).  For now these are internal only.
typedef enum {
	NNI_TYPE_OPAQUE,
	NNI_TYPE_BOOL,
	NNI_TYPE_INT32,
	NNI_TYPE_UINT32,
	NNI_TYPE_INT64,
	NNI_TYPE_UINT64,
	NNI_TYPE_SIZE,
	NNI_TYPE_DURATION,
	NNI_TYPE_STRING,
	NNI_TYPE_SOCKADDR,
	NNI_TYPE_POINTER,
} nni_type;

typedef nni_type nni_opt_type;

#endif // CORE_DEFS_H