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/webrtc@5587 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/common_audio/resampler/sinc_resampler.h b/common_audio/resampler/sinc_resampler.h
index 60abd61..7e6237a 100644
--- a/common_audio/resampler/sinc_resampler.h
+++ b/common_audio/resampler/sinc_resampler.h
@@ -146,12 +146,12 @@
// 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
diff --git a/common_video/plane.cc b/common_video/plane.cc
index 68d32cd..3776de1 100644
--- a/common_video/plane.cc
+++ b/common_video/plane.cc
@@ -20,8 +20,7 @@
static const int kBufferAlignment = 64;
Plane::Plane()
- : buffer_(NULL),
- allocated_size_(0),
+ : allocated_size_(0),
plane_size_(0),
stride_(0) {}
@@ -42,8 +41,8 @@
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_);
}
diff --git a/common_video/plane.h b/common_video/plane.h
index 1b74f37..4031e03 100644
--- a/common_video/plane.h
+++ b/common_video/plane.h
@@ -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 @@
// 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_;
diff --git a/modules/audio_processing/utility/ring_buffer_unittest.cc b/modules/audio_processing/utility/ring_buffer_unittest.cc
index 2b7634d..bd68f94 100644
--- a/modules/audio_processing/utility/ring_buffer_unittest.cc
+++ b/modules/audio_processing/utility/ring_buffer_unittest.cc
@@ -22,7 +22,12 @@
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);
diff --git a/system_wrappers/interface/aligned_malloc.h b/system_wrappers/interface/aligned_malloc.h
index 6409999..5d343cd 100644
--- a/system_wrappers/interface/aligned_malloc.h
+++ b/system_wrappers/interface/aligned_malloc.h
@@ -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 @@
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
diff --git a/system_wrappers/interface/scoped_ptr.h b/system_wrappers/interface/scoped_ptr.h
index aeac77a..fb20363 100644
--- a/system_wrappers/interface/scoped_ptr.h
+++ b/system_wrappers/interface/scoped_ptr.h
@@ -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 @@
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_
diff --git a/system_wrappers/source/aligned_malloc_unittest.cc b/system_wrappers/source/aligned_malloc_unittest.cc
index 10e08aa..0acbf97 100644
--- a/system_wrappers/source/aligned_malloc_unittest.cc
+++ b/system_wrappers/source/aligned_malloc_unittest.cc
@@ -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 @@
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 @@
size_t alignment = 128;
EXPECT_TRUE(CorrectUsage(size, alignment));
}
+
+} // namespace webrtc
+