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 https://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 #ifndef ISC_COUNTER_H
13 #define ISC_COUNTER_H 1
14 
15 /*****
16 ***** Module Info
17 *****/
18 
19 /*! \file isc/counter.h
20  *
21  * \brief The isc_counter_t object is a simplified version of the
22  * isc_quota_t object; it tracks the consumption of limited
23  * resources, returning an error condition when the quota is
24  * exceeded.  However, unlike isc_quota_t, attaching and detaching
25  * from a counter object does not increment or decrement the counter.
26  */
27 
28 /***
29  *** Imports.
30  ***/
31 
32 #include <isc/lang.h>
33 #include <isc/mutex.h>
34 #include <isc/types.h>
35 
36 /*****
37 ***** Types.
38 *****/
39 
40 ISC_LANG_BEGINDECLS
41 
42 isc_result_t
43 isc_counter_create(isc_mem_t *mctx, int limit, isc_counter_t **counterp);
44 /*%<
45  * Allocate and initialize a counter object.
46  */
47 
48 isc_result_t
49 isc_counter_increment(isc_counter_t *counter);
50 /*%<
51  * Increment the counter.
52  *
53  * If the counter limit is nonzero and has been reached, then
54  * return ISC_R_QUOTA, otherwise ISC_R_SUCCESS. (The counter is
55  * incremented regardless of return value.)
56  */
57 
58 unsigned int
59 isc_counter_used(isc_counter_t *counter);
60 /*%<
61  * Return the current counter value.
62  */
63 
64 void
65 isc_counter_setlimit(isc_counter_t *counter, int limit);
66 /*%<
67  * Set the counter limit.
68  */
69 
70 void
71 isc_counter_attach(isc_counter_t *source, isc_counter_t **targetp);
72 /*%<
73  * Attach to a counter object, increasing its reference counter.
74  */
75 
76 void
77 isc_counter_detach(isc_counter_t **counterp);
78 /*%<
79  * Detach (and destroy if reference counter has dropped to zero)
80  * a counter object.
81  */
82 
83 ISC_LANG_ENDDECLS
84 
85 #endif /* ISC_COUNTER_H */
86