~ubuntu-branches/ubuntu/trusty/geis/trusty

« back to all changes in this revision

Viewing changes to libgeis/server/geis_dbus_proxy_box.c

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis_dbus_proxy_box.c
 
3
 * @brief Implementation of a storage facility for GEIS DBus server client
 
4
 * proxies.
 
5
 */
 
6
 
 
7
/*
 
8
 * Copyright 2011 Canonical Ltd.
 
9
 *
 
10
 * This library is free software; you can redistribute it and/or modify it under
 
11
 * the terms of the GNU Lesser General Public License as published by the Free
 
12
 * Software Foundation; either version 3 of the License, or (at your option) any
 
13
 * later version.
 
14
 *
 
15
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
16
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
18
 * details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
 
23
#include "geis_config.h"
 
24
#include "geis_dbus_proxy_box.h"
 
25
 
 
26
#include <assert.h>
 
27
#include "geis_logging.h"
 
28
 
 
29
 
 
30
typedef struct GeisDBusProxyBoxNode *GeisDBusProxyBoxNode;
 
31
 
 
32
struct GeisDBusProxyBoxNode 
 
33
{
 
34
  GeisDBusClientProxy  proxy;
 
35
  GeisDBusProxyBoxNode next;
 
36
};
 
37
 
 
38
 
 
39
/**
 
40
 * The box is represented using a circular linked list with a free pool.
 
41
 */
 
42
struct GeisDBusProxyBox 
 
43
{
 
44
  GeisDBusProxyBoxNode head;
 
45
  GeisDBusProxyBoxNode avail;
 
46
};
 
47
 
 
48
 
 
49
/*
 
50
 * Constructs a %GeisDBusProxyBox.
 
51
 */
 
52
GeisDBusProxyBox
 
53
geis_dbus_proxy_box_new()
 
54
{
 
55
  GeisDBusProxyBox box = calloc(1, sizeof(struct GeisDBusProxyBox));
 
56
  if (!box)
 
57
  {
 
58
    geis_error("error allocating GeisDBusProxyBox");
 
59
  }
 
60
 
 
61
  return box;
 
62
}
 
63
 
 
64
 
 
65
/*
 
66
 * Destroys a %GeisDBusProxyBox.
 
67
 *
 
68
 * Assumes all contained client proxies have already been freed, otherwise there
 
69
 * will be resource leaks because this container does not know how to free them.
 
70
 */
 
71
void
 
72
geis_dbus_proxy_box_delete(GeisDBusProxyBox box)
 
73
{
 
74
  GeisDBusProxyBoxNode p = box->avail;
 
75
  while (p)
 
76
  {
 
77
    box->avail = p->next;
 
78
    free(p);
 
79
    p = box->avail;
 
80
  }
 
81
  free(box);
 
82
}
 
83
 
 
84
 
 
85
/*
 
86
 * Inserts a %GeisDBusClientProxy in to a %GeisDBusProxyBox.
 
87
 */
 
88
void
 
89
geis_dbus_proxy_box_insert(GeisDBusProxyBox    box,
 
90
                           GeisDBusClientProxy proxy)
 
91
{
 
92
  if (box->avail == NULL)
 
93
  {
 
94
    box->avail = calloc(1, sizeof(struct GeisDBusProxyBoxNode));
 
95
    if (!box->avail)
 
96
    {
 
97
      geis_error("error allocating proxy box node");
 
98
      return;
 
99
    }
 
100
  }
 
101
 
 
102
  GeisDBusProxyBoxNode p = box->avail;
 
103
  p->proxy = proxy;
 
104
  box->avail = p->next;
 
105
 
 
106
  if (box->head == NULL)
 
107
  {
 
108
    box->head = p;
 
109
  }
 
110
  else
 
111
  {
 
112
    p->next = box->head->next;
 
113
  }
 
114
  box->head->next = p;
 
115
}
 
116
 
 
117
 
 
118
/*
 
119
 * Removes a %GeisDBusClientProxy from a %GeisDBusProxyBox.
 
120
 */
 
121
void
 
122
geis_dbus_proxy_box_remove(GeisDBusProxyBox    box,
 
123
                           GeisDBusClientProxy proxy)
 
124
{
 
125
  assert(box->head != NULL);
 
126
 
 
127
  GeisDBusProxyBoxNode ptr = box->head;
 
128
  GeisDBusProxyBoxNode p = box->head->next;
 
129
  do
 
130
  {
 
131
    if (p->proxy == proxy)
 
132
    {
 
133
      if (ptr == p)
 
134
      {
 
135
        box->head = NULL;
 
136
      }
 
137
      else
 
138
      {
 
139
        ptr->next = p->next;
 
140
        if (box->head == p)
 
141
          box->head = ptr;
 
142
      }
 
143
      p->next = box->avail;
 
144
      box->avail = p;
 
145
      break;
 
146
    }
 
147
    ptr = ptr->next;
 
148
    p = p->next;
 
149
  } while (ptr != box->head);
 
150
}
 
151
 
 
152
 
 
153
/*
 
154
 * Gets an iterator initialized to the first client proxy in a box.
 
155
 */
 
156
GeisDBusProxyBoxIterator
 
157
geis_dbus_proxy_box_begin(GeisDBusProxyBox box)
 
158
{
 
159
  if (box->head)
 
160
    return box->head->next;
 
161
  return box->head;
 
162
}
 
163
 
 
164
 
 
165
/*
 
166
 * Gets an iterator initialized to one-past-the-end of a client proxy box.
 
167
 */
 
168
GeisDBusProxyBoxIterator
 
169
geis_dbus_proxy_box_end(GeisDBusProxyBox box GEIS_UNUSED)
 
170
{
 
171
  return NULL;
 
172
}
 
173
 
 
174
 
 
175
/*
 
176
 * Gets the current client proxy from a client proxy box iterator.
 
177
 */
 
178
GeisDBusClientProxy
 
179
geis_dbus_proxy_box_iter_value(GeisDBusProxyBoxIterator iter)
 
180
{
 
181
  return iter->proxy;
 
182
}
 
183
 
 
184
 
 
185
/*
 
186
 * Advances a client proxy box iterator.
 
187
 */
 
188
GeisDBusProxyBoxIterator
 
189
geis_dbus_proxy_box_iter_next(GeisDBusProxyBox         box,
 
190
                              GeisDBusProxyBoxIterator iter)
 
191
{
 
192
  if (iter == box->head)
 
193
     return geis_dbus_proxy_box_end(box);
 
194
  else
 
195
    return iter->next;
 
196
}
 
197
 
 
198
 
 
199