Merge branch 'pu'
[jungerl.git] / lib / gd1_drv / c_src / erl_driver_tk.h
blob011d6e100fab9464d6c16e791b296a70558ed545
1 /*
2 ** Copyright (c) 2003, Scott Lystig Fritchie. All rights reserved.
3 ** See the file "../LICENSE" for license details.
4 */
6 #include <erl_driver.h>
8 /*
9 ** ErlIOVec manipulation functions, stolen from efile_drv.c
12 /* char *EV_CHAR_P(ErlIOVec *ev, int p, int q) */
13 #define EV_CHAR_P(ev, p, q) \
14 (((char *)(ev)->iov[(q)].iov_base) + (p))
16 /* char *EV_UCHAR_P(ErlIOVec *ev, int p, int q) */
17 #define EV_UCHAR_P(ev, p, q) \
18 (((unsigned char *)(ev)->iov[(q)].iov_base) + (p))
20 /* int EV_GET_CHAR(ErlIOVec *ev, char *p, int *pp, int *qp) */
21 #define EV_GET_CHAR(ev, p, pp, qp) \
22 (*(pp)+1 <= (ev)->iov[*(qp)].iov_len \
23 ? (*(p) = *EV_CHAR_P(ev, *(pp), *(qp)), \
24 *(pp) = (*(pp)+1 < (ev)->iov[*(qp)].iov_len \
25 ? *(pp)+1 \
26 : ((*(qp))++, 0)), \
27 !0) \
28 : 0)
30 #define EV_GET_UINT8(ev, p, pp, qp) EV_GET_CHAR(ev, p, pp, qp)
32 /* int EV_GET_UINT16(ErlIOVec *ev, Uint32 *p, int *pp, int *qp) */
33 #define EV_GET_UINT16(ev, p, pp, qp) \
34 (*(pp)+2 <= (ev)->iov[*(qp)].iov_len \
35 ? (*((unsigned short *) p) = (*EV_UCHAR_P(ev, *(pp), *(qp)) << 8)\
36 | *EV_UCHAR_P(ev, *(pp)+1, *(qp)), \
37 *(pp) = (*(pp)+2 < (ev)->iov[*(qp)].iov_len \
38 ? *(pp)+2 \
39 : ((*(qp))++, 0)), \
40 !0) \
41 : 0)
43 /* int EV_GET_UINT32(ErlIOVec *ev, Uint32 *p, int *pp, int *qp) */
44 #define EV_GET_UINT32(ev, p, pp, qp) \
45 (*(pp)+4 <= (ev)->iov[*(qp)].iov_len \
46 ? (*((unsigned long *) p) = (*EV_UCHAR_P(ev, *(pp), *(qp)) << 24) \
47 | (*EV_UCHAR_P(ev, *(pp)+1, *(qp)) << 16) \
48 | (*EV_UCHAR_P(ev, *(pp)+2, *(qp)) << 8)\
49 | *EV_UCHAR_P(ev, *(pp)+3, *(qp)), \
50 *(pp) = (*(pp)+4 < (ev)->iov[*(qp)].iov_len \
51 ? *(pp)+4 \
52 : ((*(qp))++, 0)), \
53 !0) \
54 : 0)
56 /* char * EV_GETPOS(ErlIOVec *ev, int p, int q) */
57 #define EV_GETPOS(ev, p, q) \
58 ((q) < (ev)->vsize \
59 ? ((ev)->iov[(q)].iov_base + p) \
60 : NULL)
62 /*********************************************************************
63 * Term manipulation functions, stolen from inet_drv.c
66 #define LOAD_NIL(vec, i) \
67 (((vec)[(i)] = ERL_DRV_NIL), \
68 (i+1))
70 #define LOAD_ATOM(vec, i, atom) \
71 (((vec)[(i)] = ERL_DRV_ATOM), \
72 ((vec)[(i)+1] = (atom)), \
73 (i+2))
75 #define LOAD_INT(vec, i, val) \
76 (((vec)[(i)] = ERL_DRV_INT), \
77 ((vec)[(i)+1] = (ErlDrvTermData)(val)), \
78 (i+2))
80 #define LOAD_PORT(vec, i, port) \
81 (((vec)[(i)] = ERL_DRV_PORT), \
82 ((vec)[(i)+1] = (port)), \
83 (i+2))
85 #define LOAD_PID(vec, i, pid) \
86 (((vec)[(i)] = ERL_DRV_PID), \
87 ((vec)[(i)+1] = (pid)), \
88 (i+2))
90 #define LOAD_BINARY(vec, i, bin, offs, len) \
91 (((vec)[(i)] = ERL_DRV_BINARY), \
92 ((vec)[(i)+1] = (ErlDrvTermData)(bin)), \
93 ((vec)[(i)+2] = (len)), \
94 ((vec)[(i)+3] = (offs)), \
95 (i+4))
97 #define LOAD_STRING(vec, i, str, len) \
98 (((vec)[(i)] = ERL_DRV_STRING), \
99 ((vec)[(i)+1] = (ErlDrvTermData)(str)), \
100 ((vec)[(i)+2] = (len)), \
101 (i+3))
103 #define LOAD_STRING_CONS(vec, i, str, len) \
104 (((vec)[(i)] = ERL_DRV_STRING_CONS), \
105 ((vec)[(i)+1] = (ErlDrvTermData)(str)), \
106 ((vec)[(i)+2] = (len)), \
107 (i+3))
109 #define LOAD_TUPLE(vec, i, size) \
110 (((vec)[(i)] = ERL_DRV_TUPLE), \
111 ((vec)[(i)+1] = (size)), \
112 (i+2))
114 #define LOAD_LIST(vec, i, size) \
115 (((vec)[(i)] = ERL_DRV_LIST), \
116 ((vec)[(i)+1] = (size)), \
117 (i+2))
119 #define get_int32(s) ((((unsigned char*) (s))[0] << 24) | \
120 (((unsigned char*) (s))[1] << 16) | \
121 (((unsigned char*) (s))[2] << 8) | \
122 (((unsigned char*) (s))[3]))
124 #define DEBUGIO stderr
126 #define PIPE_DRIVER_TERM_NIL 0
127 #define PIPE_DRIVER_TERM_ATOM 1
128 #define PIPE_DRIVER_TERM_PORT 2
129 #define PIPE_DRIVER_TERM_INT 3
130 #define PIPE_DRIVER_TERM_TUPLE 4
131 #define PIPE_DRIVER_TERM_BINARY 5
132 #define PIPE_DRIVER_TERM_STRING 6
133 #define PIPE_DRIVER_TERM_LIST 7
135 /* Forward declarations */
136 struct descriptor;
137 typedef struct descriptor descriptor_t;
139 extern int edtk_debug_flag; /* For verbose debugging trace messages */
141 /* Wrapper functions for driver_{alloc,realloc,free}_binary */
142 void *edtk_driver_alloc_wrapper(size_t);
143 void *edtk_driver_realloc_wrapper(void *, size_t);
144 void edtk_driver_free_wrapper(void *);
145 ErlDrvBinary *edtk_alloced_ptr2ErlDrvBinary(void *);
147 /* QQQ Where are these from? */
148 void *sys_alloc(size_t);
149 void *sys_realloc(void *, size_t);
150 void sys_free(void *);
152 int edtk_debug(char *f, ...);
153 void edtk_debug_errcall(const char *, char *);
154 void edtk_free_data(void *data);
156 int edtk_ev_forward_N(ErlIOVec *ev, int n, int *pp, int *qp, int);