From: Michael Beck Date: Fri, 26 Nov 2004 10:40:45 +0000 (+0000) Subject: revised all sets/maps hashing pointers to use the HASHPTR macro X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=ac3d8efebd18b5cf8c448ecdef594857e35bdd89;p=libfirm revised all sets/maps hashing pointers to use the HASHPTR macro defined in hashptr.h [r4469] --- diff --git a/ir/adt/array.h b/ir/adt/array.h index 7db99bd7d..47c9f15a9 100644 --- a/ir/adt/array.h +++ b/ir/adt/array.h @@ -293,11 +293,11 @@ ... or at the IPD, either. */ #ifdef NDEBUG # define _ARR_DBGINF_DECL -# define _ARR_SET_DBGINF(descr, co, es) ((co), (es)) +# define _ARR_SET_DBGINF(descr, co, es) #else # define _ARR_DBGINF_DECL int cookie; size_t eltsize; # define _ARR_SET_DBGINF(descr, co, es) \ - ((descr)->cookie = (co), (descr)->eltsize = (es)) + ( (descr)->cookie = (co), (descr)->eltsize = (es) ) #endif /** diff --git a/ir/adt/eset.c b/ir/adt/eset.c index 9bfe45e7d..ad1b86957 100644 --- a/ir/adt/eset.c +++ b/ir/adt/eset.c @@ -13,27 +13,29 @@ #include "eset.h" #include "set.h" - +#include "hashptr.h" struct eset { int dummy; /* dummy entry */ }; -static const int INITIAL_SLOTS = 64; +#define INITIAL_SLOTS 64 +static int pcmp(const void *p1, const void *p2, size_t size) { + const void **q1 = (const void **)p1; + const void **q2 = (const void **)p2; -static int pcmp(const void ** p1, const void ** p2, size_t size) { - return *p1 != *p2; + return *q1 != *q2; } eset * eset_create(void) { - return (eset *) new_set((set_cmp_fun) pcmp, INITIAL_SLOTS); + return (eset *) new_set(pcmp, INITIAL_SLOTS); } -eset * eset_copy(eset * source) { +eset * eset_copy(eset *source) { eset * ret = eset_create(); void * p; for (p = eset_first(source); p; p = eset_next(source)) { @@ -43,37 +45,37 @@ eset * eset_copy(eset * source) { } -void eset_destroy(eset * s) { - del_set((set *) s); +void eset_destroy(eset *s) { + del_set((set *)s); } -void eset_insert(eset * s, void * p) { +void eset_insert(eset *s, void *p) { if (!eset_contains(s, p)) { - set_insert((set *) s, &p, sizeof(void *), (unsigned) p); + set_insert((set *)s, &p, sizeof(p), HASH_PTR(p)); } } -bool eset_contains(eset * s, void * p) { - return set_find((set *) s, &p, sizeof(void *), (unsigned) p) != NULL; +bool eset_contains(eset *s, void *p) { + return set_find((set *)s, &p, sizeof(p), HASH_PTR(p)) != NULL; } -void * eset_first(eset * s) { +void * eset_first(eset *s) { void * p = set_first((set *) s); - return p == NULL ? NULL : *((void * *) p); + return p == NULL ? NULL : *((void **)p); } void * eset_next(eset *s) { - void * p = set_next((set *) s); - return p == NULL ? NULL : *((void * *) p); + void *p = set_next((set *) s); + return p == NULL ? NULL : *((void **)p); } -void eset_insert_all(eset * target, eset * source) { - void * p; +void eset_insert_all(eset *target, eset *source) { + void *p; for (p = eset_first(source); p; p = eset_next(source)) { eset_insert(target, p); } diff --git a/ir/adt/hashptr.h b/ir/adt/hashptr.h new file mode 100644 index 000000000..8f70cc4af --- /dev/null +++ b/ir/adt/hashptr.h @@ -0,0 +1,21 @@ +/* + * Project: libFIRM + * File name: ir/adt/hashptr.h + * Purpose: Hash function for pointers + * Author: Michael Beck, Sebastian Hack + * Modified by: + * Created: 2004 + * CVS-ID: $Id$ + * Copyright: (C) 2004 University of Karlsruhe + * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + */ +#ifndef __HASHPTR_H__ +#define __HASHPTR_H__ + +/** + * hash a pointer value: Pointer addresses are mostly aligned to 4 + * or 8 bytes. So we remove the lowest 3 bits + */ +#define HASHPTR(ptr) (((char *)ptr - (char *)0) >> 3) + +#endif /* __HASHPTR_H__ */ diff --git a/ir/adt/pmap.c b/ir/adt/pmap.c index 89bbda932..bd6a5c2cb 100644 --- a/ir/adt/pmap.c +++ b/ir/adt/pmap.c @@ -15,6 +15,7 @@ #include #include "set.h" +#include "hashptr.h" struct pmap { @@ -22,25 +23,28 @@ struct pmap { }; -static const int INITIAL_SLOTS = 64; +#define INITIAL_SLOTS 64 -static int pmap_entry_cmp(const pmap_entry * entry1, const pmap_entry * entry2, size_t size) { - return entry1->key == entry2->key ? 0 : 1; +static int pmap_entry_cmp(const void *p1, const void *p2, size_t size) { + const pmap_entry *entry1 = p1; + const pmap_entry *entry2 = p2; + + return entry1->key != entry2->key; } -pmap * pmap_create(void) { - return (pmap *) new_set((set_cmp_fun) pmap_entry_cmp, INITIAL_SLOTS); +pmap *pmap_create(void) { + return (pmap *)new_set(pmap_entry_cmp, INITIAL_SLOTS); } -void pmap_destroy(pmap * map) { - del_set((set *) map); +void pmap_destroy(pmap *map) { + del_set((set *)map); } -void pmap_insert(pmap * map, void * key, void * value) { +void pmap_insert(pmap *map, void *key, void *value) { if (pmap_contains(map, key)) { pmap_entry * entry = pmap_find(map, key); entry->value = value; @@ -48,32 +52,32 @@ void pmap_insert(pmap * map, void * key, void * value) { pmap_entry entry; entry.key = key; entry.value = value; - set_insert((set *) map, &entry, sizeof(pmap_entry), (unsigned) key); + set_insert((set *)map, &entry, sizeof(pmap_entry), HASH_PTR(key)); } } -bool pmap_contains(pmap * map, void * key) { - return set_find((set *) map, &key, sizeof(pmap_entry), (unsigned) key) != NULL; +bool pmap_contains(pmap *map, void *key) { + return set_find((set *)map, &key, sizeof(pmap_entry), HASH_PTR(key)) != NULL; } -pmap_entry * pmap_find(pmap * map, void * key) { - return (pmap_entry *) set_find((set *) map, &key, sizeof(pmap_entry), (unsigned) key); +pmap_entry * pmap_find(pmap *map, void *key) { + return (pmap_entry *)set_find((set *)map, &key, sizeof(pmap_entry), HASH_PTR(key)); } -void * pmap_get(pmap * map, void * key) { +void * pmap_get(pmap *map, void *key) { pmap_entry * entry = pmap_find(map, key); return entry == NULL ? NULL : entry->value; } -pmap_entry * pmap_first(pmap * map) { - return (pmap_entry *) set_first((set *) map); +pmap_entry *pmap_first(pmap *map) { + return (pmap_entry *) set_first((set *)map); } -pmap_entry * pmap_next(pmap * map) { - return (pmap_entry *) set_next((set *) map); +pmap_entry *pmap_next(pmap *map) { + return (pmap_entry *) set_next((set *)map); }