blob: f4429cdb5e1368fca08876a5a86d920fe467a2fd [file] [log] [blame]
Harald Welte216338c2017-10-15 19:46:19 +02001#pragma once
2
3/*! \file counter.h
4 * Common routines regarding counter handling */
5
6/*! structure representing a single counter */
7struct osmo_counter {
8 struct llist_head list; /*!< internal list head */
9 const char *name; /*!< human-readable name */
10 const char *description; /*!< humn-readable description */
11 unsigned long value; /*!< current value */
12 unsigned long previous; /*!< previous value */
13};
14
15/*! Decrement counter */
16static inline void osmo_counter_dec(struct osmo_counter *ctr)
17{
18 ctr->value--;
19}
20
21/*! Increment counter */
22static inline void osmo_counter_inc(struct osmo_counter *ctr)
23{
24 ctr->value++;
25}
26
27/*! Get current value of counter */
28static inline unsigned long osmo_counter_get(struct osmo_counter *ctr)
29{
30 return ctr->value;
31}
32
33/*! Reset current value of counter to 0 */
34static inline void osmo_counter_reset(struct osmo_counter *ctr)
35{
36 ctr->value = 0;
37}
38
39/*! Allocate a new counter */
40struct osmo_counter *osmo_counter_alloc(const char *name);
41
42/*! Free the specified counter
43 * \param[in] ctr Counter
44 */
45void osmo_counter_free(struct osmo_counter *ctr);
46
47/*! Iterate over all counters
48 * \param[in] handle_counter Call-back function, aborts if rc < 0
49 * \param[in] data Private dtata handed through to \a handle_counter
50 */
51int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), void *data);
52
53/*! Resolve counter by human-readable name
54 * \param[in] name human-readable name of counter
55 * \returns pointer to counter (\ref osmo_counter) or NULL otherwise
56 */
57struct osmo_counter *osmo_counter_get_by_name(const char *name);
58
59/*! Return the counter difference since the last call to this function */
60int osmo_counter_difference(struct osmo_counter *ctr);