summaryrefslogtreecommitdiff
path: root/src/vm/objectlist.cpp
blob: e3500dee32a938842677fceb6eb45a9b13137e6f (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
// 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 "common.h"
#include "objectlist.h"

#ifndef DACCESS_COMPILE

ObjectList::ObjectList( void )
: freeIndexHead_( INVALID_COMPRESSEDSTACK_INDEX ),
  listLock_( CrstObjectList, CrstFlags(CRST_UNSAFE_SAMELEVEL | CRST_UNSAFE_ANYMODE) )
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    } CONTRACTL_END;
}

#endif

#define MAX_LOOP 2

DWORD
ObjectList::AddToList( PVOID ptr )
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(ptr));
    } CONTRACTL_END;
    

	//  sanity check that the pointer low bit is not set
	 _ASSERTE(  (((DWORD)(size_t)ptr & 0x1) == 0) && "Invalid pointer" );
	
    DWORD retval = INVALID_COMPRESSEDSTACK_INDEX;

    CrstHolder ch( &listLock_ );

    // If there is an entry in the free list, simply use it.

    if (this->freeIndexHead_ != INVALID_COMPRESSEDSTACK_INDEX)
    {
        _ASSERTE( this->listLock_.OwnedByCurrentThread() );

	// grab the head of the list
	retval = (this->freeIndexHead_ >> 1);
	
	DWORD nextFreeIndex = (DWORD)(size_t)this->allEntries_.Get( retval );
	
	// index in use,  pointer values have low bit as 0
	_ASSERTE(  ((nextFreeIndex & 0x01) == 1) && "The free list points to an index that is in use" );
	 // update the head of the list with the next free index stored in the array list
	this->freeIndexHead_ = nextFreeIndex;		 
	 
	// store the pointer
	this->allEntries_.Set( retval, ptr);       
    }
    // Otherwise we place this new entry at that end of the list.
    else
    {
        _ASSERTE( this->listLock_.OwnedByCurrentThread() );
        retval = this->allEntries_.GetCount();
        IfFailThrow(this->allEntries_.Append(ptr));
    }

    _ASSERTE( retval != INVALID_COMPRESSEDSTACK_INDEX );

    return retval;
}

void
ObjectList::RemoveFromList( PVOID ptr )
{
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(ptr));
    } CONTRACTL_END;
    
	//  sanity check that the pointer low bit is not set
	 _ASSERTE(  (((DWORD)(size_t)ptr & 0x1) == 0) && "Invalid pointer" );
	
    DWORD index = INVALID_COMPRESSEDSTACK_INDEX;

    CrstHolder ch( &listLock_ );

    ObjectList::Iterator iter = Iterate();

    while (iter.Next())
    {
        if (iter.GetElement() == ptr)
        {
            index = iter.GetIndex();
            break;
        }
    }

    if (index == INVALID_COMPRESSEDSTACK_INDEX)
    {
        _ASSERTE( FALSE && "Unable to find object" );
    }
    else
    {
	// add the index to the free list ( shift the freeIndex left and set the low bit)
	this->allEntries_.Set( index, (PVOID)(size_t)(this->freeIndexHead_));
	this-> freeIndexHead_ = ((index<<1) | 0x1);
    }
}



void
ObjectList::RemoveFromList( DWORD index, PVOID ptr )
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(ptr));
    } CONTRACTL_END;

    CrstHolder ch( &listLock_ );

	//  sanity check that the pointer low bit is not set
	 _ASSERTE(  (((DWORD)(size_t)ptr & 0x1) == 0) && "Invalid pointer" );
	
    _ASSERTE( index < this->allEntries_.GetCount() );
    _ASSERTE( this->allEntries_.Get( index ) == ptr && "Index tracking failed for this object" );

     // add the index to the free list ( shift the freeIndex left and set the low bit)
	this->allEntries_.Set( index, (PVOID)(size_t)(this->freeIndexHead_));
	this-> freeIndexHead_ = ((index<<1) | 0x1);
    
}

PVOID
ObjectList::Get( DWORD index )
{
    LIMITED_METHOD_CONTRACT;
    return this->allEntries_.Get( index );
}


UnsynchronizedBlockAllocator::UnsynchronizedBlockAllocator( size_t blockSize )
: blockSize_( blockSize ),
  offset_( blockSize ),
  index_( INVALID_COMPRESSEDSTACK_INDEX )
{
    LIMITED_METHOD_CONTRACT;
    // We start off the offset at the block size to force the first
    // allocation to create a new (first) block
}

UnsynchronizedBlockAllocator::~UnsynchronizedBlockAllocator( void )
{
    LIMITED_METHOD_CONTRACT;
    ArrayList::Iterator iter = this->blockList_.Iterate();

    while (iter.Next())
    {
        delete [] (BYTE *) iter.GetElement();
    }
}


PVOID
UnsynchronizedBlockAllocator::Allocate( size_t size )
{
    CONTRACTL {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    } CONTRACTL_END;

    _ASSERTE( size <= this->blockSize_ );

    NewHolder<BYTE> buffer;

    S_SIZE_T sizecheck = S_SIZE_T(this->offset_) + S_SIZE_T(size) ;
    if( sizecheck.IsOverflow() )
    {
        ThrowOutOfMemory();
    }

    if (sizecheck.Value() > this->blockSize_)
    {
        buffer.Assign( new BYTE[this->blockSize_] );
        IfFailThrow(this->blockList_.Append( buffer ));
        buffer.SuppressRelease();
        ++this->index_;
        this->offset_ = 0;
    }
    else
    {
        buffer.Assign( (BYTE*)this->blockList_.Get( index_ ) );
        buffer.SuppressRelease();
    }

    void* retval = buffer.GetValue() + this->offset_;
    this->offset_ += size;

    return retval;
}