You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1012 B
46 lines
1012 B
10 years ago
|
#pragma once
|
||
|
|
||
|
extern "C" {
|
||
|
#include <libavutil/mem.h>
|
||
|
}
|
||
|
|
||
|
#include <stdexcept>
|
||
|
#include <memory>
|
||
|
|
||
|
namespace av {
|
||
|
// Generic error class
|
||
|
struct error : public std::runtime_error {
|
||
|
using runtime_error::runtime_error;
|
||
|
};
|
||
|
|
||
|
// Type of a freeing function (for unique_ptr)
|
||
|
template <typename T>
|
||
|
using deleter = void(*)(T*);
|
||
|
|
||
|
// Often used type
|
||
|
template <typename T>
|
||
|
using unique_ptr = std::unique_ptr<T, deleter<T>>;
|
||
|
|
||
|
// Allocator
|
||
|
template <typename T>
|
||
|
struct allocator {
|
||
|
using value_type = T;
|
||
|
using size_type = size_t;
|
||
|
|
||
|
T* allocate(size_type n) const {
|
||
|
auto ptr = av_malloc(n * sizeof(T));
|
||
|
if(!ptr) throw std::bad_alloc();
|
||
|
return static_cast<T*>(ptr);
|
||
|
}
|
||
|
|
||
|
void deallocate(T* ptr, size_type /*n*/) const noexcept {
|
||
|
av_free(ptr);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
template <typename T, typename S>
|
||
|
bool operator==(allocator<T> const &, allocator<S> const &) noexcept { return true; }
|
||
|
template <typename T, typename S>
|
||
|
bool operator!=(allocator<T> const &, allocator<S> const &) noexcept { return false; }
|
||
|
}
|