1 /*
2 * Copyright (c) 2002-2019 One Identity
3 * Copyright (c) 2019 Laszlo Budai <laszlo.budai@balabit.com>
4 * Copyright (c) 2019 László Várady <laszlo.varady@balabit.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * As an additional exemption you are allowed to compile & link against the
21 * OpenSSL libraries as published by the OpenSSL project. See the file
22 * COPYING for details.
23 *
24 */
25
26 #include <syslog-ng.h>
27 #include "dynamic-window.h"
28
29 void
dynamic_window_stat_update(DynamicWindowStat * self,gsize value)30 dynamic_window_stat_update(DynamicWindowStat *self, gsize value)
31 {
32 self->sum += value;
33 self->n++;
34 }
35
36 void
dynamic_window_stat_reset(DynamicWindowStat * self)37 dynamic_window_stat_reset(DynamicWindowStat *self)
38 {
39 self->sum = 0;
40 self->n = 0;
41 }
42
43 gsize
dynamic_window_stat_get_avg(DynamicWindowStat * self)44 dynamic_window_stat_get_avg(DynamicWindowStat *self)
45 {
46 if (self->n == 0)
47 return 0;
48
49 return self->sum / self->n;
50 }
51
52 gsize
dynamic_window_stat_get_number_of_samples(DynamicWindowStat * self)53 dynamic_window_stat_get_number_of_samples(DynamicWindowStat *self)
54 {
55 return self->n;
56 }
57
58 guint64
dynamic_window_stat_get_sum(DynamicWindowStat * self)59 dynamic_window_stat_get_sum(DynamicWindowStat *self)
60 {
61 return self->sum;
62 }
63
64 void
dynamic_window_set_pool(DynamicWindow * self,DynamicWindowPool * pool)65 dynamic_window_set_pool(DynamicWindow *self, DynamicWindowPool *pool)
66 {
67 self->pool = pool;
68 dynamic_window_stat_reset(&self->stat);
69 }
70
71 gboolean
dynamic_window_is_enabled(DynamicWindow * self)72 dynamic_window_is_enabled(DynamicWindow *self)
73 {
74 return !!self->pool;
75 }
76
77 gsize
dynamic_window_request(DynamicWindow * self,gsize size)78 dynamic_window_request(DynamicWindow *self, gsize size)
79 {
80 if (!self->pool)
81 return 0;
82
83 return dynamic_window_pool_request(self->pool, size);
84 }
85
86 void
dynamic_window_release(DynamicWindow * self,gsize size)87 dynamic_window_release(DynamicWindow *self, gsize size)
88 {
89 if (!self->pool)
90 return;
91
92 dynamic_window_pool_release(self->pool, size);
93 }
94