summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-resources.c
blob: 7c7aabd0c68d4edbde01f7b4e9b35426b7a95b67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- 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 <dbus/dbus-resources.h>
#include <dbus/dbus-internals.h>

/**
 * @defgroup DBusResources Resource limits related code
 * @ingroup  DBusInternals
 * @brief DBusCounter and other stuff related to resource limits
 *
 * Types and functions related to tracking resource limits,
 * such as the maximum amount of memory/unix fds a connection can use
 * for messages, etc.
 */

/**
 * @defgroup DBusResourcesInternals Resource limits implementation details
 * @ingroup  DBusInternals
 * @brief Resource limits implementation details
 *
 * Implementation details of resource limits code.
 *
 * @{
 */

/**
 * @brief Internals of DBusCounter.
 * 
 * DBusCounter internals. DBusCounter is an opaque object, it must be
 * used via accessor functions.
 */
struct DBusCounter
{
  int refcount;  /**< reference count */

  long size_value;       /**< current size counter value */
  long unix_fd_value;    /**< current unix fd counter value */

  long notify_size_guard_value;    /**< call notify function when crossing this size value */
  long notify_unix_fd_guard_value; /**< call notify function when crossing this unix fd value */

  DBusCounterNotifyFunction notify_function; /**< notify function */
  void *notify_data; /**< data for notify function */
};

/** @} */  /* end of resource limits internals docs */

/**
 * @addtogroup DBusResources
 * @{
 */

/**
 * Creates a new DBusCounter. DBusCounter is used
 * to count usage of some resource such as memory.
 *
 * @returns new counter or #NULL on failure
 */
DBusCounter*
_dbus_counter_new (void)
{
  DBusCounter *counter;

  counter = dbus_new (DBusCounter, 1);
  if (counter == NULL)
    return NULL;
  
  counter->refcount = 1;
  counter->size_value = 0;
  counter->unix_fd_value = 0;

  counter->notify_size_guard_value = 0;
  counter->notify_unix_fd_guard_value = 0;
  counter->notify_function = NULL;
  counter->notify_data = NULL;
  
  return counter;
}

/**
 * Increments refcount of the counter
 *
 * @param counter the counter
 * @returns the counter
 */
DBusCounter *
_dbus_counter_ref (DBusCounter *counter)
{
  _dbus_assert (counter->refcount > 0);
  
  counter->refcount += 1;

  return counter;
}

/**
 * Decrements refcount of the counter and possibly
 * finalizes the counter.
 *
 * @param counter the counter
 */
void
_dbus_counter_unref (DBusCounter *counter)
{
  _dbus_assert (counter->refcount > 0);

  counter->refcount -= 1;

  if (counter->refcount == 0)
    {
      
      dbus_free (counter);
    }
}

/**
 * Adjusts the value of the size counter by the given
 * delta which may be positive or negative.
 * Calls the notify function from _dbus_counter_set_notify()
 * if that function has been specified.
 *
 * @param counter the counter
 * @param delta value to add to the size counter's current value
 */
void
_dbus_counter_adjust_size (DBusCounter *counter,
                           long         delta)
{
  long old = counter->size_value;

  counter->size_value += delta;

#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_function) (counter, counter->notify_data);
}

/**
 * Adjusts the value of the unix fd counter by the given
 * delta which may be positive or negative.
 * Calls the notify function from _dbus_counter_set_notify()
 * if that function has been specified.
 *
 * @param counter the counter
 * @param delta value to add to the unix fds counter's current value
 */
void
_dbus_counter_adjust_unix_fd (DBusCounter *counter,
                              long         delta)
{
  long old = counter->unix_fd_value;
  
  counter->unix_fd_value += delta;

#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_function) (counter, counter->notify_data);
}

/**
 * Gets the current value of the size counter.
 *
 * @param counter the counter
 * @returns its current size value
 */
long
_dbus_counter_get_size_value (DBusCounter *counter)
{
  return counter->size_value;
}

/**
 * Gets the current value of the unix fd counter.
 *
 * @param counter the counter
 * @returns its current unix fd value
 */
long
_dbus_counter_get_unix_fd_value (DBusCounter *counter)
{
  return counter->unix_fd_value;
}

/**
 * Sets the notify function for this counter; the notify function is
 * called whenever the counter's values cross the guard values in
 * either direction (moving up, or moving down).
 *
 * @param counter the counter
 * @param size_guard_value the value we're notified if the size counter crosses
 * @param unix_fd_guard_value the value we're notified if the unix fd counter crosses
 * @param function function to call in order to notify
 * @param user_data data to pass to the function
 */
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;
}

/** @} */  /* end of resource limits exported API */