diff options
author | Max Kellermann <max@duempel.org> | 2013-01-30 09:18:52 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-30 09:18:52 +0100 |
commit | fe3f0332f71258354b70e5db685b56934f0df703 (patch) | |
tree | 9f3f51ad0ee7203be19ce7d168985da52fa6a9a2 /src/page.h | |
parent | 718fd97612c298b7eac47289c1803a2a19d9a859 (diff) | |
download | mpd-fe3f0332f71258354b70e5db685b56934f0df703.tar.gz mpd-fe3f0332f71258354b70e5db685b56934f0df703.tar.xz mpd-fe3f0332f71258354b70e5db685b56934f0df703.zip |
page: convert to C++
Diffstat (limited to '')
-rw-r--r-- | src/Page.hxx (renamed from src/page.h) | 96 |
1 files changed, 49 insertions, 47 deletions
diff --git a/src/page.h b/src/Page.hxx index 8629298cd..2bc9a6ac5 100644 --- a/src/page.h +++ b/src/Page.hxx @@ -22,81 +22,83 @@ * This is a library which manages reference counted buffers. */ -#ifndef MPD_PAGE_H -#define MPD_PAGE_H +#ifndef MPD_PAGE_HXX +#define MPD_PAGE_HXX + +#include "util/RefCount.hxx" + +#include <algorithm> #include <stddef.h> -#include <stdbool.h> /** * A dynamically allocated buffer which keeps track of its reference * count. This is useful for passing buffers around, when several * instances hold references to one buffer. */ -struct page { +class Page { /** * The number of references to this buffer. This library uses * atomic functions to access it, i.e. no locks are required. * As soon as this attribute reaches zero, the buffer is * freed. */ - int ref; + RefCount ref; +public: /** * The size of this buffer in bytes. */ - size_t size; + const size_t size; /** * Dynamic array containing the buffer data. */ unsigned char data[sizeof(long)]; -}; -#ifdef __cplusplus -extern "C" { -#endif +protected: + Page(size_t _size):size(_size) {} + ~Page() = default; -/** - * Creates a new #page object, and copies data from the specified - * buffer. It is initialized with a reference count of 1. - * - * @param data the source buffer - * @param size the size of the source buffer - * @return the new #page object - */ -struct page * -page_new_copy(const void *data, size_t size); + /** + * Allocates a new #Page object, without filling the data + * element. + */ + static Page *Create(size_t size); -/** - * Concatenates two pages to a new page. - * - * @param a the first page - * @param b the second page, which is appended - */ -struct page * -page_new_concat(const struct page *a, const struct page *b); +public: + /** + * Creates a new #page object, and copies data from the + * specified buffer. It is initialized with a reference count + * of 1. + * + * @param data the source buffer + * @param size the size of the source buffer + */ + static Page *Copy(const void *data, size_t size); -/** - * Increases the reference counter. - * - * @param page the #page object - */ -void -page_ref(struct page *page); + /** + * Concatenates two pages to a new page. + * + * @param a the first page + * @param b the second page, which is appended + */ + static Page *Concat(const Page &a, const Page &b); -/** - * Decreases the reference counter. If it reaches zero, the #page is - * freed. - * - * @param page the #page object - * @return true if the #page has been freed - */ -bool -page_unref(struct page *page); + /** + * Increases the reference counter. + */ + void Ref() { + ref.Increment(); + } -#ifdef __cplusplus -} -#endif + /** + * Decreases the reference counter. If it reaches zero, the #page is + * freed. + * + * @return true if the #page has been freed + */ + bool Unref(); +}; #endif |