1135446Strhodes/*
2193149Sdougb * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 1998-2001  Internet Software Consortium.
4135446Strhodes *
5193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18234010Sdougb/* $Id: event.c,v 1.21 2007/06/19 23:47:17 tbox Exp $ */
19135446Strhodes
20170222Sdougb/*!
21170222Sdougb * \file
22170222Sdougb * \author Principal Author: Bob Halley
23135446Strhodes */
24135446Strhodes
25135446Strhodes#include <config.h>
26135446Strhodes
27135446Strhodes#include <isc/event.h>
28135446Strhodes#include <isc/mem.h>
29135446Strhodes#include <isc/util.h>
30135446Strhodes
31135446Strhodes/***
32135446Strhodes *** Events.
33135446Strhodes ***/
34135446Strhodes
35135446Strhodesstatic void
36135446Strhodesdestroy(isc_event_t *event) {
37135446Strhodes	isc_mem_t *mctx = event->ev_destroy_arg;
38135446Strhodes
39135446Strhodes	isc_mem_put(mctx, event, event->ev_size);
40135446Strhodes}
41135446Strhodes
42135446Strhodesisc_event_t *
43135446Strhodesisc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
44135446Strhodes		   isc_taskaction_t action, const void *arg, size_t size)
45135446Strhodes{
46135446Strhodes	isc_event_t *event;
47135446Strhodes	void *deconst_arg;
48135446Strhodes
49135446Strhodes	REQUIRE(size >= sizeof(struct isc_event));
50135446Strhodes	REQUIRE(action != NULL);
51135446Strhodes
52135446Strhodes	event = isc_mem_get(mctx, size);
53135446Strhodes	if (event == NULL)
54135446Strhodes		return (NULL);
55135446Strhodes
56135446Strhodes	/*
57135446Strhodes	 * Removing the const attribute from "arg" is the best of two
58135446Strhodes	 * evils here.  If the event->ev_arg member is made const, then
59135446Strhodes	 * it affects a great many users of the task/event subsystem
60135446Strhodes	 * which are not passing in an "arg" which starts its life as
61135446Strhodes	 * const.  Changing isc_event_allocate() and isc_task_onshutdown()
62135446Strhodes	 * to not have "arg" prototyped as const (which is quite legitimate,
63135446Strhodes	 * because neither of those functions modify arg) can cause
64135446Strhodes	 * compiler whining anytime someone does want to use a const
65135446Strhodes	 * arg that they themselves never modify, such as with
66135446Strhodes	 * gcc -Wwrite-strings and using a string "arg".
67135446Strhodes	 */
68135446Strhodes	DE_CONST(arg, deconst_arg);
69135446Strhodes
70135446Strhodes	ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
71135446Strhodes		       sender, destroy, mctx);
72135446Strhodes
73135446Strhodes	return (event);
74135446Strhodes}
75135446Strhodes
76135446Strhodesvoid
77135446Strhodesisc_event_free(isc_event_t **eventp) {
78135446Strhodes	isc_event_t *event;
79135446Strhodes
80135446Strhodes	REQUIRE(eventp != NULL);
81135446Strhodes	event = *eventp;
82135446Strhodes	REQUIRE(event != NULL);
83135446Strhodes
84135446Strhodes	if (event->ev_destroy != NULL)
85135446Strhodes		(event->ev_destroy)(event);
86135446Strhodes
87135446Strhodes	*eventp = NULL;
88135446Strhodes}
89