summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMattias EngdegÄrd <mattiase@acm.org>2023-12-28 19:04:43 +0100
committerMattias EngdegÄrd <mattiase@acm.org>2024-01-12 18:03:02 +0100
commit29e3d1c56f07a53d1955c9a71e68f70f3b901728 (patch)
tree8af3a2032f9e99368dd2a4ac14bacc26be23d9ea
parent484e04efa4fcb81968cba8e05835812c62856287 (diff)
downloademacs-29e3d1c56f07a53d1955c9a71e68f70f3b901728.tar.gz
emacs-29e3d1c56f07a53d1955c9a71e68f70f3b901728.tar.bz2
emacs-29e3d1c56f07a53d1955c9a71e68f70f3b901728.zip
Abstract predicate and constant for unused hash keys
Qunbound is used for many things; using a predicate and constant for the specific purpose of unused hash entry keys allows us to locate them and make changes much more easily. * src/lisp.h (HASH_UNUSED_ENTRY_KEY, hash_unused_entry_key_p): New constant and function. * src/comp.c (compile_function, Fcomp__compile_ctxt_to_file): * src/composite.c (composition_gstring_cache_clear_font): * src/emacs-module.c (module_global_reference_p): * src/fns.c (make_hash_table, maybe_resize_hash_table, hash_put) (hash_remove_from_table, hash_clear, sweep_weak_table, Fmaphash): * src/json.c (lisp_to_json_nonscalar_1): * src/minibuf.c (Ftry_completion, Fall_completions, Ftest_completion): * src/print.c (print, print_object): Use them.
-rw-r--r--src/comp.c8
-rw-r--r--src/composite.c2
-rw-r--r--src/emacs-module.c2
-rw-r--r--src/fns.c14
-rw-r--r--src/json.c2
-rw-r--r--src/lisp.h12
-rw-r--r--src/minibuf.c10
-rw-r--r--src/print.c4
8 files changed, 32 insertions, 22 deletions
diff --git a/src/comp.c b/src/comp.c
index 347f8924793..2872c28a2b1 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -4334,7 +4334,7 @@ compile_function (Lisp_Object func)
{
Lisp_Object block_name = HASH_KEY (ht, i);
if (!EQ (block_name, Qentry)
- && !BASE_EQ (block_name, Qunbound))
+ && !hash_unused_entry_key_p (block_name))
declare_block (block_name);
}
@@ -4347,7 +4347,7 @@ compile_function (Lisp_Object func)
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (ht); i++)
{
Lisp_Object block_name = HASH_KEY (ht, i);
- if (!BASE_EQ (block_name, Qunbound))
+ if (!hash_unused_entry_key_p (block_name))
{
Lisp_Object block = HASH_VALUE (ht, i);
Lisp_Object insns = CALL1I (comp-block-insns, block);
@@ -4966,12 +4966,12 @@ DEFUN ("comp--compile-ctxt-to-file", Fcomp__compile_ctxt_to_file,
struct Lisp_Hash_Table *func_h =
XHASH_TABLE (CALL1I (comp-ctxt-funcs-h, Vcomp_ctxt));
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (func_h); i++)
- if (!BASE_EQ (HASH_KEY (func_h, i), Qunbound))
+ if (!hash_unused_entry_key_p (HASH_KEY (func_h, i)))
declare_function (HASH_VALUE (func_h, i));
/* Compile all functions. Can't be done before because the
relocation structs has to be already defined. */
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (func_h); i++)
- if (!BASE_EQ (HASH_KEY (func_h, i), Qunbound))
+ if (!hash_unused_entry_key_p (HASH_KEY (func_h, i)))
compile_function (HASH_VALUE (func_h, i));
/* Work around bug#46495 (GCC PR99126). */
diff --git a/src/composite.c b/src/composite.c
index 7c7f4720514..ed1aeb380a0 100644
--- a/src/composite.c
+++ b/src/composite.c
@@ -690,7 +690,7 @@ composition_gstring_cache_clear_font (Lisp_Object font_object)
{
Lisp_Object k = HASH_KEY (h, i);
- if (!BASE_EQ (k, Qunbound))
+ if (!hash_unused_entry_key_p (k))
{
Lisp_Object gstring = HASH_VALUE (h, i);
diff --git a/src/emacs-module.c b/src/emacs-module.c
index 46bd732e8eb..283703b3651 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -412,7 +412,7 @@ module_global_reference_p (emacs_value v, ptrdiff_t *n)
reference that's identical to some global reference. */
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
- if (!BASE_EQ (HASH_KEY (h, i), Qunbound)
+ if (!hash_unused_entry_key_p (HASH_KEY (h, i))
&& &XMODULE_GLOBAL_REFERENCE (HASH_VALUE (h, i))->value == v)
return true;
}
diff --git a/src/fns.c b/src/fns.c
index 56b4e9a18c0..d8da8992ce9 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4575,7 +4575,7 @@ make_hash_table (struct hash_table_test test, EMACS_INT size,
h->rehash_threshold = rehash_threshold;
h->rehash_size = rehash_size;
h->count = 0;
- h->key_and_value = make_vector (2 * size, Qunbound);
+ h->key_and_value = make_vector (2 * size, HASH_UNUSED_ENTRY_KEY);
h->hash = make_nil_vector (size);
h->next = make_vector (size, make_fixnum (-1));
h->index = make_vector (hash_index_size (h, size), make_fixnum (-1));
@@ -4678,7 +4678,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
Lisp_Object key_and_value
= alloc_larger_vector (h->key_and_value, 2 * new_size);
for (ptrdiff_t i = 2 * old_size; i < 2 * new_size; i++)
- ASET (key_and_value, i, Qunbound);
+ ASET (key_and_value, i, HASH_UNUSED_ENTRY_KEY);
Lisp_Object hash = alloc_larger_vector (h->hash, new_size);
memclear (XVECTOR (hash)->contents + old_size,
@@ -4782,7 +4782,7 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
/* Store key/value in the key_and_value vector. */
ptrdiff_t i = h->next_free;
eassert (NILP (HASH_HASH (h, i)));
- eassert (BASE_EQ (Qunbound, (HASH_KEY (h, i))));
+ eassert (hash_unused_entry_key_p (HASH_KEY (h, i)));
h->next_free = HASH_NEXT (h, i);
set_hash_key_slot (h, i, key);
set_hash_value_slot (h, i, value);
@@ -4824,7 +4824,7 @@ hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
/* Clear slots in key_and_value and add the slots to
the free list. */
- set_hash_key_slot (h, i, Qunbound);
+ set_hash_key_slot (h, i, HASH_UNUSED_ENTRY_KEY);
set_hash_value_slot (h, i, Qnil);
set_hash_hash_slot (h, i, Qnil);
set_hash_next_slot (h, i, h->next_free);
@@ -4851,7 +4851,7 @@ hash_clear (struct Lisp_Hash_Table *h)
for (ptrdiff_t i = 0; i < size; i++)
{
set_hash_next_slot (h, i, i < size - 1 ? i + 1 : -1);
- set_hash_key_slot (h, i, Qunbound);
+ set_hash_key_slot (h, i, HASH_UNUSED_ENTRY_KEY);
set_hash_value_slot (h, i, Qnil);
}
@@ -4922,7 +4922,7 @@ sweep_weak_table (struct Lisp_Hash_Table *h, bool remove_entries_p)
h->next_free = i;
/* Clear key, value, and hash. */
- set_hash_key_slot (h, i, Qunbound);
+ set_hash_key_slot (h, i, HASH_UNUSED_ENTRY_KEY);
set_hash_value_slot (h, i, Qnil);
if (!NILP (h->hash))
set_hash_hash_slot (h, i, Qnil);
@@ -5535,7 +5535,7 @@ FUNCTION is called with two arguments, KEY and VALUE.
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
Lisp_Object k = HASH_KEY (h, i);
- if (!BASE_EQ (k, Qunbound))
+ if (!hash_unused_entry_key_p (k))
call2 (function, k, HASH_VALUE (h, i));
}
diff --git a/src/json.c b/src/json.c
index af5f30c7275..d98b312ecc9 100644
--- a/src/json.c
+++ b/src/json.c
@@ -364,7 +364,7 @@ lisp_to_json_nonscalar_1 (Lisp_Object lisp,
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
Lisp_Object key = HASH_KEY (h, i);
- if (!BASE_EQ (key, Qunbound))
+ if (!hash_unused_entry_key_p (key))
{
CHECK_STRING (key);
Lisp_Object ekey = json_encode (key);
diff --git a/src/lisp.h b/src/lisp.h
index a34726adbcb..549b51d3f7f 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2462,7 +2462,7 @@ struct Lisp_Hash_Table
/* Vector of keys and values. The key of item I is found at index
2 * I, the value is found at index 2 * I + 1.
- If the key is equal to Qunbound, then this slot is unused.
+ If the key is HASH_UNUSED_ENTRY_KEY, then this slot is unused.
This is gc_marked specially if the table is weak. */
Lisp_Object key_and_value;
@@ -2478,6 +2478,16 @@ struct Lisp_Hash_Table
/* Sanity-check pseudovector layout. */
verify (offsetof (struct Lisp_Hash_Table, weak) == header_size);
+/* Key value that marks an unused hash table entry. */
+#define HASH_UNUSED_ENTRY_KEY Qunbound
+
+/* KEY is a key of an unused hash table entry. */
+INLINE bool
+hash_unused_entry_key_p (Lisp_Object key)
+{
+ return BASE_EQ (key, HASH_UNUSED_ENTRY_KEY);
+}
+
INLINE bool
HASH_TABLE_P (Lisp_Object a)
{
diff --git a/src/minibuf.c b/src/minibuf.c
index f4f9da9c3f9..22bb8fa1d75 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -1680,8 +1680,8 @@ or from one of the possible completions. */)
else /* if (type == hash_table) */
{
while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
- && BASE_EQ (HASH_KEY (XHASH_TABLE (collection), idx),
- Qunbound))
+ && hash_unused_entry_key_p (HASH_KEY (XHASH_TABLE (collection),
+ idx)))
idx++;
if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
break;
@@ -1918,8 +1918,8 @@ with a space are ignored unless STRING itself starts with a space. */)
else /* if (type == 3) */
{
while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
- && BASE_EQ (HASH_KEY (XHASH_TABLE (collection), idx),
- Qunbound))
+ && hash_unused_entry_key_p (HASH_KEY (XHASH_TABLE (collection),
+ idx)))
idx++;
if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
break;
@@ -2117,7 +2117,7 @@ the values STRING, PREDICATE and `lambda'. */)
for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
tem = HASH_KEY (h, i);
- if (BASE_EQ (tem, Qunbound)) continue;
+ if (hash_unused_entry_key_p (tem)) continue;
Lisp_Object strkey = (SYMBOLP (tem) ? Fsymbol_name (tem) : tem);
if (!STRINGP (strkey)) continue;
if (BASE_EQ (Fcompare_strings (string, Qnil, Qnil,
diff --git a/src/print.c b/src/print.c
index 26ed52b4653..e22f3b6778c 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1290,7 +1290,7 @@ print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
Lisp_Object key = HASH_KEY (h, i);
- if (!BASE_EQ (key, Qunbound)
+ if (!hash_unused_entry_key_p (key)
&& EQ (HASH_VALUE (h, i), Qt))
Fremhash (key, Vprint_number_table);
}
@@ -2770,7 +2770,7 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
{
Lisp_Object key;
ptrdiff_t idx = e->u.hash.idx;
- while (BASE_EQ ((key = HASH_KEY (h, idx)), Qunbound))
+ while (hash_unused_entry_key_p ((key = HASH_KEY (h, idx))))
idx++;
e->u.hash.idx = idx;
obj = key;