File: Dkresource.c

package info (click to toggle)
virtuoso-opensource 6.1.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 260,060 kB
  • ctags: 123,765
  • sloc: ansic: 652,532; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (313 lines) | stat: -rw-r--r-- 7,322 bytes parent folder | download | duplicates (2)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 *  Dkresource.c
 *
 *  $Id$
 *
 *  Resource management
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project 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; only version 2 of the License, dated June 1991.
 *
 *  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 St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "Dk.h"


#ifdef MALLOC_DEBUG
#define RC_DBG
#endif


/*##**********************************************************************
 *
 *              resource_allocate
 *
 * Allocates a resource of a given capacity. A resource is a stack-like
 * structure of fixed capacity. resource_get returns an element stored
 * in a resource if any. resource_store adds an element to the resource
 * if there is room.
 *
 * Input params :        - Capacity - how many items can be stored.
 *                       - The function for creating new entries for
 *                         the resource. if not null used by resource_get
 *                         when there are no items in the resource.
 *                       - destructor. Called on items that do not fit in the
 *                         resource in resource_store.
 *                       - clear_func. Called on stored items in
 *                         resource_store before placing on the resource pool.
 *                       - client_data - Argument to the constructor function
 *                         called in resource_get.
 *
 *
 * Output params:    - none
 *
 * Return value :    The resource object.
 *
 * Limitations  :
 *
 * Globals used :
 */
resource_t *
resource_allocate (uint32 sz, rc_constr_t constructor, rc_destr_t destructor, rc_destr_t clear_func, void *client_data)
{
  resource_t *rc;

  rc = (resource_t *) malloc (sizeof (resource_t));
  /* use malloc so as to be usable inside the dk_alloc cache system */
  memset (rc, 0, sizeof (resource_t));
  rc->rc_items = (void **) malloc (sizeof (void *) * sz);
  rc->rc_fill = 0;
  rc->rc_size = sz;
  rc->rc_constructor = constructor;
  rc->rc_destructor = destructor;
  rc->rc_clear_func = clear_func;
  rc->rc_client_data = client_data;

  rc->rc_gets = 0;
  rc->rc_stores = 0;

  rc->rc_mtx = mutex_allocate ();
  return (rc);
}


resource_t *
resource_allocate_primitive (uint32 sz, int max_sz)
{
  resource_t *rc;

  rc = (resource_t *) malloc (sizeof (resource_t));
  memset (rc, 0, sizeof (resource_t));
  rc->rc_items = (void **) malloc (sizeof (void *) * sz);
  rc->rc_fill = 0;
  rc->rc_size = sz;
  rc->rc_max_size = max_sz;
  return (rc);
}


void
resource_no_sem (resource_t * rc)
{
  if (rc->rc_mtx)
    {
      mutex_free (rc->rc_mtx);
      rc->rc_mtx = NULL;
    }
}


void
_resource_adjust (resource_t * rc)
{
  /* if there are over 5% underflows and the resource overflows at least half as
   * frequently as it underflows and it's not over max size, expand */

  if (rc->rc_fill)
    GPF_T1 ("can only adjust empty rc's");
  if (rc->rc_size >= rc->rc_max_size)
    return;
  if (rc->rc_gets > 10000000 || rc->rc_n_empty > rc->rc_gets)
    {
      rc->rc_gets = 0;
      rc->rc_stores = 0;
      rc->rc_n_empty = 0;
      rc->rc_n_full = 0;
      return;
    }
  if (rc->rc_n_empty > rc->rc_gets / 20 && rc->rc_n_full > rc->rc_n_empty / 2)
    {
      void **arr = (void **) malloc (rc->rc_size * 2 * sizeof (void *));
      rc->rc_size = rc->rc_size * 2;
      free ((void *) rc->rc_items);
      rc->rc_items = arr;
      rc->rc_gets = 0;
      rc->rc_stores = 0;
      rc->rc_n_empty = 0;
      rc->rc_n_full = 0;
    }
}


