"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/quota.c" (7 Sep 2020, 2456 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "quota.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 /*! \file */
   14 
   15 #include <config.h>
   16 
   17 #include <stddef.h>
   18 
   19 #include <isc/quota.h>
   20 #include <isc/util.h>
   21 
   22 isc_result_t
   23 isc_quota_init(isc_quota_t *quota, int max) {
   24     quota->max = max;
   25     quota->used = 0;
   26     quota->soft = 0;
   27     return (isc_mutex_init(&quota->lock));
   28 }
   29 
   30 void
   31 isc_quota_destroy(isc_quota_t *quota) {
   32     INSIST(quota->used == 0);
   33     quota->max = 0;
   34     quota->used = 0;
   35     quota->soft = 0;
   36     DESTROYLOCK(&quota->lock);
   37 }
   38 
   39 void
   40 isc_quota_soft(isc_quota_t *quota, int soft) {
   41     LOCK(&quota->lock);
   42     quota->soft = soft;
   43     UNLOCK(&quota->lock);
   44 }
   45 
   46 void
   47 isc_quota_max(isc_quota_t *quota, int max) {
   48     LOCK(&quota->lock);
   49     quota->max = max;
   50     UNLOCK(&quota->lock);
   51 }
   52 
   53 isc_result_t
   54 isc_quota_reserve(isc_quota_t *quota) {
   55     isc_result_t result;
   56     LOCK(&quota->lock);
   57     if (quota->max == 0 || quota->used < quota->max) {
   58         if (quota->soft == 0 || quota->used < quota->soft)
   59             result = ISC_R_SUCCESS;
   60         else
   61             result = ISC_R_SOFTQUOTA;
   62         quota->used++;
   63     } else
   64         result = ISC_R_QUOTA;
   65     UNLOCK(&quota->lock);
   66     return (result);
   67 }
   68 
   69 void
   70 isc_quota_release(isc_quota_t *quota) {
   71     LOCK(&quota->lock);
   72     INSIST(quota->used > 0);
   73     quota->used--;
   74     UNLOCK(&quota->lock);
   75 }
   76 
   77 static isc_result_t
   78 doattach(isc_quota_t *quota, isc_quota_t **p, bool force) {
   79     isc_result_t result;
   80     REQUIRE(p != NULL && *p == NULL);
   81 
   82     result = isc_quota_reserve(quota);
   83     if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA) {
   84         *p = quota;
   85     } else if (result == ISC_R_QUOTA && force) {
   86         /* attach anyway */
   87         LOCK(&quota->lock);
   88         quota->used++;
   89         UNLOCK(&quota->lock);
   90 
   91         *p = quota;
   92         result = ISC_R_SUCCESS;
   93     }
   94 
   95     return (result);
   96 }
   97 
   98 isc_result_t
   99 isc_quota_attach(isc_quota_t *quota, isc_quota_t **p) {
  100     return (doattach(quota, p, false));
  101 }
  102 
  103 isc_result_t
  104 isc_quota_force(isc_quota_t *quota, isc_quota_t **p) {
  105     return (doattach(quota, p, true));
  106 }
  107 
  108 void
  109 isc_quota_detach(isc_quota_t **p) {
  110     INSIST(p != NULL && *p != NULL);
  111     isc_quota_release(*p);
  112     *p = NULL;
  113 }
  114 
  115 unsigned int
  116 isc_quota_getused(isc_quota_t *quota) {
  117     int used;
  118 
  119     LOCK(&quota->lock);
  120     used = quota->used;
  121     UNLOCK(&quota->lock);
  122     return (used);
  123 }