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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*/
#include <config.h>
#include <parc/assert/parc_Assert.h>
#include <parc/algol/parc_Memory.h>
#include "internal_parc_Event.h"
/**
* Current implementation based on top of libevent2
*/
#include <event2/event.h>
#include <event2/bufferevent.h>
void *
internal_parc_alloc(size_t size)
{
void *pointer;
pointer = parcMemory_AllocateAndClear(size);
return pointer;
}
void *
internal_parc_realloc(void *pointer, size_t newSize)
{
void *newPointer;
newPointer = parcMemory_Reallocate(pointer, newSize);
return newPointer;
}
// We interpose on the standard "free" interface to protect against freed memory accesses.
void
internal_parc_free(void *ptr)
{
parcMemory_Deallocate((void **) &ptr);
}
static int _libeventInitialized = 0;
void
internal_parc_initializeLibevent(void)
{
if (_libeventInitialized) {
return;
}
_libeventInitialized = 1;
// 0x AA BB CC XX
// AA = major
// BB = minor
// CC = patchlevel
//
uint32_t version = event_get_version_number();
parcTrapIllegalValueIf(version < 0x02001000UL,
"Libevent version must be at least 2.0.16, got %s",
event_get_version());
// Make sure libevent uses our memory allocator.
// Libevent allocates an internal object the first time a base is allocated
// that it never releases. In order to ensure our outstanding memory counters
// start at zero we trigger this allocation before interposing our memory allocator.
//
// Create a scheduler event base, an event, then free both of them.
//
struct event_base *evbase = event_base_new();
parcAssertNotNull(evbase, "Libevent event_base_new returned NULL");
struct event *event = event_new(evbase, -1, 0, NULL, NULL);
parcAssertNotNull(event, "Libevent event_new returned NULL");
event_del(event);
event_base_free(evbase);
event_free(event);
event_set_mem_functions(internal_parc_alloc,
internal_parc_realloc,
internal_parc_free);
}
PARCEventPriority
internal_libevent_priority_to_PARCEventPriority(short evpriority)
{
PARCEventPriority priority = 0;
switch (evpriority) {
case 0: priority = PARCEventPriority_Maximum;
break;
case 1: priority = PARCEventPriority_Normal;
break;
case 2: priority = PARCEventPriority_Minimum;
break;
default:
parcAssertTrue(0, "Unknown Libevent priority 0x%x\n", evpriority);
break;
}
return priority;
}
short
internal_PARCEventPriority_to_libevent_priority(PARCEventPriority priority)
{
short evpriority = 0;
switch (priority) {
case PARCEventPriority_Maximum: evpriority = 0;
break;
case PARCEventPriority_Normal: evpriority = 1;
break;
case PARCEventPriority_Minimum: evpriority = 2;
break;
default:
parcAssertTrue(0, "Unknown PARCEventPriority 0x%x\n", evpriority);
break;
}
return evpriority;
}
PARCEventSchedulerDispatchType
internal_eventloop_options_to_PARCEventSchedulerDispatchType(short evoptions)
{
PARCEventSchedulerDispatchType options = 0;
switch (evoptions) {
case 0: options = PARCEventSchedulerDispatchType_Blocking;
break;
case EVLOOP_ONCE: options = PARCEventSchedulerDispatchType_LoopOnce;
break;
case EVLOOP_NONBLOCK: options = PARCEventSchedulerDispatchType_NonBlocking;
break;
default:
parcAssertTrue(0, "Unknown Libevent dispatcher flag 0x%x\n", evoptions);
break;
}
return options;
}
short
internal_PARCEventSchedulerDispatchType_to_eventloop_options(PARCEventSchedulerDispatchType options)
{
short evoptions = 0;
switch (options) {
case PARCEventSchedulerDispatchType_Blocking: evoptions = 0;
break;
case PARCEventSchedulerDispatchType_LoopOnce: evoptions = EVLOOP_ONCE;
break;
case PARCEventSchedulerDispatchType_NonBlocking: evoptions = EVLOOP_NONBLOCK;
break;
default:
parcAssertTrue(0, "Unknown PARCEventSchedulerDispatchType option 0x%x\n", evoptions);
break;
}
return evoptions;
}
PARCEventQueueOption
internal_bufferevent_options_to_PARCEventQueueOption(short evflags)
{
PARCEventQueueOption flags = 0;
if (evflags & BEV_OPT_CLOSE_ON_FREE) {
flags |= PARCEventQueueOption_CloseOnFree;
evflags &= ~BEV_OPT_CLOSE_ON_FREE;
}
if (evflags & BEV_OPT_DEFER_CALLBACKS) {
flags |= PARCEventQueueOption_DeferCallbacks;
evflags &= ~BEV_OPT_DEFER_CALLBACKS;
}
parcAssertTrue(evflags == 0, "Unknown Libevent option flag 0x%x\n", evflags);
return flags;
}
short
internal_PARCEventQueueOption_to_bufferevent_options(PARCEventQueueOption flags)
{
short evflags = 0;
if (flags & PARCEventQueueOption_CloseOnFree) {
evflags |= BEV_OPT_CLOSE_ON_FREE;
flags &= ~PARCEventQueueOption_CloseOnFree;
}
if (flags & PARCEventQueueOption_DeferCallbacks) {
evflags |= BEV_OPT_DEFER_CALLBACKS;
flags &= ~PARCEventQueueOption_DeferCallbacks;
}
parcAssertTrue(flags == 0, "Unknown PARCEventQueueOption flag 0x%x\n", flags);
return evflags;
}
PARCEventQueueEventType
internal_bufferevent_type_to_PARCEventQueueEventType(short evtypes)
{
PARCEventQueueEventType types = 0;
if (evtypes & BEV_EVENT_READING) {
types |= PARCEventQueueEventType_Reading;
evtypes &= ~BEV_EVENT_READING;
}
if (evtypes & BEV_EVENT_WRITING) {
types |= PARCEventQueueEventType_Writing;
evtypes &= ~BEV_EVENT_WRITING;
}
if (evtypes & BEV_EVENT_EOF) {
types |= PARCEventQueueEventType_EOF;
evtypes &= ~BEV_EVENT_EOF;
}
if (evtypes & BEV_EVENT_ERROR) {
types |= PARCEventQueueEventType_Error;
evtypes &= ~BEV_EVENT_ERROR;
}
if (evtypes & BEV_EVENT_TIMEOUT) {
types |= PARCEventQueueEventType_Timeout;
evtypes &= ~BEV_EVENT_TIMEOUT;
}
if (evtypes & BEV_EVENT_CONNECTED) {
types |= PARCEventQueueEventType_Connected;
evtypes &= ~BEV_EVENT_CONNECTED;
}
parcAssertTrue(evtypes == 0, "Unknown Libevent type flag 0x%x\n", evtypes);
return types;
}
short
internal_PARCEventQueueEventType_to_bufferevent_type(PARCEventQueueEventType types)
{
short evtypes = 0;
if (types & PARCEventQueueEventType_Reading) {
evtypes |= BEV_EVENT_READING;
types &= ~PARCEventQueueEventType_Reading;
}
if (types & PARCEventQueueEventType_Writing) {
evtypes |= BEV_EVENT_WRITING;
types &= ~PARCEventQueueEventType_Writing;
}
if (types & PARCEventQueueEventType_EOF) {
evtypes |= BEV_EVENT_EOF;
types &= ~PARCEventQueueEventType_EOF;
}
if (types & PARCEventQueueEventType_Error) {
evtypes |= BEV_EVENT_ERROR;
types &= ~PARCEventQueueEventType_Error;
}
if (types & PARCEventQueueEventType_Timeout) {
evtypes |= BEV_EVENT_TIMEOUT;
types &= ~PARCEventQueueEventType_Timeout;
}
if (types & PARCEventQueueEventType_Connected) {
evtypes |= BEV_EVENT_CONNECTED;
types &= ~PARCEventQueueEventType_Connected;
}
parcAssertTrue(types == 0, "Unknown PARCEventQueueEventType 0x%x\n", types);
return evtypes;
}
PARCEventType
internal_libevent_type_to_PARCEventType(short evtypes)
{
PARCEventType types = 0;
if (evtypes & EV_TIMEOUT) {
types |= PARCEventType_Timeout;
evtypes &= ~EV_TIMEOUT;
}
if (evtypes & EV_READ) {
types |= PARCEventType_Read;
evtypes &= ~EV_READ;
}
if (evtypes & EV_WRITE) {
types |= PARCEventType_Write;
evtypes &= ~EV_WRITE;
}
if (evtypes & EV_SIGNAL) {
types |= PARCEventType_Signal;
evtypes &= ~EV_SIGNAL;
}
if (evtypes & EV_PERSIST) {
types |= PARCEventType_Persist;
evtypes &= ~EV_PERSIST;
}
if (evtypes & EV_ET) {
types |= PARCEventType_EdgeTriggered;
evtypes &= ~EV_ET;
}
parcAssertTrue(evtypes == 0, "Unknown Libevent event type 0x%x\n", evtypes);
return types;
}
short
internal_PARCEventType_to_libevent_type(PARCEventType types)
{
short evtypes = 0;
if (types & PARCEventType_Timeout) {
evtypes |= EV_TIMEOUT;
types &= ~PARCEventType_Timeout;
}
if (types & PARCEventType_Read) {
evtypes |= EV_READ;
types &= ~PARCEventType_Read;
}
if (types & PARCEventType_Write) {
evtypes |= EV_WRITE;
types &= ~PARCEventType_Write;
}
if (types & PARCEventType_Signal) {
evtypes |= EV_SIGNAL;
types &= ~PARCEventType_Signal;
}
if (types & PARCEventType_Persist) {
evtypes |= EV_PERSIST;
types &= ~PARCEventType_Persist;
}
if (types & PARCEventType_EdgeTriggered) {
evtypes |= EV_ET;
types &= ~PARCEventType_EdgeTriggered;
}
parcAssertTrue(types == 0, "Unknown Libevent event type 0x%x\n", types);
return evtypes;
}
|