1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 2010-2020. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 /*
22  * Common structures for both erl_driver.h and erl_nif.h
23  */
24 
25 #ifndef __ERL_DRV_NIF_H__
26 #define __ERL_DRV_NIF_H__
27 
28 typedef struct {
29     int driver_major_version;
30     int driver_minor_version;
31     char *erts_version;
32     char *otp_release;
33     int thread_support;
34     int smp_support;
35     int async_threads;
36     int scheduler_threads;
37     int nif_major_version;
38     int nif_minor_version;
39     int dirty_scheduler_support;
40 }  ErlDrvSysInfo;
41 
42 typedef struct {
43     int suggested_stack_size;
44 } ErlDrvThreadOpts;
45 
46 
47 typedef enum {
48     ERL_DIRTY_JOB_CPU_BOUND = 1,
49     ERL_DIRTY_JOB_IO_BOUND  = 2
50 } ErlDirtyJobFlags;
51 
52 /* Values for enif_select AND mode arg for driver_select() */
53 enum ErlNifSelectFlags {
54     ERL_NIF_SELECT_READ      = (1 << 0),
55     ERL_NIF_SELECT_WRITE     = (1 << 1),
56     ERL_NIF_SELECT_STOP      = (1 << 2),
57     ERL_NIF_SELECT_CANCEL    = (1 << 3),
58     ERL_NIF_SELECT_CUSTOM_MSG= (1 << 4),
59     ERL_NIF_SELECT_ERROR     = (1 << 5)
60 };
61 
62 /*
63  * A driver monitor
64  */
65 typedef struct {
66     unsigned char data[sizeof(void *)*4];
67 } ErlDrvMonitor;
68 
69 
70 #ifdef SIZEOF_CHAR
71 #  define SIZEOF_CHAR_SAVED__ SIZEOF_CHAR
72 #  undef SIZEOF_CHAR
73 #endif
74 #ifdef SIZEOF_SHORT
75 #  define SIZEOF_SHORT_SAVED__ SIZEOF_SHORT
76 #  undef SIZEOF_SHORT
77 #endif
78 #ifdef SIZEOF_INT
79 #  define SIZEOF_INT_SAVED__ SIZEOF_INT
80 #  undef SIZEOF_INT
81 #endif
82 #ifdef SIZEOF_LONG
83 #  define SIZEOF_LONG_SAVED__ SIZEOF_LONG
84 #  undef SIZEOF_LONG
85 #endif
86 #ifdef SIZEOF_LONG_LONG
87 #  define SIZEOF_LONG_LONG_SAVED__ SIZEOF_LONG_LONG
88 #  undef SIZEOF_LONG_LONG
89 #endif
90 #include "erl_int_sizes_config.h"
91 #if defined(SIZEOF_CHAR_SAVED__) && SIZEOF_CHAR_SAVED__ != SIZEOF_CHAR
92 #  error SIZEOF_CHAR mismatch
93 #endif
94 #if defined(SIZEOF_SHORT_SAVED__) && SIZEOF_SHORT_SAVED__ != SIZEOF_SHORT
95 #  error SIZEOF_SHORT mismatch
96 #endif
97 #if defined(SIZEOF_INT_SAVED__) && SIZEOF_INT_SAVED__ != SIZEOF_INT
98 #  error SIZEOF_INT mismatch
99 #endif
100 #if defined(SIZEOF_LONG_SAVED__) && SIZEOF_LONG_SAVED__ != SIZEOF_LONG
101 #  error SIZEOF_LONG mismatch
102 #endif
103 #if defined(SIZEOF_LONG_LONG_SAVED__) && SIZEOF_LONG_LONG_SAVED__ != SIZEOF_LONG_LONG
104 #  error SIZEOF_LONG_LONG mismatch
105 #endif
106 
107 #if !defined(__GNUC__) && (defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_))
108 typedef unsigned __int64 ErlNapiUInt64;
109 typedef signed __int64 ErlNapiSInt64;
110 #define ERL_NAPI_SINT64_MAX__ 9223372036854775807i64
111 #define ERL_NAPI_SINT64_MIN__ (-ERL_NAPI_SINT64_MAX__ - 1i64)
112 #elif SIZEOF_LONG == 8
113 typedef unsigned long ErlNapiUInt64;
114 typedef signed long ErlNapiSInt64;
115 #define ERL_NAPI_SINT64_MAX__ 9223372036854775807L
116 #define ERL_NAPI_SINT64_MIN__ (-ERL_NAPI_SINT64_MAX__ - 1L)
117 #elif SIZEOF_LONG_LONG == 8
118 typedef unsigned long long ErlNapiUInt64;
119 typedef signed long long ErlNapiSInt64;
120 #define ERL_NAPI_SINT64_MAX__ 9223372036854775807LL
121 #define ERL_NAPI_SINT64_MIN__ (-ERL_NAPI_SINT64_MAX__ - 1LL)
122 #else
123 #  error No 64-bit integer type
124 #endif
125 
126 #if SIZEOF_VOID_P == 8
127 typedef ErlNapiUInt64 ErlNapiUInt;
128 typedef ErlNapiSInt64 ErlNapiSInt;
129 #elif SIZEOF_VOID_P == 4
130 #  if SIZEOF_LONG == SIZEOF_VOID_P
131 typedef unsigned long ErlNapiUInt;
132 typedef signed long ErlNapiSInt;
133 #  elif SIZEOF_INT == SIZEOF_VOID_P
134 typedef unsigned int ErlNapiUInt;
135 typedef signed int ErlNapiSInt;
136 #  else
137 #    error No 32-bit integer type
138 #  endif
139 #else
140 #  error Not support arch
141 #endif
142 
143 #define ERTS_NAPI_TIME_ERROR__ ERL_NAPI_SINT64_MIN__
144 
145 #define ERTS_NAPI_SEC__		0
146 #define ERTS_NAPI_MSEC__	1
147 #define ERTS_NAPI_USEC__	2
148 #define ERTS_NAPI_NSEC__	3
149 
150 #if (defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_))
151 /*
152  * This structure can be cast to a WSABUF structure.
153  */
154 typedef struct _SysIOVec {
155     unsigned long iov_len;
156     char* iov_base;
157 } SysIOVec;
158 #else  /* Unix */
159 #  include <sys/types.h>
160 #  ifdef HAVE_SYS_UIO_H
161 #    include <sys/uio.h>
162 typedef struct iovec SysIOVec;
163 #  else
164 typedef struct {
165     char* iov_base;
166     size_t iov_len;
167 } SysIOVec;
168 #  endif
169 #endif
170 
171 #endif  /* __ERL_DRV_NIF_H__ */
172