blob: 770a76080626ba486f53351093cfd1a7626b27dd [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2004, Google Inc.
3// All rights reserved.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Sanjay Ghemawat
33//
34// Check that we do not leak memory when cycling through lots of threads.
35
36#include "config_for_unittests.h"
37#include <stdio.h>
38#ifdef HAVE_UNISTD_H
39#include <unistd.h> // for sleep()
40#endif
41#include "base/logging.h"
42#include <gperftools/malloc_extension.h>
43#include "tests/testutil.h" // for RunThread()
44
45// Size/number of objects to allocate per thread (1 MB per thread)
46static const int kObjectSize = 1024;
47static const int kNumObjects = 1024;
48
49// Number of threads to create and destroy
50static const int kNumThreads = 1000;
51
52// Allocate lots of stuff
53static void AllocStuff() {
54 void** objects = new void*[kNumObjects];
55 for (int i = 0; i < kNumObjects; i++) {
56 objects[i] = malloc(kObjectSize);
57 }
58 for (int i = 0; i < kNumObjects; i++) {
59 free(objects[i]);
60 }
61 delete[] objects;
62}
63
64int main(int argc, char** argv) {
65 static const int kDisplaySize = 1048576;
66 char* display = new char[kDisplaySize];
67
68 for (int i = 0; i < kNumThreads; i++) {
69 RunThread(&AllocStuff);
70
71 if (((i+1) % 200) == 0) {
72 fprintf(stderr, "Iteration: %d of %d\n", (i+1), kNumThreads);
73 MallocExtension::instance()->GetStats(display, kDisplaySize);
74 fprintf(stderr, "%s\n", display);
75 }
76 }
77 delete[] display;
78
79 printf("PASS\n");
80#ifdef HAVE_UNISTD_H
81 sleep(1); // Prevent exit race problem with glibc
82#endif
83 return 0;
84}