quota.c 2.65 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
Evan Hunt's avatar
Evan Hunt committed
21
isc_quota_init(isc_quota_t *quota, unsigned int max) {
22 23 24
	atomic_init(&quota->max, max);
	atomic_init(&quota->used, 0);
	atomic_init(&quota->soft, 0);
25 26 27
}

void
Evan Hunt's avatar
Evan Hunt committed
28
isc_quota_destroy(isc_quota_t *quota) {
29
	INSIST(atomic_load(&quota->used) == 0);
30 31 32
	atomic_store_release(&quota->max, 0);
	atomic_store_release(&quota->used, 0);
	atomic_store_release(&quota->soft, 0);
33 34
}

35
void
Evan Hunt's avatar
Evan Hunt committed
36
isc_quota_soft(isc_quota_t *quota, unsigned int soft) {
37
	atomic_store_release(&quota->soft, soft);
38 39 40
}

void
Evan Hunt's avatar
Evan Hunt committed
41
isc_quota_max(isc_quota_t *quota, unsigned int max) {
42
	atomic_store_release(&quota->max, max);
43 44 45
}

unsigned int
Evan Hunt's avatar
Evan Hunt committed
46
isc_quota_getmax(isc_quota_t *quota) {
47
	return (atomic_load_relaxed(&quota->max));
48 49 50
}

unsigned int
Evan Hunt's avatar
Evan Hunt committed
51
isc_quota_getsoft(isc_quota_t *quota) {
52
	return (atomic_load_relaxed(&quota->soft));
53 54 55
}

unsigned int
Evan Hunt's avatar
Evan Hunt committed
56
isc_quota_getused(isc_quota_t *quota) {
57
	return (atomic_load_relaxed(&quota->used));
58 59
}

60
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
61
isc_quota_reserve(isc_quota_t *quota) {
62
	isc_result_t result;
Evan Hunt's avatar
Evan Hunt committed
63 64 65
	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);
66 67
	if (max == 0 || used < max) {
		if (soft == 0 || used < soft) {
68
			result = ISC_R_SUCCESS;
69
		} else {
70
			result = ISC_R_SOFTQUOTA;
71 72
		}
	} else {
73
		INSIST(atomic_fetch_sub_release(&quota->used, 1) > 0);
74
		result = ISC_R_QUOTA;
75
	}
76 77 78 79
	return (result);
}

void
Evan Hunt's avatar
Evan Hunt committed
80
isc_quota_release(isc_quota_t *quota) {
81
	INSIST(atomic_fetch_sub_release(&quota->used, 1) > 0);
82 83
}

84
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
85
doattach(isc_quota_t *quota, isc_quota_t **p, bool force) {
86
	isc_result_t result;
87 88
	REQUIRE(p != NULL && *p == NULL);

89
	result = isc_quota_reserve(quota);
90 91 92 93
	if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA) {
		*p = quota;
	} else if (result == ISC_R_QUOTA && force) {
		/* attach anyway */
94
		atomic_fetch_add_relaxed(&quota->used, 1);
95
		*p = quota;
96 97 98
		result = ISC_R_SUCCESS;
	}

99
	return (result);
100
}
101

102
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
103
isc_quota_attach(isc_quota_t *quota, isc_quota_t **p) {
104 105 106 107
	return (doattach(quota, p, false));
}

isc_result_t
Evan Hunt's avatar
Evan Hunt committed
108
isc_quota_force(isc_quota_t *quota, isc_quota_t **p) {
109 110 111
	return (doattach(quota, p, true));
}

112
void
Evan Hunt's avatar
Evan Hunt committed
113
isc_quota_detach(isc_quota_t **p) {
114 115 116 117
	INSIST(p != NULL && *p != NULL);
	isc_quota_release(*p);
	*p = NULL;
}