xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/event.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: event.c,v 1.2 2024/08/18 20:47:14 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 1998-2001  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id: event.c,v 1.21 2007/06/19 23:47:17 tbox Exp  */
21897be3a4Schristos 
22897be3a4Schristos /*!
23897be3a4Schristos  * \file
24897be3a4Schristos  * \author Principal Author: Bob Halley
25897be3a4Schristos  */
26897be3a4Schristos 
27897be3a4Schristos #include <config.h>
28897be3a4Schristos 
29897be3a4Schristos #include <isc/event.h>
30897be3a4Schristos #include <isc/mem.h>
31897be3a4Schristos #include <isc/util.h>
32897be3a4Schristos 
33897be3a4Schristos /***
34897be3a4Schristos  *** Events.
35897be3a4Schristos  ***/
36897be3a4Schristos 
37897be3a4Schristos static void
38897be3a4Schristos destroy(isc_event_t *event) {
39897be3a4Schristos 	isc_mem_put(event->ev_destroy_arg, event, event->ev_size);
40897be3a4Schristos }
41897be3a4Schristos 
42897be3a4Schristos isc_event_t *
43897be3a4Schristos isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
44897be3a4Schristos 		   isc_taskaction_t action, const void *arg, size_t size)
45897be3a4Schristos {
46897be3a4Schristos 	isc_event_t *event;
47897be3a4Schristos 	void *deconst_arg;
48897be3a4Schristos 
49897be3a4Schristos 	REQUIRE(size >= sizeof(struct isc_event));
50897be3a4Schristos 	REQUIRE(action != NULL);
51897be3a4Schristos 
52897be3a4Schristos 	event = isc_mem_get(mctx, size);
53897be3a4Schristos 	if (event == NULL)
54897be3a4Schristos 		return (NULL);
55897be3a4Schristos 
56897be3a4Schristos 	/*
57897be3a4Schristos 	 * Removing the const attribute from "arg" is the best of two
58897be3a4Schristos 	 * evils here.  If the event->ev_arg member is made const, then
59897be3a4Schristos 	 * it affects a great many users of the task/event subsystem
60897be3a4Schristos 	 * which are not passing in an "arg" which starts its life as
61897be3a4Schristos 	 * const.  Changing isc_event_allocate() and isc_task_onshutdown()
62897be3a4Schristos 	 * to not have "arg" prototyped as const (which is quite legitimate,
63897be3a4Schristos 	 * because neither of those functions modify arg) can cause
64897be3a4Schristos 	 * compiler whining anytime someone does want to use a const
65897be3a4Schristos 	 * arg that they themselves never modify, such as with
66897be3a4Schristos 	 * gcc -Wwrite-strings and using a string "arg".
67897be3a4Schristos 	 */
68897be3a4Schristos 	DE_CONST(arg, deconst_arg);
69897be3a4Schristos 
70897be3a4Schristos 	ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
71897be3a4Schristos 		       sender, destroy, mctx);
72897be3a4Schristos 
73897be3a4Schristos 	return (event);
74897be3a4Schristos }
75897be3a4Schristos 
76897be3a4Schristos void
77897be3a4Schristos isc_event_free(isc_event_t **eventp) {
78897be3a4Schristos 	isc_event_t *event;
79897be3a4Schristos 
80897be3a4Schristos 	REQUIRE(eventp != NULL);
81897be3a4Schristos 	event = *eventp;
82897be3a4Schristos 	REQUIRE(event != NULL);
83897be3a4Schristos 
84897be3a4Schristos 	if (event->ev_destroy != NULL)
85897be3a4Schristos 		(event->ev_destroy)(event);
86897be3a4Schristos 
87897be3a4Schristos 	*eventp = NULL;
88897be3a4Schristos }
89