xref: /freebsd/contrib/ntp/libntp/lib/isc/event.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1998-2001  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id: event.c,v 1.21 2007/06/19 23:47:17 tbox Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*!
21*a466cc55SCy Schubert  * \file
22*a466cc55SCy Schubert  * \author Principal Author: Bob Halley
23*a466cc55SCy Schubert  */
24*a466cc55SCy Schubert 
25*a466cc55SCy Schubert #include <config.h>
26*a466cc55SCy Schubert 
27*a466cc55SCy Schubert #include <isc/event.h>
28*a466cc55SCy Schubert #include <isc/mem.h>
29*a466cc55SCy Schubert #include <isc/util.h>
30*a466cc55SCy Schubert 
31*a466cc55SCy Schubert /***
32*a466cc55SCy Schubert  *** Events.
33*a466cc55SCy Schubert  ***/
34*a466cc55SCy Schubert 
35*a466cc55SCy Schubert static void
destroy(isc_event_t * event)36*a466cc55SCy Schubert destroy(isc_event_t *event) {
37*a466cc55SCy Schubert 	isc_mem_put(event->ev_destroy_arg, event, event->ev_size);
38*a466cc55SCy Schubert }
39*a466cc55SCy Schubert 
40*a466cc55SCy Schubert isc_event_t *
isc_event_allocate(isc_mem_t * mctx,void * sender,isc_eventtype_t type,isc_taskaction_t action,const void * arg,size_t size)41*a466cc55SCy Schubert isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
42*a466cc55SCy Schubert 		   isc_taskaction_t action, const void *arg, size_t size)
43*a466cc55SCy Schubert {
44*a466cc55SCy Schubert 	isc_event_t *event;
45*a466cc55SCy Schubert 	void *deconst_arg;
46*a466cc55SCy Schubert 
47*a466cc55SCy Schubert 	REQUIRE(size >= sizeof(struct isc_event));
48*a466cc55SCy Schubert 	REQUIRE(action != NULL);
49*a466cc55SCy Schubert 
50*a466cc55SCy Schubert 	event = isc_mem_get(mctx, size);
51*a466cc55SCy Schubert 	if (event == NULL)
52*a466cc55SCy Schubert 		return (NULL);
53*a466cc55SCy Schubert 
54*a466cc55SCy Schubert 	/*
55*a466cc55SCy Schubert 	 * Removing the const attribute from "arg" is the best of two
56*a466cc55SCy Schubert 	 * evils here.  If the event->ev_arg member is made const, then
57*a466cc55SCy Schubert 	 * it affects a great many users of the task/event subsystem
58*a466cc55SCy Schubert 	 * which are not passing in an "arg" which starts its life as
59*a466cc55SCy Schubert 	 * const.  Changing isc_event_allocate() and isc_task_onshutdown()
60*a466cc55SCy Schubert 	 * to not have "arg" prototyped as const (which is quite legitimate,
61*a466cc55SCy Schubert 	 * because neither of those functions modify arg) can cause
62*a466cc55SCy Schubert 	 * compiler whining anytime someone does want to use a const
63*a466cc55SCy Schubert 	 * arg that they themselves never modify, such as with
64*a466cc55SCy Schubert 	 * gcc -Wwrite-strings and using a string "arg".
65*a466cc55SCy Schubert 	 */
66*a466cc55SCy Schubert 	DE_CONST(arg, deconst_arg);
67*a466cc55SCy Schubert 
68*a466cc55SCy Schubert 	ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
69*a466cc55SCy Schubert 		       sender, destroy, mctx);
70*a466cc55SCy Schubert 
71*a466cc55SCy Schubert 	return (event);
72*a466cc55SCy Schubert }
73*a466cc55SCy Schubert 
74*a466cc55SCy Schubert void
isc_event_free(isc_event_t ** eventp)75*a466cc55SCy Schubert isc_event_free(isc_event_t **eventp) {
76*a466cc55SCy Schubert 	isc_event_t *event;
77*a466cc55SCy Schubert 
78*a466cc55SCy Schubert 	REQUIRE(eventp != NULL);
79*a466cc55SCy Schubert 	event = *eventp;
80*a466cc55SCy Schubert 	REQUIRE(event != NULL);
81*a466cc55SCy Schubert 
82*a466cc55SCy Schubert 	if (event->ev_destroy != NULL)
83*a466cc55SCy Schubert 		(event->ev_destroy)(event);
84*a466cc55SCy Schubert 
85*a466cc55SCy Schubert 	*eventp = NULL;
86*a466cc55SCy Schubert }
87