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> | |
44 #include "base/logging.h" // for Check_GEImpl, Check_LTImpl, etc | 43 #include "base/logging.h" // for Check_GEImpl, Check_LTImpl, etc |
45 #include <gperftools/malloc_extension.h> // for MallocExtension::instance | 44 #include <google/malloc_extension.h> // for MallocExtension::instance |
46 #include "common.h" // for kAddressBits | 45 #include "common.h" // for kAddressBits |
47 | 46 |
48 class ArraySysAllocator : public SysAllocator { | 47 class ArraySysAllocator : public SysAllocator { |
49 public: | 48 public: |
50 // Was this allocator invoked at least once? | 49 // Was this allocator invoked at least once? |
51 bool invoked_; | 50 bool invoked_; |
52 | 51 |
53 ArraySysAllocator() : SysAllocator() { | 52 ArraySysAllocator() : SysAllocator() { |
54 ptr_ = 0; | 53 ptr_ = 0; |
55 invoked_ = false; | 54 invoked_ = false; |
(...skipping 21 matching lines...) Expand all Loading... |
77 if ((ptr & (alignment-1)) != 0) { | 76 if ((ptr & (alignment-1)) != 0) { |
78 ptr += alignment - (ptr & (alignment-1)); | 77 ptr += alignment - (ptr & (alignment-1)); |
79 } | 78 } |
80 | 79 |
81 ptr_ += size; | 80 ptr_ += size; |
82 return reinterpret_cast<void *>(ptr); | 81 return reinterpret_cast<void *>(ptr); |
83 } | 82 } |
84 | 83 |
85 void DumpStats() { | 84 void DumpStats() { |
86 } | 85 } |
| 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, std::min(kImplementedVirtualBits, kPointerBits)); | 118 CHECK_GE(kAddressBits, 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 // | 124 void* p = malloc(1ULL << 50); // Asking for 1P ram |
125 // There is no way to call malloc and guarantee it will fail. malloc takes a | 125 CHECK(p == NULL); |
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); | |
142 | 126 |
143 char* q = new char[1024]; | 127 char* q = new char[1024]; |
144 CHECK(q != NULL); | 128 CHECK(q != NULL); |
145 delete [] q; | 129 delete [] q; |
146 } | 130 } |
147 | 131 |
148 int main(int argc, char** argv) { | 132 int main(int argc, char** argv) { |
149 TestBasicInvoked(); | 133 TestBasicInvoked(); |
150 TestBasicRetryFailTest(); | 134 TestBasicRetryFailTest(); |
151 | 135 |
152 printf("PASS\n"); | 136 printf("PASS\n"); |
153 return 0; | 137 return 0; |
154 } | 138 } |
OLD | NEW |