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
101 _dbus_counter_ref (DBusCounter *counter)
103 _dbus_assert (counter->refcount > 0);
105 counter->refcount += 1;
109 * Decrements refcount of the counter and possibly
110 * finalizes the counter.
112 * @param counter the counter
115 _dbus_counter_unref (DBusCounter *counter)
117 _dbus_assert (counter->refcount > 0);
119 counter->refcount -= 1;
121 if (counter->refcount == 0)
129 * Adjusts the value of the counter by the given
130 * delta which may be positive or negative.
131 * Calls the notify function from _dbus_counter_set_notify()
132 * if that function has been specified.
134 * @param counter the counter
135 * @param delta value to add to the counter's current value
138 _dbus_counter_adjust (DBusCounter *counter,
141 long old = counter->value;
143 counter->value += delta;
146 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
147 old, delta, counter->value);
150 if (counter->notify_function != NULL &&
151 ((old < counter->notify_guard_value &&
152 counter->value >= counter->notify_guard_value) ||
153 (old >= counter->notify_guard_value &&
154 counter->value < counter->notify_guard_value)))
155 (* counter->notify_function) (counter, counter->notify_data);
159 * Gets the current value of the counter.
161 * @param counter the counter
162 * @returns its current value
165 _dbus_counter_get_value (DBusCounter *counter)
167 return counter->value;
171 * Sets the notify function for this counter; the notify function is
172 * called whenever the counter's value crosses the guard value in
173 * either direction (moving up, or moving down).
175 * @param counter the counter
176 * @param guard_value the value we're notified if the counter crosses
177 * @param function function to call in order to notify
178 * @param user_data data to pass to the function
181 _dbus_counter_set_notify (DBusCounter *counter,
183 DBusCounterNotifyFunction function,
186 counter->notify_guard_value = guard_value;
187 counter->notify_function = function;
188 counter->notify_data = user_data;
191 /** @} */ /* end of resource limits exported API */