1 /*
2  *  Tvheadend - memory info support
3  *  Copyright (C) 2016 Jaroslav Kysela
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TVHEADEND_MEMORYINFO_H
20 #define TVHEADEND_MEMORYINFO_H
21 
22 #include "idnode.h"
23 
24 struct memoryinfo;
25 
26 typedef void (*memoryinfo_cb_t)(struct memoryinfo *my);
27 
28 typedef struct memoryinfo {
29   idnode_t               my_idnode;
30   LIST_ENTRY(memoryinfo) my_link;
31   const char            *my_name;
32   void                  *my_opaque;
33   memoryinfo_cb_t        my_update;
34   int64_t                my_size;
35   int64_t                my_peak_size;
36   int64_t                my_count;
37   int64_t                my_peak_count;
38 } memoryinfo_t;
39 
40 extern struct memoryinfo_list memoryinfo_entries;
41 extern const idclass_t memoryinfo_class;
42 
memoryinfo_register(memoryinfo_t * my)43 static inline void memoryinfo_register(memoryinfo_t *my)
44 {
45   LIST_INSERT_HEAD(&memoryinfo_entries, my, my_link);
46   idnode_insert(&my->my_idnode, NULL, &memoryinfo_class, 0);
47 }
48 
memoryinfo_unregister(memoryinfo_t * my)49 static inline void memoryinfo_unregister(memoryinfo_t *my)
50 {
51   LIST_REMOVE(my, my_link);
52   idnode_unlink(&my->my_idnode);
53 }
54 
memoryinfo_update(memoryinfo_t * my,int64_t size,int64_t count)55 static inline void memoryinfo_update(memoryinfo_t *my, int64_t size, int64_t count)
56 {
57   atomic_set_s64_peak(&my->my_size, size, &my->my_peak_size);
58   atomic_set_s64_peak(&my->my_count, count, &my->my_peak_count);
59 }
60 
memoryinfo_alloc(memoryinfo_t * my,int64_t size)61 static inline void memoryinfo_alloc(memoryinfo_t *my, int64_t size)
62 {
63   atomic_pre_add_s64_peak(&my->my_size, size, &my->my_peak_size);
64   atomic_pre_add_s64_peak(&my->my_count, 1, &my->my_peak_count);
65 }
66 
memoryinfo_append(memoryinfo_t * my,int64_t size)67 static inline void memoryinfo_append(memoryinfo_t *my, int64_t size)
68 {
69   atomic_pre_add_s64_peak(&my->my_size, size, &my->my_peak_size);
70 }
71 
memoryinfo_free(memoryinfo_t * my,int64_t size)72 static inline void memoryinfo_free(memoryinfo_t *my, int64_t size)
73 {
74   atomic_dec_s64(&my->my_size, size);
75   atomic_dec_s64(&my->my_count, 1);
76 }
77 
memoryinfo_remove(memoryinfo_t * my,int64_t size)78 static inline void memoryinfo_remove(memoryinfo_t *my, int64_t size)
79 {
80   atomic_dec_s64(&my->my_size, size);
81 }
82 
83 #endif /* TVHEADEND_MEMORYINFO_H */
84