2 * Copyright (C) 1998 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any later version.
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
27 /* Allocate a new hash. */
29 hash_create_size (unsigned int size
, unsigned int (*hash_key
) (void *),
30 int (*hash_cmp
) (const void *, const void *))
34 hash
= XMALLOC (MTYPE_HASH
, sizeof (struct hash
));
35 hash
->index
= XMALLOC (MTYPE_HASH_INDEX
,
36 sizeof (struct hash_backet
*) * size
);
37 memset (hash
->index
, 0, sizeof (struct hash_backet
*) * size
);
39 hash
->hash_key
= hash_key
;
40 hash
->hash_cmp
= hash_cmp
;
46 /* Allocate a new hash with default hash size. */
48 hash_create (unsigned int (*hash_key
) (void *),
49 int (*hash_cmp
) (const void *, const void *))
51 return hash_create_size (HASHTABSIZE
, hash_key
, hash_cmp
);
54 /* Utility function for hash_get(). When this function is specified
55 as alloc_func, return arugment as it is. This function is used for
56 intern already allocated value. */
58 hash_alloc_intern (void *arg
)
63 /* Lookup and return hash backet in hash. If there is no
64 corresponding hash backet and alloc_func is specified, create new
67 hash_get (struct hash
*hash
, void *data
, void * (*alloc_func
) (void *))
72 struct hash_backet
*backet
;
74 key
= (*hash
->hash_key
) (data
);
75 index
= key
% hash
->size
;
77 for (backet
= hash
->index
[index
]; backet
!= NULL
; backet
= backet
->next
)
78 if (backet
->key
== key
&& (*hash
->hash_cmp
) (backet
->data
, data
))
83 newdata
= (*alloc_func
) (data
);
87 backet
= XMALLOC (MTYPE_HASH_BACKET
, sizeof (struct hash_backet
));
88 backet
->data
= newdata
;
90 backet
->next
= hash
->index
[index
];
91 hash
->index
[index
] = backet
;
100 hash_lookup (struct hash
*hash
, void *data
)
102 return hash_get (hash
, data
, NULL
);
105 /* This function release registered value from specified hash. When
106 release is successfully finished, return the data pointer in the
109 hash_release (struct hash
*hash
, void *data
)
114 struct hash_backet
*backet
;
115 struct hash_backet
*pp
;
117 key
= (*hash
->hash_key
) (data
);
118 index
= key
% hash
->size
;
120 for (backet
= pp
= hash
->index
[index
]; backet
; backet
= backet
->next
)
122 if (backet
->key
== key
&& (*hash
->hash_cmp
) (backet
->data
, data
))
125 hash
->index
[index
] = backet
->next
;
127 pp
->next
= backet
->next
;
130 XFREE (MTYPE_HASH_BACKET
, backet
);
139 /* Iterator function for hash. */
141 hash_iterate (struct hash
*hash
,
142 void (*func
) (struct hash_backet
*, void *), void *arg
)
145 struct hash_backet
*hb
;
146 struct hash_backet
*hbnext
;
148 for (i
= 0; i
< hash
->size
; i
++)
149 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
)
151 /* get pointer to next hash backet here, in case (*func)
152 * decides to delete hb by calling hash_release
161 hash_clean (struct hash
*hash
, void (*free_func
) (void *))
164 struct hash_backet
*hb
;
165 struct hash_backet
*next
;
167 for (i
= 0; i
< hash
->size
; i
++)
169 for (hb
= hash
->index
[i
]; hb
; hb
= next
)
174 (*free_func
) (hb
->data
);
176 XFREE (MTYPE_HASH_BACKET
, hb
);
179 hash
->index
[i
] = NULL
;
183 /* Free hash memory. You may call hash_clean before call this
186 hash_free (struct hash
*hash
)
188 XFREE (MTYPE_HASH_INDEX
, hash
->index
);
189 XFREE (MTYPE_HASH
, hash
);