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