| OLD | NEW |
| 1 /* Copyright (c) 2006, Google Inc. | 1 /* Copyright (c) 2006, 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 allocated.erase(it); | 139 allocated.erase(it); |
| 140 } | 140 } |
| 141 if (use_new_arena) { | 141 if (use_new_arena) { |
| 142 CHECK(LowLevelAlloc::DeleteArena(arena)); | 142 CHECK(LowLevelAlloc::DeleteArena(arena)); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 // used for counting allocates and frees | 146 // used for counting allocates and frees |
| 147 static int32 allocates; | 147 static int32 allocates; |
| 148 static int32 frees; | 148 static int32 frees; |
| 149 static MallocHook::NewHook old_alloc_hook; | |
| 150 static MallocHook::DeleteHook old_free_hook; | |
| 151 | 149 |
| 152 // called on each alloc if kCallMallocHook specified | 150 // called on each alloc if kCallMallocHook specified |
| 153 static void AllocHook(const void *p, size_t size) { | 151 static void AllocHook(const void *p, size_t size) { |
| 154 if (using_low_level_alloc) { | 152 if (using_low_level_alloc) { |
| 155 allocates++; | 153 allocates++; |
| 156 } | 154 } |
| 157 if (old_alloc_hook != 0) { | |
| 158 (*old_alloc_hook)(p, size); | |
| 159 } | |
| 160 } | 155 } |
| 161 | 156 |
| 162 // called on each free if kCallMallocHook specified | 157 // called on each free if kCallMallocHook specified |
| 163 static void FreeHook(const void *p) { | 158 static void FreeHook(const void *p) { |
| 164 if (using_low_level_alloc) { | 159 if (using_low_level_alloc) { |
| 165 frees++; | 160 frees++; |
| 166 } | 161 } |
| 167 if (old_free_hook != 0) { | |
| 168 (*old_free_hook)(p); | |
| 169 } | |
| 170 } | 162 } |
| 171 | 163 |
| 172 int main(int argc, char *argv[]) { | 164 int main(int argc, char *argv[]) { |
| 173 // This is needed by maybe_threads_unittest.sh, which parses argv[0] | 165 // This is needed by maybe_threads_unittest.sh, which parses argv[0] |
| 174 // to figure out what directory low_level_alloc_unittest is in. | 166 // to figure out what directory low_level_alloc_unittest is in. |
| 175 if (argc != 1) { | 167 if (argc != 1) { |
| 176 fprintf(stderr, "USAGE: %s\n", argv[0]); | 168 fprintf(stderr, "USAGE: %s\n", argv[0]); |
| 177 return 1; | 169 return 1; |
| 178 } | 170 } |
| 179 | 171 |
| 180 old_alloc_hook = MallocHook::SetNewHook(AllocHook); | 172 CHECK(MallocHook::AddNewHook(&AllocHook)); |
| 181 old_free_hook = MallocHook::SetDeleteHook(FreeHook); | 173 CHECK(MallocHook::AddDeleteHook(&FreeHook)); |
| 182 CHECK_EQ(allocates, 0); | 174 CHECK_EQ(allocates, 0); |
| 183 CHECK_EQ(frees, 0); | 175 CHECK_EQ(frees, 0); |
| 184 Test(false, false, 50000); | 176 Test(false, false, 50000); |
| 185 CHECK_NE(allocates, 0); // default arena calls hooks | 177 CHECK_NE(allocates, 0); // default arena calls hooks |
| 186 CHECK_NE(frees, 0); | 178 CHECK_NE(frees, 0); |
| 187 for (int i = 0; i != 16; i++) { | 179 for (int i = 0; i != 16; i++) { |
| 188 bool call_hooks = ((i & 1) == 1); | 180 bool call_hooks = ((i & 1) == 1); |
| 189 allocates = 0; | 181 allocates = 0; |
| 190 frees = 0; | 182 frees = 0; |
| 191 Test(true, call_hooks, 15000); | 183 Test(true, call_hooks, 15000); |
| 192 if (call_hooks) { | 184 if (call_hooks) { |
| 193 CHECK_GT(allocates, 5000); // arena calls hooks | 185 CHECK_GT(allocates, 5000); // arena calls hooks |
| 194 CHECK_GT(frees, 5000); | 186 CHECK_GT(frees, 5000); |
| 195 } else { | 187 } else { |
| 196 CHECK_EQ(allocates, 0); // arena doesn't call hooks | 188 CHECK_EQ(allocates, 0); // arena doesn't call hooks |
| 197 CHECK_EQ(frees, 0); | 189 CHECK_EQ(frees, 0); |
| 198 } | 190 } |
| 199 } | 191 } |
| 200 printf("\nPASS\n"); | 192 printf("\nPASS\n"); |
| 201 CHECK_EQ(MallocHook::SetNewHook(old_alloc_hook), AllocHook); | 193 CHECK(MallocHook::RemoveNewHook(&AllocHook)); |
| 202 CHECK_EQ(MallocHook::SetDeleteHook(old_free_hook), FreeHook); | 194 CHECK(MallocHook::RemoveDeleteHook(&FreeHook)); |
| 203 return 0; | 195 return 0; |
| 204 } | 196 } |
| OLD | NEW |