quota.c 2.67 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * 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/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10
 */
11

12
/*! \file */
David Lawrence's avatar
David Lawrence committed
13

Brian Wellington's avatar
Brian Wellington committed
14 15
#include <stddef.h>

16
#include <isc/atomic.h>
17 18 19
#include <isc/quota.h>
#include <isc/util.h>

20
void
21 22
isc_quota_init(isc_quota_t *quota, unsigned int max)
{
23 24 25
	atomic_init(&quota->max, max);
	atomic_init(&quota->used, 0);
	atomic_init(&quota->soft, 0);
26 27 28
}

void
29 30
isc_quota_destroy(isc_quota_t *quota)
{
31
	INSIST(atomic_load(&quota->used) == 0);
32 33 34
	atomic_store_release(&quota->max, 0);
	atomic_store_release(&quota->used, 0);
	atomic_store_release(&quota->soft, 0);
35 36
}

37
void
38 39
isc_quota_soft(isc_quota_t *quota, unsigned int soft)
{
40
	atomic_store_release(&quota->soft, soft);
41 42 43
}

void
44 45
isc_quota_max(isc_quota_t *quota, unsigned int max)
{
46
	atomic_store_release(&quota->max, max);
47 48 49
}

unsigned int
50 51
isc_quota_getmax(isc_quota_t *quota)
{
52
	return (atomic_load_relaxed(&quota->max));
53 54 55
}

unsigned int
56 57
isc_quota_getsoft(isc_quota_t *quota)
{
58
	return (atomic_load_relaxed(&quota->soft));
59 60 61
}

unsigned int
62 63
isc_quota_getused(isc_quota_t *quota)
{
64
	return (atomic_load_relaxed(&quota->used));
65 66
}

67
isc_result_t
68 69
isc_quota_reserve(isc_quota_t *quota)
{
70
	isc_result_t result;
71 72 73
	uint32_t     max = atomic_load_acquire(&quota->max);
	uint32_t     soft = atomic_load_acquire(&quota->soft);
	uint32_t     used = atomic_fetch_add_relaxed(&quota->used, 1);
74 75
	if (max == 0 || used < max) {
		if (soft == 0 || used < soft) {
76
			result = ISC_R_SUCCESS;
77
		} else {
78
			result = ISC_R_SOFTQUOTA;
79 80
		}
	} else {
81
		INSIST(atomic_fetch_sub_release(&quota->used, 1) > 0);
82
		result = ISC_R_QUOTA;
83
	}
84 85 86 87
	return (result);
}

void
88 89
isc_quota_release(isc_quota_t *quota)
{
90
	INSIST(atomic_fetch_sub_release(&quota->used, 1) > 0);
91 92
}

93
static isc_result_t
94 95
doattach(isc_quota_t *quota, isc_quota_t **p, bool force)
{
96
	isc_result_t result;
97 98
	REQUIRE(p != NULL && *p == NULL);

99
	result = isc_quota_reserve(quota);
100 101 102 103
	if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA) {
		*p = quota;
	} else if (result == ISC_R_QUOTA && force) {
		/* attach anyway */
104
		atomic_fetch_add_relaxed(&quota->used, 1);
105
		*p = quota;
106 107 108
		result = ISC_R_SUCCESS;
	}

109
	return (result);
110
}
111

112
isc_result_t
113 114
isc_quota_attach(isc_quota_t *quota, isc_quota_t **p)
{
115 116 117 118
	return (doattach(quota, p, false));
}

isc_result_t
119 120
isc_quota_force(isc_quota_t *quota, isc_quota_t **p)
{
121 122 123
	return (doattach(quota, p, true));
}

124
void
125 126
isc_quota_detach(isc_quota_t **p)
{
127 128 129 130
	INSIST(p != NULL && *p != NULL);
	isc_quota_release(*p);
	*p = NULL;
}