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.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* 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;
DBusRMutex *mutex;
};
/* 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;
_dbus_rmutex_new_at_location (&counter->mutex);
if (counter->mutex == NULL)
{
dbus_free (counter);
counter = NULL;
}
return counter;
}
DBusCounter *
_dbus_counter_ref (DBusCounter *counter)
{
_dbus_rmutex_lock (counter->mutex);
_dbus_assert (counter->refcount > 0);
counter->refcount += 1;
_dbus_rmutex_unlock (counter->mutex);
return counter;
}
void
_dbus_counter_unref (DBusCounter *counter)
{
dbus_bool_t last_ref = FALSE;
_dbus_rmutex_lock (counter->mutex);
_dbus_assert (counter->refcount > 0);
counter->refcount -= 1;
last_ref = (counter->refcount == 0);
_dbus_rmutex_unlock (counter->mutex);
if (last_ref)
{
_dbus_rmutex_free_at_location (&counter->mutex);
dbus_free (counter);
}
}
void
_dbus_counter_adjust_size (DBusCounter *counter,
long delta)
{
long old = 0;
_dbus_rmutex_lock (counter->mutex);
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;
_dbus_rmutex_unlock (counter->mutex);
}
void
_dbus_counter_notify (DBusCounter *counter)
{
DBusCounterNotifyFunction notify_function = NULL;
void *notify_data = NULL;
_dbus_rmutex_lock (counter->mutex);
if (counter->notify_pending)
{
counter->notify_pending = FALSE;
notify_function = counter->notify_function;
notify_data = counter->notify_data;
}
_dbus_rmutex_unlock (counter->mutex);
if (notify_function != NULL)
(* notify_function) (counter, notify_data);
}
void
_dbus_counter_adjust_unix_fd (DBusCounter *counter,
long delta)
{
long old = 0;
_dbus_rmutex_lock (counter->mutex);
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;
_dbus_rmutex_unlock (counter->mutex);
}
long
_dbus_counter_get_size_value (DBusCounter *counter)
{
long result;
_dbus_rmutex_lock (counter->mutex);
result = counter->size_value;
_dbus_rmutex_unlock (counter->mutex);
return result;
}
long
_dbus_counter_get_unix_fd_value (DBusCounter *counter)
{
long result;
_dbus_rmutex_lock (counter->mutex);
result = counter->unix_fd_value;
_dbus_rmutex_unlock (counter->mutex);
return result;
}
void
_dbus_counter_set_notify (DBusCounter *counter,
long size_guard_value,
long unix_fd_guard_value,
DBusCounterNotifyFunction function,
void *user_data)
{
_dbus_rmutex_lock (counter->mutex);
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;
_dbus_rmutex_unlock (counter->mutex);
}
#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 */
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
_dbus_counter_set_notify
void _dbus_counter_set_notify(DBusCounter *counter, long size_guard_value, long unix_fd_guard_value, DBusCounterNotifyFunction function, void *user_data)
Sets the notify function for this counter; the notify function is called whenever the counter's value...
Definition dbus-resources.c:313
_dbus_counter_new
DBusCounter * _dbus_counter_new(void)
Creates a new DBusCounter.
Definition dbus-resources.c:91
_dbus_counter_get_unix_fd_value
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
Definition dbus-resources.c:292
_dbus_counter_unref
void _dbus_counter_unref(DBusCounter *counter)
Decrements refcount of the counter and possibly finalizes the counter.
Definition dbus-resources.c:138
_dbus_counter_get_size_value
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.
Definition dbus-resources.c:276
_dbus_counter_adjust_unix_fd
void _dbus_counter_adjust_unix_fd(DBusCounter *counter, long delta)
Adjusts the value of the unix fd counter by the given delta which may be positive or negative.
Definition dbus-resources.c:238
_dbus_counter_notify
void _dbus_counter_notify(DBusCounter *counter)
Calls the notify function from _dbus_counter_set_notify(), if that function has been specified and th...
Definition dbus-resources.c:209
_dbus_counter_ref
DBusCounter * _dbus_counter_ref(DBusCounter *counter)
Increments refcount of the counter.
Definition dbus-resources.c:118
_dbus_counter_adjust_size
void _dbus_counter_adjust_size(DBusCounter *counter, long delta)
Adjusts the value of the size counter by the given delta which may be positive or negative.
Definition dbus-resources.c:169
_dbus_rmutex_new_at_location
void _dbus_rmutex_new_at_location(DBusRMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
Definition dbus-threads.c:56
_dbus_rmutex_unlock
DBUS_PRIVATE_EXPORT void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition dbus-threads.c:153
_dbus_rmutex_free_at_location
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition dbus-threads.c:97
_dbus_rmutex_lock
DBUS_PRIVATE_EXPORT void _dbus_rmutex_lock(DBusRMutex *mutex)
Locks a mutex.
Definition dbus-threads.c:125
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusCounter
Internals of DBusCounter.
Definition dbus-resources.c:57
DBusCounter::refcount
int refcount
reference count
Definition dbus-resources.c:58
DBusCounter::notify_data
void * notify_data
data for notify function
Definition dbus-resources.c:72
DBusCounter::notify_function
DBusCounterNotifyFunction notify_function
notify function
Definition dbus-resources.c:71
DBusCounter::notify_unix_fd_guard_value
long notify_unix_fd_guard_value
call notify function when crossing this unix fd value
Definition dbus-resources.c:69
DBusCounter::notify_pending
dbus_bool_t notify_pending
TRUE if the guard value has been crossed.
Definition dbus-resources.c:73
DBusCounter::size_value
long size_value
current size counter value
Definition dbus-resources.c:60
DBusCounter::mutex
DBusRMutex * mutex
Lock on the entire DBusCounter.
Definition dbus-resources.c:74
DBusCounter::unix_fd_value
long unix_fd_value
current unix fd counter value
Definition dbus-resources.c:61
DBusCounter::notify_size_guard_value
long notify_size_guard_value
call notify function when crossing this size value
Definition dbus-resources.c:68
DBusRMutex
Definition dbus-sysdeps-pthread.c:51