Browse Source

Added an argument for zeroing memory in new heap allocations.

David Piuva 11 months ago
parent
commit
23a4ec0820
3 changed files with 8 additions and 4 deletions
  1. 1 1
      Source/DFPSR/api/bufferAPI.h
  2. 4 1
      Source/DFPSR/base/heap.cpp
  3. 3 2
      Source/DFPSR/base/heap.h

+ 1 - 1
Source/DFPSR/api/bufferAPI.h

@@ -58,7 +58,7 @@ namespace dsr {
 	using Buffer = std::shared_ptr<BufferImpl>;
 	using Buffer = std::shared_ptr<BufferImpl>;
 
 
 	// Side-effect: Creates a new buffer head regardless of newSize, but only allocates a zeroed data allocation if newSize > 0.
 	// Side-effect: Creates a new buffer head regardless of newSize, but only allocates a zeroed data allocation if newSize > 0.
-	// Post-condition: Returns a handle to the new buffer.
+	// Post-condition: Returns a handle to the new buffer, which is initialized to zeroes.
 	// Creating a buffer without a size will only allocate the buffer's head referring to null data with size zero.
 	// Creating a buffer without a size will only allocate the buffer's head referring to null data with size zero.
 	Buffer buffer_create(int64_t newSize);
 	Buffer buffer_create(int64_t newSize);
 	// The buffer always allocate with DSR_MAXIMUM_ALIGNMENT, but you can check that your requested alignment is not too much.
 	// The buffer always allocate with DSR_MAXIMUM_ALIGNMENT, but you can check that your requested alignment is not too much.

+ 4 - 1
Source/DFPSR/base/heap.cpp

@@ -169,7 +169,7 @@ namespace dsr {
 
 
 	static HeapPool defaultHeap;
 	static HeapPool defaultHeap;
 
 
-	UnsafeAllocation heap_allocate(uintptr_t minimumSize) {
+	UnsafeAllocation heap_allocate(uintptr_t minimumSize, bool zeroed) {
 		int32_t binIndex = getBinIndex(minimumSize);
 		int32_t binIndex = getBinIndex(minimumSize);
 		UnsafeAllocation result(nullptr, nullptr);
 		UnsafeAllocation result(nullptr, nullptr);
 		if (binIndex == -1) {
 		if (binIndex == -1) {
@@ -196,6 +196,9 @@ namespace dsr {
 				}
 				}
 			}
 			}
 			defaultHeap.poolLock.unlock();
 			defaultHeap.poolLock.unlock();
+			if (zeroed && result.data != nullptr) {
+				memset(result.data, 0, paddedSize);
+			}
 		}
 		}
 		return result;
 		return result;
 	}
 	}

+ 3 - 2
Source/DFPSR/base/heap.h

@@ -28,9 +28,10 @@
 
 
 namespace dsr {
 namespace dsr {
 	// Allocate memory in the heap.
 	// Allocate memory in the heap.
-	//   The size argument is the minimum number of bytes to allocate, but the result may give you more than you asked for.
+	//   The minimumSize argument is the minimum number of bytes to allocate, but the result may give you more than you asked for.
+	//   When zeroed is true, the new memory will be zeroed. Otherwise it may contain uninitialized data.
 	// Post-condition: Returns pointers to the payload and header.
 	// Post-condition: Returns pointers to the payload and header.
-	UnsafeAllocation heap_allocate(uint64_t minimumSize);
+	UnsafeAllocation heap_allocate(uint64_t minimumSize, bool zeroed = true);
 
 
 	// Pre-condition: The allocation pointer must point to the start of a payload allocated using heap_allocate, no offsets nor other allocators allowed.
 	// Pre-condition: The allocation pointer must point to the start of a payload allocated using heap_allocate, no offsets nor other allocators allowed.
 	// Post-condition: Returns the number of available bytes in the allocation.
 	// Post-condition: Returns the number of available bytes in the allocation.