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