00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-resources.c Resource tracking/limits 00003 * 00004 * Copyright (C) 2003 Red Hat Inc. 00005 * 00006 * Licensed under the Academic Free License version 2.1 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 00101 DBusCounter * 00102 _dbus_counter_ref (DBusCounter *counter) 00103 { 00104 _dbus_assert (counter->refcount > 0); 00105 00106 counter->refcount += 1; 00107 00108 return counter; 00109 } 00110 00117 void 00118 _dbus_counter_unref (DBusCounter *counter) 00119 { 00120 _dbus_assert (counter->refcount > 0); 00121 00122 counter->refcount -= 1; 00123 00124 if (counter->refcount == 0) 00125 { 00126 00127 dbus_free (counter); 00128 } 00129 } 00130 00140 void 00141 _dbus_counter_adjust (DBusCounter *counter, 00142 long delta) 00143 { 00144 long old = counter->value; 00145 00146 counter->value += delta; 00147 00148 #if 0 00149 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n", 00150 old, delta, counter->value); 00151 #endif 00152 00153 if (counter->notify_function != NULL && 00154 ((old < counter->notify_guard_value && 00155 counter->value >= counter->notify_guard_value) || 00156 (old >= counter->notify_guard_value && 00157 counter->value < counter->notify_guard_value))) 00158 (* counter->notify_function) (counter, counter->notify_data); 00159 } 00160 00167 long 00168 _dbus_counter_get_value (DBusCounter *counter) 00169 { 00170 return counter->value; 00171 } 00172 00183 void 00184 _dbus_counter_set_notify (DBusCounter *counter, 00185 long guard_value, 00186 DBusCounterNotifyFunction function, 00187 void *user_data) 00188 { 00189 counter->notify_guard_value = guard_value; 00190 counter->notify_function = function; 00191 counter->notify_data = user_data; 00192 } 00193 /* end of resource limits exported API */