D-Bus: dbus-resources.c Source File

D-Bus 1.12.18

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 DBusRMutex *mutex;
73 };
74 /* end of resource limits internals docs */
76
88 DBusCounter*
89 _dbus_counter_new (void)
90 {
91 DBusCounter *counter;
92
93 counter = dbus_new0 (DBusCounter, 1);
94 if (counter == NULL)
95 return NULL;
96
97 counter->refcount = 1;
98
99 _dbus_rmutex_new_at_location (&counter->mutex);
100 if (counter->mutex == NULL)
101 {
102 dbus_free (counter);
103 counter = NULL;
104 }
105
106 return counter;
107 }
108
115 DBusCounter *
116 _dbus_counter_ref (DBusCounter *counter)
117 {
118 _dbus_rmutex_lock (counter->mutex);
119
120 _dbus_assert (counter->refcount > 0);
121
122 counter->refcount += 1;
123
124 _dbus_rmutex_unlock (counter->mutex);
125
126 return counter;
127 }
128
135 void
136 _dbus_counter_unref (DBusCounter *counter)
137 {
138 dbus_bool_t last_ref = FALSE;
139
140 _dbus_rmutex_lock (counter->mutex);
141
142 _dbus_assert (counter->refcount > 0);
143
144 counter->refcount -= 1;
145 last_ref = (counter->refcount == 0);
146
147 _dbus_rmutex_unlock (counter->mutex);
148
149 if (last_ref)
150 {
151 _dbus_rmutex_free_at_location (&counter->mutex);
152 dbus_free (counter);
153 }
154 }
155
166 void
167 _dbus_counter_adjust_size (DBusCounter *counter,
168 long delta)
169 {
170 long old = 0;
171
172 _dbus_rmutex_lock (counter->mutex);
173
174 old = counter->size_value;
175
176 counter->size_value += delta;
177
178 #ifdef DBUS_ENABLE_STATS
179 if (counter->peak_size_value < counter->size_value)
180 counter->peak_size_value = counter->size_value;
181 #endif
182
183 #if 0
184 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
185 old, delta, counter->size_value);
186 #endif
187
188 if (counter->notify_function != NULL &&
189 ((old < counter->notify_size_guard_value &&
190 counter->size_value >= counter->notify_size_guard_value) ||
191 (old >= counter->notify_size_guard_value &&
192 counter->size_value < counter->notify_size_guard_value)))
193 counter->notify_pending = TRUE;
194
195 _dbus_rmutex_unlock (counter->mutex);
196 }
197
206 void
207 _dbus_counter_notify (DBusCounter *counter)
208 {
209 DBusCounterNotifyFunction notify_function = NULL;
210 void *notify_data = NULL;
211
212 _dbus_rmutex_lock (counter->mutex);
213 if (counter->notify_pending)
214 {
215 counter->notify_pending = FALSE;
216 notify_function = counter->notify_function;
217 notify_data = counter->notify_data;
218 }
219 _dbus_rmutex_unlock (counter->mutex);
220
221 if (notify_function != NULL)
222 (* notify_function) (counter, notify_data);
223 }
224
235 void
236 _dbus_counter_adjust_unix_fd (DBusCounter *counter,
237 long delta)
238 {
239 long old = 0;
240
241 _dbus_rmutex_lock (counter->mutex);
242
243 old = counter->unix_fd_value;
244
245 counter->unix_fd_value += delta;
246
247 #ifdef DBUS_ENABLE_STATS
248 if (counter->peak_unix_fd_value < counter->unix_fd_value)
249 counter->peak_unix_fd_value = counter->unix_fd_value;
250 #endif
251
252 #if 0
253 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
254 old, delta, counter->unix_fd_value);
255 #endif
256
257 if (counter->notify_function != NULL &&
258 ((old < counter->notify_unix_fd_guard_value &&
259 counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
260 (old >= counter->notify_unix_fd_guard_value &&
261 counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
262 counter->notify_pending = TRUE;
263
264 _dbus_rmutex_unlock (counter->mutex);
265 }
266
273 long
274 _dbus_counter_get_size_value (DBusCounter *counter)
275 {
276 return counter->size_value;
277 }
278
285 long
286 _dbus_counter_get_unix_fd_value (DBusCounter *counter)
287 {
288 return counter->unix_fd_value;
289 }
290
302 void
303 _dbus_counter_set_notify (DBusCounter *counter,
304 long size_guard_value,
305 long unix_fd_guard_value,
306 DBusCounterNotifyFunction function,
307 void *user_data)
308 {
309 _dbus_rmutex_lock (counter->mutex);
310 counter->notify_size_guard_value = size_guard_value;
311 counter->notify_unix_fd_guard_value = unix_fd_guard_value;
312 counter->notify_function = function;
313 counter->notify_data = user_data;
314 counter->notify_pending = FALSE;
315 _dbus_rmutex_unlock (counter->mutex);
316 }
317
318 #ifdef DBUS_ENABLE_STATS
319 long
320 _dbus_counter_get_peak_size_value (DBusCounter *counter)
321 {
322 return counter->peak_size_value;
323 }
324
325 long
326 _dbus_counter_get_peak_unix_fd_value (DBusCounter *counter)
327 {
328 return counter->peak_unix_fd_value;
329 }
330 #endif
331 /* 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:702
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:152
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:116
_dbus_rmutex_new_at_location
void _dbus_rmutex_new_at_location(DBusRMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
Definition: dbus-threads.c:53
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_rmutex_unlock
DBUS_PRIVATE_EXPORT void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:150
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
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:136
_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:236
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:303
_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:207
DBusCounter::mutex
DBusRMutex * mutex
Lock on the entire DBusCounter.
Definition: dbus-resources.c:72
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_rmutex_free_at_location
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:94
_dbus_counter_new
DBusCounter * _dbus_counter_new(void)
Creates a new DBusCounter.
Definition: dbus-resources.c:89
_dbus_rmutex_lock
DBUS_PRIVATE_EXPORT void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:122
DBusCounter::refcount
int refcount
reference count
Definition: dbus-resources.c:56
FALSE
#define FALSE
Expands to "0".
DBusRMutex
Definition: dbus-sysdeps-pthread.c:49
_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:286
_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:274
_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:167