D-Bus  1.5.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_new (DBusCounter, 1);
00093   if (counter == NULL)
00094     return NULL;
00095   
00096   counter->refcount = 1;
00097   counter->size_value = 0;
00098   counter->unix_fd_value = 0;
00099 
00100 #ifdef DBUS_ENABLE_STATS
00101   counter->peak_size_value = 0;
00102   counter->peak_unix_fd_value = 0;
00103 #endif
00104 
00105   counter->notify_size_guard_value = 0;
00106   counter->notify_unix_fd_guard_value = 0;
00107   counter->notify_function = NULL;
00108   counter->notify_data = NULL;
00109   counter->notify_pending = FALSE;
00110   
00111   return counter;
00112 }
00113 
00120 DBusCounter *
00121 _dbus_counter_ref (DBusCounter *counter)
00122 {
00123   _dbus_assert (counter->refcount > 0);
00124   
00125   counter->refcount += 1;
00126 
00127   return counter;
00128 }
00129 
00136 void
00137 _dbus_counter_unref (DBusCounter *counter)
00138 {
00139   _dbus_assert (counter->refcount > 0);
00140 
00141   counter->refcount -= 1;
00142 
00143   if (counter->refcount == 0)
00144     {
00145       
00146       dbus_free (counter);
00147     }
00148 }
00149 
00160 void
00161 _dbus_counter_adjust_size (DBusCounter *counter,
00162                            long         delta)
00163 {
00164   long old = counter->size_value;
00165 
00166   counter->size_value += delta;
00167 
00168 #ifdef DBUS_ENABLE_STATS
00169   if (counter->peak_size_value < counter->size_value)
00170     counter->peak_size_value = counter->size_value;
00171 #endif
00172 
00173 #if 0
00174   _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
00175                  old, delta, counter->size_value);
00176 #endif
00177 
00178   if (counter->notify_function != NULL &&
00179       ((old < counter->notify_size_guard_value &&
00180         counter->size_value >= counter->notify_size_guard_value) ||
00181        (old >= counter->notify_size_guard_value &&
00182         counter->size_value < counter->notify_size_guard_value)))
00183     counter->notify_pending = TRUE;
00184 }
00185 
00194 void
00195 _dbus_counter_notify (DBusCounter *counter)
00196 {
00197   if (counter->notify_pending)
00198     {
00199       counter->notify_pending = FALSE;
00200       (* counter->notify_function) (counter, counter->notify_data);
00201     }
00202 }
00203 
00214 void
00215 _dbus_counter_adjust_unix_fd (DBusCounter *counter,
00216                               long         delta)
00217 {
00218   long old = counter->unix_fd_value;
00219   
00220   counter->unix_fd_value += delta;
00221 
00222 #ifdef DBUS_ENABLE_STATS
00223   if (counter->peak_unix_fd_value < counter->unix_fd_value)
00224     counter->peak_unix_fd_value = counter->unix_fd_value;
00225 #endif
00226 
00227 #if 0
00228   _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
00229                  old, delta, counter->unix_fd_value);
00230 #endif
00231   
00232   if (counter->notify_function != NULL &&
00233       ((old < counter->notify_unix_fd_guard_value &&
00234         counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
00235        (old >= counter->notify_unix_fd_guard_value &&
00236         counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
00237     counter->notify_pending = TRUE;
00238 }
00239 
00246 long
00247 _dbus_counter_get_size_value (DBusCounter *counter)
00248 {
00249   return counter->size_value;
00250 }
00251 
00258 long
00259 _dbus_counter_get_unix_fd_value (DBusCounter *counter)
00260 {
00261   return counter->unix_fd_value;
00262 }
00263 
00275 void
00276 _dbus_counter_set_notify (DBusCounter               *counter,
00277                           long                       size_guard_value,
00278                           long                       unix_fd_guard_value,
00279                           DBusCounterNotifyFunction  function,
00280                           void                      *user_data)
00281 {
00282   counter->notify_size_guard_value = size_guard_value;
00283   counter->notify_unix_fd_guard_value = unix_fd_guard_value;
00284   counter->notify_function = function;
00285   counter->notify_data = user_data;
00286   counter->notify_pending = FALSE;
00287 }
00288 
00289 #ifdef DBUS_ENABLE_STATS
00290 long
00291 _dbus_counter_get_peak_size_value (DBusCounter *counter)
00292 {
00293   return counter->peak_size_value;
00294 }
00295 
00296 long
00297 _dbus_counter_get_peak_unix_fd_value (DBusCounter *counter)
00298 {
00299   return counter->peak_unix_fd_value;
00300 }
00301 #endif
00302   /* end of resource limits exported API */