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