D-Bus: dbus-resources.c Source File

D-Bus 1.8.16

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List

dbus-resources.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-resources.c Resource tracking/limits
3 *
4 * Copyright (C) 2003 Red Hat Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #include <config.h>
25 #include <dbus/dbus-resources.h>
26 #include <dbus/dbus-internals.h>
27
54 struct DBusCounter
55 {
56 int refcount;
58 long size_value;
59 long unix_fd_value;
61 #ifdef DBUS_ENABLE_STATS
62 long peak_size_value;
63 long peak_unix_fd_value;
64 #endif
65
66 long notify_size_guard_value;
67 long notify_unix_fd_guard_value;
69 DBusCounterNotifyFunction notify_function;
70 void *notify_data;
71 dbus_bool_t notify_pending : 1;
72 };
73 /* end of resource limits internals docs */
75
87 DBusCounter*
88 _dbus_counter_new (void)
89 {
90 DBusCounter *counter;
91
92 counter = dbus_new0 (DBusCounter, 1);
93 if (counter == NULL)
94 return NULL;
95
96 counter->refcount = 1;
97
98 return counter;
99 }
100
107 DBusCounter *
108 _dbus_counter_ref (DBusCounter *counter)
109 {
110 _dbus_assert (counter->refcount > 0);
111
112 counter->refcount += 1;
113
114 return counter;
115 }
116
123 void
124 _dbus_counter_unref (DBusCounter *counter)
125 {
126 _dbus_assert (counter->refcount > 0);
127
128 counter->refcount -= 1;
129
130 if (counter->refcount == 0)
131 {
132
133 dbus_free (counter);
134 }
135 }
136
147 void
148 _dbus_counter_adjust_size (DBusCounter *counter,
149 long delta)
150 {
151 long old = counter->size_value;
152
153 counter->size_value += delta;
154
155 #ifdef DBUS_ENABLE_STATS
156 if (counter->peak_size_value < counter->size_value)
157 counter->peak_size_value = counter->size_value;
158 #endif
159
160 #if 0
161 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
162 old, delta, counter->size_value);
163 #endif
164
165 if (counter->notify_function != NULL &&
166 ((old < counter->notify_size_guard_value &&
167 counter->size_value >= counter->notify_size_guard_value) ||
168 (old >= counter->notify_size_guard_value &&
169 counter->size_value < counter->notify_size_guard_value)))
170 counter->notify_pending = TRUE;
171 }
172
181 void
182 _dbus_counter_notify (DBusCounter *counter)
183 {
184 if (counter->notify_pending)
185 {
186 counter->notify_pending = FALSE;
187 (* counter->notify_function) (counter, counter->notify_data);
188 }
189 }
190
201 void
202 _dbus_counter_adjust_unix_fd (DBusCounter *counter,
203 long delta)
204 {
205 long old = counter->unix_fd_value;
206
207 counter->unix_fd_value += delta;
208
209 #ifdef DBUS_ENABLE_STATS
210 if (counter->peak_unix_fd_value < counter->unix_fd_value)
211 counter->peak_unix_fd_value = counter->unix_fd_value;
212 #endif
213
214 #if 0
215 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
216 old, delta, counter->unix_fd_value);
217 #endif
218
219 if (counter->notify_function != NULL &&
220 ((old < counter->notify_unix_fd_guard_value &&
221 counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
222 (old >= counter->notify_unix_fd_guard_value &&
223 counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
224 counter->notify_pending = TRUE;
225 }
226
233 long
234 _dbus_counter_get_size_value (DBusCounter *counter)
235 {
236 return counter->size_value;
237 }
238
245 long
246 _dbus_counter_get_unix_fd_value (DBusCounter *counter)
247 {
248 return counter->unix_fd_value;
249 }
250
262 void
263 _dbus_counter_set_notify (DBusCounter *counter,
264 long size_guard_value,
265 long unix_fd_guard_value,
266 DBusCounterNotifyFunction function,
267 void *user_data)
268 {
269 counter->notify_size_guard_value = size_guard_value;
270 counter->notify_unix_fd_guard_value = unix_fd_guard_value;
271 counter->notify_function = function;
272 counter->notify_data = user_data;
273 counter->notify_pending = FALSE;
274 }
275
276 #ifdef DBUS_ENABLE_STATS
277 long
278 _dbus_counter_get_peak_size_value (DBusCounter *counter)
279 {
280 return counter->peak_size_value;
281 }
282
283 long
284 _dbus_counter_get_peak_unix_fd_value (DBusCounter *counter)
285 {
286 return counter->peak_unix_fd_value;
287 }
288 #endif
289 /* end of resource limits exported API */
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:135
DBusCounter
Internals of DBusCounter.
Definition: dbus-resources.c:54
DBusCounter::notify_function
DBusCounterNotifyFunction notify_function
notify function
Definition: dbus-resources.c:69
_dbus_counter_ref
DBusCounter * _dbus_counter_ref(DBusCounter *counter)
Increments refcount of the counter.
Definition: dbus-resources.c:108
DBusCounter::size_value
long size_value
current size counter value
Definition: dbus-resources.c:58
DBusCounter::notify_data
void * notify_data
data for notify function
Definition: dbus-resources.c:70
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
_dbus_counter_unref
void _dbus_counter_unref(DBusCounter *counter)
Decrements refcount of the counter and possibly finalizes the counter.
Definition: dbus-resources.c:124
_dbus_counter_adjust_unix_fd
void _dbus_counter_adjust_unix_fd(DBusCounter *counter, long delta)
Adjusts the value of the unix fd counter by the given delta which may be positive or negative...
Definition: dbus-resources.c:202
DBusCounter::notify_pending
dbus_bool_t notify_pending
TRUE if the guard value has been crossed.
Definition: dbus-resources.c:71
DBusCounter::unix_fd_value
long unix_fd_value
current unix fd counter value
Definition: dbus-resources.c:59
TRUE
#define TRUE
Expands to "1".
_dbus_counter_set_notify
void _dbus_counter_set_notify(DBusCounter *counter, long size_guard_value, long unix_fd_guard_value, DBusCounterNotifyFunction function, void *user_data)
Sets the notify function for this counter; the notify function is called whenever the counter's value...
Definition: dbus-resources.c:263
_dbus_counter_notify
void _dbus_counter_notify(DBusCounter *counter)
Calls the notify function from _dbus_counter_set_notify(), if that function has been specified and th...
Definition: dbus-resources.c:182
DBusCounter::notify_unix_fd_guard_value
long notify_unix_fd_guard_value
call notify function when crossing this unix fd value
Definition: dbus-resources.c:67
DBusCounter::notify_size_guard_value
long notify_size_guard_value
call notify function when crossing this size value
Definition: dbus-resources.c:66
_dbus_counter_new
DBusCounter * _dbus_counter_new(void)
Creates a new DBusCounter.
Definition: dbus-resources.c:88
DBusCounter::refcount
int refcount
reference count
Definition: dbus-resources.c:56
FALSE
#define FALSE
Expands to "0".
_dbus_counter_get_unix_fd_value
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
Definition: dbus-resources.c:246
_dbus_counter_get_size_value
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.
Definition: dbus-resources.c:234
_dbus_counter_adjust_size
void _dbus_counter_adjust_size(DBusCounter *counter, long delta)
Adjusts the value of the size counter by the given delta which may be positive or negative...
Definition: dbus-resources.c:148