00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 dbus_bool_t notify_pending : 1;
00067 };
00068
00070
00082 DBusCounter*
00083 _dbus_counter_new (void)
00084 {
00085 DBusCounter *counter;
00086
00087 counter = dbus_new (DBusCounter, 1);
00088 if (counter == NULL)
00089 return NULL;
00090
00091 counter->refcount = 1;
00092 counter->size_value = 0;
00093 counter->unix_fd_value = 0;
00094
00095 counter->notify_size_guard_value = 0;
00096 counter->notify_unix_fd_guard_value = 0;
00097 counter->notify_function = NULL;
00098 counter->notify_data = NULL;
00099 counter->notify_pending = FALSE;
00100
00101 return counter;
00102 }
00103
00110 DBusCounter *
00111 _dbus_counter_ref (DBusCounter *counter)
00112 {
00113 _dbus_assert (counter->refcount > 0);
00114
00115 counter->refcount += 1;
00116
00117 return counter;
00118 }
00119
00126 void
00127 _dbus_counter_unref (DBusCounter *counter)
00128 {
00129 _dbus_assert (counter->refcount > 0);
00130
00131 counter->refcount -= 1;
00132
00133 if (counter->refcount == 0)
00134 {
00135
00136 dbus_free (counter);
00137 }
00138 }
00139
00150 void
00151 _dbus_counter_adjust_size (DBusCounter *counter,
00152 long delta)
00153 {
00154 long old = counter->size_value;
00155
00156 counter->size_value += delta;
00157
00158 #if 0
00159 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
00160 old, delta, counter->size_value);
00161 #endif
00162
00163 if (counter->notify_function != NULL &&
00164 ((old < counter->notify_size_guard_value &&
00165 counter->size_value >= counter->notify_size_guard_value) ||
00166 (old >= counter->notify_size_guard_value &&
00167 counter->size_value < counter->notify_size_guard_value)))
00168 counter->notify_pending = TRUE;
00169 }
00170
00179 void
00180 _dbus_counter_notify (DBusCounter *counter)
00181 {
00182 if (counter->notify_pending)
00183 {
00184 counter->notify_pending = FALSE;
00185 (* counter->notify_function) (counter, counter->notify_data);
00186 }
00187 }
00188
00199 void
00200 _dbus_counter_adjust_unix_fd (DBusCounter *counter,
00201 long delta)
00202 {
00203 long old = counter->unix_fd_value;
00204
00205 counter->unix_fd_value += delta;
00206
00207 #if 0
00208 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
00209 old, delta, counter->unix_fd_value);
00210 #endif
00211
00212 if (counter->notify_function != NULL &&
00213 ((old < counter->notify_unix_fd_guard_value &&
00214 counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
00215 (old >= counter->notify_unix_fd_guard_value &&
00216 counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
00217 counter->notify_pending = TRUE;
00218 }
00219
00226 long
00227 _dbus_counter_get_size_value (DBusCounter *counter)
00228 {
00229 return counter->size_value;
00230 }
00231
00238 long
00239 _dbus_counter_get_unix_fd_value (DBusCounter *counter)
00240 {
00241 return counter->unix_fd_value;
00242 }
00243
00255 void
00256 _dbus_counter_set_notify (DBusCounter *counter,
00257 long size_guard_value,
00258 long unix_fd_guard_value,
00259 DBusCounterNotifyFunction function,
00260 void *user_data)
00261 {
00262 counter->notify_size_guard_value = size_guard_value;
00263 counter->notify_unix_fd_guard_value = unix_fd_guard_value;
00264 counter->notify_function = function;
00265 counter->notify_data = user_data;
00266 counter->notify_pending = FALSE;
00267 }
00268