forked from OSchip/llvm-project
[sanitizer,NFC] InternalAlloc cleanup
This commit is contained in:
parent
e63b488f27
commit
952fdb6b43
|
@ -137,8 +137,14 @@ static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
||||||
|
|
||||||
#endif // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
#endif // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
||||||
|
|
||||||
|
namespace {
|
||||||
const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
|
const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
|
||||||
|
|
||||||
|
struct BlockHeader {
|
||||||
|
u64 magic;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
|
static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
|
||||||
SetAllocatorOutOfMemory();
|
SetAllocatorOutOfMemory();
|
||||||
Report("FATAL: %s: internal allocator is out of memory trying to allocate "
|
Report("FATAL: %s: internal allocator is out of memory trying to allocate "
|
||||||
|
@ -147,27 +153,28 @@ static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
|
void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
|
||||||
if (size + sizeof(u64) < size)
|
uptr s = size + sizeof(BlockHeader);
|
||||||
|
if (s < size)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
void *p = RawInternalAlloc(size + sizeof(u64), cache, alignment);
|
BlockHeader *p = (BlockHeader *)RawInternalAlloc(s, cache, alignment);
|
||||||
if (UNLIKELY(!p))
|
if (UNLIKELY(!p))
|
||||||
ReportInternalAllocatorOutOfMemory(size + sizeof(u64));
|
ReportInternalAllocatorOutOfMemory(s);
|
||||||
((u64*)p)[0] = kBlockMagic;
|
p->magic = kBlockMagic;
|
||||||
return (char*)p + sizeof(u64);
|
return p + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
|
void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
|
||||||
if (!addr)
|
if (!addr)
|
||||||
return InternalAlloc(size, cache);
|
return InternalAlloc(size, cache);
|
||||||
if (size + sizeof(u64) < size)
|
uptr s = size + sizeof(BlockHeader);
|
||||||
|
if (s < size)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
addr = (char*)addr - sizeof(u64);
|
BlockHeader *p = (BlockHeader *)addr - 1;
|
||||||
size = size + sizeof(u64);
|
CHECK_EQ(kBlockMagic, p->magic);
|
||||||
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
|
p = (BlockHeader *)RawInternalRealloc(p, s, cache);
|
||||||
void *p = RawInternalRealloc(addr, size, cache);
|
|
||||||
if (UNLIKELY(!p))
|
if (UNLIKELY(!p))
|
||||||
ReportInternalAllocatorOutOfMemory(size);
|
ReportInternalAllocatorOutOfMemory(s);
|
||||||
return (char*)p + sizeof(u64);
|
return p + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *InternalReallocArray(void *addr, uptr count, uptr size,
|
void *InternalReallocArray(void *addr, uptr count, uptr size,
|
||||||
|
@ -198,10 +205,10 @@ void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
|
||||||
void InternalFree(void *addr, InternalAllocatorCache *cache) {
|
void InternalFree(void *addr, InternalAllocatorCache *cache) {
|
||||||
if (!addr)
|
if (!addr)
|
||||||
return;
|
return;
|
||||||
addr = (char*)addr - sizeof(u64);
|
BlockHeader *p = (BlockHeader *)addr - 1;
|
||||||
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
|
CHECK_EQ(kBlockMagic, p->magic);
|
||||||
((u64*)addr)[0] = 0;
|
p->magic = 0;
|
||||||
RawInternalFree(addr, cache);
|
RawInternalFree(p, cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
// LowLevelAllocator
|
// LowLevelAllocator
|
||||||
|
|
Loading…
Reference in New Issue