File: registry.c

package info (click to toggle)
dynamips 0.2.7-0.2.8RC2-5.1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 4,184 kB
  • sloc: ansic: 70,972; makefile: 1,639; perl: 20
file content (397 lines) | stat: -rw-r--r-- 9,254 bytes parent folder | download | duplicates (3)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * IPFlow Collector
 * Copyright (c) 2003 Christophe Fillot.
 * E-mail: cf@utc.fr
 *
 * registry.c: Object Registry.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>

#include "utils.h"
#include "hash.h"
#include "mempool.h"
#include "registry.h"

#define DEBUG_REGISTRY  0

static registry_t *registry = NULL;

#define REGISTRY_LOCK()    pthread_mutex_lock(&registry->lock)
#define REGISTRY_UNLOCK()  pthread_mutex_unlock(&registry->lock)

/* Initialize registry */
int registry_init(void)
{
   registry_entry_t *p;
   pthread_mutexattr_t attr;
   size_t len;
   int i;

   registry = malloc(sizeof(*registry));
   assert(registry != NULL);

   pthread_mutexattr_init(&attr);
   pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
   pthread_mutex_init(&registry->lock,&attr);

   /* initialize registry memory pool */
   mp_create_fixed_pool(&registry->mp,"registry");

   registry->ht_name_entries = REGISTRY_HT_NAME_ENTRIES;
   registry->ht_type_entries = REGISTRY_MAX_TYPES;

   /* initialize hash table for names, with sentinels */
   len = registry->ht_name_entries * sizeof(registry_entry_t);
   registry->ht_names = mp_alloc(&registry->mp,len);
   assert(registry->ht_names != NULL);

   for(i=0;i<registry->ht_name_entries;i++) {
      p = &registry->ht_names[i];
      p->hname_next = p->hname_prev = p;
   }

   /* initialize hash table for types, with sentinels */
   len = registry->ht_type_entries * sizeof(registry_entry_t);
   registry->ht_types = mp_alloc(&registry->mp,len);
   assert(registry->ht_types != NULL);

   for(i=0;i<registry->ht_type_entries;i++) {
      p = &registry->ht_types[i];
      p->htype_next = p->htype_prev = p;
   }

   return(0);
}

/* Insert a new entry */
static void registry_insert_entry(registry_entry_t *entry)
{      
   registry_entry_t *bucket;
   u_int h_index;

   /* insert new entry in hash table for names */
   h_index = str_hash(entry->name) % registry->ht_name_entries;
   bucket = &registry->ht_names[h_index];

   entry->hname_next = bucket->hname_next;
   entry->hname_prev = bucket;
   bucket->hname_next->hname_prev = entry;
   bucket->hname_next = entry;

   /* insert new entry in hash table for object types */
   bucket = &registry->ht_types[entry->object_type];

   entry->htype_next = bucket->htype_next;
   entry->htype_prev = bucket;
   bucket->htype_next->htype_prev = entry;
   bucket->htype_next = entry;
}

/* Remove a registry entry */
void registry_remove_entry(registry_entry_t *entry)
{
   entry->hname_prev->hname_next = entry->hname_next;
   entry->hname_next->hname_prev = entry->hname_prev;

   entry->htype_prev->htype_next = entry->htype_next;
   entry->htype_next->htype_prev = entry->htype_prev;
   
   mp_free(entry);
}

/* Locate an entry */
static inline registry_entry_t *registry_find_entry(char *name,int object_type)
{
   registry_entry_t *entry,*bucket;
   u_int h_index;

   h_index = str_hash(name) % registry->ht_name_entries;
   bucket = &registry->ht_names[h_index];

   for(entry=bucket->hname_next;entry!=bucket;entry=entry->hname_next)
      if (!strcmp(entry->name,name) && (entry->object_type == object_type))
         return entry;

   return NULL;
}

/* Add a new entry to the registry */
int registry_add(char *name,int object_type,void *data)
{
   registry_entry_t *entry;

   if (!name) 
      return(-1);

   REGISTRY_LOCK();

   /* check if we have already a reference for this name */
   if ((entry = registry_find_entry(name,object_type))) {
      REGISTRY_UNLOCK();
      return(-1);
   }

   /* create a new entry */
   if (!(entry = mp_alloc(&registry->mp,sizeof(*entry)))) {
      REGISTRY_UNLOCK();
      return(-1);
   }

   entry->name = name;
   entry->data = data;
   entry->object_type = object_type;
   entry->ref_count = 1;   /* consider object is referenced by the caller */
   registry_insert_entry(entry);

   REGISTRY_UNLOCK();
   return(0);
}

/* Delete an entry from the registry */
int registry_delete(char *name,int object_type)
{
   registry_entry_t *entry;

   if (!name) return(-1);

   REGISTRY_LOCK();

   if (!(entry = registry_find_entry(name,object_type))) {
      REGISTRY_UNLOCK();
      return(-1);
   }

   /* if the entry is referenced, just decrement ref counter */
   if (--entry->ref_count > 0) {
      REGISTRY_UNLOCK();
      return(0);
   }

   registry_remove_entry(entry);
   REGISTRY_UNLOCK();
   return(0);
}