/*##**********************************************************************
 *
 *              resource_get
 *
 * returns an item from a resource. If there are no items, and no constructor
 * is specified this returns NULL. If a constructor is specified and there are
 * no items the constructor is applied to the client data and the result
 * is returned.
 *
 *
 * Input params :        - The resource
 *
 * Output params:    - none
 *
 * Return value :    The item in the resource or NULL.
 *
 * Limitations  :
 *
 * Globals used :
 */
void *
resource_get_1 (resource_t * rc, int construct_new)
{
  dk_mutex_t *rc_mtx = rc->rc_mtx;

  if (rc_mtx)
    {
      mutex_enter (rc_mtx);

      ++rc->rc_gets;
      if (rc->rc_fill)
	{
	  void *val;

	  val = (rc->rc_items[--(rc->rc_fill)]);

	  mutex_leave (rc_mtx);
	  return val;
	}
      else
	{
	  void *data;
	  if (++rc->rc_n_empty % 1000 == 0)
	    _resource_adjust (rc);
	  mutex_leave (rc_mtx);
	  if (rc->rc_constructor && construct_new)
	    data = (*rc->rc_constructor) (rc->rc_client_data);
	  else
	    data = NULL;
	  return data;
	}
    }
  else
    {
      ++rc->rc_gets;
      if (rc->rc_fill)
	{
	  void *val;

	  val = (rc->rc_items[--(rc->rc_fill)]);

	  return val;
	}
      else
	{
	  void *data;
	  if (++rc->rc_n_empty % 1000 == 0)
	    _resource_adjust (rc);
	  if (rc->rc_constructor && construct_new)
	    data = (*rc->rc_constructor) (rc->rc_client_data);
	  else
	    data = NULL;

	  return data;
	}
    }
}


void *
resource_get (resource_t * rc)
{
  return resource_get_1 (rc, 1);
}


/*##**********************************************************************
 *
 *              resource_store
 *
 *
 * Adds an element to a resource. If the resource is full and a destructor
 * is specified this applies the destructor to the item being stored.
 * if there is room in the resource and a clear_func is specified the clear
 * function is called first.
 *
 * Input params :        - resource, element.
 *
 *
 * Output params:    - none
 *
 * Return value :    true if the element was added,
 *                   false if the resource was full.
 *
 * Limitations  :
 *
 * Globals used :
 */

int
resource_store (resource_t * rc, void *item)
{
  dk_mutex_t *rc_mtx = rc->rc_mtx;
  if (rc_mtx)
    mutex_enter (rc_mtx);

#ifdef RC_DBG
  {
    uint32 inx;
    for (inx = 0; inx < rc->rc_fill; inx++)
      if (item == rc->rc_items[inx])
	{
	  GPF_T1 ("Duplicate resource free.");
	}
  }
#endif /* RC_DBG */

  rc->rc_stores++;
  if (rc->rc_fill < rc->rc_size)
    {
      if (rc->rc_clear_func)
	(*rc->rc_clear_func) (item);

      rc->rc_items[(rc->rc_fill)++] = item;
      if (rc_mtx)
	mutex_leave (rc_mtx);
      return 1;
    }
  else
    {
      rc->rc_n_full++;
      if (rc_mtx)
	mutex_leave (rc_mtx);
      if (rc->rc_destructor)
	(*rc->rc_destructor) (item);
      return 0;
    }
}


unsigned long
resource_clear (resource_t * rc, rc_destr_t destruct)
{
  unsigned long cnt = 0;
  void *data = NULL;
  if (!destruct && !rc->rc_destructor)
    GPF_T1 ("No destructor for a resource");

  if (!destruct)
    destruct = rc->rc_destructor;
  while (NULL != (data = resource_get_1 (rc, 0)))
    {
      destruct (data);
      cnt++;
    }
  return cnt;
}