Blame lib/isc/event.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
 *
Packit Service ae04f2
 * See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
 * information regarding copyright ownership.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \file
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
#include <config.h>
Packit Service ae04f2
Packit Service ae04f2
#include <isc/event.h>
Packit Service ae04f2
#include <isc/mem.h>
Packit Service ae04f2
#include <isc/util.h>
Packit Service ae04f2
Packit Service ae04f2
/***
Packit Service ae04f2
 *** Events.
Packit Service ae04f2
 ***/
Packit Service ae04f2
Packit Service ae04f2
static void
Packit Service ae04f2
destroy(isc_event_t *event) {
Packit Service ae04f2
	isc_mem_t *mctx = event->ev_destroy_arg;
Packit Service ae04f2
Packit Service ae04f2
	isc_mem_put(mctx, event, event->ev_size);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_event_t *
Packit Service ae04f2
isc_event_allocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
Packit Service ae04f2
		   isc_taskaction_t action, void *arg, size_t size)
Packit Service ae04f2
{
Packit Service ae04f2
	isc_event_t *event;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(size >= sizeof(struct isc_event));
Packit Service ae04f2
	REQUIRE(action != NULL);
Packit Service ae04f2
Packit Service ae04f2
	event = isc_mem_get(mctx, size);
Packit Service ae04f2
	if (event == NULL)
Packit Service ae04f2
		return (NULL);
Packit Service ae04f2
Packit Service ae04f2
	ISC_EVENT_INIT(event, size, 0, NULL, type, action, arg,
Packit Service ae04f2
		       sender, destroy, mctx);
Packit Service ae04f2
Packit Service ae04f2
	return (event);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_event_t *
Packit Service ae04f2
isc_event_constallocate(isc_mem_t *mctx, void *sender, isc_eventtype_t type,
Packit Service ae04f2
			isc_taskaction_t action, const void *arg, size_t size)
Packit Service ae04f2
{
Packit Service ae04f2
	isc_event_t *event;
Packit Service ae04f2
	void *deconst_arg;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(size >= sizeof(struct isc_event));
Packit Service ae04f2
	REQUIRE(action != NULL);
Packit Service ae04f2
Packit Service ae04f2
	event = isc_mem_get(mctx, size);
Packit Service ae04f2
	if (event == NULL)
Packit Service ae04f2
		return (NULL);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Removing the const attribute from "arg" is the best of two
Packit Service ae04f2
	 * evils here.  If the event->ev_arg member is made const, then
Packit Service ae04f2
	 * it affects a great many users of the task/event subsystem
Packit Service ae04f2
	 * which are not passing in an "arg" which starts its life as
Packit Service ae04f2
	 * const.  Changing isc_event_allocate() and isc_task_onshutdown()
Packit Service ae04f2
	 * to not have "arg" prototyped as const (which is quite legitimate,
Packit Service ae04f2
	 * because neither of those functions modify arg) can cause
Packit Service ae04f2
	 * compiler whining anytime someone does want to use a const
Packit Service ae04f2
	 * arg that they themselves never modify, such as with
Packit Service ae04f2
	 * gcc -Wwrite-strings and using a string "arg".
Packit Service ae04f2
	 */
Packit Service ae04f2
	DE_CONST(arg, deconst_arg);
Packit Service ae04f2
Packit Service ae04f2
	ISC_EVENT_INIT(event, size, 0, NULL, type, action, deconst_arg,
Packit Service ae04f2
		       sender, destroy, mctx);
Packit Service ae04f2
Packit Service ae04f2
	return (event);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_event_free(isc_event_t **eventp) {
Packit Service ae04f2
	isc_event_t *event;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(eventp != NULL);
Packit Service ae04f2
	REQUIRE((*eventp) != NULL);
Packit Service ae04f2
Packit Service ae04f2
	event = *eventp;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(!ISC_LINK_LINKED(event, ev_link));
Packit Service ae04f2
	REQUIRE(!ISC_LINK_LINKED(event, ev_ratelink));
Packit Service ae04f2
Packit Service ae04f2
	if (event->ev_destroy != NULL)
Packit Service ae04f2
		(event->ev_destroy)(event);
Packit Service ae04f2
Packit Service ae04f2
	*eventp = NULL;
Packit Service ae04f2
}