2 ** Copyright (c) 2003, Scott Lystig Fritchie. All rights reserved.
3 ** See the file "../LICENSE" for license details.
6 #include <erl_driver.h>
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 \
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 \
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 \
56 /* char * EV_GETPOS(ErlIOVec *ev, int p, int q) */
57 #define EV_GETPOS(ev, p, q) \
59 ? ((ev)->iov[(q)].iov_base + p) \
62 /*********************************************************************
63 * Term manipulation functions, stolen from inet_drv.c
66 #define LOAD_NIL(vec, i) \
67 (((vec)[(i)] = ERL_DRV_NIL), \
70 #define LOAD_ATOM(vec, i, atom) \
71 (((vec)[(i)] = ERL_DRV_ATOM), \
72 ((vec)[(i)+1] = (atom)), \
75 #define LOAD_INT(vec, i, val) \
76 (((vec)[(i)] = ERL_DRV_INT), \
77 ((vec)[(i)+1] = (ErlDrvTermData)(val)), \
80 #define LOAD_PORT(vec, i, port) \
81 (((vec)[(i)] = ERL_DRV_PORT), \
82 ((vec)[(i)+1] = (port)), \
85 #define LOAD_PID(vec, i, pid) \
86 (((vec)[(i)] = ERL_DRV_PID), \
87 ((vec)[(i)+1] = (pid)), \
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)), \
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)), \
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)), \
109 #define LOAD_TUPLE(vec, i, size) \
110 (((vec)[(i)] = ERL_DRV_TUPLE), \
111 ((vec)[(i)+1] = (size)), \
114 #define LOAD_LIST(vec, i, size) \
115 (((vec)[(i)] = ERL_DRV_LIST), \
116 ((vec)[(i)+1] = (size)), \
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 */
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);