Util/Macros: replacement for GLib's G_N_ELEMENTS()

This commit is contained in:
Max Kellermann 2013-10-15 22:04:17 +02:00
parent 77429b6dd3
commit 509f8dab89
19 changed files with 100 additions and 58 deletions

View File

@ -248,6 +248,7 @@ endif
# Generic utility library
libutil_a_SOURCES = \
src/util/Macros.hxx \
src/util/Error.cxx src/util/Error.hxx \
src/util/ReusableArray.hxx \
src/util/StringUtil.cxx src/util/StringUtil.hxx \

View File

@ -24,9 +24,9 @@
#include "archive/Bzip2ArchivePlugin.hxx"
#include "archive/Iso9660ArchivePlugin.hxx"
#include "archive/ZzipArchivePlugin.hxx"
#include "util/Macros.hxx"
#include <string.h>
#include <glib.h>
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) \

View File

@ -43,8 +43,7 @@
#include "decoder/MpcdecDecoderPlugin.hxx"
#include "decoder/FluidsynthDecoderPlugin.hxx"
#include "system/FatalError.hxx"
#include <glib.h>
#include "util/Macros.hxx"
#include <string.h>
@ -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];

View File

@ -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 <glib.h>
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];

View File

@ -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);

View File

@ -23,6 +23,7 @@
#include "Idle.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
#include <string>
@ -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]);

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "UpdateQueue.hxx"
#include "util/Macros.hxx"
#include <glib.h>
@ -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);

View File

@ -23,13 +23,12 @@
#include "DecoderAPI.hxx"
#include "CheckAudioFormat.hxx"
#include "util/Error.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
#include <adplug/adplug.h>
#include <adplug/emuopl.h>
#include <glib.h>
#include <assert.h>
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 {

View File

@ -23,10 +23,9 @@
#include "CheckAudioFormat.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
#include <glib.h>
#include <fluidsynth.h>
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 */

View File

@ -25,11 +25,11 @@
#include "tag/TagHandler.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
#include <mpc/mpcdec.h>
#include <glib.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>
@ -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

View File

@ -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

View File

@ -26,6 +26,7 @@
#include "tag/ApeTag.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
#include <wavpack/wavpack.h>
@ -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);

View File

@ -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 <glib.h>
@ -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:

View File

@ -24,6 +24,7 @@
#include "MixerList.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include <glib.h>
@ -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));

View File

@ -20,8 +20,7 @@
#include "config.h"
#include "PcmDsd.hxx"
#include "dsd2pcm/dsd2pcm.h"
#include <glib.h>
#include "util/Macros.hxx"
#include <algorithm>
@ -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;

35
src/util/Macros.hxx Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2010-2013 Max Kellermann <max@duempel.org>
*
* 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

View File

@ -24,12 +24,12 @@
#include "list_sort.h"
#include "list.h"
#include "Macros.hxx"
#include "Compiler.h"
#include <glib.h>
#include <string.h>
#define unlikely G_UNLIKELY
#define ARRAY_SIZE G_N_ELEMENTS
#define unlikely gcc_unlikely
#define MAX_LIST_LENGTH_BITS 20

View File

@ -18,6 +18,7 @@
*/
#include "util/byte_reverse.h"
#include "util/Macros.hxx"
#include <glib.h>
@ -26,10 +27,10 @@ test_byte_reverse_2(void)
{
static const char src[] = "123456";
static const char result[] = "214365";
static uint8_t dest[G_N_ELEMENTS(src)];
static uint8_t dest[ARRAY_SIZE(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + G_N_ELEMENTS(src) - 1), 2);
(const uint8_t *)(src + ARRAY_SIZE(src) - 1), 2);
g_assert_cmpstr((const char *)dest, ==, result);
}
@ -38,10 +39,10 @@ test_byte_reverse_3(void)
{
static const char src[] = "123456";
static const char result[] = "321654";
static uint8_t dest[G_N_ELEMENTS(src)];
static uint8_t dest[ARRAY_SIZE(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + G_N_ELEMENTS(src) - 1), 3);
(const uint8_t *)(src + ARRAY_SIZE(src) - 1), 3);
g_assert_cmpstr((const char *)dest, ==, result);
}
@ -50,10 +51,10 @@ test_byte_reverse_4(void)
{
static const char src[] = "12345678";
static const char result[] = "43218765";
static uint8_t dest[G_N_ELEMENTS(src)];
static uint8_t dest[ARRAY_SIZE(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + G_N_ELEMENTS(src) - 1), 4);
(const uint8_t *)(src + ARRAY_SIZE(src) - 1), 4);
g_assert_cmpstr((const char *)dest, ==, result);
}
@ -62,10 +63,10 @@ test_byte_reverse_5(void)
{
static const char src[] = "1234567890";
static const char result[] = "5432109876";
static uint8_t dest[G_N_ELEMENTS(src)];
static uint8_t dest[ARRAY_SIZE(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + G_N_ELEMENTS(src) - 1), 5);
(const uint8_t *)(src + ARRAY_SIZE(src) - 1), 5);
g_assert_cmpstr((const char *)dest, ==, result);
}

View File

@ -2,6 +2,7 @@
#include "Queue.hxx"
#include "Song.hxx"
#include "Directory.hxx"
#include "util/Macros.hxx"
#include <glib.h>
@ -54,10 +55,10 @@ main(gcc_unused int argc, gcc_unused char **argv)
struct queue queue(32);
for (unsigned i = 0; i < G_N_ELEMENTS(songs); ++i)
for (unsigned i = 0; i < ARRAY_SIZE(songs); ++i)
queue.Append(&songs[i], 0);
assert(queue.GetLength() == G_N_ELEMENTS(songs));
assert(queue.GetLength() == ARRAY_SIZE(songs));
/* priority=10 for 4 items */
@ -75,7 +76,7 @@ main(gcc_unused int argc, gcc_unused char **argv)
assert(queue.PositionToOrder(i) < 4);
}
for (unsigned i = 8; i < G_N_ELEMENTS(songs); ++i) {
for (unsigned i = 8; i < ARRAY_SIZE(songs); ++i) {
assert(queue.PositionToOrder(i) >= 4);
}