Commit 509f8dab authored by Max Kellermann's avatar Max Kellermann

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

parent 77429b6d
...@@ -248,6 +248,7 @@ endif ...@@ -248,6 +248,7 @@ endif
# Generic utility library # Generic utility library
libutil_a_SOURCES = \ libutil_a_SOURCES = \
src/util/Macros.hxx \
src/util/Error.cxx src/util/Error.hxx \ src/util/Error.cxx src/util/Error.hxx \
src/util/ReusableArray.hxx \ src/util/ReusableArray.hxx \
src/util/StringUtil.cxx src/util/StringUtil.hxx \ src/util/StringUtil.cxx src/util/StringUtil.hxx \
......
...@@ -24,9 +24,9 @@ ...@@ -24,9 +24,9 @@
#include "archive/Bzip2ArchivePlugin.hxx" #include "archive/Bzip2ArchivePlugin.hxx"
#include "archive/Iso9660ArchivePlugin.hxx" #include "archive/Iso9660ArchivePlugin.hxx"
#include "archive/ZzipArchivePlugin.hxx" #include "archive/ZzipArchivePlugin.hxx"
#include "util/Macros.hxx"
#include <string.h> #include <string.h>
#include <glib.h>
const struct archive_plugin *const archive_plugins[] = { const struct archive_plugin *const archive_plugins[] = {
#ifdef HAVE_BZ2 #ifdef HAVE_BZ2
...@@ -42,7 +42,7 @@ const struct archive_plugin *const archive_plugins[] = { ...@@ -42,7 +42,7 @@ const struct archive_plugin *const archive_plugins[] = {
}; };
/** which plugins have been initialized successfully? */ /** 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) \ #define archive_plugins_for_each_enabled(plugin) \
archive_plugins_for_each(plugin) \ archive_plugins_for_each(plugin) \
......
...@@ -43,8 +43,7 @@ ...@@ -43,8 +43,7 @@
#include "decoder/MpcdecDecoderPlugin.hxx" #include "decoder/MpcdecDecoderPlugin.hxx"
#include "decoder/FluidsynthDecoderPlugin.hxx" #include "decoder/FluidsynthDecoderPlugin.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "util/Macros.hxx"
#include <glib.h>
#include <string.h> #include <string.h>
...@@ -114,9 +113,8 @@ const struct decoder_plugin *const decoder_plugins[] = { ...@@ -114,9 +113,8 @@ const struct decoder_plugin *const decoder_plugins[] = {
NULL NULL
}; };
enum { static constexpr unsigned num_decoder_plugins =
num_decoder_plugins = G_N_ELEMENTS(decoder_plugins) - 1, ARRAY_SIZE(decoder_plugins) - 1;
};
/** which plugins have been initialized successfully? */ /** which plugins have been initialized successfully? */
bool decoder_plugins_enabled[num_decoder_plugins]; bool decoder_plugins_enabled[num_decoder_plugins];
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "InputRegistry.hxx" #include "InputRegistry.hxx"
#include "util/Macros.hxx"
#include "input/FileInputPlugin.hxx" #include "input/FileInputPlugin.hxx"
#ifdef ENABLE_ARCHIVE #ifdef ENABLE_ARCHIVE
...@@ -45,8 +46,6 @@ ...@@ -45,8 +46,6 @@
#include "input/DespotifyInputPlugin.hxx" #include "input/DespotifyInputPlugin.hxx"
#endif #endif
#include <glib.h>
const struct input_plugin *const input_plugins[] = { const struct input_plugin *const input_plugins[] = {
&input_plugin_file, &input_plugin_file,
#ifdef ENABLE_ARCHIVE #ifdef ENABLE_ARCHIVE
...@@ -67,7 +66,7 @@ const struct input_plugin *const input_plugins[] = { ...@@ -67,7 +66,7 @@ const struct input_plugin *const input_plugins[] = {
#ifdef ENABLE_DESPOTIFY #ifdef ENABLE_DESPOTIFY
&input_plugin_despotify, &input_plugin_despotify,
#endif #endif
NULL nullptr
}; };
bool input_plugins_enabled[G_N_ELEMENTS(input_plugins) - 1]; bool input_plugins_enabled[ARRAY_SIZE(input_plugins) - 1];
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/StringUtil.hxx" #include "util/StringUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Macros.hxx"
#include "ConfigGlobal.hxx" #include "ConfigGlobal.hxx"
#include "ConfigData.hxx" #include "ConfigData.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
...@@ -61,8 +62,11 @@ const struct playlist_plugin *const playlist_plugins[] = { ...@@ -61,8 +62,11 @@ const struct playlist_plugin *const playlist_plugins[] = {
nullptr nullptr
}; };
static constexpr unsigned n_playlist_plugins =
ARRAY_SIZE(playlist_plugins) - 1;
/** which plugins have been initialized successfully? */ /** 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) \ #define playlist_plugins_for_each_enabled(plugin) \
playlist_plugins_for_each(plugin) \ playlist_plugins_for_each(plugin) \
...@@ -189,7 +193,7 @@ playlist_list_open_uri(const char *uri, Mutex &mutex, Cond &cond) ...@@ -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 /** this array tracks which plugins have already been tried by
playlist_list_open_uri_scheme() */ playlist_list_open_uri_scheme() */
bool tried[G_N_ELEMENTS(playlist_plugins) - 1]; bool tried[n_playlist_plugins];
assert(uri != nullptr); assert(uri != nullptr);
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "Idle.hxx" #include "Idle.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <string> #include <string>
...@@ -79,7 +80,7 @@ static const char sticker_sql_create[] = ...@@ -79,7 +80,7 @@ static const char sticker_sql_create[] =
""; "";
static sqlite3 *sticker_db; 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"); static constexpr Domain sticker_domain("sticker");
...@@ -138,7 +139,7 @@ sticker_global_init(Path &&path, Error &error) ...@@ -138,7 +139,7 @@ sticker_global_init(Path &&path, Error &error)
/* prepare the statements we're going to use */ /* 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); assert(sticker_sql[i] != NULL);
sticker_stmt[i] = sticker_prepare(sticker_sql[i], error); sticker_stmt[i] = sticker_prepare(sticker_sql[i], error);
...@@ -156,7 +157,7 @@ sticker_global_finish(void) ...@@ -156,7 +157,7 @@ sticker_global_finish(void)
/* not configured */ /* not configured */
return; 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); assert(sticker_stmt[i] != NULL);
sqlite3_finalize(sticker_stmt[i]); sqlite3_finalize(sticker_stmt[i]);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "UpdateQueue.hxx" #include "UpdateQueue.hxx"
#include "util/Macros.hxx"
#include <glib.h> #include <glib.h>
...@@ -36,9 +37,9 @@ static size_t update_queue_length; ...@@ -36,9 +37,9 @@ static size_t update_queue_length;
unsigned unsigned
update_queue_push(const char *path, bool discard, unsigned base) 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; return 0;
update_queue[update_queue_length].path = g_strdup(path); update_queue[update_queue_length].path = g_strdup(path);
......
...@@ -23,13 +23,12 @@ ...@@ -23,13 +23,12 @@
#include "DecoderAPI.hxx" #include "DecoderAPI.hxx"
#include "CheckAudioFormat.hxx" #include "CheckAudioFormat.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Macros.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <adplug/adplug.h> #include <adplug/adplug.h>
#include <adplug/emuopl.h> #include <adplug/emuopl.h>
#include <glib.h>
#include <assert.h> #include <assert.h>
static unsigned sample_rate; static unsigned sample_rate;
...@@ -65,7 +64,7 @@ adplug_file_decode(struct decoder *decoder, const char *path_fs) ...@@ -65,7 +64,7 @@ adplug_file_decode(struct decoder *decoder, const char *path_fs)
player->songlength() / 1000.); player->songlength() / 1000.);
int16_t buffer[2048]; 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; DecoderCommand cmd;
do { do {
......
...@@ -23,10 +23,9 @@ ...@@ -23,10 +23,9 @@
#include "CheckAudioFormat.hxx" #include "CheckAudioFormat.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h>
#include <fluidsynth.h> #include <fluidsynth.h>
static constexpr Domain fluidsynth_domain("fluidsynth"); static constexpr Domain fluidsynth_domain("fluidsynth");
...@@ -171,7 +170,7 @@ fluidsynth_file_decode(struct decoder *decoder, const char *path_fs) ...@@ -171,7 +170,7 @@ fluidsynth_file_decode(struct decoder *decoder, const char *path_fs)
DecoderCommand cmd; DecoderCommand cmd;
while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) { while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING) {
int16_t buffer[2048]; 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 /* read samples from fluidsynth and send them to the
MPD core */ MPD core */
......
...@@ -25,11 +25,11 @@ ...@@ -25,11 +25,11 @@
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <mpc/mpcdec.h> #include <mpc/mpcdec.h>
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
...@@ -212,7 +212,7 @@ mpcdec_decode(struct decoder *mpd_decoder, struct input_stream *is) ...@@ -212,7 +212,7 @@ mpcdec_decode(struct decoder *mpd_decoder, struct input_stream *is)
mpc_uint32_t ret = frame.samples; mpc_uint32_t ret = frame.samples;
ret *= info.channels; 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); mpc_to_mpd_buffer(chunk, sample_buffer, ret);
long bit_rate = vbr_update_bits * audio_format.sample_rate long bit_rate = vbr_update_bits * audio_format.sample_rate
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "OggCodec.hxx" #include "OggCodec.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Macros.hxx"
#include "CheckAudioFormat.hxx" #include "CheckAudioFormat.hxx"
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "Log.hxx" #include "Log.hxx"
...@@ -226,7 +227,7 @@ vorbis_stream_decode(struct decoder *decoder, ...@@ -226,7 +227,7 @@ vorbis_stream_decode(struct decoder *decoder,
#else #else
float buffer[2048]; float buffer[2048];
const int frames_per_buffer = 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; const unsigned frame_size = sizeof(buffer[0]) * audio_format.channels;
#endif #endif
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "tag/ApeTag.hxx" #include "tag/ApeTag.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <wavpack/wavpack.h> #include <wavpack/wavpack.h>
...@@ -173,7 +174,7 @@ wavpack_decode(struct decoder *decoder, WavpackContext *wpc, bool can_seek) ...@@ -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 */ /* wavpack gives us all kind of samples in a 32-bit space */
int32_t chunk[1024]; int32_t chunk[1024];
const uint32_t samples_requested = G_N_ELEMENTS(chunk) / const uint32_t samples_requested = ARRAY_SIZE(chunk) /
audio_format.channels; audio_format.channels;
decoder_initialized(decoder, audio_format, can_seek, total_time); decoder_initialized(decoder, audio_format, can_seek, total_time);
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "system/fd_util.h" #include "system/fd_util.h"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h> #include <glib.h>
...@@ -133,7 +134,7 @@ oss_output_test_default_device(void) ...@@ -133,7 +134,7 @@ oss_output_test_default_device(void)
{ {
int fd, i; 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); fd = open_cloexec(default_devices[i], O_WRONLY, 0);
if (fd >= 0) { if (fd >= 0) {
...@@ -152,11 +153,11 @@ oss_output_test_default_device(void) ...@@ -152,11 +153,11 @@ oss_output_test_default_device(void)
static struct audio_output * static struct audio_output *
oss_open_default(Error &error) oss_open_default(Error &error)
{ {
int err[G_N_ELEMENTS(default_devices)]; int err[ARRAY_SIZE(default_devices)];
enum oss_stat ret[G_N_ELEMENTS(default_devices)]; enum oss_stat ret[ARRAY_SIZE(default_devices)];
const config_param empty; 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]); ret[i] = oss_stat_device(default_devices[i], &err[i]);
if (ret[i] == OSS_STAT_NO_ERROR) { if (ret[i] == OSS_STAT_NO_ERROR) {
OssOutput *od = new OssOutput(); OssOutput *od = new OssOutput();
...@@ -170,7 +171,7 @@ oss_open_default(Error &error) ...@@ -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]; const char *dev = default_devices[i];
switch(ret[i]) { switch(ret[i]) {
case OSS_STAT_NO_ERROR: case OSS_STAT_NO_ERROR:
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "MixerList.hxx" #include "MixerList.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Macros.hxx"
#include <glib.h> #include <glib.h>
...@@ -183,7 +184,7 @@ winmm_output_open(struct audio_output *ao, AudioFormat &audio_format, ...@@ -183,7 +184,7 @@ winmm_output_open(struct audio_output *ao, AudioFormat &audio_format,
return false; 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)); memset(&wo->buffers[i].hdr, 0, sizeof(wo->buffers[i].hdr));
} }
...@@ -197,7 +198,7 @@ winmm_output_close(struct audio_output *ao) ...@@ -197,7 +198,7 @@ winmm_output_close(struct audio_output *ao)
{ {
WinmmOutput *wo = (WinmmOutput *)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(); wo->buffers[i].buffer.Clear();
waveOutClose(wo->handle); waveOutClose(wo->handle);
...@@ -285,7 +286,7 @@ winmm_output_play(struct audio_output *ao, const void *chunk, size_t size, Error ...@@ -285,7 +286,7 @@ winmm_output_play(struct audio_output *ao, const void *chunk, size_t size, Error
/* mark our buffer as "used" */ /* mark our buffer as "used" */
wo->next_buffer = (wo->next_buffer + 1) % wo->next_buffer = (wo->next_buffer + 1) %
G_N_ELEMENTS(wo->buffers); ARRAY_SIZE(wo->buffers);
return size; return size;
} }
...@@ -293,7 +294,7 @@ winmm_output_play(struct audio_output *ao, const void *chunk, size_t size, Error ...@@ -293,7 +294,7 @@ winmm_output_play(struct audio_output *ao, const void *chunk, size_t size, Error
static bool static bool
winmm_drain_all_buffers(WinmmOutput *wo, Error &error) 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)) if (!winmm_drain_buffer(wo, &wo->buffers[i], error))
return false; return false;
...@@ -309,7 +310,7 @@ winmm_stop(WinmmOutput *wo) ...@@ -309,7 +310,7 @@ winmm_stop(WinmmOutput *wo)
{ {
waveOutReset(wo->handle); 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]; WinmmBuffer *buffer = &wo->buffers[i];
waveOutUnprepareHeader(wo->handle, &buffer->hdr, waveOutUnprepareHeader(wo->handle, &buffer->hdr,
sizeof(buffer->hdr)); sizeof(buffer->hdr));
......
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
#include "config.h" #include "config.h"
#include "PcmDsd.hxx" #include "PcmDsd.hxx"
#include "dsd2pcm/dsd2pcm.h" #include "dsd2pcm/dsd2pcm.h"
#include "util/Macros.hxx"
#include <glib.h>
#include <algorithm> #include <algorithm>
...@@ -30,12 +29,12 @@ ...@@ -30,12 +29,12 @@
PcmDsd::PcmDsd() PcmDsd::PcmDsd()
{ {
std::fill_n(dsd2pcm, G_N_ELEMENTS(dsd2pcm), nullptr); std::fill_n(dsd2pcm, ARRAY_SIZE(dsd2pcm), nullptr);
} }
PcmDsd::~PcmDsd() 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) if (dsd2pcm[i] != nullptr)
dsd2pcm_destroy(dsd2pcm[i]); dsd2pcm_destroy(dsd2pcm[i]);
} }
...@@ -43,7 +42,7 @@ PcmDsd::~PcmDsd() ...@@ -43,7 +42,7 @@ PcmDsd::~PcmDsd()
void void
PcmDsd::Reset() 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) if (dsd2pcm[i] != nullptr)
dsd2pcm_reset(dsd2pcm[i]); dsd2pcm_reset(dsd2pcm[i]);
} }
...@@ -56,7 +55,7 @@ PcmDsd::ToFloat(unsigned channels, bool lsbfirst, ...@@ -56,7 +55,7 @@ PcmDsd::ToFloat(unsigned channels, bool lsbfirst,
assert(src != nullptr); assert(src != nullptr);
assert(src_size > 0); assert(src_size > 0);
assert(src_size % channels == 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_samples = src_size;
const unsigned num_frames = src_size / channels; const unsigned num_frames = src_size / channels;
......
/*
* 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
...@@ -24,12 +24,12 @@ ...@@ -24,12 +24,12 @@
#include "list_sort.h" #include "list_sort.h"
#include "list.h" #include "list.h"
#include "Macros.hxx"
#include "Compiler.h"
#include <glib.h>
#include <string.h> #include <string.h>
#define unlikely G_UNLIKELY #define unlikely gcc_unlikely
#define ARRAY_SIZE G_N_ELEMENTS
#define MAX_LIST_LENGTH_BITS 20 #define MAX_LIST_LENGTH_BITS 20
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
#include "util/byte_reverse.h" #include "util/byte_reverse.h"
#include "util/Macros.hxx"
#include <glib.h> #include <glib.h>
...@@ -26,10 +27,10 @@ test_byte_reverse_2(void) ...@@ -26,10 +27,10 @@ test_byte_reverse_2(void)
{ {
static const char src[] = "123456"; static const char src[] = "123456";
static const char result[] = "214365"; 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, 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); g_assert_cmpstr((const char *)dest, ==, result);
} }
...@@ -38,10 +39,10 @@ test_byte_reverse_3(void) ...@@ -38,10 +39,10 @@ test_byte_reverse_3(void)
{ {
static const char src[] = "123456"; static const char src[] = "123456";
static const char result[] = "321654"; 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, 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); g_assert_cmpstr((const char *)dest, ==, result);
} }
...@@ -50,10 +51,10 @@ test_byte_reverse_4(void) ...@@ -50,10 +51,10 @@ test_byte_reverse_4(void)
{ {
static const char src[] = "12345678"; static const char src[] = "12345678";
static const char result[] = "43218765"; 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, 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); g_assert_cmpstr((const char *)dest, ==, result);
} }
...@@ -62,10 +63,10 @@ test_byte_reverse_5(void) ...@@ -62,10 +63,10 @@ test_byte_reverse_5(void)
{ {
static const char src[] = "1234567890"; static const char src[] = "1234567890";
static const char result[] = "5432109876"; 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, 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); g_assert_cmpstr((const char *)dest, ==, result);
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "Queue.hxx" #include "Queue.hxx"
#include "Song.hxx" #include "Song.hxx"
#include "Directory.hxx" #include "Directory.hxx"
#include "util/Macros.hxx"
#include <glib.h> #include <glib.h>
...@@ -54,10 +55,10 @@ main(gcc_unused int argc, gcc_unused char **argv) ...@@ -54,10 +55,10 @@ main(gcc_unused int argc, gcc_unused char **argv)
struct queue queue(32); 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); queue.Append(&songs[i], 0);
assert(queue.GetLength() == G_N_ELEMENTS(songs)); assert(queue.GetLength() == ARRAY_SIZE(songs));
/* priority=10 for 4 items */ /* priority=10 for 4 items */
...@@ -75,7 +76,7 @@ main(gcc_unused int argc, gcc_unused char **argv) ...@@ -75,7 +76,7 @@ main(gcc_unused int argc, gcc_unused char **argv)
assert(queue.PositionToOrder(i) < 4); 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); assert(queue.PositionToOrder(i) >= 4);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment