libosmocore  1.5.1
Osmocom core library
use_count.h
Go to the documentation of this file.
1 
4 /*
5  * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
6  *
7  * All Rights Reserved
8  *
9  * Author: Neels Hofmeyr <neels@hofmeyr.de>
10  *
11  * SPDX-License-Identifier: GPL-2.0+
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 
28 #pragma once
29 
30 #include <stdint.h>
31 #include <stdlib.h>
32 
33 #include <osmocom/core/linuxlist.h>
34 
41 
66 typedef int (* osmo_use_count_cb_t )(struct osmo_use_count_entry *use_count_entry, int32_t old_use_count,
67  const char *file, int line);
68 
179  struct llist_head use_counts;
180 };
181 
196  struct llist_head entry;
200  const char *use;
203  int32_t count;
204 };
205 
212 #define osmo_use_count_get_put(USE_LIST, USE, CHANGE) \
213  _osmo_use_count_get_put(USE_LIST, USE, CHANGE, __FILE__, __LINE__)
214 
215 int _osmo_use_count_get_put(struct osmo_use_count *uc, const char *use, int32_t change,
216  const char *file, int line);
217 
218 const char *osmo_use_count_name_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc);
219 int osmo_use_count_to_str_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc);
220 char *osmo_use_count_to_str_c(void *ctx, const struct osmo_use_count *uc);
221 
222 int32_t osmo_use_count_total(const struct osmo_use_count *uc);
223 int32_t osmo_use_count_by(const struct osmo_use_count *uc, const char *use);
224 
225 struct osmo_use_count_entry *osmo_use_count_find(const struct osmo_use_count *uc, const char *use);
226 void osmo_use_count_free(struct osmo_use_count_entry *use_count_entry);
227 
229  size_t buf_n_entries);
230 
osmo_use_count::use_counts
struct llist_head use_counts
List of use tokens.
Definition: use_count.h:179
osmo_use_count_total
int32_t osmo_use_count_total(const struct osmo_use_count *uc)
Return the sum of all use counts, min- and max-clamped at INT32_MIN and INT32_MAX.
Definition: use_count.c:71
osmo_use_count_entry::use
const char * use
Use token string that was passed to osmo_use_count_get_put().
Definition: use_count.h:200
osmo_use_count
Use counter state for one used object, managing N distinct named counters.
Definition: use_count.h:172
osmo_use_count_entry
One named counter in the list managed by osmo_use_count.
Definition: use_count.h:194
osmo_use_count_to_str_buf
int osmo_use_count_to_str_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc)
Write a comprehensive listing of use counts to a string buffer.
Definition: use_count.c:121
osmo_use_count_by
int32_t osmo_use_count_by(const struct osmo_use_count *uc, const char *use)
Return use count by a single use token.
Definition: use_count.c:90
osmo_use_count_name_buf
const char * osmo_use_count_name_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc)
Write a comprehensive listing of use counts to a string buffer.
Definition: use_count.c:108
osmo_use_count_entry::count
int32_t count
Current use count amount for only this use token string.
Definition: use_count.h:203
_osmo_use_count_get_put
int _osmo_use_count_get_put(struct osmo_use_count *uc, const char *use, int32_t change, const char *file, int line)
Implementation for osmo_use_count_get_put(), which can also be directly invoked to pass source file i...
Definition: use_count.c:237
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
osmo_use_count_make_static_entries
void osmo_use_count_make_static_entries(struct osmo_use_count *uc, struct osmo_use_count_entry *buf, size_t buf_n_entries)
Add N static use token entries to avoid dynamic allocation of use count tokens.
Definition: use_count.c:293
osmo_use_count_cb_t
int(* osmo_use_count_cb_t)(struct osmo_use_count_entry *use_count_entry, int32_t old_use_count, const char *file, int line)
Invoked when a use count changes.
Definition: use_count.h:66
osmo_use_count_entry::entry
struct llist_head entry
Entry in osmo_use_count->use_counts.
Definition: use_count.h:196
osmo_use_count_entry::use_count
struct osmo_use_count * use_count
Parent use count and backpointer to the talloc_object.
Definition: use_count.h:198
osmo_use_count::talloc_object
void * talloc_object
Context to talloc-allocate use count entries from (if at all necessary); back-pointer to the owning o...
Definition: use_count.h:175
file
write Write running configuration to or terminal n Write configuration to the file(same as write file)\n") ALIAS(config_write_file
osmo_use_count::use_cb
osmo_use_count_cb_t use_cb
If not NULL, this is invoked for each use count change.
Definition: use_count.h:177
osmo_use_count_free
void osmo_use_count_free(struct osmo_use_count_entry *use_count_entry)
Deallocate a use count entry.
Definition: use_count.c:224
osmo_use_count_find
struct osmo_use_count_entry * osmo_use_count_find(const struct osmo_use_count *uc, const char *use)
Definition: use_count.c:168
linuxlist.h
osmo_use_count_to_str_c
char * osmo_use_count_to_str_c(void *ctx, const struct osmo_use_count *uc)
Write a comprehensive listing of use counts to a talloc allocated string buffer.
Definition: use_count.c:158