1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-resources.c Resource tracking/limits
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <dbus/dbus-resources.h>
24 #include <dbus/dbus-internals.h>
27 * @defgroup DBusResources Resource limits related code
28 * @ingroup DBusInternals
29 * @brief DBusCounter and other stuff related to resource limits
31 * Types and functions related to tracking resource limits,
32 * such as the maximum amount of memory a connection can use
37 * @defgroup DBusResourcesInternals Resource limits implementation details
38 * @ingroup DBusInternals
39 * @brief Resource limits implementation details
41 * Implementation details of resource limits code.
47 * @brief Internals of DBusCounter.
49 * DBusCounter internals. DBusCounter is an opaque object, it must be
50 * used via accessor functions.
54 int refcount; /**< reference count */
56 long value; /**< current counter value */
58 long notify_guard_value; /**< call notify function when crossing this value */
59 DBusCounterNotifyFunction notify_function; /**< notify function */
60 void *notify_data; /**< data for notify function */
63 /** @} */ /* end of resource limits internals docs */
66 * @addtogroup DBusResources
71 * Creates a new DBusCounter. DBusCounter is used
72 * to count usage of some resource such as memory.
74 * @returns new counter or #NULL on failure
77 _dbus_counter_new (void)
81 counter = dbus_new (DBusCounter, 1);
85 counter->refcount = 1;
88 counter->notify_guard_value = 0;
89 counter->notify_function = NULL;
90 counter->notify_data = NULL;
96 * Increments refcount of the counter
98 * @param counter the counter
99 * @returns the counter
102 _dbus_counter_ref (DBusCounter *counter)
104 _dbus_assert (counter->refcount > 0);
106 counter->refcount += 1;
112 * Decrements refcount of the counter and possibly
113 * finalizes the counter.
115 * @param counter the counter
118 _dbus_counter_unref (DBusCounter *counter)
120 _dbus_assert (counter->refcount > 0);
122 counter->refcount -= 1;
124 if (counter->refcount == 0)
132 * Adjusts the value of the counter by the given
133 * delta which may be positive or negative.
134 * Calls the notify function from _dbus_counter_set_notify()
135 * if that function has been specified.
137 * @param counter the counter
138 * @param delta value to add to the counter's current value
141 _dbus_counter_adjust (DBusCounter *counter,
144 long old = counter->value;
146 counter->value += delta;
149 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
150 old, delta, counter->value);
153 if (counter->notify_function != NULL &&
154 ((old < counter->notify_guard_value &&
155 counter->value >= counter->notify_guard_value) ||
156 (old >= counter->notify_guard_value &&
157 counter->value < counter->notify_guard_value)))
158 (* counter->notify_function) (counter, counter->notify_data);
162 * Gets the current value of the counter.
164 * @param counter the counter
165 * @returns its current value
168 _dbus_counter_get_value (DBusCounter *counter)
170 return counter->value;
174 * Sets the notify function for this counter; the notify function is
175 * called whenever the counter's value crosses the guard value in
176 * either direction (moving up, or moving down).
178 * @param counter the counter
179 * @param guard_value the value we're notified if the counter crosses
180 * @param function function to call in order to notify
181 * @param user_data data to pass to the function
184 _dbus_counter_set_notify (DBusCounter *counter,
186 DBusCounterNotifyFunction function,
189 counter->notify_guard_value = guard_value;
190 counter->notify_function = function;
191 counter->notify_data = user_data;
194 /** @} */ /* end of resource limits exported API */