| 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 21 matching lines...) Expand all Loading... |
| 32 */ | 32 */ |
| 33 | 33 |
| 34 #ifndef BASE_MEMORY_REGION_MAP_H_ | 34 #ifndef BASE_MEMORY_REGION_MAP_H_ |
| 35 #define BASE_MEMORY_REGION_MAP_H_ | 35 #define BASE_MEMORY_REGION_MAP_H_ |
| 36 | 36 |
| 37 #include <config.h> | 37 #include <config.h> |
| 38 | 38 |
| 39 #ifdef HAVE_PTHREAD | 39 #ifdef HAVE_PTHREAD |
| 40 #include <pthread.h> | 40 #include <pthread.h> |
| 41 #endif | 41 #endif |
| 42 #include <stddef.h> |
| 42 #include <set> | 43 #include <set> |
| 43 #include "base/stl_allocator.h" | 44 #include "base/stl_allocator.h" |
| 44 #include "base/spinlock.h" | 45 #include "base/spinlock.h" |
| 45 #include "base/thread_annotations.h" | 46 #include "base/thread_annotations.h" |
| 46 #include "base/low_level_alloc.h" | 47 #include "base/low_level_alloc.h" |
| 47 | 48 |
| 48 // TODO(maxim): add a unittest: | 49 // TODO(maxim): add a unittest: |
| 49 // execute a bunch of mmaps and compare memory map what strace logs | 50 // execute a bunch of mmaps and compare memory map what strace logs |
| 50 // execute a bunch of mmap/munmup and compare memory map with | 51 // execute a bunch of mmap/munmup and compare memory map with |
| 51 // own accounting of what those mmaps generated | 52 // own accounting of what those mmaps generated |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // MemoryRegionMap::Lock()/Unlock() to manage the locks. | 92 // MemoryRegionMap::Lock()/Unlock() to manage the locks. |
| 92 // Uses Lock/Unlock inside. | 93 // Uses Lock/Unlock inside. |
| 93 static void Init(int max_stack_depth); | 94 static void Init(int max_stack_depth); |
| 94 | 95 |
| 95 // Try to shutdown this module undoing what Init() did. | 96 // Try to shutdown this module undoing what Init() did. |
| 96 // Returns true iff could do full shutdown (or it was not attempted). | 97 // Returns true iff could do full shutdown (or it was not attempted). |
| 97 // Full shutdown is attempted when the number of Shutdown() calls equals | 98 // Full shutdown is attempted when the number of Shutdown() calls equals |
| 98 // the number of Init() calls. | 99 // the number of Init() calls. |
| 99 static bool Shutdown(); | 100 static bool Shutdown(); |
| 100 | 101 |
| 101 // Check that our hooks are still in place and crash if not. | |
| 102 // No need for locking. | |
| 103 static void CheckMallocHooks(); | |
| 104 | |
| 105 // Locks to protect our internal data structures. | 102 // Locks to protect our internal data structures. |
| 106 // These also protect use of arena_ if our Init() has been done. | 103 // These also protect use of arena_ if our Init() has been done. |
| 107 // The lock is recursive. | 104 // The lock is recursive. |
| 108 static void Lock() EXCLUSIVE_LOCK_FUNCTION(lock_); | 105 static void Lock() EXCLUSIVE_LOCK_FUNCTION(lock_); |
| 109 static void Unlock() UNLOCK_FUNCTION(lock_); | 106 static void Unlock() UNLOCK_FUNCTION(lock_); |
| 110 | 107 |
| 111 // Returns true when the lock is held by this thread (for use in RAW_CHECK-s). | 108 // Returns true when the lock is held by this thread (for use in RAW_CHECK-s). |
| 112 static bool LockIsHeld(); | 109 static bool LockIsHeld(); |
| 113 | 110 |
| 114 // Locker object that acquires the MemoryRegionMap::Lock | 111 // Locker object that acquires the MemoryRegionMap::Lock |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 bool operator()(const Region& x, const Region& y) const { | 221 bool operator()(const Region& x, const Region& y) const { |
| 225 return x.end_addr < y.end_addr; | 222 return x.end_addr < y.end_addr; |
| 226 } | 223 } |
| 227 }; | 224 }; |
| 228 | 225 |
| 229 // We allocate STL objects in our own arena. | 226 // We allocate STL objects in our own arena. |
| 230 struct MyAllocator { | 227 struct MyAllocator { |
| 231 static void *Allocate(size_t n) { | 228 static void *Allocate(size_t n) { |
| 232 return LowLevelAlloc::AllocWithArena(n, arena_); | 229 return LowLevelAlloc::AllocWithArena(n, arena_); |
| 233 } | 230 } |
| 234 static void Free(const void *p) { | 231 static void Free(const void *p, size_t /* n */) { |
| 235 LowLevelAlloc::Free(const_cast<void*>(p)); | 232 LowLevelAlloc::Free(const_cast<void*>(p)); |
| 236 } | 233 } |
| 237 }; | 234 }; |
| 238 | 235 |
| 239 // Set of the memory regions | 236 // Set of the memory regions |
| 240 typedef std::set<Region, RegionCmp, | 237 typedef std::set<Region, RegionCmp, |
| 241 STL_Allocator<Region, MyAllocator> > RegionSet; | 238 STL_Allocator<Region, MyAllocator> > RegionSet; |
| 242 | 239 |
| 243 public: // more in-depth interface ========================================== | 240 public: // more in-depth interface ========================================== |
| 244 | 241 |
| 245 // STL iterator with values of Region | 242 // STL iterator with values of Region |
| 246 typedef RegionSet::const_iterator RegionIterator; | 243 typedef RegionSet::const_iterator RegionIterator; |
| 247 | 244 |
| 248 // Return the begin/end iterators to all the regions. | 245 // Return the begin/end iterators to all the regions. |
| 249 // These need Lock/Unlock protection around their whole usage (loop). | 246 // These need Lock/Unlock protection around their whole usage (loop). |
| 250 // Even when the same thread causes modifications during such a loop | 247 // Even when the same thread causes modifications during such a loop |
| 251 // (which are permitted due to recursive locking) | 248 // (which are permitted due to recursive locking) |
| 252 // the loop iterator will still be valid as long as its region | 249 // the loop iterator will still be valid as long as its region |
| 253 // has not been deleted, but EndRegionLocked should be | 250 // has not been deleted, but EndRegionLocked should be |
| 254 // re-evaluated whenever the set of regions has changed. | 251 // re-evaluated whenever the set of regions has changed. |
| 255 static RegionIterator BeginRegionLocked(); | 252 static RegionIterator BeginRegionLocked(); |
| 256 static RegionIterator EndRegionLocked(); | 253 static RegionIterator EndRegionLocked(); |
| 257 | 254 |
| 258 // Effectively private type from our .cc ================================= | 255 // Effectively private type from our .cc ================================= |
| 259 // public to let us declare global objects: | 256 // public to let us declare global objects: |
| 260 union RegionSetRep; | 257 union RegionSetRep; |
| 261 | 258 |
| 262 private: | 259 private: |
| 263 | |
| 264 // representation =========================================================== | 260 // representation =========================================================== |
| 265 | 261 |
| 266 // Counter of clients of this module that have called Init(). | 262 // Counter of clients of this module that have called Init(). |
| 267 static int client_count_; | 263 static int client_count_; |
| 268 | 264 |
| 269 // Maximal number of caller stack frames to save (>= 0). | 265 // Maximal number of caller stack frames to save (>= 0). |
| 270 static int max_stack_depth_; | 266 static int max_stack_depth_; |
| 271 | 267 |
| 272 // Arena used for our allocations in regions_. | 268 // Arena used for our allocations in regions_. |
| 273 static LowLevelAlloc::Arena* arena_; | 269 static LowLevelAlloc::Arena* arena_; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 static void SbrkHook(const void* result, std::ptrdiff_t increment); | 322 static void SbrkHook(const void* result, std::ptrdiff_t increment); |
| 327 | 323 |
| 328 // Log all memory regions; Useful for debugging only. | 324 // Log all memory regions; Useful for debugging only. |
| 329 // Assumes Lock() is held | 325 // Assumes Lock() is held |
| 330 static void LogAllLocked(); | 326 static void LogAllLocked(); |
| 331 | 327 |
| 332 DISALLOW_COPY_AND_ASSIGN(MemoryRegionMap); | 328 DISALLOW_COPY_AND_ASSIGN(MemoryRegionMap); |
| 333 }; | 329 }; |
| 334 | 330 |
| 335 #endif // BASE_MEMORY_REGION_MAP_H_ | 331 #endif // BASE_MEMORY_REGION_MAP_H_ |
| OLD | NEW |