Commit ba2e3a8a authored by Philipp Stephani's avatar Philipp Stephani
Browse files

Refactoring: Reduce code duplication

* src/emacs-module.c (value_storage_contains_p): New function.
(module_free_global_ref, value_to_lisp): Use it.
parent a59c41ee
Pipeline #1362 failed with stage
in 68 minutes and 47 seconds
...@@ -209,6 +209,8 @@ static void module_non_local_exit_throw_1 (emacs_env *, ...@@ -209,6 +209,8 @@ static void module_non_local_exit_throw_1 (emacs_env *,
Lisp_Object, Lisp_Object); Lisp_Object, Lisp_Object);
static void module_out_of_memory (emacs_env *); static void module_out_of_memory (emacs_env *);
static void module_reset_handlerlist (struct handler **); static void module_reset_handlerlist (struct handler **);
static bool value_storage_contains_p (const struct emacs_value_storage *,
emacs_value, ptrdiff_t *);
static bool module_assertions = false; static bool module_assertions = false;
...@@ -403,16 +405,8 @@ module_free_global_ref (emacs_env *env, emacs_value ref) ...@@ -403,16 +405,8 @@ module_free_global_ref (emacs_env *env, emacs_value ref)
if (module_assertions) if (module_assertions)
{ {
ptrdiff_t count = 0; ptrdiff_t count = 0;
for (struct emacs_value_frame *frame = &global_storage.initial; if (value_storage_contains_p (&global_storage, ref, &count))
frame != NULL; frame = frame->next) return;
{
for (int i = 0; i < frame->offset; ++i)
{
if (&frame->objects[i] == ref)
return;
++count;
}
}
module_abort ("Global value was not found in list of %"pD"d globals", module_abort ("Global value was not found in list of %"pD"d globals",
count); count);
} }
...@@ -978,29 +972,13 @@ value_to_lisp (emacs_value v) ...@@ -978,29 +972,13 @@ value_to_lisp (emacs_value v)
if (&priv->non_local_exit_symbol == v if (&priv->non_local_exit_symbol == v
|| &priv->non_local_exit_data == v) || &priv->non_local_exit_data == v)
goto ok; goto ok;
for (struct emacs_value_frame *frame = &priv->storage.initial; if (value_storage_contains_p (&priv->storage, v, &num_values))
frame != NULL; frame = frame->next) goto ok;
{
for (int i = 0; i < frame->offset; ++i)
{
if (&frame->objects[i] == v)
goto ok;
++num_values;
}
}
++num_environments; ++num_environments;
} }
/* Also check global values. */ /* Also check global values. */
for (struct emacs_value_frame *frame = &global_storage.initial; if (value_storage_contains_p (&global_storage, v, &num_values))
frame != NULL; frame = frame->next) goto ok;
{
for (int i = 0; i < frame->offset; ++i)
{
if (&frame->objects[i] == v)
goto ok;
++num_values;
}
}
module_abort (("Emacs value not found in %"pD"d values " module_abort (("Emacs value not found in %"pD"d values "
"of %"pD"d environments"), "of %"pD"d environments"),
num_values, num_environments); num_values, num_environments);
...@@ -1215,6 +1193,26 @@ init_module_assertions (bool enable) ...@@ -1215,6 +1193,26 @@ init_module_assertions (bool enable)
initialize_storage (&global_storage); initialize_storage (&global_storage);
} }
/* Return whether STORAGE contains VALUE. Used to check module
assertions. Increment *COUNT by the number of values searched. */
static bool
value_storage_contains_p (const struct emacs_value_storage *storage,
emacs_value value, ptrdiff_t *count)
{
for (const struct emacs_value_frame *frame = &storage->initial; frame != NULL;
frame = frame->next)
{
for (int i = 0; i < frame->offset; ++i)
{
if (&frame->objects[i] == value)
return true;
++count;
}
}
return false;
}
static AVOID ATTRIBUTE_FORMAT_PRINTF (1, 2) static AVOID ATTRIBUTE_FORMAT_PRINTF (1, 2)
module_abort (const char *format, ...) module_abort (const char *format, ...)
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment