From: Ben Pfaff Date: Mon, 11 Jan 2021 01:29:38 +0000 (-0800) Subject: str: Add function xstrdup_if_nonnull() and introduce many users. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb08510bbbab7646bc1031427243489024d22a3b;p=pspp str: Add function xstrdup_if_nonnull() and introduce many users. --- diff --git a/src/data/file-handle-def.c b/src/data/file-handle-def.c index 716a029cc7..075f7b0790 100644 --- a/src/data/file-handle-def.c +++ b/src/data/file-handle-def.c @@ -224,7 +224,7 @@ create_handle (const char *id, char *handle_name, enum fh_referent referent, struct file_handle *handle = xzalloc (sizeof *handle); handle->ref_cnt = 1; - handle->id = id != NULL ? xstrdup (id) : NULL; + handle->id = xstrdup_if_nonnull (id); handle->name = handle_name; handle->referent = referent; handle->encoding = xstrdup (encoding); @@ -261,7 +261,7 @@ fh_create_file (const char *id, const char *file_name, const char *file_name_enc handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name); handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding); handle->file_name = xstrdup (file_name); - handle->file_name_encoding = file_name_encoding ? xstrdup (file_name_encoding) : NULL; + handle->file_name_encoding = xstrdup_if_nonnull (file_name_encoding); handle->mode = properties->mode; handle->line_ends = properties->line_ends; handle->record_width = properties->record_width; diff --git a/src/data/format.c b/src/data/format.c index a25e08f62b..a4c52b788e 100644 --- a/src/data/format.c +++ b/src/data/format.c @@ -1157,7 +1157,7 @@ static struct fmt_affix fmt_affix_clone (const struct fmt_affix *old) { return (struct fmt_affix) { - .s = old->s ? xstrdup (old->s) : NULL, + .s = xstrdup_if_nonnull (old->s), .width = old->width, }; } diff --git a/src/data/mrset.c b/src/data/mrset.c index 38b0ab2d52..63f4b3fe33 100644 --- a/src/data/mrset.c +++ b/src/data/mrset.c @@ -40,7 +40,7 @@ mrset_clone (const struct mrset *old) new = xmalloc (sizeof *new); new->name = xstrdup (old->name); - new->label = old->label != NULL ? xstrdup (old->label) : NULL; + new->label = xstrdup_if_nonnull (old->label); new->type = old->type; new->vars = xmemdup (old->vars, old->n_vars * sizeof *old->vars); new->n_vars = old->n_vars; diff --git a/src/data/sys-file-reader.c b/src/data/sys-file-reader.c index ec8ae3b32f..3a54801c3b 100644 --- a/src/data/sys-file-reader.c +++ b/src/data/sys-file-reader.c @@ -1999,7 +1999,7 @@ rename_var_and_save_short_names (struct sfm_reader *r, off_t pos, for (i = 0; i < n_short_names; i++) { const char *s = var_get_short_name (var, i); - short_names[i] = s != NULL ? xstrdup (s) : NULL; + short_names[i] = xstrdup_if_nonnull (s); } /* Set long name. */ diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index 820a481271..ceeb153c3e 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -141,7 +141,7 @@ void lex_reader_set_file_name (struct lex_reader *reader, const char *file_name) { free (reader->file_name); - reader->file_name = file_name != NULL ? xstrdup (file_name) : NULL; + reader->file_name = xstrdup_if_nonnull (file_name); } /* Creates and returns a new lexer. */ @@ -1610,7 +1610,7 @@ lex_reader_for_file (const char *file_name, const char *encoding, r->reader.syntax = syntax; r->reader.error = error; r->reader.file_name = xstrdup (file_name); - r->reader.encoding = encoding ? xstrdup (encoding) : NULL; + r->reader.encoding = xstrdup_if_nonnull (encoding); r->reader.line_number = 1; r->istream = istream; @@ -1679,7 +1679,7 @@ lex_reader_for_substring_nocopy (struct substring s, const char *encoding) r = xmalloc (sizeof *r); lex_reader_init (&r->reader, &lex_string_reader_class); r->reader.syntax = LEX_SYNTAX_AUTO; - r->reader.encoding = encoding ? xstrdup (encoding) : NULL; + r->reader.encoding = xstrdup_if_nonnull (encoding); r->s = s; r->offset = 0; diff --git a/src/language/lexer/token.c b/src/language/lexer/token.c index 80c6615c5a..9c5fef9991 100644 --- a/src/language/lexer/token.c +++ b/src/language/lexer/token.c @@ -135,8 +135,6 @@ string_representation (struct substring ss) char * token_to_string (const struct token *token) { - const char *name; - switch (token->type) { case T_POS_NUM: @@ -150,8 +148,7 @@ token_to_string (const struct token *token) return string_representation (token->string); default: - name = token_type_to_name (token->type); - return name != NULL ? xstrdup (name) : NULL; + return xstrdup_if_nonnull (token_type_to_name (token->type)); } } diff --git a/src/language/stats/autorecode.c b/src/language/stats/autorecode.c index 91fffcf27a..3b51928fc6 100644 --- a/src/language/stats/autorecode.c +++ b/src/language/stats/autorecode.c @@ -245,7 +245,7 @@ cmd_autorecode (struct lexer *lexer, struct dataset *ds) spec->format = *var_get_print_format (src_vars[i]); const char *label = var_get_label (src_vars[i]); - spec->label = label ? xstrdup (label) : NULL; + spec->label = xstrdup_if_nonnull (label); if (group && i > 0) spec->items = arc->specs[0].items; diff --git a/src/libpspp/line-reader.c b/src/libpspp/line-reader.c index cb4bd014b9..c7d024b44a 100644 --- a/src/libpspp/line-reader.c +++ b/src/libpspp/line-reader.c @@ -125,7 +125,7 @@ line_reader_for_fd (const char *encoding, int fd) && !strcmp (r->encoding, "ASCII")) { r->state = S_AUTO; - r->auto_encoding = encoding ? xstrdup (encoding) : NULL; + r->auto_encoding = xstrdup_if_nonnull (encoding); } else r->state = r->encoding_info.unit == 1 ? S_UNIBYTE : S_MULTIBYTE; diff --git a/src/libpspp/str.h b/src/libpspp/str.h index 66552a3964..7917d58a70 100644 --- a/src/libpspp/str.h +++ b/src/libpspp/str.h @@ -29,6 +29,8 @@ #include "memcasecmp.h" #include "xstrndup.h" #include "xvasprintf.h" + +#include "gl/xalloc.h" /* Miscellaneous. */ @@ -50,6 +52,8 @@ void str_lowercase (char *); bool str_format_26adic (unsigned long int number, bool uppercase, char buffer[], size_t); +static inline char *xstrdup_if_nonnull (const char *); + void *mempset (void *, int, size_t); /* Common character classes for use with substring and string functions. */ @@ -281,4 +285,10 @@ ss_buffer (const char *buffer, size_t cnt) return ss; } +static inline char * +xstrdup_if_nonnull (const char *s) +{ + return s ? xstrdup (s) : NULL; +} + #endif /* str_h */ diff --git a/src/output/chart-item.c b/src/output/chart-item.c index 3b3761372e..cedfa37a60 100644 --- a/src/output/chart-item.c +++ b/src/output/chart-item.c @@ -24,6 +24,7 @@ #include "libpspp/cast.h" #include "libpspp/compiler.h" +#include "libpspp/str.h" #include "output/driver.h" #include "output/output-item-provider.h" @@ -46,7 +47,7 @@ chart_item_init (struct chart_item *item, const struct chart_item_class *class, *item = (struct chart_item) { .output_item = OUTPUT_ITEM_INITIALIZER (&chart_item_class), .class = class, - .title = title ? xstrdup (title) : NULL + .title = xstrdup_if_nonnull (title) }; } @@ -67,7 +68,7 @@ chart_item_set_title (struct chart_item *item, const char *title) { assert (!chart_item_is_shared (item)); free (item->title); - item->title = title != NULL ? xstrdup (title) : NULL; + item->title = xstrdup_if_nonnull (title); } /* Submits ITEM to the configured output drivers, and transfers ownership to diff --git a/src/output/driver.c b/src/output/driver.c index a4718c939a..824a1ee211 100644 --- a/src/output/driver.c +++ b/src/output/driver.c @@ -227,9 +227,8 @@ output_submit (struct output_item *item) if (e->n_groups >= e->allocated_groups) e->groups = x2nrealloc (e->groups, &e->allocated_groups, sizeof *e->groups); - e->groups[e->n_groups] = (group_open_item->command_name - ? xstrdup (group_open_item->command_name) - : NULL); + e->groups[e->n_groups] = xstrdup_if_nonnull ( + group_open_item->command_name); e->n_groups++; } else if (is_group_close_item (item)) @@ -298,7 +297,7 @@ static void output_set_title__ (struct output_engine *e, char **dst, const char *src) { free (*dst); - *dst = src ? xstrdup (src) : NULL; + *dst = xstrdup_if_nonnull (src); char *page_title = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle) diff --git a/src/output/group-item.c b/src/output/group-item.c index 3bfb44cd09..1c8fce5e8b 100644 --- a/src/output/group-item.c +++ b/src/output/group-item.c @@ -21,6 +21,7 @@ #include #include "libpspp/compiler.h" +#include "libpspp/str.h" #include "output/driver.h" #include "output/output-item-provider.h" @@ -33,8 +34,8 @@ struct group_open_item * group_open_item_create (const char *command_name, const char *label) { return group_open_item_create_nocopy ( - command_name ? xstrdup (command_name) : NULL, - label ? xstrdup (label) : NULL); + xstrdup_if_nonnull (command_name), + xstrdup_if_nonnull (label)); } struct group_open_item * diff --git a/src/output/options.c b/src/output/options.c index f5ab6fbc87..a212d1c63e 100644 --- a/src/output/options.c +++ b/src/output/options.c @@ -51,8 +51,8 @@ driver_option_create (const char *driver_name, const char *name, struct driver_option *o = xmalloc (sizeof *o); o->driver_name = xstrdup (driver_name); o->name = xstrdup (name); - o->value = value != NULL ? xstrdup (value) : NULL; - o->default_value = default_value ? xstrdup (default_value) : NULL; + o->value = xstrdup_if_nonnull (value); + o->default_value = xstrdup_if_nonnull (default_value); return o; } diff --git a/src/output/output-item-provider.h b/src/output/output-item-provider.h index 7517996119..552549cee9 100644 --- a/src/output/output-item-provider.h +++ b/src/output/output-item-provider.h @@ -17,6 +17,7 @@ #ifndef OUTPUT_ITEM_PROVIDER_H #define OUTPUT_ITEM_PROVIDER_H 1 +#include "libpspp/str.h" #include "output/output-item.h" /* Class structure for an output item. @@ -40,7 +41,7 @@ struct output_item_class { \ .class = (SRC)->class, \ .ref_cnt = 1, \ - .label = (SRC)->label ? xstrdup ((SRC)->label) : NULL, \ + .label = xstrdup_if_nonnull ((SRC)->label), \ } #endif /* output/output-item-provider.h */ diff --git a/src/output/output-item.c b/src/output/output-item.c index 860678b25f..cd37031b65 100644 --- a/src/output/output-item.c +++ b/src/output/output-item.c @@ -23,6 +23,7 @@ #include "libpspp/assertion.h" #include "libpspp/cast.h" +#include "libpspp/str.h" #include "gl/xalloc.h" @@ -79,7 +80,7 @@ output_item_get_label (const struct output_item *item) void output_item_set_label (struct output_item *item, const char *label) { - output_item_set_label_nocopy (item, label ? xstrdup (label) : NULL); + output_item_set_label_nocopy (item, xstrdup_if_nonnull (label)); } /* Sets the label for ITEM to LABEL, transferring ownership of LABEL to ITEM. diff --git a/src/output/pivot-table.c b/src/output/pivot-table.c index 1b3014c629..3698b78559 100644 --- a/src/output/pivot-table.c +++ b/src/output/pivot-table.c @@ -913,12 +913,6 @@ pivot_table_ref (const struct pivot_table *table_) return table; } -static char * -xstrdup_if_nonnull (const char *s) -{ - return s ? xstrdup (s) : NULL; -} - static struct pivot_table_sizing clone_sizing (const struct pivot_table_sizing *s) { diff --git a/src/output/spv/spv-legacy-decoder.c b/src/output/spv/spv-legacy-decoder.c index 0b224405b7..099d96cbcc 100644 --- a/src/output/spv/spv-legacy-decoder.c +++ b/src/output/spv/spv-legacy-decoder.c @@ -787,7 +787,7 @@ decode_spvdx_source_variable (const struct spvxml_node *node, struct spv_series *s = xzalloc (sizeof *s); s->name = xstrdup (node->id); s->xml = node; - s->label = sv->label ? xstrdup (sv->label) : NULL; + s->label = xstrdup_if_nonnull (sv->label); s->label_series = label_series; s->values = spv_data_values_clone (var->values, var->n_values); s->n_values = var->n_values; diff --git a/src/output/spv/spv-output.c b/src/output/spv/spv-output.c index edca066ed2..49cc195341 100644 --- a/src/output/spv/spv-output.c +++ b/src/output/spv/spv-output.c @@ -33,6 +33,6 @@ spv_text_submit (const struct spv_item *in) : class == SPV_CLASS_PAGETITLE ? TEXT_ITEM_PAGE_TITLE : TEXT_ITEM_LOG), pivot_value_clone (spv_item_get_text (in)), - in->label ? xstrdup (in->label) : NULL); + xstrdup_if_nonnull (in->label)); text_item_submit (item); } diff --git a/src/output/spv/spv-table-look.c b/src/output/spv/spv-table-look.c index 3e5f962bd3..75d5f97e82 100644 --- a/src/output/spv/spv-table-look.c +++ b/src/output/spv/spv-table-look.c @@ -131,7 +131,7 @@ spv_table_look_decode (const struct spvsx_table_properties *in, struct pivot_table_look *out = pivot_table_look_new_builtin_default (); char *error = NULL; - out->name = in->name ? xstrdup (in->name) : NULL; + out->name = xstrdup_if_nonnull (in->name); const struct spvsx_general_properties *g = in->general_properties; out->omit_empty = g->hide_empty_rows != 0; diff --git a/src/output/text-item.c b/src/output/text-item.c index 7bcf2aa10d..a0428acc82 100644 --- a/src/output/text-item.c +++ b/src/output/text-item.c @@ -75,7 +75,7 @@ text_item_create (enum text_item_type type, const char *text, const char *label) { return text_item_create_nocopy (type, xstrdup (text), - label ? xstrdup (label) : NULL); + xstrdup_if_nonnull (label)); } /* Creates and returns a new text item containing VALUE, TYPE, and LABEL. diff --git a/src/ui/terminal/terminal-reader.c b/src/ui/terminal/terminal-reader.c index 071e435eca..c7a9a311c2 100644 --- a/src/ui/terminal/terminal-reader.c +++ b/src/ui/terminal/terminal-reader.c @@ -22,6 +22,7 @@ #include #include +#include "libpspp/str.h" #if HAVE_READLINE #include @@ -408,7 +409,7 @@ command_generator (const char *text, int state) if (state == 0) cmd = NULL; name = cmd_complete (text, &cmd); - return name ? xstrdup (name) : NULL; + return xstrdup_if_nonnull (name); } #else /* !HAVE_READLINE */ diff --git a/tests/libpspp/stringi-map-test.c b/tests/libpspp/stringi-map-test.c index 20b8ce5c2c..a78fd56557 100644 --- a/tests/libpspp/stringi-map-test.c +++ b/tests/libpspp/stringi-map-test.c @@ -69,58 +69,6 @@ check_func (bool ok, int line) terminates. */ #define check(EXPR) check_func ((EXPR), __LINE__) -/* Prints a message about memory exhaustion and exits with a - failure code. */ -static void -xalloc_die (void) -{ - printf ("virtual memory exhausted\n"); - exit (EXIT_FAILURE); -} - -static void *xmalloc (size_t n) MALLOC_LIKE; -static void *xnmalloc (size_t n, size_t m) MALLOC_LIKE; -static void *xmemdup (const void *p, size_t n) MALLOC_LIKE; - -/* Allocates and returns N bytes of memory. */ -static void * -xmalloc (size_t n) -{ - if (n != 0) - { - void *p = malloc (n); - if (p == NULL) - xalloc_die (); - - return p; - } - else - return NULL; -} - -static void * -xmemdup (const void *p, size_t n) -{ - void *q = xmalloc (n); - memcpy (q, p, n); - return q; -} - -/* Clone STRING. */ -static char * -xstrdup (const char *string) -{ - return xmemdup (string, strlen (string) + 1); -} - -/* Allocates and returns N * M bytes of memory. */ -static void * -xnmalloc (size_t n, size_t m) -{ - if ((size_t) -1 / m <= n) - xalloc_die (); - return xmalloc (n * m); -} /* Support routines. */ diff --git a/tests/libpspp/stringi-set-test.c b/tests/libpspp/stringi-set-test.c index 2bcc760780..74f1ba11e6 100644 --- a/tests/libpspp/stringi-set-test.c +++ b/tests/libpspp/stringi-set-test.c @@ -63,59 +63,6 @@ check_func (bool ok, int line) If not, prints a message citing the calling line number and terminates. */ #define check(EXPR) check_func ((EXPR), __LINE__) - -/* Prints a message about memory exhaustion and exits with a - failure code. */ -static void -xalloc_die (void) -{ - printf ("virtual memory exhausted\n"); - exit (EXIT_FAILURE); -} - -static void *xmalloc (size_t n) MALLOC_LIKE; -static void *xnmalloc (size_t n, size_t m) MALLOC_LIKE; -static void *xmemdup (const void *p, size_t n) MALLOC_LIKE; - -/* Allocates and returns N bytes of memory. */ -static void * -xmalloc (size_t n) -{ - if (n != 0) - { - void *p = malloc (n); - if (p == NULL) - xalloc_die (); - - return p; - } - else - return NULL; -} - -static void * -xmemdup (const void *p, size_t n) -{ - void *q = xmalloc (n); - memcpy (q, p, n); - return q; -} - -/* Clone STRING. */ -static char * -xstrdup (const char *string) -{ - return xmemdup (string, strlen (string) + 1); -} - -/* Allocates and returns N * M bytes of memory. */ -static void * -xnmalloc (size_t n, size_t m) -{ - if ((size_t) -1 / m <= n) - xalloc_die (); - return xmalloc (n * m); -} /* Support routines. */