D-Bus  1.6.8
dbus-resources.c
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 #ifdef DBUS_ENABLE_STATS
00062   long peak_size_value;     
00063   long peak_unix_fd_value;  
00064 #endif
00065 
00066   long notify_size_guard_value;    
00067   long notify_unix_fd_guard_value; 
00069   DBusCounterNotifyFunction notify_function; 
00070   void *notify_data; 
00071   dbus_bool_t notify_pending : 1; 
00072 };
00073   /* end of resource limits internals docs */
00075 
00087 DBusCounter*
00088 _dbus_counter_new (void)
00089 {
00090   DBusCounter *counter;
00091 
00092   counter = dbus_new0 (DBusCounter, 1);
00093   if (counter == NULL)
00094     return NULL;
00095 
00096   counter->refcount = 1;
00097 
00098   return counter;
00099 }
00100 
00107 DBusCounter *
00108 _dbus_counter_ref (DBusCounter *counter)
00109 {
00110   _dbus_assert (counter->refcount > 0);
00111   
00112   counter->refcount += 1;
00113 
00114   return counter;
00115 }
00116 
00123 void
00124 _dbus_counter_unref (DBusCounter *counter)
00125 {
00126   _dbus_assert (counter->refcount > 0);
00127 
00128   counter->refcount -= 1;
00129 
00130   if (counter->refcount == 0)
00131     {
00132       
00133       dbus_free (counter);
00134     }
00135 }
00136 
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 #ifdef DBUS_ENABLE_STATS
00156   if (counter->peak_size_value < counter->size_value)
00157     counter->peak_size_value = counter->size_value;
00158 #endif
00159 
00160 #if 0
00161   _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
00162                  old, delta, counter->size_value);
00163 #endif
00164 
00165   if (counter->notify_function != NULL &&
00166       ((old < counter->notify_size_guard_value &&
00167         counter->size_value >= counter->notify_size_guard_value) ||
00168        (old >= counter->notify_size_guard_value &&
00169         counter->size_value < counter->notify_size_guard_value)))
00170     counter->notify_pending = TRUE;
00171 }
00172 
00181 void
00182 _dbus_counter_notify (DBusCounter *counter)
00183 {
00184   if (counter->notify_pending)
00185     {
00186       counter->notify_pending = FALSE;
00187       (* counter->notify_function) (counter, counter->notify_data);
00188     }
00189 }
00190 
00201 void
00202 _dbus_counter_adjust_unix_fd (DBusCounter *counter,
00203                               long         delta)
00204 {
00205   long old = counter->unix_fd_value;
00206   
00207   counter->unix_fd_value += delta;
00208 
00209 #ifdef DBUS_ENABLE_STATS
00210   if (counter->peak_unix_fd_value < counter->unix_fd_value)
00211     counter->peak_unix_fd_value = counter->unix_fd_value;
00212 #endif
00213 
00214 #if 0
00215   _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
00216                  old, delta, counter->unix_fd_value);
00217 #endif
00218   
00219   if (counter->notify_function != NULL &&
00220       ((old < counter->notify_unix_fd_guard_value &&
00221         counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
00222        (old >= counter->notify_unix_fd_guard_value &&
00223         counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
00224     counter->notify_pending = TRUE;
00225 }
00226 
00233 long
00234 _dbus_counter_get_size_value (DBusCounter *counter)
00235 {
00236   return counter->size_value;
00237 }
00238 
00245 long
00246 _dbus_counter_get_unix_fd_value (DBusCounter *counter)
00247 {
00248   return counter->unix_fd_value;
00249 }
00250 
00262 void
00263 _dbus_counter_set_notify (DBusCounter               *counter,
00264                           long                       size_guard_value,
00265                           long                       unix_fd_guard_value,
00266                           DBusCounterNotifyFunction  function,
00267                           void                      *user_data)
00268 {
00269   counter->notify_size_guard_value = size_guard_value;
00270   counter->notify_unix_fd_guard_value = unix_fd_guard_value;
00271   counter->notify_function = function;
00272   counter->notify_data = user_data;
00273   counter->notify_pending = FALSE;
00274 }
00275 
00276 #ifdef DBUS_ENABLE_STATS
00277 long
00278 _dbus_counter_get_peak_size_value (DBusCounter *counter)
00279 {
00280   return counter->peak_size_value;
00281 }
00282 
00283 long
00284 _dbus_counter_get_peak_unix_fd_value (DBusCounter *counter)
00285 {
00286   return counter->peak_unix_fd_value;
00287 }
00288 #endif
00289   /* end of resource limits exported API */