Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: 5.8: LTO exposes some new issues



On Tue, Jul 28, 2020 at 10:26 AM Peter Stephenson
<p.w.stephenson@xxxxxxxxxxxx> wrote:
>
> > On 28 July 2020 at 08:53 Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> >
> > It's clearly correct, but as written, the patch loses the distinction
> > that these members are private to hashtable.c and should not be accessed
> > by other parts of the code.  Could you address that, please?  If
> > there's an easy way to have the compiler enforce this restriction,
> > great; else, we can at least add a comment.
>
> One way is to have a "struct { ... } private" substructure,
> which it makes it clear what's going on within the code (though comments
> are obviously useful, too).

How about this? The diff is a bit larger but the code is fairly
straightforward. Only hashtable.c has access to internal fields, just
like before the patch.

In a nutshell, struct hashtable has only public data members. Within
hashtable.c there is struct hashtableimpl, which has struct hashtable
as the first data member. C allows casting a pointer to a struct to a
pointer to its first data member and back without violating aliasing
rules. Thus hashtable.c can cast struct hashtable* to struct
hashtableimpl* in order to get access to internal fields.

Roman.
diff --git a/Src/hashtable.c b/Src/hashtable.c
index e210ddeca..e49f4e1da 100644
--- a/Src/hashtable.c
+++ b/Src/hashtable.c
@@ -28,25 +28,27 @@
  */
 
 #include "../config.h"
+#include "zsh.mdh"
+#include "hashtable.pro"
+
+typedef struct scanstatus *ScanStatus;
+typedef struct hashtableimpl* HashTableImpl;
+
+struct hashtableimpl {
+    struct hashtable pub;
+
+    /* HASHTABLE INTERNAL MEMBERS */
+    ScanStatus scan;		/* status of a scan over this hashtable     */
 
 #ifdef ZSH_HASH_DEBUG
-# define HASHTABLE_DEBUG_MEMBERS \
-    /* Members of struct hashtable used for debugging hash tables */ \
-    HashTable next, last;	/* linked list of all hash tables           */ \
-    char *tablename;		/* string containing name of the hash table */ \
+    /* HASHTABLE DEBUG MEMBERS */
+    HashTableImpl next, last;	/* linked list of all hash tables           */
+    char *tablename;		/* string containing name of the hash table */
     PrintTableStats printinfo;	/* pointer to function to print table stats */
-#else /* !ZSH_HASH_DEBUG */
-# define HASHTABLE_DEBUG_MEMBERS
 #endif /* !ZSH_HASH_DEBUG */
+};
 
-#define HASHTABLE_INTERNAL_MEMBERS \
-    ScanStatus scan;		/* status of a scan over this hashtable     */ \
-    HASHTABLE_DEBUG_MEMBERS
-
-typedef struct scanstatus *ScanStatus;
-
-#include "zsh.mdh"
-#include "hashtable.pro"
+static inline HashTableImpl impl(HashTable ht) { return (HashTableImpl)ht; }
 
 /* Structure for recording status of a hashtable scan in progress.  When a *
  * scan starts, the .scan member of the hashtable structure points to one  *
@@ -71,7 +73,8 @@ struct scanstatus {
 /********************************/
 
 #ifdef ZSH_HASH_DEBUG
-static HashTable firstht, lastht;
+static void printhashtabinfo(HashTable ht);
+static HashTableImpl firstht, lastht;
 #endif /* ZSH_HASH_DEBUG */
 
 /* Generic hash function */
