dbus-resources.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-resources.c Resource tracking/limits
*
* Copyright (C) 2003 Red Hat Inc.
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include <dbus/dbus-resources.h>
#include <dbus/dbus-internals.h>
struct DBusCounter
{
int refcount;
long size_value;
long unix_fd_value;
#ifdef DBUS_ENABLE_STATS
long peak_size_value;
long peak_unix_fd_value;
#endif
long notify_size_guard_value;
long notify_unix_fd_guard_value;
DBusCounterNotifyFunction notify_function;
void *notify_data;
dbus_bool_t notify_pending : 1;
};
/* end of resource limits internals docs */
DBusCounter*
_dbus_counter_new (void)
{
DBusCounter *counter;
counter = dbus_new0 (DBusCounter, 1);
if (counter == NULL)
return NULL;
counter->refcount = 1;
return counter;
}
DBusCounter *
_dbus_counter_ref (DBusCounter *counter)
{
_dbus_assert (counter->refcount > 0);
counter->refcount += 1;
return counter;
}
void
_dbus_counter_unref (DBusCounter *counter)
{
_dbus_assert (counter->refcount > 0);
counter->refcount -= 1;
if (counter->refcount == 0)
{
dbus_free (counter);
}
}
void
_dbus_counter_adjust_size (DBusCounter *counter,
long delta)
{
long old = counter->size_value;
counter->size_value += delta;
#ifdef DBUS_ENABLE_STATS
if (counter->peak_size_value < counter->size_value)
counter->peak_size_value = counter->size_value;
#endif
#if 0
_dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
old, delta, counter->size_value);
#endif
if (counter->notify_function != NULL &&
((old < counter->notify_size_guard_value &&
counter->size_value >= counter->notify_size_guard_value) ||
(old >= counter->notify_size_guard_value &&
counter->size_value < counter->notify_size_guard_value)))
counter->notify_pending = TRUE;
}
void
_dbus_counter_notify (DBusCounter *counter)
{
if (counter->notify_pending)
{
counter->notify_pending = FALSE;
(* counter->notify_function) (counter, counter->notify_data);
}
}
void
_dbus_counter_adjust_unix_fd (DBusCounter *counter,
long delta)
{
long old = counter->unix_fd_value;
counter->unix_fd_value += delta;
#ifdef DBUS_ENABLE_STATS
if (counter->peak_unix_fd_value < counter->unix_fd_value)
counter->peak_unix_fd_value = counter->unix_fd_value;
#endif
#if 0
_dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
old, delta, counter->unix_fd_value);
#endif
if (counter->notify_function != NULL &&
((old < counter->notify_unix_fd_guard_value &&
counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
(old >= counter->notify_unix_fd_guard_value &&
counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
counter->notify_pending = TRUE;
}
long
_dbus_counter_get_size_value (DBusCounter *counter)
{
return counter->size_value;
}
long
_dbus_counter_get_unix_fd_value (DBusCounter *counter)
{
return counter->unix_fd_value;
}
void
_dbus_counter_set_notify (DBusCounter *counter,
long size_guard_value,
long unix_fd_guard_value,
DBusCounterNotifyFunction function,
void *user_data)
{
counter->notify_size_guard_value = size_guard_value;
counter->notify_unix_fd_guard_value = unix_fd_guard_value;
counter->notify_function = function;
counter->notify_data = user_data;
counter->notify_pending = FALSE;
}
#ifdef DBUS_ENABLE_STATS
long
_dbus_counter_get_peak_size_value (DBusCounter *counter)
{
return counter->peak_size_value;
}
long
_dbus_counter_get_peak_unix_fd_value (DBusCounter *counter)
{
return counter->peak_unix_fd_value;
}
#endif
/* end of resource limits exported API */