2000-01-11 21:08:13 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-01-11 21:08:13 +00:00
|
|
|
*/
|
2000-06-09 20:59:32 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <config.h>
|
2000-01-11 21:08:13 +00:00
|
|
|
|
2000-08-24 23:26:13 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2000-01-11 21:08:13 +00:00
|
|
|
#include <isc/quota.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_quota_init(isc_quota_t *quota, int max) {
|
|
|
|
quota->max = max;
|
|
|
|
quota->used = 0;
|
2005-07-27 02:29:01 +00:00
|
|
|
quota->soft = 0;
|
2000-01-11 21:08:13 +00:00
|
|
|
return (isc_mutex_init("a->lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_quota_destroy(isc_quota_t *quota) {
|
2000-02-10 22:17:54 +00:00
|
|
|
INSIST(quota->used == 0);
|
2005-07-27 02:29:01 +00:00
|
|
|
quota->max = 0;
|
|
|
|
quota->used = 0;
|
|
|
|
quota->soft = 0;
|
2000-08-26 01:23:17 +00:00
|
|
|
DESTROYLOCK("a->lock);
|
2000-01-11 21:08:13 +00:00
|
|
|
}
|
|
|
|
|
2001-10-22 07:09:25 +00:00
|
|
|
void
|
2005-07-27 02:29:01 +00:00
|
|
|
isc_quota_soft(isc_quota_t *quota, int soft) {
|
|
|
|
LOCK("a->lock);
|
2001-10-22 07:09:25 +00:00
|
|
|
quota->soft = soft;
|
2005-07-27 02:29:01 +00:00
|
|
|
UNLOCK("a->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_quota_max(isc_quota_t *quota, int max) {
|
|
|
|
LOCK("a->lock);
|
|
|
|
quota->max = max;
|
|
|
|
UNLOCK("a->lock);
|
2001-10-22 07:09:25 +00:00
|
|
|
}
|
|
|
|
|
2000-01-11 21:08:13 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_quota_reserve(isc_quota_t *quota) {
|
|
|
|
isc_result_t result;
|
|
|
|
LOCK("a->lock);
|
2005-07-27 02:29:01 +00:00
|
|
|
if (quota->max == 0 || quota->used < quota->max) {
|
|
|
|
if (quota->soft == 0 || quota->used < quota->soft)
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
else
|
2001-10-22 07:09:25 +00:00
|
|
|
result = ISC_R_SOFTQUOTA;
|
2005-07-27 02:29:01 +00:00
|
|
|
quota->used++;
|
|
|
|
} else
|
|
|
|
result = ISC_R_QUOTA;
|
2000-01-11 21:08:13 +00:00
|
|
|
UNLOCK("a->lock);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_quota_release(isc_quota_t *quota) {
|
|
|
|
LOCK("a->lock);
|
|
|
|
INSIST(quota->used > 0);
|
|
|
|
quota->used--;
|
2000-08-01 01:33:37 +00:00
|
|
|
UNLOCK("a->lock);
|
2000-01-11 21:08:13 +00:00
|
|
|
}
|
|
|
|
|
2000-01-15 00:32:42 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_quota_attach(isc_quota_t *quota, isc_quota_t **p)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
INSIST(p != NULL && *p == NULL);
|
|
|
|
result = isc_quota_reserve(quota);
|
2001-10-22 07:09:25 +00:00
|
|
|
if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA)
|
|
|
|
*p = quota;
|
|
|
|
return (result);
|
2000-01-15 00:32:42 +00:00
|
|
|
}
|
2000-01-11 21:08:13 +00:00
|
|
|
|
2000-01-15 00:32:42 +00:00
|
|
|
void
|
|
|
|
isc_quota_detach(isc_quota_t **p)
|
|
|
|
{
|
|
|
|
INSIST(p != NULL && *p != NULL);
|
|
|
|
isc_quota_release(*p);
|
|
|
|
*p = NULL;
|
|
|
|
}
|