From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Peter Stephenson <p.w.stephenson@ntlworld.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: 5.8: LTO exposes some new issues
Date: Tue, 28 Jul 2020 12:52:38 +0200 [thread overview]
Message-ID: <CAN=4vMp0GP=JJrfP=N6q7s_yCXp-_LMJBL1dC8Kd2weMNPvm0A@mail.gmail.com> (raw)
In-Reply-To: <727383568.664238.1595924724485@mail2.virginmedia.com>
[-- Attachment #1: Type: text/plain, Size: 1240 bytes --]
On Tue, Jul 28, 2020 at 10:26 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> > On 28 July 2020 at 08:53 Daniel Shahaf <d.s@daniel.shahaf.name> 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.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 7619 bytes --]
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 */
next prev parent reply other threads:[~2020-07-28 10:53 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-21 23:41 Tomasz Kłoczko
2020-07-22 5:59 ` Daniel Shahaf
2020-07-25 17:43 ` Bart Schaefer
[not found] ` <CABB28CxSD5w-SY-iCVYuQ4kJfBpNJOWhpk4HOrS1DNPfMVztgw@mail.gmail.com>
2020-07-25 20:05 ` Fwd: " Bart Schaefer
2020-07-27 2:12 ` Daniel Shahaf
2020-07-27 10:07 ` Tomasz Kłoczko
2020-07-27 11:09 ` Roman Perepelitsa
2020-07-27 12:19 ` Roman Perepelitsa
2020-07-27 12:46 ` Tomasz Kłoczko
2020-07-27 14:13 ` Roman Perepelitsa
2020-07-27 14:19 ` Roman Perepelitsa
2020-07-28 8:09 ` Daniel Shahaf
2020-07-28 10:55 ` Fwd: " Roman Perepelitsa
2020-07-28 8:19 ` Daniel Shahaf
2020-07-28 7:53 ` Daniel Shahaf
2020-07-28 8:25 ` Peter Stephenson
2020-07-28 10:52 ` Roman Perepelitsa [this message]
2020-07-28 11:19 ` Daniel Shahaf
2020-07-28 11:31 ` Roman Perepelitsa
2020-07-28 11:51 ` Daniel Shahaf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN=4vMp0GP=JJrfP=N6q7s_yCXp-_LMJBL1dC8Kd2weMNPvm0A@mail.gmail.com' \
--to=roman.perepelitsa@gmail.com \
--cc=p.w.stephenson@ntlworld.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).