00001 /* -*- mode: C; c-file-style: "gnu" -*- */ 00002 /* dbus-resources.c Resource tracking/limits 00003 * 00004 * Copyright (C) 2003 Red Hat Inc. 00005 * 00006 * Licensed under the Academic Free License version 1.2 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 * 00022 */ 00023 #include <dbus/dbus-resources.h> 00024 #include <dbus/dbus-internals.h> 00025 00052 struct DBusCounter 00053 { 00054 int refcount; 00056 long value; 00058 long notify_guard_value; 00059 DBusCounterNotifyFunction notify_function; 00060 void *notify_data; 00061 }; 00062 /* end of resource limits internals docs */ 00064 00076 DBusCounter* 00077 _dbus_counter_new (void) 00078 { 00079 DBusCounter *counter; 00080 00081 counter = dbus_new (DBusCounter, 1); 00082 if (counter == NULL) 00083 return NULL; 00084 00085 counter->refcount = 1; 00086 counter->value = 0; 00087 00088 counter->notify_guard_value = 0; 00089 counter->notify_function = NULL; 00090 counter->notify_data = NULL; 00091 00092 return counter; 00093 } 00094 00100 void 00101 _dbus_counter_ref (DBusCounter *counter) 00102 { 00103 _dbus_assert (counter->refcount > 0); 00104 00105 counter->refcount += 1; 00106 } 00107 00114 void 00115 _dbus_counter_unref (DBusCounter *counter) 00116 { 00117 _dbus_assert (counter->refcount > 0); 00118 00119 counter->refcount -= 1; 00120 00121 if (counter->refcount == 0) 00122 { 00123 00124 dbus_free (counter); 00125 } 00126 } 00127 00137 void 00138 _dbus_counter_adjust (DBusCounter *counter, 00139 long delta) 00140 { 00141 long old = counter->value; 00142 00143 counter->value += delta; 00144 00145 #if 0 00146 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n", 00147 old, delta, counter->value); 00148 #endif 00149 00150 if (counter->notify_function != NULL && 00151 ((old < counter->notify_guard_value && 00152 counter->value >= counter->notify_guard_value) || 00153 (old >= counter->notify_guard_value && 00154 counter->value < counter->notify_guard_value))) 00155 (* counter->notify_function) (counter, counter->notify_data); 00156 } 00157 00164 long 00165 _dbus_counter_get_value (DBusCounter *counter) 00166 { 00167 return counter->value; 00168 } 00169 00180 void 00181 _dbus_counter_set_notify (DBusCounter *counter, 00182 long guard_value, 00183 DBusCounterNotifyFunction function, 00184 void *user_data) 00185 { 00186 counter->notify_guard_value = guard_value; 00187 counter->notify_function = function; 00188 counter->notify_data = user_data; 00189 } 00190 /* end of resource limits exported API */