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