From faa521749378dbfd97f49a0e5c48f6da2ce1ddba Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 5 Apr 2014 12:30:36 -0700 Subject: [PATCH] Prefer 'ARRAYELTS (x)' to 'sizeof x / sizeof *x'. * alloc.c (memory_full): * charset.c (syms_of_charset): * doc.c (Fsnarf_documentation): * emacs.c (main): * font.c (BUILD_STYLE_TABLE): * keyboard.c (make_lispy_event): * profiler.c (setup_cpu_timer): * xgselect.c (xg_select): * xterm.c (record_event, STORE_KEYSYM_FOR_DEBUG): Use ARRAYELTS. * font.c (FONT_PROPERTY_TABLE_SIZE): Remove. Replace the only use with ARRAYELTS (font_property_table). * xfaces.c (DIM): Remove. All uses replaced by ARRAYELTS. --- src/ChangeLog | 17 +++++++++++++++++ src/alloc.c | 45 +++++++++++++++++++++++---------------------- src/charset.c | 2 +- src/coding.c | 4 ++-- src/coding.h | 2 +- src/doc.c | 2 +- src/emacs.c | 4 ++-- src/font.c | 9 ++------- src/gnutls.c | 12 ++++++------ src/keyboard.c | 11 ++++++----- src/keymap.c | 2 +- src/keymap.h | 2 +- src/lisp.h | 4 ++-- src/process.c | 6 +++--- src/profiler.c | 2 +- src/sysdep.c | 2 +- src/unexcoff.c | 2 +- src/xdisp.c | 2 +- src/xfaces.c | 14 +++++--------- src/xgselect.c | 2 +- src/xsmfns.c | 2 +- src/xterm.c | 4 ++-- src/xterm.h | 2 +- 23 files changed, 82 insertions(+), 72 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 7152890f45..a474c26118 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,20 @@ +2014-04-05 Paul Eggert + + Prefer 'ARRAYELTS (x)' to 'sizeof x / sizeof *x'. + * alloc.c (memory_full): + * charset.c (syms_of_charset): + * doc.c (Fsnarf_documentation): + * emacs.c (main): + * font.c (BUILD_STYLE_TABLE): + * keyboard.c (make_lispy_event): + * profiler.c (setup_cpu_timer): + * xgselect.c (xg_select): + * xterm.c (record_event, STORE_KEYSYM_FOR_DEBUG): + Use ARRAYELTS. + * font.c (FONT_PROPERTY_TABLE_SIZE): Remove. + Replace the only use with ARRAYELTS (font_property_table). + * xfaces.c (DIM): Remove. All uses replaced by ARRAYELTS. + 2014-04-03 Daniel Colascione * xterm.c (x_term_init): diff --git a/src/alloc.c b/src/alloc.c index 9740afe2ab..2919c21dfe 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -208,19 +208,19 @@ const char *pending_malloc_warning; #ifdef SUSPICIOUS_OBJECT_CHECKING struct suspicious_free_record { - void* suspicious_object; + void *suspicious_object; #ifdef HAVE_EXECINFO_H - void* backtrace[128]; + void *backtrace[128]; #endif }; -static void* suspicious_objects[32]; +static void *suspicious_objects[32]; static int suspicious_object_index; struct suspicious_free_record suspicious_free_history[64]; static int suspicious_free_history_index; /* Find the first currently-monitored suspicious pointer in range [begin,end) or NULL if no such pointer exists. */ -static void* find_suspicious_object_in_range (void* begin, void* end); -static void detect_suspicious_free (void* ptr); +static void *find_suspicious_object_in_range (void *begin, void *end); +static void detect_suspicious_free (void *ptr); #else #define find_suspicious_object_in_range(begin, end) NULL #define detect_suspicious_free(ptr) (void) @@ -3116,7 +3116,7 @@ allocate_vectorlike (ptrdiff_t len) mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); #endif - if (find_suspicious_object_in_range (p, (char*)p + nbytes)) + if (find_suspicious_object_in_range (p, (char *) p + nbytes)) emacs_abort (); consing_since_gc += nbytes; @@ -3765,7 +3765,7 @@ memory_full (size_t nbytes) memory_full_cons_threshold = sizeof (struct cons_block); /* The first time we get here, free the spare memory. */ - for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++) + for (i = 0; i < ARRAYELTS (spare_memory); i++) if (spare_memory[i]) { if (i == 0) @@ -3818,7 +3818,6 @@ refill_memory_reserve (void) Vmemory_full = Qnil; #endif } - /************************************************************************ C Stack Marking @@ -6829,23 +6828,24 @@ which_symbols (Lisp_Object obj, EMACS_INT find_max) #ifdef SUSPICIOUS_OBJECT_CHECKING static void* -find_suspicious_object_in_range (void* begin, void* end) +find_suspicious_object_in_range (void *begin, void *end) { - char* begin_a = begin; - char* end_a = end; + char *begin_a = begin; + char *end_a = end; int i; - for (i = 0; i < ARRAYELTS (suspicious_objects); ++i) { - char* suspicious_object = suspicious_objects[i]; - if (begin_a <= suspicious_object && suspicious_object < end_a) - return suspicious_object; - } + for (i = 0; i < ARRAYELTS (suspicious_objects); ++i) + { + char *suspicious_object = suspicious_objects[i]; + if (begin_a <= suspicious_object && suspicious_object < end_a) + return suspicious_object; + } return NULL; } static void -detect_suspicious_free (void* ptr) +detect_suspicious_free (void *ptr) { int i; struct suspicious_free_record* rec; @@ -6882,11 +6882,12 @@ garbage collection bugs. Otherwise, do nothing and return OBJ. */) { #ifdef SUSPICIOUS_OBJECT_CHECKING /* Right now, we care only about vectors. */ - if (VECTORLIKEP (obj)) { - suspicious_objects[suspicious_object_index++] = XVECTOR (obj); - if (suspicious_object_index == ARRAYELTS (suspicious_objects)) - suspicious_object_index = 0; - } + if (VECTORLIKEP (obj)) + { + suspicious_objects[suspicious_object_index++] = XVECTOR (obj); + if (suspicious_object_index == ARRAYELTS (suspicious_objects)) + suspicious_object_index = 0; + } #endif return obj; } diff --git a/src/charset.c b/src/charset.c index 3566b15673..baa692232c 100644 --- a/src/charset.c +++ b/src/charset.c @@ -2386,7 +2386,7 @@ syms_of_charset (void) } charset_table = charset_table_init; - charset_table_size = sizeof charset_table_init / sizeof *charset_table_init; + charset_table_size = ARRAYELTS (charset_table_init); charset_table_used = 0; defsubr (&Scharsetp); diff --git a/src/coding.c b/src/coding.c index 654e39c0e3..fbe14f1695 100644 --- a/src/coding.c +++ b/src/coding.c @@ -8443,11 +8443,11 @@ from_unicode (Lisp_Object str) } Lisp_Object -from_unicode_buffer (const wchar_t* wstr) +from_unicode_buffer (const wchar_t *wstr) { return from_unicode ( make_unibyte_string ( - (char*) wstr, + (char *) wstr, /* we get one of the two final 0 bytes for free. */ 1 + sizeof (wchar_t) * wcslen (wstr))); } diff --git a/src/coding.h b/src/coding.h index 4e8b1056e4..f3efcca031 100644 --- a/src/coding.h +++ b/src/coding.h @@ -743,7 +743,7 @@ extern wchar_t *to_unicode (Lisp_Object str, Lisp_Object *buf); extern Lisp_Object from_unicode (Lisp_Object str); /* Convert WSTR to an Emacs string. */ -extern Lisp_Object from_unicode_buffer (const wchar_t* wstr); +extern Lisp_Object from_unicode_buffer (const wchar_t *wstr); #endif /* WINDOWSNT || CYGWIN */ diff --git a/src/doc.c b/src/doc.c index b6a4cd0a27..df8cfba3f2 100644 --- a/src/doc.c +++ b/src/doc.c @@ -595,7 +595,7 @@ the same file name is found in the `doc-directory'. */) { #include "buildobj.h" }; - int i = sizeof buildobj / sizeof *buildobj; + int i = ARRAYELTS (buildobj); while (0 <= --i) Vbuild_files = Fcons (build_string (buildobj[i]), Vbuild_files); Vbuild_files = Fpurecopy (Vbuild_files); diff --git a/src/emacs.c b/src/emacs.c index 75498dce50..deebb2280c 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -136,7 +136,7 @@ static void *malloc_state_ptr; /* From glibc, a routine that returns a copy of the malloc internal state. */ extern void *malloc_get_state (void); /* From glibc, a routine that overwrites the malloc internal state. */ -extern int malloc_set_state (void*); +extern int malloc_set_state (void *); /* True if the MALLOC_CHECK_ environment variable was set while dumping. Used to work around a bug in glibc's malloc. */ static bool malloc_using_checking; @@ -1008,7 +1008,7 @@ main (int argc, char **argv) { int i; printf ("Usage: %s [OPTION-OR-FILENAME]...\n", argv[0]); - for (i = 0; i < sizeof usage_message / sizeof *usage_message; i++) + for (i = 0; i < ARRAYELTS (usage_message); i++) fputs (usage_message[i], stdout); exit (0); } diff --git a/src/font.c b/src/font.c index e99141bfe5..5faf477fa9 100644 --- a/src/font.c +++ b/src/font.c @@ -662,10 +662,6 @@ static const struct { &QCotf, font_prop_validate_otf } }; -/* Size (number of elements) of the above table. */ -#define FONT_PROPERTY_TABLE_SIZE \ - ((sizeof font_property_table) / (sizeof *font_property_table)) - /* Return an index number of font property KEY or -1 if KEY is not an already known property. */ @@ -674,7 +670,7 @@ get_font_prop_index (Lisp_Object key) { int i; - for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++) + for (i = 0; i < ARRAYELTS (font_property_table); i++) if (EQ (key, *font_property_table[i].key)) return i; return -1; @@ -4935,8 +4931,7 @@ If the named font is not yet loaded, return nil. */) #endif -#define BUILD_STYLE_TABLE(TBL) \ - build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry)) +#define BUILD_STYLE_TABLE(TBL) build_style_table (TBL, ARRAYELTS (TBL)) static Lisp_Object build_style_table (const struct table_entry *entry, int nelement) diff --git a/src/gnutls.c b/src/gnutls.c index 03c29d0301..d9b417b46e 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -56,7 +56,7 @@ static Lisp_Object QCgnutls_bootprop_verify_error; static Lisp_Object QCgnutls_bootprop_callbacks_verify; static void gnutls_log_function (int, const char *); -static void gnutls_log_function2 (int, const char*, const char*); +static void gnutls_log_function2 (int, const char *, const char *); #ifdef HAVE_GNUTLS3 static void gnutls_audit_log_function (gnutls_session_t, const char *); #endif @@ -267,7 +267,7 @@ init_gnutls_functions (void) #ifdef HAVE_GNUTLS3 /* Function to log a simple audit message. */ static void -gnutls_audit_log_function (gnutls_session_t session, const char* string) +gnutls_audit_log_function (gnutls_session_t session, const char *string) { if (global_gnutls_log_level >= 1) { @@ -278,21 +278,21 @@ gnutls_audit_log_function (gnutls_session_t session, const char* string) /* Function to log a simple message. */ static void -gnutls_log_function (int level, const char* string) +gnutls_log_function (int level, const char *string) { message ("gnutls.c: [%d] %s", level, string); } /* Function to log a message and a string. */ static void -gnutls_log_function2 (int level, const char* string, const char* extra) +gnutls_log_function2 (int level, const char *string, const char *extra) { message ("gnutls.c: [%d] %s %s", level, string, extra); } /* Function to log a message and an integer. */ static void -gnutls_log_function2i (int level, const char* string, int extra) +gnutls_log_function2i (int level, const char *string, int extra) { message ("gnutls.c: [%d] %s %d", level, string, extra); } @@ -794,7 +794,7 @@ one trustfile (usually a CA bundle). */) Lisp_Object global_init; char const *priority_string_ptr = "NORMAL"; /* default priority string. */ unsigned int peer_verification; - char* c_hostname; + char *c_hostname; /* Placeholders for the property list elements. */ Lisp_Object priority_string; diff --git a/src/keyboard.c b/src/keyboard.c index 6de537085f..277d4f1047 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -5478,7 +5478,7 @@ make_lispy_event (struct input_event *event) case NON_ASCII_KEYSTROKE_EVENT: button_down_time = 0; - for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++) + for (i = 0; i < ARRAYELTS (lispy_accent_codes); i++) if (event->code == lispy_accent_codes[i]) return modify_event_symbol (i, event->modifiers, @@ -5511,7 +5511,7 @@ make_lispy_event (struct input_event *event) if (event->code & (1 << 28) || event->code - FUNCTION_KEY_OFFSET < 0 || (event->code - FUNCTION_KEY_OFFSET - >= sizeof lispy_function_keys / sizeof *lispy_function_keys) + >= ARRAYELTS (lispy_function_keys)) || !lispy_function_keys[event->code - FUNCTION_KEY_OFFSET]) { /* We need to use an alist rather than a vector as the cache @@ -7282,7 +7282,7 @@ store_user_signal_events (void) } -static void menu_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, void*); +static void menu_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, void *); static Lisp_Object menu_bar_one_keymap_changed_items; /* These variables hold the vector under construction within @@ -7292,7 +7292,7 @@ static Lisp_Object menu_bar_items_vector; static int menu_bar_items_index; -static const char* separator_names[] = { +static const char *separator_names[] = { "space", "no-line", "single-line", @@ -7900,7 +7900,8 @@ static Lisp_Object QCrtl; /* Function prototypes. */ static void init_tool_bar_items (Lisp_Object); -static void process_tool_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, void*); +static void process_tool_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, + void *); static bool parse_tool_bar_item (Lisp_Object, Lisp_Object); static void append_tool_bar_item (void); diff --git a/src/keymap.c b/src/keymap.c index c1416f8a78..663c4661be 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -1894,7 +1894,7 @@ struct accessible_keymaps_data { static void accessible_keymaps_1 (Lisp_Object key, Lisp_Object cmd, Lisp_Object args, void *data) -/* Use void* data to be compatible with map_keymap_function_t. */ +/* Use void * data to be compatible with map_keymap_function_t. */ { struct accessible_keymaps_data *d = data; /* Cast! */ Lisp_Object maps = d->maps; diff --git a/src/keymap.h b/src/keymap.h index 4a408c6a2a..b01886dfa6 100644 --- a/src/keymap.h +++ b/src/keymap.h @@ -46,7 +46,7 @@ extern void syms_of_keymap (void); extern void keys_of_keymap (void); typedef void (*map_keymap_function_t) - (Lisp_Object key, Lisp_Object val, Lisp_Object args, void* data); + (Lisp_Object key, Lisp_Object val, Lisp_Object args, void *data); extern void map_keymap (Lisp_Object, map_keymap_function_t, Lisp_Object, void *, bool); extern void map_keymap_canonical (Lisp_Object map, diff --git a/src/lisp.h b/src/lisp.h index a697e39a20..03c0d99fb2 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -58,8 +58,8 @@ INLINE_HEADER_BEGIN #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) -/* Find number of elements in array */ -#define ARRAYELTS(arr) (sizeof (arr) / sizeof ((arr)[0])) +/* Number of elements in an array. */ +#define ARRAYELTS(arr) (sizeof (arr) / sizeof (arr)[0]) /* EMACS_INT - signed integer wide enough to hold an Emacs value EMACS_INT_MAX - maximum value of EMACS_INT; can be used in #if diff --git a/src/process.c b/src/process.c index fe365a136d..5a5ce0ce67 100644 --- a/src/process.c +++ b/src/process.c @@ -2022,11 +2022,11 @@ conv_sockaddr_to_lisp (struct sockaddr *sa, int len) terminator, however. */ if (name_length > 0 && sockun->sun_path[0] != '\0') { - const char* terminator = - memchr (sockun->sun_path, '\0', name_length); + const char *terminator + = memchr (sockun->sun_path, '\0', name_length); if (terminator) - name_length = terminator - (const char*) sockun->sun_path; + name_length = terminator - (const char *) sockun->sun_path; } return make_unibyte_string (sockun->sun_path, name_length); diff --git a/src/profiler.c b/src/profiler.c index 8b092dcc81..ff97fe88b7 100644 --- a/src/profiler.c +++ b/src/profiler.c @@ -294,7 +294,7 @@ setup_cpu_timer (Lisp_Object sampling_interval) sigev.sigev_signo = SIGPROF; sigev.sigev_notify = SIGEV_SIGNAL; - for (i = 0; i < sizeof system_clock / sizeof *system_clock; i++) + for (i = 0; i < ARRAYELTS (system_clock); i++) if (timer_create (system_clock[i], &sigev, &profiler_timer) == 0) { profiler_timer_ok = 1; diff --git a/src/sysdep.c b/src/sysdep.c index 964dc419d2..7a888834bc 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -128,7 +128,7 @@ static const int baud_convert[] = /* Return the current working directory. Returns NULL on errors. Any other returned value must be freed with free. This is used only when get_current_dir_name is not defined on the system. */ -char* +char * get_current_dir_name (void) { char *buf; diff --git a/src/unexcoff.c b/src/unexcoff.c index 5710ac0457..cb0b6f2f0b 100644 --- a/src/unexcoff.c +++ b/src/unexcoff.c @@ -120,7 +120,7 @@ static int pagemask; into an int which is the number of a byte. This is a no-op on ordinary machines, but not on all. */ -#define ADDR_CORRECT(x) ((char *)(x) - (char*)0) +#define ADDR_CORRECT(x) ((char *) (x) - (char *) 0) #include "lisp.h" diff --git a/src/xdisp.c b/src/xdisp.c index 53bd46328f..3431a7bad2 100644 --- a/src/xdisp.c +++ b/src/xdisp.c @@ -22639,7 +22639,7 @@ decode_mode_spec (struct window *w, register int c, int field_width, return decode_mode_spec_buf; no_value: { - char* p = decode_mode_spec_buf; + char *p = decode_mode_spec_buf; int pad = width - 2; while (pad-- > 0) *p++ = ' '; diff --git a/src/xfaces.c b/src/xfaces.c index 7a630704fc..4e599d0bd0 100644 --- a/src/xfaces.c +++ b/src/xfaces.c @@ -273,10 +273,6 @@ along with GNU Emacs. If not, see . */ #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface) -/* Value is the number of elements of VECTOR. */ - -#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR)) - /* Size of hash table of realized faces in face caches (should be a prime number). */ @@ -5093,14 +5089,14 @@ Value is ORDER. */) { Lisp_Object list; int i; - int indices[DIM (font_sort_order)]; + int indices[ARRAYELTS (font_sort_order)]; CHECK_LIST (order); memset (indices, 0, sizeof indices); i = 0; for (list = order; - CONSP (list) && i < DIM (indices); + CONSP (list) && i < ARRAYELTS (indices); list = XCDR (list), ++i) { Lisp_Object attr = XCAR (list); @@ -5122,9 +5118,9 @@ Value is ORDER. */) indices[i] = xlfd; } - if (!NILP (list) || i != DIM (indices)) + if (!NILP (list) || i != ARRAYELTS (indices)) signal_error ("Invalid font sort order", order); - for (i = 0; i < DIM (font_sort_order); ++i) + for (i = 0; i < ARRAYELTS (font_sort_order); ++i) if (indices[i] == 0) signal_error ("Invalid font sort order", order); @@ -6348,7 +6344,7 @@ DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */) int i; fprintf (stderr, "font selection order: "); - for (i = 0; i < DIM (font_sort_order); ++i) + for (i = 0; i < ARRAYELTS (font_sort_order); ++i) fprintf (stderr, "%d ", font_sort_order[i]); fprintf (stderr, "\n"); diff --git a/src/xgselect.c b/src/xgselect.c index 1d3f916c9f..5f71ff8401 100644 --- a/src/xgselect.c +++ b/src/xgselect.c @@ -41,7 +41,7 @@ xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds, int have_wfds = wfds != NULL; GPollFD gfds_buf[128]; GPollFD *gfds = gfds_buf; - int gfds_size = sizeof gfds_buf / sizeof *gfds_buf; + int gfds_size = ARRAYELTS (gfds_buf); int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1; int i, nfds, tmo_in_millisec; bool need_to_dispatch; diff --git a/src/xsmfns.c b/src/xsmfns.c index 9a31f518f3..81b012690f 100644 --- a/src/xsmfns.c +++ b/src/xsmfns.c @@ -395,7 +395,7 @@ x_session_initialize (struct x_display_info *dpyinfo) { #define SM_ERRORSTRING_LEN 512 char errorstring[SM_ERRORSTRING_LEN]; - char* previous_id = NULL; + char *previous_id = NULL; SmcCallbacks callbacks; ptrdiff_t name_len = 0; diff --git a/src/xterm.c b/src/xterm.c index a04f2fef05..dd71a8a198 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -308,7 +308,7 @@ int event_record_index; void record_event (char *locus, int type) { - if (event_record_index == sizeof (event_record) / sizeof (struct record)) + if (event_record_index == ARRAYELTS (event_record)) event_record_index = 0; event_record[event_record_index].locus = locus; @@ -5624,7 +5624,7 @@ static int temp_index; static short temp_buffer[100]; #define STORE_KEYSYM_FOR_DEBUG(keysym) \ - if (temp_index == sizeof temp_buffer / sizeof (short)) \ + if (temp_index == ARRAYELTS (temp_buffer)) \ temp_index = 0; \ temp_buffer[temp_index++] = (keysym) diff --git a/src/xterm.h b/src/xterm.h index 50df88cb59..90d2e13171 100644 --- a/src/xterm.h +++ b/src/xterm.h @@ -996,7 +996,7 @@ extern Lisp_Object x_get_focus_frame (struct frame *); #ifdef USE_GTK extern int xg_set_icon (struct frame *, Lisp_Object); -extern int xg_set_icon_from_xpm_data (struct frame *, const char**); +extern int xg_set_icon_from_xpm_data (struct frame *, const char **); #endif /* USE_GTK */ extern void x_implicitly_set_name (struct frame *, Lisp_Object, Lisp_Object); -- 2.20.1