summaryrefslogtreecommitdiff
path: root/src/pal/src/objmgr/palobjbase.cpp
blob: 63e0f866d688d83b08fd03d634c203671df0ffac (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

/*++



Module Name:

    palobjbase.cpp

Abstract:
    PAL object base class



--*/

#include "palobjbase.hpp"
#include "pal/malloc.hpp"
#include "pal/dbgmsg.h"

SET_DEFAULT_DEBUG_CHANNEL(PAL);

using namespace CorUnix;

CObjectType* CObjectType::s_rgotIdMapping[ObjectTypeIdCount];

/*++
Function:
  CPalObjectBase::Initialize

  Performs possibly-failing initialization for a newly-constructed
  object

Parameters:
  pthr -- thread data for calling thread
  poa -- the object attributes (e.g., name) for the object
--*/

PAL_ERROR
CPalObjectBase::Initialize(
    CPalThread *pthr,
    CObjectAttributes *poa
    )
{
    PAL_ERROR palError = NO_ERROR;

    _ASSERTE(NULL != pthr);
    _ASSERTE(NULL != poa);

    ENTRY("CPalObjectBase::Initialize"
        "(this = %p, pthr = %p, poa = %p)\n",
        this,
        pthr,
        poa
        );

    if (0 != m_pot->GetImmutableDataSize())
    {
        m_pvImmutableData = InternalMalloc(m_pot->GetImmutableDataSize());
        if (NULL != m_pvImmutableData)
        {
            ZeroMemory(m_pvImmutableData, m_pot->GetImmutableDataSize());
        }
        else
        {
            ERROR("Unable to allocate immutable data\n");
            palError = ERROR_OUTOFMEMORY;
            goto IntializeExit;
        }
    }

    if (0 != m_pot->GetProcessLocalDataSize())
    {
        palError = m_sdlLocalData.Initialize();
        if (NO_ERROR != palError)
        {
            ERROR("Unable to initialize local data lock!\n");
            goto IntializeExit;
        }
        
        m_pvLocalData = InternalMalloc(m_pot->GetProcessLocalDataSize());
        if (NULL != m_pvLocalData)
        {
            ZeroMemory(m_pvLocalData, m_pot->GetProcessLocalDataSize());
        }
        else
        {
            ERROR("Unable to allocate local data\n");
            palError = ERROR_OUTOFMEMORY;
            goto IntializeExit;
        }
    }

    if (0 != poa->sObjectName.GetStringLength())
    {
        palError = m_oa.sObjectName.CopyString(&poa->sObjectName);
    }

IntializeExit:

    LOGEXIT("CPalObjectBase::Initialize returns %d\n", palError);

    return palError;
}

/*++
Function:
  CPalObjectBase::GetObjectType

  Returns the type of the object
--*/

CObjectType *
CPalObjectBase::GetObjectType(
    VOID
    )
{
    ENTRY("CPalObjectBase::GetObjectType(this = %p)\n", this);
    LOGEXIT("CPalObjectBase::GetObjectType returns %p\n", m_pot);
    
    return m_pot;
}

/*++
Function:
  CPalObjectBase::GetObjectAttributes

  Returns the attributes of the object
--*/

CObjectAttributes *
CPalObjectBase::GetObjectAttributes(
    VOID
    )
{
    ENTRY("CPalObjectBase::GetObjectAttributes(this = %p)\n", this);
    LOGEXIT("CPalObjectBase::GetObjectAttributes returns %p\n", &m_oa);
    
    return &m_oa;
}

/*++
Function:
  CPalObjectBase::GetImmutableData

  Provides the caller access to the object's immutable data (if any)

Parameters:
  ppvImmutableData -- on success, receives a pointer to the object's
    immutable data
--*/

PAL_ERROR
CPalObjectBase::GetImmutableData(
    void **ppvImmutableData             // OUT
    )
{
    _ASSERTE(NULL != ppvImmutableData);
    
    ENTRY("CPalObjectBase::GetImmutableData"
        "(this = %p, ppvImmutableData = %p)\n",
        this,
        ppvImmutableData
        );
    
    _ASSERTE(0 < m_pot->GetImmutableDataSize());

    *ppvImmutableData = m_pvImmutableData;

    LOGEXIT("CPalObjectBase::GetImmutableData returns %d\n", NO_ERROR);

    return NO_ERROR;
}

/*++
Function:
  CPalObjectBase::GetProcessLocalData

  Provides the caller access to the object's local data (if any)

Parameters:
  pthr -- thread data for calling thread
  eLockRequest -- specifies if the caller desires a read lock or a
    write lock on the data (currently ignored)
  ppDataLock -- on success, receives a pointer to the data lock instance
    for the local data
  ppvProcssLocalData -- on success, receives a pointer to the local data
--*/

PAL_ERROR
CPalObjectBase::GetProcessLocalData(
    CPalThread *pthr,
    LockType eLockRequest,
    IDataLock **ppDataLock,             // OUT
    void **ppvProcessLocalData          // OUT
    )
{
    _ASSERTE(NULL != pthr);
    _ASSERTE(ReadLock == eLockRequest || WriteLock == eLockRequest);
    _ASSERTE(NULL != ppDataLock);
    _ASSERTE(NULL != ppvProcessLocalData);
    
    ENTRY("CPalObjectBase::GetProcessLocalData"
        "(this = %p, pthr = %p, eLockRequest = %d, ppDataLock = %p,"
        " ppvProcessLocalData = %p)\n",
        this,
        pthr,
        eLockRequest,
        ppDataLock,
        ppvProcessLocalData
        );
    
    _ASSERTE(0 < m_pot->GetProcessLocalDataSize());

    m_sdlLocalData.AcquireLock(pthr, ppDataLock);
    *ppvProcessLocalData = m_pvLocalData;

    LOGEXIT("CPalObjectBase::GetProcessLocalData returns %d\n", NO_ERROR);
    
    return NO_ERROR;
}

/*++
Function:
  CPalObjectBase::AddReference

  Increments the object's reference count. The updated count is returned
  for diagnostic purposes only
--*/

DWORD
CPalObjectBase::AddReference(
    void
    )
{
    LONG lRefCount;

    ENTRY("CPalObjectBase::AddReference(this = %p)\n", this);

    _ASSERTE(m_lRefCount > 0);
    lRefCount = InterlockedIncrement(&m_lRefCount);

    LOGEXIT("CPalObjectBase::AddReference returns %d\n", lRefCount);

    return lRefCount;
}

/*++
Function:
  CPalObjectBase::ReleaseReference

  Decrements the object's reference count. The updated count is returned
  for diagnostic purposes only

Parameters:
  pthr -- thread data for calling thread
--*/

DWORD
CPalObjectBase::ReleaseReference(
    CPalThread *pthr
    )
{
    LONG lRefCount;

    _ASSERTE(NULL != pthr);

    ENTRY("CPalObjectBase::ReleaseReference"
        "(this = %p, pthr = %p)\n",
        this,
        pthr
        );

    AcquireObjectDestructionLock(pthr);

    _ASSERTE(m_lRefCount > 0); 

    //
    // Even though object destruction takes place under a lock
    // we still need to use an interlocked decrement, as AddRef
    // operates lock free
    //

    lRefCount = InterlockedDecrement(&m_lRefCount);

    if (0 == lRefCount)
    {
        bool fCleanupSharedState = ReleaseObjectDestructionLock(pthr, TRUE);

        //
        // We need to do two things with the calling thread data here:
        // 1) store it in m_pthrCleanup so it is available to the destructors
        // 2) Add a reference to it before starting any cleanup, and release
        //    that reference afterwords.
        //
        // Step 2 is necessary when we're cleaning up the thread object that
        // represents the calling thread -- it ensures that the thread data
        // is available throughout the entire cleanup process.
        //

        m_pthrCleanup = pthr;
        pthr->AddThreadReference();

        if (NULL != m_pot->GetObjectCleanupRoutine())
        {
            (*m_pot->GetObjectCleanupRoutine())(
                pthr,
                static_cast<IPalObject*>(this),
                FALSE,
                fCleanupSharedState
                );
        }

        InternalDelete(this);

        pthr->ReleaseThreadReference();
    }
    else
    {       
        ReleaseObjectDestructionLock(pthr, FALSE);
    }

    LOGEXIT("CPalObjectBase::ReleaseReference returns %d\n", lRefCount);

    return lRefCount;
}

/*++
Function:
  CPalObjectBase::~CPalObjectBase

  Object destructor
--*/

CPalObjectBase::~CPalObjectBase()
{
    ENTRY("CPalObjectBase::~CPalObjectBase(this = %p)\n", this);

    // There is no need to call InternalGetCurrentThread here because
    // ReleaseReference already stores the thread object that
    // deletes this object in m_pthrCleanup to make sure the
    // thread object is alive throughout the object cleanup process.

    if (NULL != m_pvImmutableData)
    {
        InternalFree(m_pvImmutableData);
    }

    if (NULL != m_pvLocalData)
    {
        InternalFree(m_pvLocalData);
    }

    if (NULL != m_oa.sObjectName.GetString())
    {
        m_oa.sObjectName.FreeBuffer(m_pthrCleanup);
    }

    LOGEXIT("CPalObjectBase::~CPalObjectBase\n");
}