Thanks to Matthew Riley who noticed this issue and provided the initial proposal for the fix.

There's a bug in the new allocator<T> implementation used by wasteful_vector.  It inherits the base class' implementation of allocator and doesn't implement allocate() so it goes to the heap instead of the PageAllocator -- the very thing wasteful_vector was trying to avoid!  As a side effect it was also leaking heap memory.

Thanks,
-Ivan
Review URL: https://breakpad.appspot.com/599002

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1188 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
ivan.penkov@gmail.com 2013-05-23 18:47:49 +00:00
parent 2b1f82e1c8
commit 5bdee65f7a
2 changed files with 36 additions and 12 deletions

View File

@ -30,13 +30,14 @@
#ifndef GOOGLE_BREAKPAD_COMMON_MEMORY_H_ #ifndef GOOGLE_BREAKPAD_COMMON_MEMORY_H_
#define GOOGLE_BREAKPAD_COMMON_MEMORY_H_ #define GOOGLE_BREAKPAD_COMMON_MEMORY_H_
#include <memory>
#include <vector>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <memory>
#include <vector>
#ifdef __APPLE__ #ifdef __APPLE__
#define sys_mmap mmap #define sys_mmap mmap
#define sys_mmap2 mmap #define sys_mmap2 mmap
@ -87,12 +88,27 @@ class PageAllocator {
if (!ret) if (!ret)
return NULL; return NULL;
page_offset_ = (page_size_ - (page_size_ * pages - (bytes + sizeof(PageHeader)))) % page_size_; page_offset_ =
(page_size_ - (page_size_ * pages - (bytes + sizeof(PageHeader)))) %
page_size_;
current_page_ = page_offset_ ? ret + page_size_ * (pages - 1) : NULL; current_page_ = page_offset_ ? ret + page_size_ * (pages - 1) : NULL;
return ret + sizeof(PageHeader); return ret + sizeof(PageHeader);
} }
// Checks whether the page allocator owns the passed-in pointer.
// This method exists for testing pursposes only.
bool OwnsPointer(const void* p) {
PageHeader* header = last_;
for (header = last_; header; header = header->next) {
const char* current = reinterpret_cast<char*>(header);
if ((p >= current) && (p < current + header->num_pages * page_size_))
return true;
}
return false;
}
private: private:
uint8_t *GetNPages(unsigned num_pages) { uint8_t *GetNPages(unsigned num_pages) {
#ifdef __x86_64 #ifdef __x86_64
@ -135,16 +151,16 @@ class PageAllocator {
// Wrapper to use with STL containers // Wrapper to use with STL containers
template <typename T> template <typename T>
struct PageStdAllocator: public std::allocator<T> { struct PageStdAllocator : public std::allocator<T> {
typedef typename std::allocator<T>::pointer pointer; typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::size_type size_type; typedef typename std::allocator<T>::size_type size_type;
PageStdAllocator(PageAllocator& allocator): allocator_(allocator) {} explicit PageStdAllocator(PageAllocator& allocator): allocator_(allocator) {}
template <class Other> PageStdAllocator(const PageStdAllocator<Other>& other): template <class Other> PageStdAllocator(const PageStdAllocator<Other>& other)
allocator_(other.allocator_) {} : allocator_(other.allocator_) {}
inline pointer allocator(size_type n, const void* = 0) { inline pointer allocate(size_type n, const void* = 0) {
return allocator_.Alloc(sizeof(T) * n); return static_cast<pointer>(allocator_.Alloc(sizeof(T) * n));
} }
inline void deallocate(pointer, size_type) { inline void deallocate(pointer, size_type) {
@ -155,7 +171,7 @@ struct PageStdAllocator: public std::allocator<T> {
typedef PageStdAllocator<U> other; typedef PageStdAllocator<U> other;
}; };
private: private:
PageAllocator& allocator_; PageAllocator& allocator_;
}; };
@ -163,7 +179,7 @@ private:
// PageAllocator. It's wasteful because, when resizing, it always allocates a // PageAllocator. It's wasteful because, when resizing, it always allocates a
// whole new array since the PageAllocator doesn't support realloc. // whole new array since the PageAllocator doesn't support realloc.
template<class T> template<class T>
class wasteful_vector: public std::vector<T, PageStdAllocator<T> > { class wasteful_vector : public std::vector<T, PageStdAllocator<T> > {
public: public:
wasteful_vector(PageAllocator* allocator, unsigned size_hint = 16) wasteful_vector(PageAllocator* allocator, unsigned size_hint = 16)
: std::vector<T, PageStdAllocator<T> >(PageStdAllocator<T>(*allocator)) { : std::vector<T, PageStdAllocator<T> >(PageStdAllocator<T>(*allocator)) {
@ -175,7 +191,7 @@ class wasteful_vector: public std::vector<T, PageStdAllocator<T> > {
inline void* operator new(size_t nbytes, inline void* operator new(size_t nbytes,
google_breakpad::PageAllocator& allocator) { google_breakpad::PageAllocator& allocator) {
return allocator.Alloc(nbytes); return allocator.Alloc(nbytes);
} }
#endif // GOOGLE_BREAKPAD_COMMON_MEMORY_H_ #endif // GOOGLE_BREAKPAD_COMMON_MEMORY_H_

View File

@ -87,3 +87,11 @@ TEST(WastefulVectorTest, Simple) {
for (unsigned i = 0; i < 256; ++i) for (unsigned i = 0; i < 256; ++i)
ASSERT_EQ(v[i], i); ASSERT_EQ(v[i], i);
} }
TEST(WastefulVectorTest, UsesPageAllocator) {
PageAllocator allocator_;
wasteful_vector<unsigned> v(&allocator_);
v.push_back(1);
ASSERT_TRUE(allocator_.OwnsPointer(&v[0]));
}