revised all sets/maps hashing pointers to use the HASHPTR macro
authorMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Fri, 26 Nov 2004 10:40:45 +0000 (10:40 +0000)
committerMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Fri, 26 Nov 2004 10:40:45 +0000 (10:40 +0000)
defined in hashptr.h

[r4469]

ir/adt/array.h
ir/adt/eset.c
ir/adt/hashptr.h [new file with mode: 0644]
ir/adt/pmap.c

index 7db99bd..47c9f15 100644 (file)
    ... 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
 
 /**
index 9bfe45e..ad1b869 100644 (file)
 
 #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 (file)
index 0000000..8f70cc4
--- /dev/null
@@ -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__ */
index 89bbda9..bd6a5c2 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <assert.h>
 #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);
 }