1 /* $NetBSD: quota.c,v 1.4 2014/12/10 04:37:59 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2000, 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: quota.c,v 1.18 2007/06/19 23:47:17 tbox Exp */ 21 22 /*! \file */ 23 24 #include <config.h> 25 26 #include <stddef.h> 27 28 #include <isc/quota.h> 29 #include <isc/util.h> 30 31 isc_result_t 32 isc_quota_init(isc_quota_t *quota, int max) { 33 quota->max = max; 34 quota->used = 0; 35 quota->soft = 0; 36 return (isc_mutex_init("a->lock)); 37 } 38 39 void 40 isc_quota_destroy(isc_quota_t *quota) { 41 INSIST(quota->used == 0); 42 quota->max = 0; 43 quota->used = 0; 44 quota->soft = 0; 45 DESTROYLOCK("a->lock); 46 } 47 48 void 49 isc_quota_soft(isc_quota_t *quota, int soft) { 50 LOCK("a->lock); 51 quota->soft = soft; 52 UNLOCK("a->lock); 53 } 54 55 void 56 isc_quota_max(isc_quota_t *quota, int max) { 57 LOCK("a->lock); 58 quota->max = max; 59 UNLOCK("a->lock); 60 } 61 62 isc_result_t 63 isc_quota_reserve(isc_quota_t *quota) { 64 isc_result_t result; 65 LOCK("a->lock); 66 if (quota->max == 0 || quota->used < quota->max) { 67 if (quota->soft == 0 || quota->used < quota->soft) 68 result = ISC_R_SUCCESS; 69 else 70 result = ISC_R_SOFTQUOTA; 71 quota->used++; 72 } else 73 result = ISC_R_QUOTA; 74 UNLOCK("a->lock); 75 return (result); 76 } 77 78 void 79 isc_quota_release(isc_quota_t *quota) { 80 LOCK("a->lock); 81 INSIST(quota->used > 0); 82 quota->used--; 83 UNLOCK("a->lock); 84 } 85 86 isc_result_t 87 isc_quota_attach(isc_quota_t *quota, isc_quota_t **p) 88 { 89 isc_result_t result; 90 INSIST(p != NULL && *p == NULL); 91 result = isc_quota_reserve(quota); 92 if (result == ISC_R_SUCCESS || result == ISC_R_SOFTQUOTA) 93 *p = quota; 94 return (result); 95 } 96 97 void 98 isc_quota_detach(isc_quota_t **p) 99 { 100 INSIST(p != NULL && *p != NULL); 101 isc_quota_release(*p); 102 *p = NULL; 103 } 104