Merge from vendor branch FILE:
[dragonfly.git] / contrib / bind-9.3 / lib / isc / event.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.15.12.3 2004/03/08 09:04:48 marka Exp $ */
19
20 /*
21  * Principal Author: Bob Halley
22  */
23
24 #include <config.h>
25
26 #include <isc/event.h>
27 #include <isc/mem.h>
28 #include <isc/util.h>
29
30 /***
31  *** Events.
32  ***/
33
34 static void
35 destroy(isc_event_t *event) {
36         isc_mem_t *mctx = event->ev_destroy_arg;
37
38         isc_mem_put(mctx, event, event->ev_size);
39 }
40
41 isc_event_t *
42 isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
43                    isc_taskaction_t action, const void *arg, size_t size)
44 {
45         isc_event_t *event;
46         void *deconst_arg;
47
48         REQUIRE(size >= sizeof(struct isc_event));
49         REQUIRE(action != NULL);
50
51         event = isc_mem_get(mctx, size);
52         if (event == NULL)
53                 return (NULL);
54
55         /*
56          * Removing the const attribute from "arg" is the best of two
57          * evils here.  If the event->ev_arg member is made const, then
58          * it affects a great many users of the task/event subsystem
59          * which are not passing in an "arg" which starts its life as
60          * const.  Changing isc_event_allocate() and isc_task_onshutdown()
61          * to not have "arg" prototyped as const (which is quite legitimate,
62          * because neither of those functions modify arg) can cause
63          * compiler whining anytime someone does want to use a const
64          * arg that they themselves never modify, such as with
65          * gcc -Wwrite-strings and using a string "arg".
66          */
67         DE_CONST(arg, deconst_arg);
68
69         ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
70                        sender, destroy, mctx);
71
72         return (event);
73 }
74
75 void
76 isc_event_free(isc_event_t **eventp) {
77         isc_event_t *event;
78
79         REQUIRE(eventp != NULL);
80         event = *eventp;
81         REQUIRE(event != NULL);
82
83         if (event->ev_destroy != NULL)
84                 (event->ev_destroy)(event);
85
86         *eventp = NULL;
87 }