1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*!
13 * \file
14 */
15
16 #include <isc/event.h>
17 #include <isc/mem.h>
18 #include <isc/util.h>
19
20 /***
21 *** Events.
22 ***/
23
24 static void
destroy(isc_event_t * event)25 destroy(isc_event_t *event) {
26 isc_mem_t *mctx = event->ev_destroy_arg;
27
28 isc_mem_put(mctx, event, event->ev_size);
29 }
30
31 isc_event_t *
isc_event_allocate(isc_mem_t * mctx,void * sender,isc_eventtype_t type,isc_taskaction_t action,void * arg,size_t size)32 isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
33 isc_taskaction_t action, void *arg, size_t size) {
34 isc_event_t *event;
35
36 REQUIRE(size >= sizeof(struct isc_event));
37 REQUIRE(action != NULL);
38
39 event = isc_mem_get(mctx, size);
40
41 ISC_EVENT_INIT(event, size, 0, NULL, type, action, arg, sender, destroy,
42 mctx);
43
44 return (event);
45 }
46
47 isc_event_t *
isc_event_constallocate(isc_mem_t * mctx,void * sender,isc_eventtype_t type,isc_taskaction_t action,const void * arg,size_t size)48 isc_event_constallocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
49 isc_taskaction_t action, const void *arg, size_t size) {
50 isc_event_t *event;
51 void *deconst_arg;
52
53 REQUIRE(size >= sizeof(struct isc_event));
54 REQUIRE(action != NULL);
55
56 event = isc_mem_get(mctx, size);
57
58 /*
59 * Removing the const attribute from "arg" is the best of two
60 * evils here. If the event->ev_arg member is made const, then
61 * it affects a great many users of the task/event subsystem
62 * which are not passing in an "arg" which starts its life as
63 * const. Changing isc_event_allocate() and isc_task_onshutdown()
64 * to not have "arg" prototyped as const (which is quite legitimate,
65 * because neither of those functions modify arg) can cause
66 * compiler whining anytime someone does want to use a const
67 * arg that they themselves never modify, such as with
68 * gcc -Wwrite-strings and using a string "arg".
69 */
70 DE_CONST(arg, deconst_arg);
71
72 ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg, sender,
73 destroy, mctx);
74
75 return (event);
76 }
77
78 void
isc_event_free(isc_event_t ** eventp)79 isc_event_free(isc_event_t **eventp) {
80 isc_event_t *event;
81
82 REQUIRE(eventp != NULL);
83 event = *eventp;
84 *eventp = NULL;
85 REQUIRE(event != NULL);
86
87 REQUIRE(!ISC_LINK_LINKED(event, ev_link));
88 REQUIRE(!ISC_LINK_LINKED(event, ev_ratelink));
89
90 if (event->ev_destroy != NULL) {
91 (event->ev_destroy)(event);
92 }
93 }
94