message: Intern file names in msg_location to make them cheaper to copy.
[pspp] / src / libpspp / message.c
index f1afc8fe3050481df753ff4b93bd03ae22fb9d39..faeb83a449d3e3058507e163d9bd7049192ea917 100644 (file)
@@ -27,6 +27,7 @@
 #include <unistd.h>
 
 #include "libpspp/cast.h"
+#include "libpspp/intern.h"
 #include "libpspp/str.h"
 #include "libpspp/version.h"
 #include "data/settings.h"
@@ -108,7 +109,7 @@ msg_set_handler (void (*handler) (const struct msg *, void *aux), void *aux)
 void
 msg_location_uninit (struct msg_location *loc)
 {
-  free (loc->file_name);
+  intern_unref (loc->file_name);
 }
 
 void
@@ -129,7 +130,7 @@ msg_location_dup (const struct msg_location *src)
 
   struct msg_location *dst = xmalloc (sizeof *dst);
   *dst = (struct msg_location) {
-    .file_name = xstrdup_if_nonnull (src->file_name),
+    .file_name = intern_new_if_nonnull (src->file_name),
     .first_line = src->first_line,
     .last_line = src->last_line,
     .first_column = src->first_column,
@@ -210,7 +211,7 @@ msg_stack_destroy (struct msg_stack *stack)
 {
   if (stack)
     {
-      msg_location_uninit (&stack->location);
+      msg_location_destroy (stack->location);
       free (stack->description);
       free (stack);
     }
@@ -221,10 +222,9 @@ msg_stack_dup (const struct msg_stack *src)
 {
   struct msg_stack *dst = xmalloc (sizeof *src);
   *dst = (struct msg_stack) {
-    .location = src->location,
+    .location = msg_location_dup (src->location),
     .description = xstrdup_if_nonnull (src->description),
   };
-  dst->location.file_name = xstrdup_if_nonnull (dst->location.file_name);
   return dst;
 }
 \f
@@ -296,9 +296,9 @@ msg_to_string (const struct msg *m)
   for (size_t i = 0; i < m->n_stack; i++)
     {
       const struct msg_stack *ms = m->stack[i];
-      if (!msg_location_is_empty (&ms->location))
+      if (!msg_location_is_empty (ms->location))
         {
-          msg_location_format (&ms->location, &s);
+          msg_location_format (ms->location, &s);
           ds_put_cstr (&s, ": ");
         }
       ds_put_format (&s, "%s\n", ms->description);