1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // A binary doing various malloc calls to check that the malloc policy works as 16 // expected. 17 18 #include <cstdlib> 19 #include <vector> 20 test()21void test() { 22 std::vector<void*> ptrs; 23 24 for (size_t n = 1; n <= 0x1000000; n *= 2) { 25 void* buf = malloc(n); 26 if (buf == nullptr) { 27 exit(EXIT_FAILURE); 28 } 29 ptrs.push_back(buf); 30 } 31 32 for (size_t n = 1; n <= 0x1000000; n *= 2) { 33 void* buf = calloc(5, n); 34 if (buf == nullptr) { 35 exit(EXIT_FAILURE); 36 } 37 ptrs.push_back(buf); 38 } 39 40 for (int n = 0; n < ptrs.size(); n++) { 41 void* buf = realloc(ptrs[n], 100); 42 if (buf == nullptr) { 43 exit(EXIT_FAILURE); 44 } 45 ptrs[n] = buf; 46 } 47 48 for (auto ptr : ptrs) { 49 free(ptr); 50 } 51 ptrs.clear(); 52 53 // Apply a bit of memory pressure, to trigger alternate allocator behaviors. 54 for (size_t n = 0; n < 0x200; n++) { 55 void* buf = malloc(0x400); 56 if (buf == nullptr) { 57 exit(EXIT_FAILURE); 58 } 59 ptrs.push_back(buf); 60 } 61 62 for (auto ptr : ptrs) { 63 free(ptr); 64 } 65 } 66 main()67int main() { 68 test(); 69 return EXIT_SUCCESS; 70 } 71