Page.cxx 1.51 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Page.hxx"
22 23 24

#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
25 26
#include <new>

27 28 29
#include <assert.h>
#include <string.h>

Max Kellermann's avatar
Max Kellermann committed
30 31
Page *
Page::Create(size_t size)
32
{
Max Kellermann's avatar
Max Kellermann committed
33 34 35
	void *p = g_malloc(sizeof(Page) + size -
			   sizeof(Page::data));
	return ::new(p) Page(size);
36 37
}

Max Kellermann's avatar
Max Kellermann committed
38 39
Page *
Page::Copy(const void *data, size_t size)
40
{
Max Kellermann's avatar
Max Kellermann committed
41
	assert(data != nullptr);
42

Max Kellermann's avatar
Max Kellermann committed
43
	Page *page = Create(size);
44 45 46 47
	memcpy(page->data, data, size);
	return page;
}

Max Kellermann's avatar
Max Kellermann committed
48 49
Page *
Page::Concat(const Page &a, const Page &b)
50
{
Max Kellermann's avatar
Max Kellermann committed
51
	Page *page = Create(a.size + b.size);
52

Max Kellermann's avatar
Max Kellermann committed
53 54
	memcpy(page->data, a.data, a.size);
	memcpy(page->data + a.size, b.data, b.size);
55 56 57 58

	return page;
}

59
bool
Max Kellermann's avatar
Max Kellermann committed
60
Page::Unref()
61
{
Max Kellermann's avatar
Max Kellermann committed
62
	bool unused = ref.Decrement();
63

Max Kellermann's avatar
Max Kellermann committed
64 65 66 67
	if (unused) {
		this->Page::~Page();
		g_free(this);
	}
68 69 70

	return unused;
}