@@ -94,9 +97,9 @@ hasher(const char *str)
 mod_export HashTable
 newhashtable(int size, UNUSED(char const *name), UNUSED(PrintTableStats printinfo))
 {
-    HashTable ht;
+    HashTableImpl ht;
 
-    ht = (HashTable) zshcalloc(sizeof *ht);
+    ht = (HashTableImpl) zshcalloc(sizeof *ht);
 #ifdef ZSH_HASH_DEBUG
     ht->next = NULL;
     if(!firstht)
@@ -108,12 +111,12 @@ newhashtable(int size, UNUSED(char const *name), UNUSED(PrintTableStats printinf
     ht->printinfo = printinfo ? printinfo : printhashtabinfo;
     ht->tablename = ztrdup(name);
 #endif /* ZSH_HASH_DEBUG */
-    ht->nodes = (HashNode *) zshcalloc(size * sizeof(HashNode));
-    ht->hsize = size;
-    ht->ct = 0;
+    ht->pub.nodes = (HashNode *) zshcalloc(size * sizeof(HashNode));
+    ht->pub.hsize = size;
+    ht->pub.ct = 0;
     ht->scan = NULL;
-    ht->scantab = NULL;
-    return ht;
+    ht->pub.scantab = NULL;
+    return &ht->pub;
 }
 
 /* Delete a hash table.  After this function has been used, any *
@@ -125,18 +128,18 @@ deletehashtable(HashTable ht)
 {
     ht->emptytable(ht);
 #ifdef ZSH_HASH_DEBUG
-    if(ht->next)
-	ht->next->last = ht->last;
+    if(impl(ht)->next)
+	impl(ht)->next->last = impl(ht)->last;
     else
-	lastht = ht->last;
-    if(ht->last)
-	ht->last->next = ht->next;
+	lastht = impl(ht)->last;
+    if(impl(ht)->last)
+	impl(ht)->last->next = impl(ht)->next;
     else
-	firstht = ht->next;
-    zsfree(ht->tablename);
+	firstht = impl(ht)->next;
+    zsfree(impl(ht)->tablename);
 #endif /* ZSH_HASH_DEBUG */
     zfree(ht->nodes, ht->hsize * sizeof(HashNode));
-    zfree(ht, sizeof(*ht));
+    zfree(ht, sizeof(struct hashtableimpl));
 }
 
 /* Add a node to a hash table.                          *
@@ -175,7 +178,7 @@ addhashnode2(HashTable ht, char *nam, void *nodeptr)
     if (!hp) {
 	hn->next = NULL;
 	ht->nodes[hashval] = hn;
-	if (++ht->ct >= ht->hsize * 2 && !ht->scan)
+	if (++ht->ct >= ht->hsize * 2 && !impl(ht)->scan)
 	    expandhashtable(ht);
 	return NULL;
     }
@@ -185,15 +188,15 @@ addhashnode2(HashTable ht, char *nam, void *nodeptr)
 	ht->nodes[hashval] = hn;
 	replacing:
 	hn->next = hp->next;
-	if(ht->scan) {
-	    if(ht->scan->sorted) {
-		HashNode *hashtab = ht->scan->u.s.hashtab;
+	if(impl(ht)->scan) {
+	    if(impl(ht)->scan->sorted) {
+		HashNode *hashtab = impl(ht)->scan->u.s.hashtab;
 		int i;
-		for(i = ht->scan->u.s.ct; i--; )
+		for(i = impl(ht)->scan->u.s.ct; i--; )
 		    if(hashtab[i] == hp)
 			hashtab[i] = hn;
-	    } else if(ht->scan->u.u == hp)
-		ht->scan->u.u = hn;
+	    } else if(impl(ht)->scan->u.u == hp)
+		impl(ht)->scan->u.u = hn;
 	}
 	return hp;
     }
@@ -211,7 +214,7 @@ addhashnode2(HashTable ht, char *nam, void *nodeptr)
     /* else just add it at the front of the list */
     hn->next = ht->nodes[hashval];
     ht->nodes[hashval] = hn;
-    if (++ht->ct >= ht->hsize * 2 && !ht->scan)
+    if (++ht->ct >= ht->hsize * 2 && !impl(ht)->scan)
         expandhashtable(ht);
     return NULL;
 }
