OLD | NEW |
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 22 matching lines...) Expand all Loading... |
33 #include "config_for_unittests.h" | 33 #include "config_for_unittests.h" |
34 #include "system-alloc.h" | 34 #include "system-alloc.h" |
35 #include <stdio.h> | 35 #include <stdio.h> |
36 #if defined HAVE_STDINT_H | 36 #if defined HAVE_STDINT_H |
37 #include <stdint.h> // to get uintptr_t | 37 #include <stdint.h> // to get uintptr_t |
38 #elif defined HAVE_INTTYPES_H | 38 #elif defined HAVE_INTTYPES_H |
39 #include <inttypes.h> // another place uintptr_t might be defined | 39 #include <inttypes.h> // another place uintptr_t might be defined |
40 #endif | 40 #endif |
41 #include <sys/types.h> | 41 #include <sys/types.h> |
42 #include <algorithm> | 42 #include <algorithm> |
| 43 #include <limits> |
43 #include "base/logging.h" // for Check_GEImpl, Check_LTImpl, etc | 44 #include "base/logging.h" // for Check_GEImpl, Check_LTImpl, etc |
44 #include <google/malloc_extension.h> // for MallocExtension::instance | 45 #include <gperftools/malloc_extension.h> // for MallocExtension::instance |
45 #include "common.h" // for kAddressBits | 46 #include "common.h" // for kAddressBits |
46 | 47 |
47 class ArraySysAllocator : public SysAllocator { | 48 class ArraySysAllocator : public SysAllocator { |
48 public: | 49 public: |
49 // Was this allocator invoked at least once? | 50 // Was this allocator invoked at least once? |
50 bool invoked_; | 51 bool invoked_; |
51 | 52 |
52 ArraySysAllocator() : SysAllocator() { | 53 ArraySysAllocator() : SysAllocator() { |
53 ptr_ = 0; | 54 ptr_ = 0; |
54 invoked_ = false; | 55 invoked_ = false; |
(...skipping 21 matching lines...) Expand all Loading... |
76 if ((ptr & (alignment-1)) != 0) { | 77 if ((ptr & (alignment-1)) != 0) { |
77 ptr += alignment - (ptr & (alignment-1)); | 78 ptr += alignment - (ptr & (alignment-1)); |
78 } | 79 } |
79 | 80 |
80 ptr_ += size; | 81 ptr_ += size; |
81 return reinterpret_cast<void *>(ptr); | 82 return reinterpret_cast<void *>(ptr); |
82 } | 83 } |
83 | 84 |
84 void DumpStats() { | 85 void DumpStats() { |
85 } | 86 } |
86 void FlagsInitialized() {} | |
87 | 87 |
88 private: | 88 private: |
89 static const int kArraySize = 8 * 1024 * 1024; | 89 static const int kArraySize = 8 * 1024 * 1024; |
90 char array_[kArraySize]; | 90 char array_[kArraySize]; |
91 // We allocate the next chunk from here | 91 // We allocate the next chunk from here |
92 int ptr_; | 92 int ptr_; |
93 | 93 |
94 }; | 94 }; |
95 const int ArraySysAllocator::kArraySize; | 95 const int ArraySysAllocator::kArraySize; |
96 ArraySysAllocator a; | 96 ArraySysAllocator a; |
(...skipping 11 matching lines...) Expand all Loading... |
108 } | 108 } |
109 | 109 |
110 #if 0 // could port this to various OSs, but won't bother for now | 110 #if 0 // could port this to various OSs, but won't bother for now |
111 TEST(AddressBits, CpuVirtualBits) { | 111 TEST(AddressBits, CpuVirtualBits) { |
112 // Check that kAddressBits is as least as large as either the number of bits | 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. | 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. | 114 // To be effective this test must be run on each processor model. |
115 const int kPointerBits = 8 * sizeof(void*); | 115 const int kPointerBits = 8 * sizeof(void*); |
116 const int kImplementedVirtualBits = NumImplementedVirtualBits(); | 116 const int kImplementedVirtualBits = NumImplementedVirtualBits(); |
117 | 117 |
118 CHECK_GE(kAddressBits, min(kImplementedVirtualBits, kPointerBits)); | 118 CHECK_GE(kAddressBits, std::min(kImplementedVirtualBits, kPointerBits)); |
119 } | 119 } |
120 #endif | 120 #endif |
121 | 121 |
122 static void TestBasicRetryFailTest() { | 122 static void TestBasicRetryFailTest() { |
123 // Check with the allocator still works after a failed allocation. | 123 // Check with the allocator still works after a failed allocation. |
124 void* p = malloc(1ULL << 50); // Asking for 1P ram | 124 // |
125 CHECK(p == NULL); | 125 // There is no way to call malloc and guarantee it will fail. malloc takes a |
| 126 // size_t parameter and the C++ standard does not constrain the size of |
| 127 // size_t. For example, consider an implementation where size_t is 32 bits |
| 128 // and pointers are 64 bits. |
| 129 // |
| 130 // It is likely, though, that sizeof(size_t) == sizeof(void*). In that case, |
| 131 // the first allocation here might succeed but the second allocation must |
| 132 // fail. |
| 133 // |
| 134 // If the second allocation succeeds, you will have to rewrite or |
| 135 // disable this test. |
| 136 // The weird parens are to avoid macro-expansion of 'max' on windows. |
| 137 const size_t kHugeSize = (std::numeric_limits<size_t>::max)() / 2; |
| 138 void* p1 = malloc(kHugeSize); |
| 139 void* p2 = malloc(kHugeSize); |
| 140 CHECK(p2 == NULL); |
| 141 if (p1 != NULL) free(p1); |
126 | 142 |
127 char* q = new char[1024]; | 143 char* q = new char[1024]; |
128 CHECK(q != NULL); | 144 CHECK(q != NULL); |
129 delete [] q; | 145 delete [] q; |
130 } | 146 } |
131 | 147 |
132 int main(int argc, char** argv) { | 148 int main(int argc, char** argv) { |
133 TestBasicInvoked(); | 149 TestBasicInvoked(); |
134 TestBasicRetryFailTest(); | 150 TestBasicRetryFailTest(); |
135 | 151 |
136 printf("PASS\n"); | 152 printf("PASS\n"); |
137 return 0; | 153 return 0; |
138 } | 154 } |
OLD | NEW |