"Fossies" - the Fresh Open Source Software Archive 
Member "FunctionCheck-3.2.0/src/fcmanager/fc_hash.c" (26 May 2012, 3189 Bytes) of package /linux/privat/old/FunctionCheck-3.2.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * FunctionCheck profiler
3 * (C) Copyright 2000-2012 Yannick Perret
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /** fc_hash.c: manage hash tables for functions **/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include "fc_global.h"
26 #include "fc_hash.h"
27 #include "fc_tools.h"
28
29 /* create a hash table with initial size */
30
31 /* size MUST be a power of 2 (not checked) */
32 FC_FHash *fc_fhash_create(int size)
33 {
34 FC_FHash *tmp;
35 int i;
36
37 tmp = malloc(sizeof (FC_FHash));
38 if (tmp == NULL)
39 {
40 fc_message("cannot allocate %d bytes for a hash table.", sizeof (FC_FHash));
41 return (NULL);
42 }
43
44 /* create the elements */
45 tmp->functions = malloc(sizeof (FC_Function) * size);
46 if (tmp->functions == NULL)
47 {
48 fc_message("cannot allocate %d bytes for a hash table.", sizeof (FC_Function) * size);
49 free(tmp);
50 }
51
52 memset((void*) tmp->functions, 0, sizeof (FC_Function) * size);
53
54 tmp->current_size = size;
55 tmp->current_mask = size - 1;
56 tmp->current_nb = 0;
57
58 for (i = 0; i < size; i++)
59 {
60 fc_functions_init(&(tmp->functions[i]));
61 }
62
63 return (tmp);
64 }
65
66 /* destroy a hash table */
67 void fc_fhash_delete(FC_FHash *hash)
68 {
69 if (hash->functions != NULL)
70 free(hash->functions);
71 free(hash);
72 }
73
74 /* add an element in the hash table */
75 FC_Function *fc_fhash_insert(FC_FHash *hash, FC_Function *fnc)
76 {
77 unsigned int pos;
78
79 /* compute the position */
80 pos = ((unsigned int) (fnc->symbol)) & hash->current_mask;
81
82 /* hash table is full ? */
83 if (((float) hash->current_nb / hash->current_size) > FC_HASH_MAX_RATIO)
84 {
85 fc_message_fatal(FC_ERR_HASH, "too many colisions in the hash table.");
86 }
87
88 /* search the next free entry */
89 hash->current_nb++;
90 while ((hash->functions[pos].symbol != NULL) || (hash->functions[pos].faked))
91 {
92 pos = (pos + 1) & hash->current_mask;
93 }
94
95 /* set the value */
96 hash->functions[pos] = *fnc;
97 return (&(hash->functions[pos]));
98 }
99
100 /* find an element in the hash table */
101 FC_Function *fc_fhash_find(FC_FHash *hash, void *fnc)
102 {
103 unsigned int pos;
104
105 /* compute the position */
106 pos = ((unsigned int) (fnc)) & hash->current_mask;
107
108 /* search the entry */
109 while (hash->functions[pos].symbol != fnc)
110 {
111 /* the entry does not exist */
112 if (hash->functions[pos].symbol == NULL)
113 return (NULL);
114 pos = pos + 1;
115 }
116
117 return (&(hash->functions[pos]));
118 }
119