@@ -284,15 +287,15 @@ removehashnode(HashTable ht, const char *nam)
 	ht->nodes[hashval] = hp->next;
 	gotit:
 	ht->ct--;
-	if(ht->scan) {
-	    if(ht->scan->sorted) {
-		HashNode *hashtab = ht->scan->u.s.hashtab;
+	if(impl(ht)->scan) {
+	    if(impl(ht)->scan->sorted) {
+		HashNode *hashtab = impl(ht)->scan->u.s.hashtab;
 		int i;
-		for(i = ht->scan->u.s.ct; i--; )
+		for(i = impl(ht)->scan->u.s.ct; i--; )
 		    if(hashtab[i] == hp)
 			hashtab[i] = NULL;
-	    } else if(ht->scan->u.u == hp)
-		ht->scan->u.u = hp->next;
+	    } else if(impl(ht)->scan->u.u == hp)
+		impl(ht)->scan->u.u = hp->next;
 	}
 	return hp;
     }
@@ -399,7 +402,7 @@ scanmatchtable(HashTable ht, Patprog pprog, int sorted,
 	st.sorted = 1;
 	st.u.s.hashtab = hnsorttab;
 	st.u.s.ct = ct;
-	ht->scan = &st;
+	impl(ht)->scan = &st;
 
 	for (htp = hnsorttab, i = 0; i < ct; i++, htp++) {
 	    if ((!flags1 || ((*htp)->flags & flags1)) &&
@@ -410,13 +413,13 @@ scanmatchtable(HashTable ht, Patprog pprog, int sorted,
 	    }
 	}
 
-	ht->scan = NULL;
+	impl(ht)->scan = NULL;
     } else {
 	int i, hsize = ht->hsize;
 	HashNode *nodes = ht->nodes;
 
 	st.sorted = 0;
-	ht->scan = &st;
+	impl(ht)->scan = &st;
 
 	for (i = 0; i < hsize; i++)
 	    for (st.u.u = nodes[i]; st.u.u; ) {
@@ -429,7 +432,7 @@ scanmatchtable(HashTable ht, Patprog pprog, int sorted,
 		}
 	    }
 
-	ht->scan = NULL;
+	impl(ht)->scan = NULL;
     }
 
     return match;
@@ -531,7 +534,7 @@ printhashtabinfo(HashTable ht)
     int chainlen[MAXDEPTH + 1];
     int i, tmpcount, total;
 
-    printf("name of table   : %s\n",   ht->tablename);
+    printf("name of table   : %s\n",   impl(ht)->tablename);
     printf("size of nodes[] : %d\n",   ht->hsize);
     printf("number of nodes : %d\n\n", ht->ct);
 
@@ -560,12 +563,12 @@ printhashtabinfo(HashTable ht)
 int
 bin_hashinfo(UNUSED(char *nam), UNUSED(char **args), UNUSED(Options ops), UNUSED(int func))
 {
-    HashTable ht;
+    HashTableImpl ht;
 
     printf("----------------------------------------------------\n");
     queue_signals();
     for(ht = firstht; ht; ht = ht->next) {
-	ht->printinfo(ht);
+	ht->printinfo(&ht->pub);
 	printf("----------------------------------------------------\n");
     }
     unqueue_signals();
diff --git a/Src/zsh.h b/Src/zsh.h
index c48be4ffd..6101cf242 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -1204,10 +1204,6 @@ struct hashtable {
     FreeNodeFunc freenode;	/* pointer to function to free a node         */
     ScanFunc printnode;		/* pointer to function to print a node        */
     ScanTabFunc scantab;	/* pointer to function to scan table          */
-
-#ifdef HASHTABLE_INTERNAL_MEMBERS
-    HASHTABLE_INTERNAL_MEMBERS	/* internal use in hashtable.c                */
-#endif
 };
 
 /* generic hash table node */


Messages sorted by: Reverse Date, Date, Thread, Author