~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to src/tests/low_level_alloc_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006, Google Inc.
 
2
 * All rights reserved.
 
3
 * 
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 * 
 
8
 *     * Redistributions of source code must retain the above copyright
 
9
 * notice, this list of conditions and the following disclaimer.
 
10
 *     * Redistributions in binary form must reproduce the above
 
11
 * copyright notice, this list of conditions and the following disclaimer
 
12
 * in the documentation and/or other materials provided with the
 
13
 * distribution.
 
14
 *     * Neither the name of Google Inc. nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from
 
16
 * this software without specific prior written permission.
 
17
 * 
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
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.
 
29
 *
 
30
 * ---
 
31
 * Author: Mike Burrows
 
32
 */
 
33
 
 
34
// A test for low_level_alloc.cc
 
35
 
 
36
#include <map>
 
37
#include "base/low_level_alloc.h"
 
38
#include "base/logging.h"
 
39
#include <google/malloc_hook.h>
 
40
 
 
41
using std::map;
 
42
 
 
43
// a block of memory obtained from the allocator
 
44
struct BlockDesc {
 
45
  char *ptr;      // pointer to memory
 
46
  int len;        // number of bytes
 
47
  int fill;       // filled with data starting with this
 
48
};
 
49
 
 
50
// Check that the pattern placed in the block d
 
51
// by RandomizeBlockDesc is still there.
 
