1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 // ---------------------------------------------------------------------------
8 // ---------------------------------------------------------------------------
14 #include "stdmacros.h"
16 // Try to minimize the performance impact of contracts on CHK build.
18 #pragma inline_depth (20)
21 const DWORD g_garbageFillBuffer[GARBAGE_FILL_BUFFER_ITEMS] =
23 GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
24 GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
25 GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
26 GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
29 //----------------------------------------------------------------------------
31 // Low level buffer reallocate routine
32 //----------------------------------------------------------------------------
33 void SBuffer::ReallocateBuffer(COUNT_T allocation, Preserve preserve)
37 PRECONDITION(CheckPointer(this));
38 PRECONDITION(CheckBufferClosed());
39 PRECONDITION(CheckAllocation(allocation));
40 PRECONDITION(allocation >= m_size);
41 POSTCONDITION(m_allocation == allocation);
42 if (allocation > 0) THROWS; else NOTHROW;
44 SUPPORTS_DAC_HOST_ONLY;
48 BYTE *newBuffer = NULL;
51 newBuffer = NewBuffer(allocation);
53 if (preserve == PRESERVE)
55 // Copy the relevant contents of the old buffer over
56 DebugMoveBuffer(newBuffer, m_buffer, m_size);
61 DeleteBuffer(m_buffer, m_allocation);
64 m_allocation = allocation;
76 void SBuffer::Replace(const Iterator &i, COUNT_T deleteSize, COUNT_T insertSize)
82 PRECONDITION(CheckPointer(this));
83 PRECONDITION(CheckIteratorRange(i, deleteSize));
84 SUPPORTS_DAC_HOST_ONLY;
88 COUNT_T startRange = (COUNT_T) (i.m_ptr - m_buffer);
89 // The PRECONDITION(CheckIterationRange(i, deleteSize)) should check this in
90 // contract-checking builds, but this ensures we don't integer overflow if someone
91 // passes in an egregious deleteSize by capping it to the remaining length in the
93 if ((COUNT_T)(m_buffer + m_size - i.m_ptr) < deleteSize)
95 _ASSERTE(!"Trying to replace more bytes than are remaining in the buffer.");
96 deleteSize = (COUNT_T)(m_buffer + m_size - i.m_ptr);
98 COUNT_T endRange = startRange + deleteSize;
101 SCOUNT_T delta = insertSize - deleteSize;
105 // Buffer is shrinking
107 DebugDestructBuffer(i.m_ptr, deleteSize);
109 DebugMoveBuffer(m_buffer + endRange + delta,
113 Resize(m_size+delta, PRESERVE);
115 i.Resync(this, m_buffer + startRange);
122 ResizePadded(m_size+delta);
124 i.Resync(this, m_buffer + startRange);
126 DebugDestructBuffer(i.m_ptr, deleteSize);
128 DebugMoveBuffer(m_buffer + endRange + delta,
135 // Buffer stays the same size. We need to DebugDestruct it first to keep
136 // the invariant that the new space is clean.
138 DebugDestructBuffer(i.m_ptr, insertSize);
141 DebugConstructBuffer(i.m_ptr, insertSize);