OLD | NEW |
---|---|
1 // Copyright (c) 2000, Google Inc. | 1 // Copyright (c) 2000, 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 725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
736 // which is a fake header. The following subtraction works for both zero | 736 // which is a fake header. The following subtraction works for both zero |
737 // and non-zero values. | 737 // and non-zero values. |
738 return reinterpret_cast<MallocBlock *>( | 738 return reinterpret_cast<MallocBlock *>( |
739 reinterpret_cast<char *>(mb) - mb->offset_); | 739 reinterpret_cast<char *>(mb) - mb->offset_); |
740 } | 740 } |
741 static const MallocBlock* FromRawPointer(const void* p) { | 741 static const MallocBlock* FromRawPointer(const void* p) { |
742 // const-safe version: we just cast about | 742 // const-safe version: we just cast about |
743 return FromRawPointer(const_cast<void*>(p)); | 743 return FromRawPointer(const_cast<void*>(p)); |
744 } | 744 } |
745 | 745 |
746 // Return whether p points to memory returned by memalign. | |
747 // Requires that p be non-zero and has been checked for sanity with | |
748 // FromRawPointer(). | |
749 static bool IsMemaligned(const void* p) { | |
750 const MallocBlock* mb = reinterpret_cast<const MallocBlock*>( | |
751 reinterpret_cast<const char*>(p) - MallocBlock::data_offset()); | |
752 // If the offset is non-zero, the block was allocated by memalign | |
753 // (see FromRawPointer above). | |
754 return mb->offset_ != 0; | |
jar (doing other things)
2013/01/16 00:59:32
Why is this safe to call if you don't know if this
Paweł Hajdan Jr.
2013/01/16 01:05:09
1. Note this has been reviewed and committed Googl
| |
755 } | |
756 | |
746 void Check(int type) const { | 757 void Check(int type) const { |
747 alloc_map_lock_.Lock(); | 758 alloc_map_lock_.Lock(); |
748 CheckLocked(type); | 759 CheckLocked(type); |
749 alloc_map_lock_.Unlock(); | 760 alloc_map_lock_.Unlock(); |
750 } | 761 } |
751 | 762 |
752 static bool CheckEverything() { | 763 static bool CheckEverything() { |
753 alloc_map_lock_.Lock(); | 764 alloc_map_lock_.Lock(); |
754 if (alloc_map_ != NULL) alloc_map_->Iterate(CheckCallback, 0); | 765 if (alloc_map_ != NULL) alloc_map_->Iterate(CheckCallback, 0); |
755 alloc_map_lock_.Unlock(); | 766 alloc_map_lock_.Unlock(); |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1183 MallocHook::InvokeDeleteHook(ptr); | 1194 MallocHook::InvokeDeleteHook(ptr); |
1184 DebugDeallocate(ptr, MallocBlock::kMallocType); | 1195 DebugDeallocate(ptr, MallocBlock::kMallocType); |
1185 } | 1196 } |
1186 | 1197 |
1187 extern "C" PERFTOOLS_DLL_DECL void* tc_realloc(void* ptr, size_t size) __THROW { | 1198 extern "C" PERFTOOLS_DLL_DECL void* tc_realloc(void* ptr, size_t size) __THROW { |
1188 if (ptr == NULL) { | 1199 if (ptr == NULL) { |
1189 ptr = do_debug_malloc_or_debug_cpp_alloc(size); | 1200 ptr = do_debug_malloc_or_debug_cpp_alloc(size); |
1190 MallocHook::InvokeNewHook(ptr, size); | 1201 MallocHook::InvokeNewHook(ptr, size); |
1191 return ptr; | 1202 return ptr; |
1192 } | 1203 } |
1204 MallocBlock* old = MallocBlock::FromRawPointer(ptr); | |
1205 old->Check(MallocBlock::kMallocType); | |
1206 if (MallocBlock::IsMemaligned(ptr)) { | |
1207 RAW_LOG(FATAL, "realloc/memalign mismatch at %p: " | |
1208 "non-NULL pointers passed to realloc must be obtained " | |
1209 "from malloc, calloc, or realloc", ptr); | |
1210 } | |
1193 if (size == 0) { | 1211 if (size == 0) { |
1194 MallocHook::InvokeDeleteHook(ptr); | 1212 MallocHook::InvokeDeleteHook(ptr); |
1195 DebugDeallocate(ptr, MallocBlock::kMallocType); | 1213 DebugDeallocate(ptr, MallocBlock::kMallocType); |
1196 return NULL; | 1214 return NULL; |
1197 } | 1215 } |
1198 MallocBlock* old = MallocBlock::FromRawPointer(ptr); | |
1199 old->Check(MallocBlock::kMallocType); | |
1200 MallocBlock* p = MallocBlock::Allocate(size, MallocBlock::kMallocType); | 1216 MallocBlock* p = MallocBlock::Allocate(size, MallocBlock::kMallocType); |
1201 | 1217 |
1202 // If realloc fails we are to leave the old block untouched and | 1218 // If realloc fails we are to leave the old block untouched and |
1203 // return null | 1219 // return null |
1204 if (p == NULL) return NULL; | 1220 if (p == NULL) return NULL; |
1205 | 1221 |
1206 memcpy(p->data_addr(), old->data_addr(), | 1222 memcpy(p->data_addr(), old->data_addr(), |
1207 (old->data_size() < size) ? old->data_size() : size); | 1223 (old->data_size() < size) ? old->data_size() : size); |
1208 MallocHook::InvokeDeleteHook(ptr); | 1224 MallocHook::InvokeDeleteHook(ptr); |
1209 MallocHook::InvokeNewHook(p->data_addr(), size); | 1225 MallocHook::InvokeNewHook(p->data_addr(), size); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1412 | 1428 |
1413 #ifdef HAVE_STRUCT_MALLINFO | 1429 #ifdef HAVE_STRUCT_MALLINFO |
1414 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { | 1430 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { |
1415 return BASE_MALLINFO(); | 1431 return BASE_MALLINFO(); |
1416 } | 1432 } |
1417 #endif | 1433 #endif |
1418 | 1434 |
1419 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { | 1435 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { |
1420 return MallocExtension::instance()->GetAllocatedSize(ptr); | 1436 return MallocExtension::instance()->GetAllocatedSize(ptr); |
1421 } | 1437 } |
OLD | NEW |