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