Add an AlignedFreeDeleter and remove scoped_ptr_malloc.

- Transition scoped_ptr_mallocs to scoped_ptr.
- AlignedFreeDeleter matches Chromium's version.

TESTED=try bots
R=turaj@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/8969005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@5587 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
andrew@webrtc.org 2014-02-20 21:08:36 +00:00
parent d4d5be8781
commit d617a44a4f
7 changed files with 37 additions and 99 deletions

View File

@ -146,12 +146,12 @@ class SincResampler {
// Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize.
// The kernel offsets are sub-sample shifts of a windowed sinc shifted from
// 0.0 to 1.0 sample.
scoped_ptr_malloc<float, AlignedFree> kernel_storage_;
scoped_ptr_malloc<float, AlignedFree> kernel_pre_sinc_storage_;
scoped_ptr_malloc<float, AlignedFree> kernel_window_storage_;
scoped_ptr<float, AlignedFreeDeleter> kernel_storage_;
scoped_ptr<float, AlignedFreeDeleter> kernel_pre_sinc_storage_;
scoped_ptr<float, AlignedFreeDeleter> kernel_window_storage_;
// Data from the source is copied into this buffer for each processing pass.
scoped_ptr_malloc<float, AlignedFree> input_buffer_;
scoped_ptr<float, AlignedFreeDeleter> input_buffer_;
// Stores the runtime selection of which Convolve function to use.
// TODO(ajm): Move to using a global static which must only be initialized

View File

@ -20,8 +20,7 @@ namespace webrtc {
static const int kBufferAlignment = 64;
Plane::Plane()
: buffer_(NULL),
allocated_size_(0),
: allocated_size_(0),
plane_size_(0),
stride_(0) {}
@ -42,8 +41,8 @@ int Plane::MaybeResize(int new_size) {
return -1;
if (new_size <= allocated_size_)
return 0;
Allocator<uint8_t>::scoped_ptr_aligned new_buffer(
AlignedMalloc<uint8_t>(new_size, kBufferAlignment));
scoped_ptr<uint8_t, AlignedFreeDeleter> new_buffer(static_cast<uint8_t*>(
AlignedMalloc(new_size, kBufferAlignment)));
if (buffer_.get()) {
memcpy(new_buffer.get(), buffer_.get(), plane_size_);
}

View File

@ -12,6 +12,7 @@
#define COMMON_VIDEO_PLANE_H
#include "webrtc/system_wrappers/interface/aligned_malloc.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
#include "webrtc/typedefs.h"
namespace webrtc {
@ -63,7 +64,7 @@ class Plane {
// Return value: 0 on success ,-1 on error.
int MaybeResize(int new_size);
Allocator<uint8_t>::scoped_ptr_aligned buffer_;
scoped_ptr<uint8_t, AlignedFreeDeleter> buffer_;
int allocated_size_;
int plane_size_;
int stride_;

View File

@ -22,7 +22,12 @@ extern "C" {
namespace webrtc {
typedef scoped_ptr_malloc<RingBuffer, WebRtc_FreeBuffer> scoped_ring_buffer;
struct FreeBufferDeleter {
inline void operator()(void* ptr) const {
WebRtc_FreeBuffer(ptr);
}
};
typedef scoped_ptr<RingBuffer, FreeBufferDeleter> scoped_ring_buffer;
static void AssertElementEq(int expected, int actual) {
ASSERT_EQ(expected, actual);

View File

@ -19,8 +19,6 @@
#include <stddef.h>
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
namespace webrtc {
// Returns a pointer to the first boundry of |alignment| bytes following the
@ -48,10 +46,12 @@ T* AlignedMalloc(size_t size, size_t alignment) {
return reinterpret_cast<T*>(AlignedMalloc(size, alignment));
}
// Scoped pointer to AlignedMalloc-memory.
template<typename T>
struct Allocator {
typedef scoped_ptr_malloc<T, AlignedFree> scoped_ptr_aligned;
// Deleter for use with scoped_ptr. E.g., use as
// scoped_ptr<Foo, AlignedFreeDeleter> foo;
struct AlignedFreeDeleter {
inline void operator()(void* ptr) const {
AlignedFree(ptr);
}
};
} // namespace webrtc

View File

@ -96,7 +96,7 @@
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_PTR_H_
// This is an implementation designed to match the anticipated future TR2
// implementation of the scoped_ptr class and scoped_ptr_malloc (deprecated).
// implementation of the scoped_ptr class.
#include <assert.h>
#include <stddef.h>
@ -639,77 +639,6 @@ void swap(scoped_array<T>& a, scoped_array<T>& b) {
a.swap(b);
}
// DEPRECATED: Use scoped_ptr<C, webrtc::FreeDeleter> instead.
// TODO(ajm): Remove scoped_ptr_malloc.
//
// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
// second template argument, the function used to free the object.
template<typename T, void (*FF)(void*) = free> class scoped_ptr_malloc {
private:
T* ptr;
scoped_ptr_malloc(scoped_ptr_malloc const &);
scoped_ptr_malloc & operator=(scoped_ptr_malloc const &);
public:
typedef T element_type;
explicit scoped_ptr_malloc(T* p = 0): ptr(p) {}
~scoped_ptr_malloc() {
FF(static_cast<void*>(ptr));
}
void reset(T* p = 0) {
if (ptr != p) {
FF(static_cast<void*>(ptr));
ptr = p;
}
}
T& operator*() const {
assert(ptr != 0);
return *ptr;
}
T* operator->() const {
assert(ptr != 0);
return ptr;
}
T* get() const {
return ptr;
}
void swap(scoped_ptr_malloc & b) {
T* tmp = b.ptr;
b.ptr = ptr;
ptr = tmp;
}
T* release() {
T* tmp = ptr;
ptr = 0;
return tmp;
}
T** accept() {
if (ptr) {
FF(static_cast<void*>(ptr));
ptr = 0;
}
return &ptr;
}
};
template<typename T, void (*FF)(void*)> inline
void swap(scoped_ptr_malloc<T,FF>& a, scoped_ptr_malloc<T,FF>& b) {
a.swap(b);
}
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SCOPED_PTR_H_

View File

@ -16,14 +16,16 @@
#include <stdint.h>
#endif
#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
#include "webrtc/typedefs.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace webrtc {
// Returns true if |size| and |alignment| are valid combinations.
bool CorrectUsage(size_t size, size_t alignment) {
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
webrtc::AlignedMalloc<char>(size, alignment));
scoped_ptr<char, AlignedFreeDeleter> scoped(
static_cast<char*>(AlignedMalloc(size, alignment)));
if (scoped.get() == NULL) {
return false;
}
@ -34,16 +36,15 @@ bool CorrectUsage(size_t size, size_t alignment) {
TEST(AlignedMalloc, GetRightAlign) {
const size_t size = 100;
const size_t alignment = 32;
const size_t left_missalignment = 8;
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
webrtc::AlignedMalloc<char>(size, alignment));
const size_t left_misalignment = 1;
scoped_ptr<char, AlignedFreeDeleter> scoped(
static_cast<char*>(AlignedMalloc(size, alignment)));
EXPECT_TRUE(scoped.get() != NULL);
const uintptr_t aligned_address = reinterpret_cast<uintptr_t> (scoped.get());
const uintptr_t missaligned_address = aligned_address - left_missalignment;
const char* missaligned_ptr = reinterpret_cast<const char*>(
missaligned_address);
const char* realigned_ptr = webrtc::GetRightAlign(
missaligned_ptr, alignment);
const uintptr_t misaligned_address = aligned_address - left_misalignment;
const char* misaligned_ptr = reinterpret_cast<const char*>(
misaligned_address);
const char* realigned_ptr = GetRightAlign(misaligned_ptr, alignment);
EXPECT_EQ(scoped.get(), realigned_ptr);
}
@ -76,3 +77,6 @@ TEST(AlignedMalloc, AlignTo128Bytes) {
size_t alignment = 128;
EXPECT_TRUE(CorrectUsage(size, alignment));
}
} // namespace webrtc