event.c revision 1.1
1/*	$NetBSD: event.c,v 1.1 2018/08/12 12:08:24 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14
15/*!
16 * \file
17 */
18
19#include <config.h>
20
21#include <isc/event.h>
22#include <isc/mem.h>
23#include <isc/util.h>
24
25/***
26 *** Events.
27 ***/
28
29static void
30destroy(isc_event_t *event) {
31	isc_mem_t *mctx = event->ev_destroy_arg;
32
33	isc_mem_put(mctx, event, event->ev_size);
34}
35
36isc_event_t *
37isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
38		   isc_taskaction_t action, void *arg, size_t size)
39{
40	isc_event_t *event;
41
42	REQUIRE(size >= sizeof(struct isc_event));
43	REQUIRE(action != NULL);
44
45	event = isc_mem_get(mctx, size);
46	if (event == NULL)
47		return (NULL);
48
49	ISC_EVENT_INIT(event, size, 0, NULL, type, action, arg,
50		       sender, destroy, mctx);
51
52	return (event);
53}
54
55isc_event_t *
56isc_event_constallocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
57			isc_taskaction_t action, const void *arg, size_t size)
58{
59	isc_event_t *event;
60	void *deconst_arg;
61
62	REQUIRE(size >= sizeof(struct isc_event));
63	REQUIRE(action != NULL);
64
65	event = isc_mem_get(mctx, size);
66	if (event == NULL)
67		return (NULL);
68
69	/*
70	 * Removing the const attribute from "arg" is the best of two
71	 * evils here.  If the event->ev_arg member is made const, then
72	 * it affects a great many users of the task/event subsystem
73	 * which are not passing in an "arg" which starts its life as
74	 * const.  Changing isc_event_allocate() and isc_task_onshutdown()
75	 * to not have "arg" prototyped as const (which is quite legitimate,
76	 * because neither of those functions modify arg) can cause
77	 * compiler whining anytime someone does want to use a const
78	 * arg that they themselves never modify, such as with
79	 * gcc -Wwrite-strings and using a string "arg".
80	 */
81	DE_CONST(arg, deconst_arg);
82
83	ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
84		       sender, destroy, mctx);
85
86	return (event);
87}
88
89void
90isc_event_free(isc_event_t **eventp) {
91	isc_event_t *event;
92
93	REQUIRE(eventp != NULL);
94	event = *eventp;
95	REQUIRE(event != NULL);
96
97	REQUIRE(!ISC_LINK_LINKED(event, ev_link));
98	REQUIRE(!ISC_LINK_LINKED(event, ev_ratelink));
99
100	if (event->ev_destroy != NULL)
101		(event->ev_destroy)(event);
102
103	*eventp = NULL;
104}
105