/* Find an entry (increment the reference count) */
void *registry_find(char *name,int object_type)
{
   registry_entry_t *entry;
   void *data;

   if (!name) return NULL;

   REGISTRY_LOCK();

   if ((entry = registry_find_entry(name,object_type))) {
      entry->ref_count++;
      data = entry->data;
   } else
      data = NULL;

   REGISTRY_UNLOCK();
   return data;
}

/* Check if entry exists (does not change reference count) */
void *registry_exists(char *name,int object_type)
{
   registry_entry_t *entry;
   void *data = NULL;
   
   if (!name) 
      return NULL;

   REGISTRY_LOCK();
   entry = registry_find_entry(name,object_type);
   if (entry) 
      data = entry->data;
   REGISTRY_UNLOCK();
   return data;
}

/* Release a reference of an entry (decrement the reference count) */
int registry_unref(char *name,int object_type)
{
   registry_entry_t *entry;
   int res = -1;

   if (!name) return(-1);

   REGISTRY_LOCK();

   if ((entry = registry_find_entry(name,object_type)))
   {
      entry->ref_count--;

#if DEBUG_REGISTRY
      printf("Registry: object %s: ref_count = %d after unref.\n",
             name, entry->ref_count);
#endif

      if (entry->ref_count < 0) {
         fprintf(stderr,"Registry: object %s (type %d): negative ref_count.\n",
                 name, object_type);
      } else
         res = 0;
   }

   REGISTRY_UNLOCK();
   return(res);
}

/* 
 * Execute action on an object if its reference count is less or equal to
 * the specified count.
 */
int registry_exec_refcount(char *name,int object_type,int max_ref,int reg_del,
                           registry_exec obj_action,void *opt_arg)
{
   registry_entry_t *entry;
   int res = -1;
   int status;

   if (!name) return(-1);

   REGISTRY_LOCK();

   entry = registry_find_entry(name,object_type);

   if (entry) 
   {
      if (entry->ref_count <= max_ref)
      {
         status = TRUE;

         if (obj_action != NULL)
            status = obj_action(entry->data,opt_arg);

         if (reg_del && status)
            registry_remove_entry(entry);

         res = 1;
      } else
         res = 0;
   }

   REGISTRY_UNLOCK();
   return(res);
}

/* Delete object if unused */
int registry_delete_if_unused(char *name,int object_type,
                              registry_exec obj_destructor,void *opt_arg)
{
   return(registry_exec_refcount(name,object_type,0,TRUE,
                                 obj_destructor,opt_arg));
}

/* Execute a callback function for all objects of specified type */
int registry_foreach_type(int object_type,registry_foreach cb,
                          void *opt,int *err)
{
   registry_entry_t *p,*bucket,*next;
   int count = 0;

   REGISTRY_LOCK();

   bucket = &registry->ht_types[object_type];

   for(p=bucket->htype_next;p!=bucket;p=next) {
      next = p->htype_next;
      if (cb) cb(p,opt,err);
      count++;
   }

   REGISTRY_UNLOCK();
   return(count);
}

/* Delete all objects of the specified type */
int registry_delete_type(int object_type,registry_exec cb,void *opt)
{
   registry_entry_t *p,*bucket,*next;
   int count = 0;
   int status;

   REGISTRY_LOCK();

   bucket = &registry->ht_types[object_type];

   for(p=bucket->htype_next;p!=bucket;p=next) {
      next = p->htype_next;

      if (p->ref_count == 0) {
         status = TRUE;
         
         if (cb != NULL) 
            status = cb(p->data,opt);

         if (status) {
            registry_remove_entry(p);
            count++;
         }
      } else {
         fprintf(stderr,"registry_delete_type: object \"%s\" (type %d) still "
                 "referenced (count=%d)\n",p->name,object_type,p->ref_count);
      }
   }

   REGISTRY_UNLOCK();
   return(count);
}

/* Dump the registry */
void registry_dump(void)
{
   registry_entry_t *p,*bucket;
   int i;

   REGISTRY_LOCK();

   printf("Registry dump:\n");

   printf("  Objects (from name hash table):\n");

   /* dump hash table of names */
   for(i=0;i<registry->ht_name_entries;i++)
   {
      bucket = &registry->ht_names[i];

      for(p=bucket->hname_next;p!=bucket;p=p->hname_next)
         printf("     %s (type %d, ref_count=%d)\n",
                p->name,p->object_type,p->ref_count);
   }

   printf("\n  Objects classed by types:\n");

   /* dump hash table of types */
   for(i=0;i<registry->ht_type_entries;i++)
   {         
      printf("     Type %d: ",i);

      bucket = &registry->ht_types[i];
      for(p=bucket->htype_next;p!=bucket;p=p->htype_next)
         printf("%s(%d) ",p->name,p->ref_count);
         
      printf("\n");
   }

   REGISTRY_UNLOCK();
}