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