summaryrefslogtreecommitdiff
path: root/src/utilcode/sbuffer.cpp
blob: b97e12614f3c14b9a3b5c922c4df389140be2596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ---------------------------------------------------------------------------

// 
// Buffer.cpp
// ---------------------------------------------------------------------------

#include "stdafx.h"
#include "sbuffer.h"
#include "ex.h"
#include "holder.h"
#include "stdmacros.h"

// Try to minimize the performance impact of contracts on CHK build.
#if defined(_MSC_VER)
#pragma inline_depth (20)
#endif

const DWORD g_garbageFillBuffer[GARBAGE_FILL_BUFFER_ITEMS] = 
{
        GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
        GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
        GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
        GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD, GARBAGE_FILL_DWORD,
};

//----------------------------------------------------------------------------
// ReallocateBuffer
// Low level buffer reallocate routine
//----------------------------------------------------------------------------
void SBuffer::ReallocateBuffer(COUNT_T allocation, Preserve preserve)
{
    CONTRACT_VOID
    {
        PRECONDITION(CheckPointer(this));
        PRECONDITION(CheckBufferClosed());
        PRECONDITION(CheckAllocation(allocation));
        PRECONDITION(allocation >= m_size);
        POSTCONDITION(m_allocation == allocation);
        if (allocation > 0) THROWS; else NOTHROW;
        GC_NOTRIGGER;
        SUPPORTS_DAC_HOST_ONLY;
    } 
    CONTRACT_END;

    BYTE *newBuffer = NULL;
    if (allocation > 0)
    {
        newBuffer = NewBuffer(allocation);

        if (preserve == PRESERVE)
        {
            // Copy the relevant contents of the old buffer over
            DebugMoveBuffer(newBuffer, m_buffer, m_size);
        }
    }

    if (IsAllocated())
        DeleteBuffer(m_buffer, m_allocation);

    m_buffer = newBuffer;
    m_allocation = allocation;

    if (allocation > 0)
        SetAllocated();
    else
        ClearAllocated();

    ClearImmutable();

    RETURN;
}

void SBuffer::Replace(const Iterator &i, COUNT_T deleteSize, COUNT_T insertSize)
{
    CONTRACT_VOID 
    {
        THROWS;
        GC_NOTRIGGER;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(CheckIteratorRange(i, deleteSize));
        SUPPORTS_DAC_HOST_ONLY;
    } 
    CONTRACT_END;

    COUNT_T startRange = (COUNT_T) (i.m_ptr - m_buffer);
    // The PRECONDITION(CheckIterationRange(i, deleteSize)) should check this in
    // contract-checking builds, but this ensures we don't integer overflow if someone
    // passes in an aggregious deleteSize by capping it to the remaining length in the
    // buffer.
    if ((COUNT_T)(m_buffer + m_size - i.m_ptr) < deleteSize)
    {
        _ASSERTE(!"Trying to replace more bytes than are remaining in the buffer.");
        deleteSize = (COUNT_T)(m_buffer + m_size - i.m_ptr);
    }
    COUNT_T endRange = startRange + deleteSize;
    COUNT_T end = m_size;

    SCOUNT_T delta = insertSize - deleteSize;

    if (delta < 0)
    {
        // Buffer is shrinking

        DebugDestructBuffer(i.m_ptr, deleteSize);

        DebugMoveBuffer(m_buffer + endRange + delta,
                        m_buffer + endRange, 
                        end - endRange);

        Resize(m_size+delta, PRESERVE);

        i.Resync(this, m_buffer + startRange);

    }
    else if (delta > 0)
    {
        // Buffer is growing

        ResizePadded(m_size+delta);

        i.Resync(this, m_buffer + startRange);

        DebugDestructBuffer(i.m_ptr, deleteSize);

        DebugMoveBuffer(m_buffer + endRange + delta, 
                        m_buffer + endRange, 
                        end - endRange);

    }
    else
    {
        // Buffer stays the same size.  We need to DebugDestruct it first to keep
        // the invariant that the new space is clean.

        DebugDestructBuffer(i.m_ptr, insertSize);
    }

    DebugConstructBuffer(i.m_ptr, insertSize);

    RETURN;
}