| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42110af commit 4f832b1
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,8 @@ | |||
| 8 | 8 | ||
| 9 | 9 | namespace node { | |
| 10 | 10 | namespace mem { | |
| 11 | + static constexpr size_t kReserveSizeAndAlign = | ||
| 12 | + std::max(sizeof(size_t), alignof(max_align_t)); | ||
| 11 | 13 | ||
| 12 | 14 | template <typename Class, typename AllocatorStruct> | |
| 13 | 15 | AllocatorStruct NgLibMemoryManager<Class, AllocatorStruct>::MakeAllocator() { | |
@@ -30,19 +32,18 @@ void* NgLibMemoryManager<Class, T>::ReallocImpl(void* ptr, | |||
| 30 | 32 | char* original_ptr = nullptr; | |
| 31 | 33 | ||
| 32 | 34 | // We prepend each allocated buffer with a size_t containing the full | |
| 33 | - // size of the allocation. | ||
| 34 | - if (size > 0) size += sizeof(size_t); | ||
| 35 | + // size of the allocation, while keeping the returned pointer aligned. | ||
| 36 | + if (size > 0) size += kReserveSizeAndAlign; | ||
| 35 | 37 | ||
| 36 | 38 | if (ptr != nullptr) { | |
| 37 | 39 | // We are free()ing or re-allocating. | |
| 38 | - original_ptr = static_cast<char*>(ptr) - sizeof(size_t); | ||
| 40 | + original_ptr = static_cast<char*>(ptr) - kReserveSizeAndAlign; | ||
| 39 | 41 | previous_size = *reinterpret_cast<size_t*>(original_ptr); | |
| 40 | 42 | // This means we called StopTracking() on this pointer before. | |
| 41 | 43 | if (previous_size == 0) { | |
| 42 | 44 | // Fall back to the standard Realloc() function. | |
| 43 | 45 | char* ret = UncheckedRealloc(original_ptr, size); | |
| 44 | - if (ret != nullptr) | ||
| 45 | - ret += sizeof(size_t); | ||
| 46 | + if (ret != nullptr) ret += kReserveSizeAndAlign; | ||
| 46 | 47 | return ret; | |
| 47 | 48 | } | |
| 48 | 49 | } | |
@@ -62,7 +63,7 @@ void* NgLibMemoryManager<Class, T>::ReallocImpl(void* ptr, | |||
| 62 | 63 | manager->env()->external_memory_accounter()->Update( | |
| 63 | 64 | manager->env()->isolate(), new_size); | |
| 64 | 65 | *reinterpret_cast<size_t*>(mem) = size; | |
| 65 | - mem += sizeof(size_t); | ||
| 66 | + mem += kReserveSizeAndAlign; | ||
| 66 | 67 | } else if (size == 0) { | |
| 67 | 68 | manager->DecreaseAllocatedSize(previous_size); | |
| 68 | 69 | manager->env()->external_memory_accounter()->Decrease( | |
@@ -95,8 +96,8 @@ void* NgLibMemoryManager<Class, T>::CallocImpl(size_t nmemb, | |||
| 95 | 96 | ||
| 96 | 97 | template <typename Class, typename T> | |
| 97 | 98 | void NgLibMemoryManager<Class, T>::StopTrackingMemory(void* ptr) { | |
| 98 | - size_t* original_ptr = reinterpret_cast<size_t*>( | ||
| 99 | - static_cast<char*>(ptr) - sizeof(size_t)); | ||
| 99 | + size_t* original_ptr = | ||
| 100 | + reinterpret_cast<size_t*>(static_cast<char*>(ptr) - kReserveSizeAndAlign); | ||
| 100 | 101 | Class* manager = static_cast<Class*>(this); | |
| 101 | 102 | manager->DecreaseAllocatedSize(*original_ptr); | |
| 102 | 103 | manager->env()->external_memory_accounter()->Decrease( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments