thin-provisioning-tools/persistent-data/buffer.h

104 lines
2.7 KiB
C
Raw Normal View History

2013-01-22 18:16:38 +05:30
// Copyright (C) 2013 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools 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 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools 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 thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#ifndef BUFFER_H
#define BUFFER_H
#include <stdint.h>
2013-01-23 17:58:00 +05:30
// #include <stdlib.h>
#include <malloc.h>
2013-01-22 18:16:38 +05:30
#include <boost/noncopyable.hpp>
2013-01-23 17:58:00 +05:30
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
2013-01-23 19:09:42 +05:30
#include <boost/static_assert.hpp>
2013-01-23 17:58:00 +05:30
#include <stdexcept>
2013-01-22 18:16:38 +05:30
//----------------------------------------------------------------
namespace persistent_data {
2013-01-23 17:58:00 +05:30
uint32_t const DEFAULT_BUFFER_SIZE = 4096;
2013-01-22 18:16:38 +05:30
2013-01-23 17:58:00 +05:30
// Allocate buffer of Size with Alignment imposed.
2013-01-22 18:16:38 +05:30
//
2013-01-23 19:02:03 +05:30
// Allocation needs to be on the heap in order to provide alignment
// guarantees.
//
2013-01-23 17:58:00 +05:30
// Alignment must be a power of two.
template <uint32_t Size = DEFAULT_BUFFER_SIZE, uint32_t Alignment = 512>
2013-01-22 18:16:38 +05:30
class buffer : private boost::noncopyable {
public:
2013-01-23 19:09:42 +05:30
BOOST_STATIC_ASSERT_MSG((Alignment > 1) & !(Alignment & (Alignment - 1)),
"Alignment must be a power of two.");
2013-01-23 17:58:00 +05:30
typedef boost::shared_ptr<buffer> ptr;
2013-01-23 19:02:03 +05:30
typedef boost::shared_ptr<buffer const> const_ptr;
2013-01-23 17:58:00 +05:30
2013-01-22 18:16:38 +05:30
unsigned char &operator[](unsigned index) {
check_index(index);
2013-01-23 17:58:00 +05:30
2013-01-22 18:16:38 +05:30
return data_[index];
}
unsigned char const &operator[](unsigned index) const {
check_index(index);
2013-01-23 17:58:00 +05:30
2013-01-22 18:16:38 +05:30
return data_[index];
}
unsigned char *raw() {
return data_;
}
unsigned char const *raw() const {
return data_;
}
static void *operator new(size_t s) {
2013-01-23 17:58:00 +05:30
// void *r;
// return posix_memalign(&r, Alignment, s) ? NULL : r;
2013-01-22 18:16:38 +05:30
2013-01-23 17:58:00 +05:30
// Allocates size bytes and returns a pointer to the
// allocated memory. The memory address will be a
// multiple of 'Alignment', which must be a power of two
2013-01-23 19:09:42 +05:30
void *mem = memalign(Alignment, s);
if (!mem)
throw std::bad_alloc();
return mem;
2013-01-22 18:16:38 +05:30
}
static void operator delete(void *p) {
free(p);
}
2013-01-23 18:21:13 +05:30
private:
2013-01-23 17:58:00 +05:30
unsigned char data_[Size];
2013-01-22 18:16:38 +05:30
static void check_index(unsigned index) {
2013-01-23 17:58:00 +05:30
if (index >= Size)
throw std::range_error("buffer index out of bounds");
2013-01-22 18:16:38 +05:30
}
};
}
//----------------------------------------------------------------
#endif