From 509f8dab8946cfc311def89f62352c0881e73348 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Oct 2013 22:04:17 +0200 Subject: Util/Macros: replacement for GLib's G_N_ELEMENTS() --- src/ArchiveList.cxx | 4 ++-- src/DecoderList.cxx | 8 +++----- src/InputRegistry.cxx | 7 +++---- src/PlaylistRegistry.cxx | 8 ++++++-- src/StickerDatabase.cxx | 7 ++++--- src/UpdateQueue.cxx | 5 +++-- src/decoder/AdPlugDecoderPlugin.cxx | 5 ++--- src/decoder/FluidsynthDecoderPlugin.cxx | 5 ++--- src/decoder/MpcdecDecoderPlugin.cxx | 4 ++-- src/decoder/VorbisDecoderPlugin.cxx | 3 ++- src/decoder/WavpackDecoderPlugin.cxx | 3 ++- src/output/OssOutputPlugin.cxx | 11 ++++++----- src/output/WinmmOutputPlugin.cxx | 11 ++++++----- src/pcm/PcmDsd.cxx | 11 +++++------ src/util/Macros.hxx | 35 +++++++++++++++++++++++++++++++++ src/util/list_sort.c | 6 +++--- 16 files changed, 86 insertions(+), 47 deletions(-) create mode 100644 src/util/Macros.hxx (limited to 'src') diff --git a/src/ArchiveList.cxx b/src/ArchiveList.cxx index 894e31031..d4d6835d1 100644 --- a/src/ArchiveList.cxx +++ b/src/ArchiveList.cxx @@ -24,9 +24,9 @@ #include "archive/Bzip2ArchivePlugin.hxx" #include "archive/Iso9660ArchivePlugin.hxx" #include "archive/ZzipArchivePlugin.hxx" +#include "util/Macros.hxx" #include -#include const struct archive_plugin *const archive_plugins[] = { #ifdef HAVE_BZ2 @@ -42,7 +42,7 @@ const struct archive_plugin *const archive_plugins[] = { }; /** which plugins have been initialized successfully? */ -static bool archive_plugins_enabled[G_N_ELEMENTS(archive_plugins) - 1]; +static bool archive_plugins_enabled[ARRAY_SIZE(archive_plugins) - 1]; #define archive_plugins_for_each_enabled(plugin) \ archive_plugins_for_each(plugin) \ diff --git a/src/DecoderList.cxx b/src/DecoderList.cxx index fd80514dd..0cdcc07ae 100644 --- a/src/DecoderList.cxx +++ b/src/DecoderList.cxx @@ -43,8 +43,7 @@ #include "decoder/MpcdecDecoderPlugin.hxx" #include "decoder/FluidsynthDecoderPlugin.hxx" #include "system/FatalError.hxx" - -#include +#include "util/Macros.hxx" #include @@ -114,9 +113,8 @@ const struct decoder_plugin *const decoder_plugins[] = { NULL }; -enum { - num_decoder_plugins = G_N_ELEMENTS(decoder_plugins) - 1, -}; +static constexpr unsigned num_decoder_plugins = + ARRAY_SIZE(decoder_plugins) - 1; /** which plugins have been initialized successfully? */ bool decoder_plugins_enabled[num_decoder_plugins]; diff --git a/src/InputRegistry.cxx b/src/InputRegistry.cxx index 75a106c24..d7481ebee 100644 --- a/src/InputRegistry.cxx +++ b/src/InputRegistry.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "InputRegistry.hxx" +#include "util/Macros.hxx" #include "input/FileInputPlugin.hxx" #ifdef ENABLE_ARCHIVE @@ -45,8 +46,6 @@ #include "input/DespotifyInputPlugin.hxx" #endif -#include - const struct input_plugin *const input_plugins[] = { &input_plugin_file, #ifdef ENABLE_ARCHIVE @@ -67,7 +66,7 @@ const struct input_plugin *const input_plugins[] = { #ifdef ENABLE_DESPOTIFY &input_plugin_despotify, #endif - NULL + nullptr }; -bool input_plugins_enabled[G_N_ELEMENTS(input_plugins) - 1]; +bool input_plugins_enabled[ARRAY_SIZE(input_plugins) - 1]; diff --git a/src/PlaylistRegistry.cxx b/src/PlaylistRegistry.cxx index fa33596f0..fe6b4cd33 100644 --- a/src/PlaylistRegistry.cxx +++ b/src/PlaylistRegistry.cxx @@ -34,6 +34,7 @@ #include "util/UriUtil.hxx" #include "util/StringUtil.hxx" #include "util/Error.hxx" +#include "util/Macros.hxx" #include "ConfigGlobal.hxx" #include "ConfigData.hxx" #include "system/FatalError.hxx" @@ -61,8 +62,11 @@ const struct playlist_plugin *const playlist_plugins[] = { nullptr }; +static constexpr unsigned n_playlist_plugins = + ARRAY_SIZE(playlist_plugins) - 1; + /** which plugins have been initialized successfully? */ -static bool playlist_plugins_enabled[G_N_ELEMENTS(playlist_plugins)]; +static bool playlist_plugins_enabled[n_playlist_plugins]; #define playlist_plugins_for_each_enabled(plugin) \ playlist_plugins_for_each(plugin) \ @@ -189,7 +193,7 @@ playlist_list_open_uri(const char *uri, Mutex &mutex, Cond &cond) { /** this array tracks which plugins have already been tried by playlist_list_open_uri_scheme() */ - bool tried[G_N_ELEMENTS(playlist_plugins) - 1]; + bool tried[n_playlist_plugins]; assert(uri != nullptr); diff --git a/src/StickerDatabase.cxx b/src/StickerDatabase.cxx index 1cd47a605..21bf7706f 100644 --- a/src/StickerDatabase.cxx +++ b/src/StickerDatabase.cxx @@ -23,6 +23,7 @@ #include "Idle.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/Macros.hxx" #include "Log.hxx" #include @@ -79,7 +80,7 @@ static const char sticker_sql_create[] = ""; static sqlite3 *sticker_db; -static sqlite3_stmt *sticker_stmt[G_N_ELEMENTS(sticker_sql)]; +static sqlite3_stmt *sticker_stmt[ARRAY_SIZE(sticker_sql)]; static constexpr Domain sticker_domain("sticker"); @@ -138,7 +139,7 @@ sticker_global_init(Path &&path, Error &error) /* prepare the statements we're going to use */ - for (unsigned i = 0; i < G_N_ELEMENTS(sticker_sql); ++i) { + for (unsigned i = 0; i < ARRAY_SIZE(sticker_sql); ++i) { assert(sticker_sql[i] != NULL); sticker_stmt[i] = sticker_prepare(sticker_sql[i], error); @@ -156,7 +157,7 @@ sticker_global_finish(void) /* not configured */ return; - for (unsigned i = 0; i < G_N_ELEMENTS(sticker_stmt); ++i) { + for (unsigned i = 0; i < ARRAY_SIZE(sticker_stmt); ++i) { assert(sticker_stmt[i] != NULL); sqlite3_finalize(sticker_stmt[i]); diff --git a/src/UpdateQueue.cxx b/src/UpdateQueue.cxx index 85eb22358..9479e76f5 100644 --- a/src/UpdateQueue.cxx +++ b/src/UpdateQueue.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "UpdateQueue.hxx" +#include "util/Macros.hxx" #include @@ -36,9 +37,9 @@ static size_t update_queue_length; unsigned update_queue_push(const char *path, bool discard, unsigned base) { - assert(update_queue_length <= G_N_ELEMENTS(update_queue)); + assert(update_queue_length <= ARRAY_SIZE(update_queue)); - if (update_queue_length == G_N_ELEMENTS(update_queue)) + if (update_queue_length == ARRAY_SIZE(update_queue)) return 0; update_queue[update_queue_length].path = g_strdup(path); diff --git a/src/decoder/AdPlugDecoderPlugin.cxx b/src/decoder/AdPlugDecoderPlugin.cxx index 3ec48cb23..daac0a740 100644 --- a/src/decoder/AdPlugDecoderPlugin.cxx +++ b/src/decoder/AdPlugDecoderPlugin.cxx @@ -23,13 +23,12 @@ #include "DecoderAPI.hxx" #include "CheckAudioFormat.hxx" #include "util/Error.hxx" +#include "util/Macros.hxx" #include "Log.hxx" #include #include -#include - #include static unsigned sample_rate; @@ -65,7 +64,7 @@ adplug_file_decode(struct decoder *decoder, const char *path_fs) player->songlength() / 1000.); int16_t buffer[2048]; - const unsigned frames_per_buffer = G_N_ELEMENTS(buffer) / 2; + const unsigned frames_per_buffer = ARRAY_SIZE(buffer) / 2; DecoderCommand cmd; do { diff --git a/src/decoder/FluidsynthDecoderPlugin.cxx b/src/decoder/FluidsynthDecoderPlugin.cxx index 99f1598c8..5b8909492 100644 --- a/src/decoder/FluidsynthDecoderPlugin.cxx +++ b/src/decoder/FluidsynthDecoderPlugin.cxx @@ -23,10 +23,9 @@ #include "CheckAudioFormat.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/Macros.hxx" #include "Log.hxx" -#include - #include static constexpr Domain fluidsynth_domain("fluidsynth"); @@ -171,7 +170,7 @@ fluidsynth_file_decode(struct decoder *decoder, const char *path_fs) DecoderCommand cmd; while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) { int16_t buffer[2048]; - const unsigned max_frames = G_N_ELEMENTS(buffer) / 2; + const unsigned max_frames = ARRAY_SIZE(buffer) / 2; /* read samples from fluidsynth and send them to the MPD core */ diff --git a/src/decoder/MpcdecDecoderPlugin.cxx b/src/decoder/MpcdecDecoderPlugin.cxx index c0785accc..dd3319831 100644 --- a/src/decoder/MpcdecDecoderPlugin.cxx +++ b/src/decoder/MpcdecDecoderPlugin.cxx @@ -25,11 +25,11 @@ #include "tag/TagHandler.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/Macros.hxx" #include "Log.hxx" #include -#include #include #include #include @@ -212,7 +212,7 @@ mpcdec_decode(struct decoder *mpd_decoder, struct input_stream *is) mpc_uint32_t ret = frame.samples; ret *= info.channels; - int32_t chunk[G_N_ELEMENTS(sample_buffer)]; + int32_t chunk[ARRAY_SIZE(sample_buffer)]; mpc_to_mpd_buffer(chunk, sample_buffer, ret); long bit_rate = vbr_update_bits * audio_format.sample_rate diff --git a/src/decoder/VorbisDecoderPlugin.cxx b/src/decoder/VorbisDecoderPlugin.cxx index 55ce943e8..9ef5e2eea 100644 --- a/src/decoder/VorbisDecoderPlugin.cxx +++ b/src/decoder/VorbisDecoderPlugin.cxx @@ -26,6 +26,7 @@ #include "OggCodec.hxx" #include "util/Error.hxx" #include "util/UriUtil.hxx" +#include "util/Macros.hxx" #include "CheckAudioFormat.hxx" #include "tag/TagHandler.hxx" #include "Log.hxx" @@ -226,7 +227,7 @@ vorbis_stream_decode(struct decoder *decoder, #else float buffer[2048]; const int frames_per_buffer = - G_N_ELEMENTS(buffer) / audio_format.channels; + ARRAY_SIZE(buffer) / audio_format.channels; const unsigned frame_size = sizeof(buffer[0]) * audio_format.channels; #endif diff --git a/src/decoder/WavpackDecoderPlugin.cxx b/src/decoder/WavpackDecoderPlugin.cxx index 8ee898e30..8c6d9f927 100644 --- a/src/decoder/WavpackDecoderPlugin.cxx +++ b/src/decoder/WavpackDecoderPlugin.cxx @@ -26,6 +26,7 @@ #include "tag/ApeTag.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/Macros.hxx" #include "Log.hxx" #include @@ -173,7 +174,7 @@ wavpack_decode(struct decoder *decoder, WavpackContext *wpc, bool can_seek) /* wavpack gives us all kind of samples in a 32-bit space */ int32_t chunk[1024]; - const uint32_t samples_requested = G_N_ELEMENTS(chunk) / + const uint32_t samples_requested = ARRAY_SIZE(chunk) / audio_format.channels; decoder_initialized(decoder, audio_format, can_seek, total_time); diff --git a/src/output/OssOutputPlugin.cxx b/src/output/OssOutputPlugin.cxx index 781e2bf43..147ad7975 100644 --- a/src/output/OssOutputPlugin.cxx +++ b/src/output/OssOutputPlugin.cxx @@ -24,6 +24,7 @@ #include "system/fd_util.h" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/Macros.hxx" #include "Log.hxx" #include @@ -133,7 +134,7 @@ oss_output_test_default_device(void) { int fd, i; - for (i = G_N_ELEMENTS(default_devices); --i >= 0; ) { + for (i = ARRAY_SIZE(default_devices); --i >= 0; ) { fd = open_cloexec(default_devices[i], O_WRONLY, 0); if (fd >= 0) { @@ -152,11 +153,11 @@ oss_output_test_default_device(void) static struct audio_output * oss_open_default(Error &error) { - int err[G_N_ELEMENTS(default_devices)]; - enum oss_stat ret[G_N_ELEMENTS(default_devices)]; + int err[ARRAY_SIZE(default_devices)]; + enum oss_stat ret[ARRAY_SIZE(default_devices)]; const config_param empty; - for (int i = G_N_ELEMENTS(default_devices); --i >= 0; ) { + for (int i = ARRAY_SIZE(default_devices); --i >= 0; ) { ret[i] = oss_stat_device(default_devices[i], &err[i]); if (ret[i] == OSS_STAT_NO_ERROR) { OssOutput *od = new OssOutput(); @@ -170,7 +171,7 @@ oss_open_default(Error &error) } } - for (int i = G_N_ELEMENTS(default_devices); --i >= 0; ) { + for (int i = ARRAY_SIZE(default_devices); --i >= 0; ) { const char *dev = default_devices[i]; switch(ret[i]) { case OSS_STAT_NO_ERROR: diff --git a/src/output/WinmmOutputPlugin.cxx b/src/output/WinmmOutputPlugin.cxx index 44d0a8a5f..d2508ee2a 100644 --- a/src/output/WinmmOutputPlugin.cxx +++ b/src/output/WinmmOutputPlugin.cxx @@ -24,6 +24,7 @@ #include "MixerList.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/Macros.hxx" #include @@ -183,7 +184,7 @@ winmm_output_open(struct audio_output *ao, AudioFormat &audio_format, return false; } - for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) { + for (unsigned i = 0; i < ARRAY_SIZE(wo->buffers); ++i) { memset(&wo->buffers[i].hdr, 0, sizeof(wo->buffers[i].hdr)); } @@ -197,7 +198,7 @@ winmm_output_close(struct audio_output *ao) { WinmmOutput *wo = (WinmmOutput *)ao; - for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) + for (unsigned i = 0; i < ARRAY_SIZE(wo->buffers); ++i) wo->buffers[i].buffer.Clear(); waveOutClose(wo->handle); @@ -285,7 +286,7 @@ winmm_output_play(struct audio_output *ao, const void *chunk, size_t size, Error /* mark our buffer as "used" */ wo->next_buffer = (wo->next_buffer + 1) % - G_N_ELEMENTS(wo->buffers); + ARRAY_SIZE(wo->buffers); return size; } @@ -293,7 +294,7 @@ winmm_output_play(struct audio_output *ao, const void *chunk, size_t size, Error static bool winmm_drain_all_buffers(WinmmOutput *wo, Error &error) { - for (unsigned i = wo->next_buffer; i < G_N_ELEMENTS(wo->buffers); ++i) + for (unsigned i = wo->next_buffer; i < ARRAY_SIZE(wo->buffers); ++i) if (!winmm_drain_buffer(wo, &wo->buffers[i], error)) return false; @@ -309,7 +310,7 @@ winmm_stop(WinmmOutput *wo) { waveOutReset(wo->handle); - for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) { + for (unsigned i = 0; i < ARRAY_SIZE(wo->buffers); ++i) { WinmmBuffer *buffer = &wo->buffers[i]; waveOutUnprepareHeader(wo->handle, &buffer->hdr, sizeof(buffer->hdr)); diff --git a/src/pcm/PcmDsd.cxx b/src/pcm/PcmDsd.cxx index 096c5464a..4db274635 100644 --- a/src/pcm/PcmDsd.cxx +++ b/src/pcm/PcmDsd.cxx @@ -20,8 +20,7 @@ #include "config.h" #include "PcmDsd.hxx" #include "dsd2pcm/dsd2pcm.h" - -#include +#include "util/Macros.hxx" #include @@ -30,12 +29,12 @@ PcmDsd::PcmDsd() { - std::fill_n(dsd2pcm, G_N_ELEMENTS(dsd2pcm), nullptr); + std::fill_n(dsd2pcm, ARRAY_SIZE(dsd2pcm), nullptr); } PcmDsd::~PcmDsd() { - for (unsigned i = 0; i < G_N_ELEMENTS(dsd2pcm); ++i) + for (unsigned i = 0; i < ARRAY_SIZE(dsd2pcm); ++i) if (dsd2pcm[i] != nullptr) dsd2pcm_destroy(dsd2pcm[i]); } @@ -43,7 +42,7 @@ PcmDsd::~PcmDsd() void PcmDsd::Reset() { - for (unsigned i = 0; i < G_N_ELEMENTS(dsd2pcm); ++i) + for (unsigned i = 0; i < ARRAY_SIZE(dsd2pcm); ++i) if (dsd2pcm[i] != nullptr) dsd2pcm_reset(dsd2pcm[i]); } @@ -56,7 +55,7 @@ PcmDsd::ToFloat(unsigned channels, bool lsbfirst, assert(src != nullptr); assert(src_size > 0); assert(src_size % channels == 0); - assert(channels <= G_N_ELEMENTS(dsd2pcm)); + assert(channels <= ARRAY_SIZE(dsd2pcm)); const unsigned num_samples = src_size; const unsigned num_frames = src_size / channels; diff --git a/src/util/Macros.hxx b/src/util/Macros.hxx new file mode 100644 index 000000000..6f6bd9f48 --- /dev/null +++ b/src/util/Macros.hxx @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2010-2013 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MACROS_HPP +#define MACROS_HPP + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + +#endif diff --git a/src/util/list_sort.c b/src/util/list_sort.c index ddf3208e9..8534f3360 100644 --- a/src/util/list_sort.c +++ b/src/util/list_sort.c @@ -24,12 +24,12 @@ #include "list_sort.h" #include "list.h" +#include "Macros.hxx" +#include "Compiler.h" -#include #include -#define unlikely G_UNLIKELY -#define ARRAY_SIZE G_N_ELEMENTS +#define unlikely gcc_unlikely #define MAX_LIST_LENGTH_BITS 20 -- cgit v1.2.3