comparison libiberty/hashtab.c @ 67:f6334be47118

update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 22 Mar 2011 17:18:12 +0900
parents 77e2b8dfacca
children 04ced10e8804
comparison
equal deleted inserted replaced
65:65488c3d617d 67:f6334be47118
1 /* An expandable hash tables datatype. 1 /* An expandable hash tables datatype.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2009 2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2009, 2010
3 Free Software Foundation, Inc. 3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov (vmakarov@cygnus.com). 4 Contributed by Vladimir Makarov (vmakarov@cygnus.com).
5 5
6 This file is part of the libiberty library. 6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or 7 Libiberty is free software; you can redistribute it and/or
289 289
290 htab_t 290 htab_t
291 htab_create_alloc (size_t size, htab_hash hash_f, htab_eq eq_f, 291 htab_create_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
292 htab_del del_f, htab_alloc alloc_f, htab_free free_f) 292 htab_del del_f, htab_alloc alloc_f, htab_free free_f)
293 { 293 {
294 htab_t result; 294 return htab_create_typed_alloc (size, hash_f, eq_f, del_f, alloc_f, alloc_f,
295 unsigned int size_prime_index; 295 free_f);
296 296 }
297 size_prime_index = higher_prime_index (size); 297
298 size = prime_tab[size_prime_index].prime; 298 /* As above, but uses the variants of ALLOC_F and FREE_F which accept
299
300 result = (htab_t) (*alloc_f) (1, sizeof (struct htab));
301 if (result == NULL)
302 return NULL;
303 result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
304 if (result->entries == NULL)
305 {
306 if (free_f != NULL)
307 (*free_f) (result);
308 return NULL;
309 }
310 result->size = size;
311 result->size_prime_index = size_prime_index;
312 result->hash_f = hash_f;
313 result->eq_f = eq_f;
314 result->del_f = del_f;
315 result->alloc_f = alloc_f;
316 result->free_f = free_f;
317 return result;
318 }
319
320 /* As above, but use the variants of alloc_f and free_f which accept
321 an extra argument. */ 299 an extra argument. */
322 300
323 htab_t 301 htab_t
324 htab_create_alloc_ex (size_t size, htab_hash hash_f, htab_eq eq_f, 302 htab_create_alloc_ex (size_t size, htab_hash hash_f, htab_eq eq_f,
325 htab_del del_f, void *alloc_arg, 303 htab_del del_f, void *alloc_arg,
326 htab_alloc_with_arg alloc_f, 304 htab_alloc_with_arg alloc_f,
327 htab_free_with_arg free_f) 305 htab_free_with_arg free_f)
328 { 306 {
329 htab_t result; 307 htab_t result;
330 unsigned int size_prime_index; 308 unsigned int size_prime_index;
331 309
350 result->alloc_arg = alloc_arg; 328 result->alloc_arg = alloc_arg;
351 result->alloc_with_arg_f = alloc_f; 329 result->alloc_with_arg_f = alloc_f;
352 result->free_with_arg_f = free_f; 330 result->free_with_arg_f = free_f;
353 return result; 331 return result;
354 } 332 }
333
334 /*
335
336 @deftypefn Supplemental htab_t htab_create_typed_alloc (size_t @var{size}, @
337 htab_hash @var{hash_f}, htab_eq @var{eq_f}, htab_del @var{del_f}, @
338 htab_alloc @var{alloc_tab_f}, htab_alloc @var{alloc_f}, @
339 htab_free @var{free_f})
340
341 This function creates a hash table that uses two different allocators
342 @var{alloc_tab_f} and @var{alloc_f} to use for allocating the table itself
343 and its entries respectively. This is useful when variables of different
344 types need to be allocated with different allocators.
345
346 The created hash table is slightly larger than @var{size} and it is
347 initially empty (all the hash table entries are @code{HTAB_EMPTY_ENTRY}).
348 The function returns the created hash table, or @code{NULL} if memory
349 allocation fails.
350
351 @end deftypefn
352
353 */
354
355 htab_t
356 htab_create_typed_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
357 htab_del del_f, htab_alloc alloc_tab_f,
358 htab_alloc alloc_f, htab_free free_f)
359 {
360 htab_t result;
361 unsigned int size_prime_index;
362
363 size_prime_index = higher_prime_index (size);
364 size = prime_tab[size_prime_index].prime;
365
366 result = (htab_t) (*alloc_tab_f) (1, sizeof (struct htab));
367 if (result == NULL)
368 return NULL;
369 result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
370 if (result->entries == NULL)
371 {
372 if (free_f != NULL)
373 (*free_f) (result);
374 return NULL;
375 }
376 result->size = size;
377 result->size_prime_index = size_prime_index;
378 result->hash_f = hash_f;
379 result->eq_f = eq_f;
380 result->del_f = del_f;
381 result->alloc_f = alloc_f;
382 result->free_f = free_f;
383 return result;
384 }
385
355 386
356 /* Update the function pointers and allocation parameter in the htab_t. */ 387 /* Update the function pointers and allocation parameter in the htab_t. */
357 388
358 void 389 void
359 htab_set_functions_ex (htab_t htab, htab_hash hash_f, htab_eq eq_f, 390 htab_set_functions_ex (htab_t htab, htab_hash hash_f, htab_eq eq_f,