Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: third_party/tcmalloc/chromium/src/tests/system-alloc_unittest.cc

Issue 7050034: Merge google-perftools r109 (the current contents of third_party/tcmalloc/vendor) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2007, Google Inc. 1 // Copyright (c) 2007, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 // --- 30 // ---
31 // Author: Arun Sharma 31 // Author: Arun Sharma
32 32
33 #include "config_for_unittests.h" 33 #include "config_for_unittests.h"
34 #include "system-alloc.h"
34 #include <stdio.h> 35 #include <stdio.h>
35 #if defined HAVE_STDINT_H 36 #if defined HAVE_STDINT_H
36 #include <stdint.h> // to get uintptr_t 37 #include <stdint.h> // to get uintptr_t
37 #elif defined HAVE_INTTYPES_H 38 #elif defined HAVE_INTTYPES_H
38 #include <inttypes.h> // another place uintptr_t might be defined 39 #include <inttypes.h> // another place uintptr_t might be defined
39 #endif 40 #endif
40 #include <sys/types.h> 41 #include <sys/types.h>
41 #include "base/logging.h" 42 #include <algorithm>
42 #include "system-alloc.h" 43 #include "base/logging.h" // for Check_GEImpl, Check_LTImpl, etc
44 #include <google/malloc_extension.h> // for MallocExtension::instance
45 #include "common.h" // for kAddressBits
43 46
44 class ArraySysAllocator : public SysAllocator { 47 class ArraySysAllocator : public SysAllocator {
45 public: 48 public:
46 // Was this allocator invoked at least once? 49 // Was this allocator invoked at least once?
47 bool invoked_; 50 bool invoked_;
48 51
49 ArraySysAllocator() : SysAllocator() { 52 ArraySysAllocator() : SysAllocator() {
50 ptr_ = 0; 53 ptr_ = 0;
51 invoked_ = false; 54 invoked_ = false;
52 } 55 }
53 56
54 void* Alloc(size_t size, size_t *actual_size, size_t alignment) { 57 void* Alloc(size_t size, size_t *actual_size, size_t alignment) {
55 invoked_ = true; 58 invoked_ = true;
59
60 if (size > kArraySize) {
61 return NULL;
62 }
63
56 void *result = &array_[ptr_]; 64 void *result = &array_[ptr_];
57 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); 65 uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
58 66
59 if (actual_size) { 67 if (actual_size) {
60 *actual_size = size; 68 *actual_size = size;
61 } 69 }
62 70
63 // Try to get more memory for alignment 71 // Try to get more memory for alignment
64 size_t extra = alignment - (ptr & (alignment-1)); 72 size_t extra = alignment - (ptr & (alignment-1));
65 size += extra; 73 size += extra;
66 CHECK_LT(ptr_ + size, kArraySize); 74 CHECK_LT(ptr_ + size, kArraySize);
67 75
68 if ((ptr & (alignment-1)) != 0) { 76 if ((ptr & (alignment-1)) != 0) {
69 ptr += alignment - (ptr & (alignment-1)); 77 ptr += alignment - (ptr & (alignment-1));
70 } 78 }
71 79
72 ptr_ += size; 80 ptr_ += size;
73 return reinterpret_cast<void *>(ptr); 81 return reinterpret_cast<void *>(ptr);
74 } 82 }
75 83
76 void DumpStats(TCMalloc_Printer* printer) { 84 void DumpStats() {
77 } 85 }
86 void FlagsInitialized() {}
78 87
79 private: 88 private:
80 static const int kArraySize = 8 * 1024 * 1024; 89 static const int kArraySize = 8 * 1024 * 1024;
81 char array_[kArraySize]; 90 char array_[kArraySize];
82 // We allocate the next chunk from here 91 // We allocate the next chunk from here
83 int ptr_; 92 int ptr_;
84 93
85 }; 94 };
86 const int ArraySysAllocator::kArraySize; 95 const int ArraySysAllocator::kArraySize;
87 ArraySysAllocator a; 96 ArraySysAllocator a;
88 97
89 static void TestBasicInvoked() { 98 static void TestBasicInvoked() {
90 RegisterSystemAllocator(&a, 0); 99 MallocExtension::instance()->SetSystemAllocator(&a);
91 100
92 // An allocation size that is likely to trigger the system allocator. 101 // An allocation size that is likely to trigger the system allocator.
93 // XXX: this is implementation specific. 102 // XXX: this is implementation specific.
94 char *p = new char[1024 * 1024]; 103 char *p = new char[1024 * 1024];
95 delete [] p; 104 delete [] p;
96 105
97 // Make sure that our allocator was invoked. 106 // Make sure that our allocator was invoked.
98 CHECK(a.invoked_); 107 CHECK(a.invoked_);
99 } 108 }
100 109
110 #if 0 // could port this to various OSs, but won't bother for now
111 TEST(AddressBits, CpuVirtualBits) {
112 // Check that kAddressBits is as least as large as either the number of bits
113 // in a pointer or as the number of virtual bits handled by the processor.
114 // To be effective this test must be run on each processor model.
115 const int kPointerBits = 8 * sizeof(void*);
116 const int kImplementedVirtualBits = NumImplementedVirtualBits();
117
118 CHECK_GE(kAddressBits, min(kImplementedVirtualBits, kPointerBits));
119 }
120 #endif
121
122 static void TestBasicRetryFailTest() {
123 // Check with the allocator still works after a failed allocation.
124 void* p = malloc(1ULL << 50); // Asking for 1P ram
125 CHECK(p == NULL);
126
127 char* q = new char[1024];
128 CHECK(q != NULL);
129 delete [] q;
130 }
131
101 int main(int argc, char** argv) { 132 int main(int argc, char** argv) {
102 TestBasicInvoked(); 133 TestBasicInvoked();
134 TestBasicRetryFailTest();
103 135
104 printf("PASS\n"); 136 printf("PASS\n");
105 return 0; 137 return 0;
106 } 138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698