52
static void CheckBlockDesc(const BlockDesc &d) {
 
53
  for (int i = 0; i != d.len; i++) {
 
54
    CHECK((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
 
55
  }
 
56
}
 
57
 
 
58
// Fill the block "*d" with a pattern
 
59
// starting with a random byte.
 
60
static void RandomizeBlockDesc(BlockDesc *d) {
 
61
  d->fill = rand() & 0xff;
 
62
  for (int i = 0; i != d->len; i++) {
 
63
    d->ptr[i] = (d->fill + i) & 0xff;
 
64
  }
 
65
}
 
66
 
 
67
// Use to indicate to the malloc hooks that
 
68
// this calls is from LowLevelAlloc.
 
69
static bool using_low_level_alloc = false;
 
70
 
 
71
// n times, toss a coin, and based on the outcome
 
72
// either allocate a new block or deallocate an old block.
 
73
// New blocks are placed in a map with a random key
 
74
// and initialized with RandomizeBlockDesc().
 
75
// If keys conflict, the older block is freed.
 
76
// Old blocks are always checked with CheckBlockDesc()
 
77
// before being freed.  At the end of the run,
 
78
// all remaining allocated blocks are freed.
 
79
// If use_new_arena is true, use a fresh arena, and then delete it.
 
80
// If call_malloc_hook is true and user_arena is true, 
 
81
// allocations and deallocations are reported via the MallocHook
 
82
// interface.
 
83
static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
 
84
  typedef map<int, BlockDesc> AllocMap;
 
85
  AllocMap allocated;
 
86
  AllocMap::iterator it;
 
87
  BlockDesc block_desc;
 
88
  int rnd;
 
89
  LowLevelAlloc::Arena *arena = 0;
 
90
  if (use_new_arena) {
 
91
    int32 flags = call_malloc_hook?  LowLevelAlloc::kCallMallocHook :  0;
 
92
    arena = LowLevelAlloc::NewArena(flags, LowLevelAlloc::DefaultArena());
 
93
  }
 
94
  for (int i = 0; i != n; i++) {
 
95
    switch(rand() & 1) {      // toss a coin
 
96
    case 0:     // coin came up heads: add a block
 
97
      using_low_level_alloc = true;
 
98
      block_desc.len = rand() & 0x3fff;
 
99
      block_desc.ptr =
 
100
        reinterpret_cast<char *>(
 
101
                        arena == 0
 
102
                        ? LowLevelAlloc::Alloc(block_desc.len)
 
103
                        : LowLevelAlloc::AllocWithArena(block_desc.len, arena));
 
104
      using_low_level_alloc = false;
 
105
      RandomizeBlockDesc(&block_desc);
 
106
      rnd = rand();
 
107
      it = allocated.find(rnd);
 
108
      if (it != allocated.end()) {
 
109
        CheckBlockDesc(it->second);
 
110
        using_low_level_alloc = true;
 
111
        LowLevelAlloc::Free(it->second.ptr);
 
112
        using_low_level_alloc = false;
 
113
        it->second = block_desc;
 
114
      } else {
 
115
        allocated[rnd] = block_desc;
 
116
      }
 
117
      break;
 
118
    case 1:     // coin came up tails: remove a block
 
119
      it = allocated.begin();
 
120
      if (it != allocated.end()) {
 
121
        CheckBlockDesc(it->second);
 
122
        using_low_level_alloc = true;
 
123
        LowLevelAlloc::Free(it->second.ptr);
 
124
        using_low_level_alloc = false;
 
125
        allocated.erase(it);
 
126
      }
 
127
      break;
 
128
    }
 
129
  }
 
130
  // remove all remaniing blocks
 
131
  while ((it = allocated.begin()) != allocated.end()) {
 
132
    CheckBlockDesc(it->second);
 
133
    using_low_level_alloc = true;
 
134
    LowLevelAlloc::Free(it->second.ptr);
 
135
    using_low_level_alloc = false;
 
136
    allocated.erase(it);
 
137
  }
 
138
  if (use_new_arena) {
 
139
    CHECK(LowLevelAlloc::DeleteArena(arena));
 
140
  }
 
141
}
 
142
 
 
143
// used for counting allocates and frees
 
144
static int32 allocates;
 
145
static int32 frees;
 
146
static MallocHook::NewHook old_alloc_hook;
 
147
static MallocHook::DeleteHook old_free_hook;
 
148
 
 
149
// called on each alloc if kCallMallocHook specified
 
150
static void AllocHook(const void *p, size_t size) {
 
151
  if (using_low_level_alloc) {
 
152
    allocates++;
 
153
  }
 
154
  if (old_alloc_hook != 0) {
 
155
    (*old_alloc_hook)(p, size);
 
156
  }
 
157
}
 
158
 
 
159
// called on each free if kCallMallocHook specified
 
160
static void FreeHook(const void *p) {
 
161
  if (using_low_level_alloc) {
 
162
    frees++;
 
163
  }
 
164
  if (old_free_hook != 0) {
 
165
    (*old_free_hook)(p);
 
166
  }
 
167
}
 
168
 
 
169
int main(int argc, char *argv[]) {
 
170
  // This is needed by maybe_threads_unittest.sh, which parses argv[0]
 
171
  // to figure out what directory low_level_alloc_unittest is in.
 
172
  if (argc != 1) {
 
173
    fprintf(stderr, "USAGE: %s\n", argv[0]);
 
174
    return 1;
 
175
  }
 
176
 
 
177
  old_alloc_hook = MallocHook::SetNewHook(AllocHook);
 
178
  old_free_hook = MallocHook::SetDeleteHook(FreeHook);
 
179
  CHECK_EQ(allocates, 0);
 
180
  CHECK_EQ(frees, 0);
 
181
  Test(false, false, 50000);
 
182
  CHECK_NE(allocates, 0);   // default arena calls hooks
 
183
  CHECK_NE(frees, 0);
 
184
  for (int i = 0; i != 16; i++) {
 
185
    bool call_hooks = ((i & 1) == 1);
 
186
    allocates = 0;
 
187
    frees = 0;
 
188
    Test(true, call_hooks, 15000);
 
189
    if (call_hooks) {
 
190
      CHECK_GT(allocates, 5000); // arena calls hooks
 
191
      CHECK_GT(frees, 5000);
 
192
    } else {
 
193
      CHECK_EQ(allocates, 0);    // arena doesn't call hooks
 
194
      CHECK_EQ(frees, 0);
 
195
    }
 
196
  }
 
197
  printf("PASS\n");
 
198
  CHECK_EQ(MallocHook::SetNewHook(old_alloc_hook), AllocHook);
 
199
  CHECK_EQ(MallocHook::SetDeleteHook(old_free_hook), FreeHook);
 
200
  return 0;
 
201
}