D-Bus
1.4.10
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 */ 00023 00024 #include <config.h> 00025 #include <dbus/dbus-resources.h> 00026 #include <dbus/dbus-internals.h> 00027 00054 struct DBusCounter 00055 { 00056 int refcount; 00058 long size_value; 00059 long unix_fd_value; 00061 long notify_size_guard_value; 00062 long notify_unix_fd_guard_value; 00064 DBusCounterNotifyFunction notify_function; 00065 void *notify_data; 00066 }; 00067 /* end of resource limits internals docs */ 00069 00081 DBusCounter* 00082 _dbus_counter_new (void) 00083 { 00084 DBusCounter *counter; 00085 00086 counter = dbus_new (DBusCounter, 1); 00087 if (counter == NULL) 00088 return NULL; 00089 00090 counter->refcount = 1; 00091 counter->size_value = 0; 00092 counter->unix_fd_value = 0; 00093 00094 counter->notify_size_guard_value = 0; 00095 counter->notify_unix_fd_guard_value = 0; 00096 counter->notify_function = NULL; 00097 counter->notify_data = NULL; 00098 00099 return counter; 00100 } 00101 00108 DBusCounter * 00109 _dbus_counter_ref (DBusCounter *counter) 00110 { 00111 _dbus_assert (counter->refcount > 0); 00112 00113 counter->refcount += 1; 00114 00115 return counter; 00116 } 00117 00124 void 00125 _dbus_counter_unref (DBusCounter *counter) 00126 { 00127 _dbus_assert (counter->refcount > 0); 00128 00129 counter->refcount -= 1; 00130 00131 if (counter->refcount == 0) 00132 { 00133 00134 dbus_free (counter); 00135 } 00136 } 00137 00147 void 00148 _dbus_counter_adjust_size (DBusCounter *counter, 00149 long delta) 00150 { 00151 long old = counter->size_value; 00152 00153 counter->size_value += delta; 00154 00155 #if 0 00156 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n", 00157 old, delta, counter->size_value); 00158 #endif 00159 00160 if (counter->notify_function != NULL && 00161 ((old < counter->notify_size_guard_value && 00162 counter->size_value >= counter->notify_size_guard_value) || 00163 (old >= counter->notify_size_guard_value && 00164 counter->size_value < counter->notify_size_guard_value))) 00165 (* counter->notify_function) (counter, counter->notify_data); 00166 } 00167 00177 void 00178 _dbus_counter_adjust_unix_fd (DBusCounter *counter, 00179 long delta) 00180 { 00181 long old = counter->unix_fd_value; 00182 00183 counter->unix_fd_value += delta; 00184 00185 #if 0 00186 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n", 00187 old, delta, counter->unix_fd_value); 00188 #endif 00189 00190 if (counter->notify_function != NULL && 00191 ((old < counter->notify_unix_fd_guard_value && 00192 counter->unix_fd_value >= counter->notify_unix_fd_guard_value) || 00193 (old >= counter->notify_unix_fd_guard_value && 00194 counter->unix_fd_value < counter->notify_unix_fd_guard_value))) 00195 (* counter->notify_function) (counter, counter->notify_data); 00196 } 00197 00204 long 00205 _dbus_counter_get_size_value (DBusCounter *counter) 00206 { 00207 return counter->size_value; 00208 } 00209 00216 long 00217 _dbus_counter_get_unix_fd_value (DBusCounter *counter) 00218 { 00219 return counter->unix_fd_value; 00220 } 00221 00233 void 00234 _dbus_counter_set_notify (DBusCounter *counter, 00235 long size_guard_value, 00236 long unix_fd_guard_value, 00237 DBusCounterNotifyFunction function, 00238 void *user_data) 00239 { 00240 counter->notify_size_guard_value = size_guard_value; 00241 counter->notify_unix_fd_guard_value = unix_fd_guard_value; 00242 counter->notify_function = function; 00243 counter->notify_data = user_data; 00244 } 00245 /* end of resource limits exported API */