Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | /*! \file counter.h |
| 4 | * Common routines regarding counter handling */ |
| 5 | |
| 6 | /*! structure representing a single counter */ |
| 7 | struct 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 */ |
| 16 | static inline void osmo_counter_dec(struct osmo_counter *ctr) |
| 17 | { |
| 18 | ctr->value--; |
| 19 | } |
| 20 | |
| 21 | /*! Increment counter */ |
| 22 | static inline void osmo_counter_inc(struct osmo_counter *ctr) |
| 23 | { |
| 24 | ctr->value++; |
| 25 | } |
| 26 | |
| 27 | /*! Get current value of counter */ |
| 28 | static 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 */ |
| 34 | static inline void osmo_counter_reset(struct osmo_counter *ctr) |
| 35 | { |
| 36 | ctr->value = 0; |
| 37 | } |
| 38 | |
| 39 | /*! Allocate a new counter */ |
| 40 | struct osmo_counter *osmo_counter_alloc(const char *name); |
| 41 | |
| 42 | /*! Free the specified counter |
| 43 | * \param[in] ctr Counter |
| 44 | */ |
| 45 | void 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 | */ |
| 51 | int 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 | */ |
| 57 | struct osmo_counter *osmo_counter_get_by_name(const char *name); |
| 58 | |
| 59 | /*! Return the counter difference since the last call to this function */ |
| 60 | int osmo_counter_difference(struct osmo_counter *ctr); |