diff options
author | Max Kellermann <max@duempel.org> | 2013-01-29 23:20:19 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-29 23:20:19 +0100 |
commit | ba51045d9e85b8e48afed629d6d87ac3338acd46 (patch) | |
tree | 01449b65415f9cd93212ad4c673d783b01b36ba2 /src | |
parent | fa34dd7bd3e0222811010dda6d1b40c4e3b3291b (diff) | |
download | mpd-ba51045d9e85b8e48afed629d6d87ac3338acd46.tar.gz mpd-ba51045d9e85b8e48afed629d6d87ac3338acd46.tar.xz mpd-ba51045d9e85b8e48afed629d6d87ac3338acd46.zip |
refcount: convert to C++
Diffstat (limited to '')
-rw-r--r-- | src/archive/Bzip2ArchivePlugin.cxx | 9 | ||||
-rw-r--r-- | src/archive/Iso9660ArchivePlugin.cxx | 9 | ||||
-rw-r--r-- | src/archive/ZzipArchivePlugin.cxx | 9 | ||||
-rw-r--r-- | src/input/CdioParanoiaInputPlugin.cxx | 1 | ||||
-rw-r--r-- | src/util/RefCount.hxx (renamed from src/refcount.h) | 44 |
5 files changed, 30 insertions, 42 deletions
diff --git a/src/archive/Bzip2ArchivePlugin.cxx b/src/archive/Bzip2ArchivePlugin.cxx index be7388d5b..f93718128 100644 --- a/src/archive/Bzip2ArchivePlugin.cxx +++ b/src/archive/Bzip2ArchivePlugin.cxx @@ -29,7 +29,7 @@ #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" -#include "refcount.h" +#include "util/RefCount.hxx" #include <stdint.h> #include <stddef.h> @@ -45,18 +45,17 @@ struct Bzip2ArchiveFile { struct archive_file base; - struct refcount ref; + RefCount ref; char *name; struct input_stream *istream; Bzip2ArchiveFile() { archive_file_init(&base, &bz2_archive_plugin); - refcount_init(&ref); } void Unref() { - if (!refcount_dec(&ref)) + if (!ref.Decrement()) return; g_free(name); @@ -174,7 +173,7 @@ Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context, const char *uri, :base(bz2_inputplugin, uri, mutex, cond), archive(&_context), eof(false) { - refcount_inc(&archive->ref); + archive->ref.Increment(); } Bzip2InputStream::~Bzip2InputStream() diff --git a/src/archive/Iso9660ArchivePlugin.cxx b/src/archive/Iso9660ArchivePlugin.cxx index 895087efc..344cdac6a 100644 --- a/src/archive/Iso9660ArchivePlugin.cxx +++ b/src/archive/Iso9660ArchivePlugin.cxx @@ -29,7 +29,7 @@ #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" -#include "refcount.h" +#include "util/RefCount.hxx" #include <cdio/cdio.h> #include <cdio/iso9660.h> @@ -44,14 +44,13 @@ struct Iso9660ArchiveFile { struct archive_file base; - struct refcount ref; + RefCount ref; iso9660_t *iso; Iso9660ArchiveFile(iso9660_t *_iso) :iso(_iso) { archive_file_init(&base, &iso9660_archive_plugin); - refcount_init(&ref); } ~Iso9660ArchiveFile() { @@ -59,7 +58,7 @@ struct Iso9660ArchiveFile { } void Unref() { - if (refcount_dec(&ref)) + if (ref.Decrement()) delete this; } @@ -161,7 +160,7 @@ struct Iso9660InputStream { base.ready = true; base.size = statbuf->size; - refcount_inc(&archive->ref); + archive->ref.Increment(); } ~Iso9660InputStream() { diff --git a/src/archive/ZzipArchivePlugin.cxx b/src/archive/ZzipArchivePlugin.cxx index 4075e2179..1b089a484 100644 --- a/src/archive/ZzipArchivePlugin.cxx +++ b/src/archive/ZzipArchivePlugin.cxx @@ -29,7 +29,7 @@ #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" -#include "refcount.h" +#include "util/RefCount.hxx" #include <zzip/zzip.h> #include <glib.h> @@ -38,17 +38,16 @@ struct ZzipArchiveFile { struct archive_file base; - struct refcount ref; + RefCount ref; ZZIP_DIR *dir; ZzipArchiveFile() { archive_file_init(&base, &zzip_archive_plugin); - refcount_init(&ref); } void Unref() { - if (!refcount_dec(&ref)) + if (!ref.Decrement()) return; //close archive @@ -136,7 +135,7 @@ struct ZzipInputStream { zzip_file_stat(file, &z_stat); base.size = z_stat.st_size; - refcount_inc(&archive->ref); + archive->ref.Increment(); } ~ZzipInputStream() { diff --git a/src/input/CdioParanoiaInputPlugin.cxx b/src/input/CdioParanoiaInputPlugin.cxx index 942eefb99..f0fa835b3 100644 --- a/src/input/CdioParanoiaInputPlugin.cxx +++ b/src/input/CdioParanoiaInputPlugin.cxx @@ -26,7 +26,6 @@ #include "InputInternal.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" -#include "refcount.h" #include <stdio.h> #include <stdint.h> diff --git a/src/refcount.h b/src/util/RefCount.hxx index a882d76b0..9a45a585b 100644 --- a/src/refcount.h +++ b/src/util/RefCount.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * Copyright (C) 2003-2013 The Music Player Daemon Project * http://www.musicpd.org * * Redistribution and use in source and binary forms, with or without @@ -33,35 +33,27 @@ * A very simple reference counting library. */ -#ifndef MPD_REFCOUNT_H -#define MPD_REFCOUNT_H +#ifndef MPD_REFCOUNT_HXX +#define MPD_REFCOUNT_HXX -#include <glib.h> -#include <stdbool.h> +#include <atomic> -struct refcount { - gint n; -}; +class RefCount { + std::atomic_uint n; -static inline void -refcount_init(struct refcount *r) -{ - r->n = 1; -} +public: + constexpr RefCount():n(1) {} -static inline void -refcount_inc(struct refcount *r) -{ - g_atomic_int_inc(&r->n); -} + void Increment() { + ++n; + } -/** - * @return true if the number of references has been dropped to 0 - */ -static inline bool -refcount_dec(struct refcount *r) -{ - return g_atomic_int_dec_and_test(&r->n); -} + /** + * @return true if the number of references has been dropped to 0 + */ + bool Decrement() { + return --n == 0; + } +}; #endif |