summaryrefslogtreecommitdiff
path: root/src/utilcode/memorypool.cpp
blob: 1db8291e6a17bc8f27db9c93ea6ccfd3b586f0a5 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// 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.

#include "stdafx.h"

#include "memorypool.h"
#include "ex.h"

size_t MemoryPool::GetSize()
{
    LIMITED_METHOD_CONTRACT;
    size_t retval=0;
    
    Block *block = m_blocks;
    while (block != NULL)
    {
        retval+=(BYTE*)block->elementsEnd-(BYTE*)block->elements;
        block = block->next;
    }
    return retval;  
}

#ifndef DACCESS_COMPILE

BOOL MemoryPool::AddBlock(SIZE_T elementCount)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        INJECT_FAULT(return FALSE;);
    } CONTRACTL_END;

    //
    // Check for arithmetic overflow
    //
    S_SIZE_T cbBlockSize = S_SIZE_T(elementCount) * S_SIZE_T(m_elementSize);
    S_SIZE_T cbAllocSize = S_SIZE_T(sizeof(Block)) + cbBlockSize;
    if (cbBlockSize.IsOverflow() || cbAllocSize.IsOverflow())
        return FALSE;

    //
    // Allocate the new block.
    //
    
    Block *block = (Block *) new (nothrow) BYTE [cbAllocSize.Value()];

    if (block == NULL)
        return FALSE;
    
    //
    // Chain all elements together for the free list
    //
    
    _ASSERTE(m_freeList == NULL);
    Element **prev = &m_freeList;
    
    Element *e = block->elements;
    Element *eEnd = (Element *) ((BYTE*) block->elements + elementCount*m_elementSize);
    while (e < eEnd)
    {
        *prev = e;
        prev = &e->next;
#if _DEBUG
        DeadBeef(e);
#endif
        e = (Element*) ((BYTE*)e + m_elementSize);
    }

    *prev = NULL;
    
    //
    // Initialize the other block fields & link the block into the block list
    //
    
    block->elementsEnd = e;
    block->next = m_blocks;
    m_blocks = block;

    return TRUE;
}

void MemoryPool::DeadBeef(Element *element)
{
#if _DEBUG
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        CANNOT_TAKE_LOCK;
    } CONTRACTL_END;

    int *i = &element->deadBeef;
    int *iEnd = (int*) ((BYTE*)element + m_elementSize);
    while (i < iEnd)
        *i++ = (int) 0xdeadbeef;
#else
    LIMITED_METHOD_CONTRACT;
#endif
}

MemoryPool::MemoryPool(SIZE_T elementSize, SIZE_T initGrowth, SIZE_T initCount)
    : m_elementSize(elementSize),
      m_growCount(initGrowth),
      m_blocks(NULL),
      m_freeList(NULL)
{
    CONTRACTL {
        if (initCount) THROWS; else NOTHROW;
        GC_NOTRIGGER;
    } CONTRACTL_END;

    _ASSERTE(elementSize >= sizeof(Element));
    _ASSERTE((elementSize & ((sizeof(PVOID)-1))) == 0);
    
    if (initCount > 0)
        AddBlock(initCount);
}

MemoryPool::~MemoryPool()
{
    LIMITED_METHOD_CONTRACT;
    Block *block = m_blocks;
    while (block != NULL)
    {
        Block *next = block->next;
        delete [] (BYTE*) block;
        block = next;
    }
}

BOOL MemoryPool::IsElement(void *element)
{
    LIMITED_METHOD_CONTRACT;

    Block *block = m_blocks;
    while (block != NULL)
    {
        if (element >= block->elements    && 
            element <  block->elementsEnd  )
        {
            return ((BYTE *)element - (BYTE*)block->elements) % m_elementSize == 0;
        }
        block = block->next;
    }
    
    return FALSE;
}

BOOL MemoryPool::IsAllocatedElement(void *element)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        CANNOT_TAKE_LOCK;
    } CONTRACTL_END;

    if (!IsElement(element))
        return FALSE;

    //
    // Now, make sure the element isn't
    // in the free list.
    //

#if _DEBUG
    //
    // In a debug build, all objects on the free list
    // will be marked with deadbeef.  This means that 
    // if the object is not deadbeef, it's not on the
    // free list.
    //
    // This check will give us decent performance in
    // a debug build for FreeElement, since we 
    // always expect to return TRUE in that case.
    //
    
    if (((Element*)element)->deadBeef != (int) 0xdeadBeef)
        return TRUE;
#endif

    Element *f = m_freeList;
    while (f != NULL)
    {
        if (f == element)
            return FALSE;
        f = f->next;
    }
    
#if _DEBUG
    //
    // We should never get here in a debug build, because
    // all free elements should be deadbeefed.
    //
    _ASSERTE(!"Unreachable");
#endif

    return TRUE;
}

void *MemoryPool::AllocateElement()
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
    } CONTRACTL_END;

    void *element = AllocateElementNoThrow();
    if (element == NULL)
        ThrowOutOfMemory();

    return element;
}

void *MemoryPool::AllocateElementNoThrow()
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        INJECT_FAULT( return FALSE; );
    } CONTRACTL_END;

    void *element = m_freeList;

    if (element == NULL)
    {
        if (!AddBlock(m_growCount))
            return NULL;

        m_growCount *= 2;
        element = m_freeList;
    }
    
    // if we come there means that addblock succeeded and m_freelist isn't null anymore
    PREFIX_ASSUME(m_freeList!= NULL);
    m_freeList = m_freeList->next;
    
    return element;
}

void MemoryPool::FreeElement(void *element)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        CANNOT_TAKE_LOCK;
    } CONTRACTL_END;

#if _DEBUG // don't want to do this assert in a non-debug build; it is expensive
    _ASSERTE(IsAllocatedElement(element));
#endif

    Element *e = (Element *) element;

#if _DEBUG
    DeadBeef(e);
#endif

    e->next = m_freeList;
    m_freeList = e;
}

void MemoryPool::FreeAllElements()
{
    LIMITED_METHOD_CONTRACT;
    
    Block *block = m_blocks;
    while (block != NULL)
    {
        Block *next = block->next;
        delete [] block;
        block = next;
    }
    
    m_freeList = NULL;
    m_blocks = NULL;
}



MemoryPool::Iterator::Iterator(MemoryPool *pool)
{
    LIMITED_METHOD_CONTRACT;

    //
    // Warning!  This only works if you haven't freed
    // any elements.
    //

    m_next = pool->m_blocks;
    m_e = NULL;
    m_eEnd = NULL;
    m_end = (BYTE*) pool->m_freeList;
    m_size = pool->m_elementSize;
}

BOOL MemoryPool::Iterator::Next()
{
    LIMITED_METHOD_CONTRACT;

    if (m_e == m_eEnd
        || (m_e == m_end && m_end != NULL))
    {
        if (m_next == NULL)
            return FALSE;
        m_e = (BYTE*) m_next->elements;
        m_eEnd = (BYTE*) m_next->elementsEnd;
        m_next = m_next->next;
        if (m_e == m_end)
            return FALSE;
    }
    
    m_e += m_size;
    
    return TRUE;
}

#endif