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 https://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 /*!
14  * \file
15  */
16 
17 #include <config.h>
18 
19 #include <isc/event.h>
20 #include <isc/mem.h>
21 #include <isc/util.h>
22 
23 /***
24  *** Events.
25  ***/
26 
27 static void
destroy(isc_event_t * event)28 destroy(isc_event_t *event) {
29 	isc_mem_t *mctx = event->ev_destroy_arg;
30 
31 	isc_mem_put(mctx, event, event->ev_size);
32 }
33 
34 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)35 isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
36 		   isc_taskaction_t action, void *arg, size_t size)
37 {
38 	isc_event_t *event;
39 
40 	REQUIRE(size >= sizeof(struct isc_event));
41 	REQUIRE(action != NULL);
42 
43 	event = isc_mem_get(mctx, size);
44 	if (event == NULL)
45 		return (NULL);
46 
47 	ISC_EVENT_INIT(event, size, 0, NULL, type, action, arg,
48 		       sender, destroy, mctx);
49 
50 	return (event);
51 }
52 
53 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)54 isc_event_constallocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
55 			isc_taskaction_t action, const void *arg, size_t size)
56 {
57 	isc_event_t *event;
58 	void *deconst_arg;
59 
60 	REQUIRE(size >= sizeof(struct isc_event));
61 	REQUIRE(action != NULL);
62 
63 	event = isc_mem_get(mctx, size);
64 	if (event == NULL)
65 		return (NULL);
66 
67 	/*
68 	 * Removing the const attribute from "arg" is the best of two
69 	 * evils here.  If the event->ev_arg member is made const, then
70 	 * it affects a great many users of the task/event subsystem
71 	 * which are not passing in an "arg" which starts its life as
72 	 * const.  Changing isc_event_allocate() and isc_task_onshutdown()
73 	 * to not have "arg" prototyped as const (which is quite legitimate,
74 	 * because neither of those functions modify arg) can cause
75 	 * compiler whining anytime someone does want to use a const
76 	 * arg that they themselves never modify, such as with
77 	 * gcc -Wwrite-strings and using a string "arg".
78 	 */
79 	DE_CONST(arg, deconst_arg);
80 
81 	ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
82 		       sender, destroy, mctx);
83 
84 	return (event);
85 }
86 
87 void
isc_event_free(isc_event_t ** eventp)88 isc_event_free(isc_event_t **eventp) {
89 	isc_event_t *event;
90 
91 	REQUIRE(eventp != NULL);
92 	REQUIRE((*eventp) != NULL);
93 
94 	event = *eventp;
95 
96 	REQUIRE(!ISC_LINK_LINKED(event, ev_link));
97 	REQUIRE(!ISC_LINK_LINKED(event, ev_ratelink));
98 
99 	if (event->ev_destroy != NULL)
100 		(event->ev_destroy)(event);
101 
102 	*eventp = NULL;
103 }
104