quota.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: quota.c,v 1.18 2007/06/19 23:47:17 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <stddef.h>
00025 
00026 #include <isc/quota.h>
00027 #include <isc/util.h>
00028 
00029 isc_result_t
00030 isc_quota_init(isc_quota_t *quota, int max) {
00031         quota->max = max;
00032         quota->used = 0;
00033         quota->soft = 0;
00034         return (isc_mutex_init(&quota->lock));
00035 }
00036 
00037 void
00038 isc_quota_destroy(isc_quota_t *quota) {
00039         INSIST(quota->used == 0);
00040         quota->max = 0;
00041         quota->used = 0;
00042         quota->soft = 0;
00043         DESTROYLOCK(&quota->lock);
00044 }
00045 
00046 void
00047 isc_quota_soft(isc_quota_t *quota, int soft) {
00048         LOCK(&quota->lock);
00049         quota->soft = soft;
00050         UNLOCK(&quota->lock);
00051 }
00052 
00053 void
00054 isc_quota_max(isc_quota_t *quota, int max) {
00055         LOCK(&quota->lock);
00056         quota->max = max;
00057         UNLOCK(&quota->lock);
00058 }
00059 
00060 isc_result_t
00061 isc_quota_reserve(isc_quota_t *quota) {
00062         isc_result_t result;
00063         LOCK(&quota->lock);
00064         if (quota->max == 0 || quota->used < quota->max) {
00065                 if (quota->soft == 0 || quota->used < quota->soft)
00066                         result = ISC_R_SUCCESS;
00067                 else
00068                         result = ISC_R_SOFTQUOTA;
00069                 quota->used++;
00070         } else
00071                 result = ISC_R_QUOTA;
00072         UNLOCK(&quota->lock);
00073         return (result);
00074 }
00075 
00076 void
00077 isc_quota_release(isc_quota_t *quota) {
00078         LOCK(&quota->lock);
00079         INSIST(quota->used > 0);
00080         quota->used--;
00081         UNLOCK(&quota->lock);
00082 }
00083 
00084 isc_result_t
00085 isc_quota_attach(isc_quota_t *quota, isc_quota_t **p)
00086 {
00087         isc_result_t result;
00088         INSIST(p != NULL && *p == NULL);
00089         result = isc_quota_reserve(quota);
00090         if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA)
00091                 *p = quota;
00092         return (result);
00093 }
00094 
00095 void
00096 isc_quota_detach(isc_quota_t **p)
00097 {
00098         INSIST(p != NULL && *p != NULL);
00099         isc_quota_release(*p);
00100         *p = NULL;
00101 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha