forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshmobject.cpp
More file actions
1472 lines (1153 loc) · 35 KB
/
shmobject.cpp
File metadata and controls
1472 lines (1153 loc) · 35 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
shmobject.hpp
Abstract:
Shared memory based object
--*/
#include "shmobject.hpp"
#include "pal/malloc.hpp"
#include "pal/cs.hpp"
#include "pal/dbgmsg.h"
#include <stddef.h>
SET_DEFAULT_DEBUG_CHANNEL(PAL);
using namespace CorUnix;
/*++
Function:
CSharedMemoryObject::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
CSharedMemoryObject::Initialize(
CPalThread *pthr,
CObjectAttributes *poa
)
{
PAL_ERROR palError = NO_ERROR;
SHMObjData *psmod = NULL;
_ASSERTE(NULL != pthr);
_ASSERTE(NULL != poa);
ENTRY("CSharedMemoryObject::Initialize"
"(this = %p, pthr = %p, poa = %p)\n",
this,
pthr,
poa
);
palError = CPalObjectBase::Initialize(pthr, poa);
if (NO_ERROR != palError)
{
goto InitializeExit;
}
//
// If this is a named object it needs to go into the shared domain;
// otherwise it remains local
//
if (0 != m_oa.sObjectName.GetStringLength())
{
m_ObjectDomain = SharedObject;
palError = AllocateSharedDataItems(&m_shmod, &psmod);
if (NO_ERROR != palError || NULL == psmod)
{
goto InitializeExit;
}
}
if (0 != m_pot->GetSharedDataSize())
{
if (SharedObject == m_ObjectDomain)
{
//
// Map the shared data into our address space
//
if (NULL == psmod)
{
ASSERT("psmod should not be NULL");
palError = ERROR_INTERNAL_ERROR;
goto InitializeExit;
}
m_pvSharedData = SHMPTR_TO_TYPED_PTR(VOID, psmod->shmObjSharedData);
if (NULL == m_pvSharedData)
{
ASSERT("Unable to map shared data area\n");
palError = ERROR_INTERNAL_ERROR;
goto InitializeExit;
}
}
else
{
//
// Initialize the local shared data lock.
//
palError = m_sdlSharedData.Initialize();
if (NO_ERROR != palError)
{
ERROR("Failure initializing m_sdlSharedData\n");
goto InitializeExit;
}
//
// Allocate local memory to hold the shared data
//
m_pvSharedData = InternalMalloc(m_pot->GetSharedDataSize());
if (NULL == m_pvSharedData)
{
ERROR("Failure allocating m_pvSharedData (local copy)\n");
palError = ERROR_OUTOFMEMORY;
goto InitializeExit;
}
}
ZeroMemory(m_pvSharedData, m_pot->GetSharedDataSize());
}
InitializeExit:
LOGEXIT("CSharedMemoryObject::Initialize returns %d\n", palError);
return palError;
}
/*++
Function:
CSharedMemoryObject::InitializeFromExistingSharedData
Performs possibly-failing initialization for a newly-constructed
object that is to represent an existing object (i.e., importing
a shared object into this process)
The shared memory lock must be held when calling this method
Parameters:
pthr -- thread data for calling thread
poa -- the object attributes for the object
--*/
PAL_ERROR
CSharedMemoryObject::InitializeFromExistingSharedData(
CPalThread *pthr,
CObjectAttributes *poa
)
{
PAL_ERROR palError = NO_ERROR;
SHMObjData *psmod = NULL;
_ASSERTE(NULL != pthr);
_ASSERTE(NULL != poa);
ENTRY("CSharedMemoryObject::InitializeFromExistingSharedData"
"(this = %p, pthr = %p, poa = %p)\n",
this,
pthr,
poa
);
//
// This object is obviously shared...
//
m_ObjectDomain = SharedObject;
_ASSERTE(SHMNULL != m_shmod);
psmod = SHMPTR_TO_TYPED_PTR(SHMObjData, m_shmod);
if (NULL == psmod)
{
ASSERT("Unable to map shared object data\n");
palError = ERROR_INTERNAL_ERROR;
goto InitializeFromExistingSharedDataExit;
}
//
// When we're being called on the duplicate handle path the passed
// in object attributes likely won't have an object name in it.
// If there is an object name in the shared data place that in the
// object attributes so that the constructed object has a local copy
// of the name
//
if (0 == poa->sObjectName.GetStringLength()
&& 0 != psmod->dwNameLength)
{
WCHAR *wsz;
wsz = SHMPTR_TO_TYPED_PTR(WCHAR, psmod->shmObjName);
if (NULL != wsz)
{
poa->sObjectName.SetStringWithLength(wsz, psmod->dwNameLength);
}
else
{
ASSERT("Unable to map object name\n");
palError = ERROR_INTERNAL_ERROR;
goto InitializeFromExistingSharedDataExit;
}
}
#if _DEBUG
else if (0 != psmod->dwNameLength)
{
WCHAR *wsz;
//
// Verify that the names are consistent
//
wsz = SHMPTR_TO_TYPED_PTR(WCHAR, psmod->shmObjName);
_ASSERTE(NULL != wsz);
_ASSERTE(0 == PAL_wcscmp(wsz, poa->sObjectName.GetString()));
}
#endif // debug
palError = CPalObjectBase::Initialize(pthr, poa);
if (NO_ERROR != palError)
{
goto InitializeFromExistingSharedDataExit;
}
if (SHMNULL != psmod->shmObjImmutableData)
{
VOID *pv = SHMPTR_TO_TYPED_PTR(VOID, psmod->shmObjImmutableData);
if (NULL != pv)
{
memcpy(m_pvImmutableData, pv, m_pot->GetImmutableDataSize());
}
else
{
ASSERT("Unable to map object immutable data\n");
palError = ERROR_INTERNAL_ERROR;
goto InitializeFromExistingSharedDataExit;
}
}
if (SHMNULL != psmod->shmObjSharedData)
{
m_pvSharedData = SHMPTR_TO_TYPED_PTR(VOID, psmod->shmObjSharedData);
if (NULL == m_pvSharedData)
{
ASSERT("Unable to map object shared data\n");
palError = ERROR_INTERNAL_ERROR;
goto InitializeFromExistingSharedDataExit;
}
}
if (NULL != m_pot->GetObjectInitRoutine())
{
palError = (*m_pot->GetObjectInitRoutine())(
pthr,
m_pot,
m_pvImmutableData,
m_pvSharedData,
m_pvLocalData
);
}
InitializeFromExistingSharedDataExit:
LOGEXIT("CSharedMemoryObject::InitalizeFromExistingSharedData returns %d\n", palError);
return palError;
}
/*++
Function:
CSharedMemoryObject::AllocatedSharedDataItems
Allocates and initialiazes the shared memory structures necessary to make an
object available to other processes
Parameters:
pshmObjData -- on success, receives the shared memory pointer for the
shared memory object data
ppsmod -- on success, receives the locally-mapped pointer for the shared
memory object data
--*/
PAL_ERROR
CSharedMemoryObject::AllocateSharedDataItems(
SHMPTR *pshmObjData,
SHMObjData **ppsmod
)
{
PAL_ERROR palError = NO_ERROR;
SHMPTR shmod = SHMNULL;
SHMObjData *psmod = NULL;
_ASSERTE(NULL != pshmObjData);
_ASSERTE(NULL != ppsmod);
ENTRY("CSharedMemoryObject::AllocateSharedDataItems"
"(this = %p, pshmObjData = %p, ppsmod = %p)\n",
this,
pshmObjData,
ppsmod
);
//
// We're about to make a number of shared memory allocations,
// so grab the lock for the entirety of the routine.
//
SHMLock();
shmod = SHMalloc(sizeof(SHMObjData));
if (SHMNULL == shmod)
{
ERROR("Unable to allocate m_shmod for new object\n");
palError = ERROR_OUTOFMEMORY;
goto AllocateSharedDataItemsExit;
}
psmod = SHMPTR_TO_TYPED_PTR(SHMObjData, shmod);
_ASSERTE(NULL != psmod);
ZeroMemory(psmod, sizeof(*psmod));
psmod->eTypeId = m_pot->GetId();
psmod->lProcessRefCount = 1;
if (0 != m_oa.sObjectName.GetStringLength())
{
psmod->dwNameLength = m_oa.sObjectName.GetStringLength();
psmod->shmObjName = SHMWStrDup(m_oa.sObjectName.GetString());
if (SHMNULL == psmod->shmObjName)
{
ERROR("Unable to allocate psmod->shmObjName for new object\n");
palError = ERROR_OUTOFMEMORY;
goto AllocateSharedDataItemsExit;
}
}
if (0 != m_pot->GetImmutableDataSize())
{
//
// The shared copy of the object's immutable data will be initialized
// by CSharedMemoryObjectManager::RegisterObject or PromoteSharedData
//
psmod->shmObjImmutableData = SHMalloc(m_pot->GetImmutableDataSize());
if (SHMNULL == psmod->shmObjImmutableData)
{
ERROR("Unable to allocate psmod->shmObjImmutableData for new object\n");
palError = ERROR_OUTOFMEMORY;
goto AllocateSharedDataItemsExit;
}
}
if (0 != m_pot->GetSharedDataSize())
{
psmod->shmObjSharedData = SHMalloc(m_pot->GetSharedDataSize());
if (SHMNULL == psmod->shmObjSharedData)
{
ERROR("Unable to allocate psmod->shmObjSharedData for new object\n");
palError = ERROR_OUTOFMEMORY;
goto AllocateSharedDataItemsExit;
}
}
*pshmObjData = shmod;
*ppsmod = psmod;
AllocateSharedDataItemsExit:
if (NO_ERROR != palError && SHMNULL != shmod)
{
FreeSharedDataAreas(shmod);
}
SHMRelease();
LOGEXIT("CSharedMemoryObject::AllocateSharedDataItems returns %d\n", palError);
return palError;
}
/*++
Function:
CSharedMemoryObject::FreeSharedDataItems
Frees the shared memory structures referenced by the provided shared
memory pointer
Parameters:
shmObjData -- shared memory pointer to the structures to free
--*/
// static
void
CSharedMemoryObject::FreeSharedDataAreas(
SHMPTR shmObjData
)
{
SHMObjData *psmod;
_ASSERTE(SHMNULL != shmObjData);
ENTRY("CSharedMemoryObject::FreeSharedDataAreas"
"(shmObjData = %p)\n",
shmObjData
);
SHMLock();
psmod = SHMPTR_TO_TYPED_PTR(SHMObjData, shmObjData);
_ASSERTE(NULL != psmod);
if (SHMNULL != psmod->shmObjImmutableData)
{
SHMfree(psmod->shmObjImmutableData);
}
if (SHMNULL != psmod->shmObjSharedData)
{
SHMfree(psmod->shmObjSharedData);
}
if (SHMNULL != psmod->shmObjName)
{
SHMfree(psmod->shmObjName);
}
SHMfree(shmObjData);
SHMRelease();
LOGEXIT("CSharedMemoryObject::FreeSharedDataAreas\n");
}
/*++
Function:
CSharedMemoryObject::PromoteShjaredData
Copies the object's state into the passed-in shared data structures
Parameters:
shmObjData -- shared memory pointer for the shared memory object data
psmod -- locally-mapped pointer for the shared memory object data
--*/
void
CSharedMemoryObject::PromoteSharedData(
SHMPTR shmObjData,
SHMObjData *psmod
)
{
_ASSERTE(SHMNULL != shmObjData);
_ASSERTE(NULL != psmod);
ENTRY("CSharedMemoryObject::PromoteSharedData"
"(this = %p, shmObjData = %p, psmod = %p)\n",
this,
shmObjData,
psmod);
//
// psmod has been zero-inited, so we don't need to worry about
// shmPrevObj, shmNextObj, fAddedToList, shmObjName, dwNameLength,
// or pvSynchData
//
psmod->lProcessRefCount = 1;
psmod->eTypeId = m_pot->GetId();
if (0 != m_pot->GetImmutableDataSize())
{
void *pvImmutableData;
pvImmutableData = SHMPTR_TO_TYPED_PTR(void, psmod->shmObjImmutableData);
_ASSERTE(NULL != pvImmutableData);
CopyMemory(
pvImmutableData,
m_pvImmutableData,
m_pot->GetImmutableDataSize()
);
}
if (0 != m_pot->GetSharedDataSize())
{
void *pvSharedData;
pvSharedData = SHMPTR_TO_TYPED_PTR(void, psmod->shmObjSharedData);
_ASSERTE(NULL != pvSharedData);
CopyMemory(
pvSharedData,
m_pvSharedData,
m_pot->GetSharedDataSize()
);
InternalFree(m_pvSharedData);
m_pvSharedData = pvSharedData;
}
m_shmod = shmObjData;
LOGEXIT("CSharedMemoryObject::PromoteSharedData\n");
}
/*++
Function:
CSharedMemoryObject::EnsureObjectIsShared
If this object is not yet in the shared domain allocate the necessary
shared memory structures for it and copy the object's data into those
structures
Parameters:
pthr -- thread data for the calling thread
--*/
PAL_ERROR
CSharedMemoryObject::EnsureObjectIsShared(
CPalThread *pthr
)
{
PAL_ERROR palError = NO_ERROR;
IDataLock *pDataLock = NULL;
SHMPTR shmObjData;
SHMObjData *psmod;
_ASSERTE(NULL != pthr);
ENTRY("CSharedMemoryObject::EnsureObjectIsShared"
"(this = %p, pthr = %p)\n",
this,
pthr
);
//
// Grab the shared memory lock and check if the object is already
// shared
//
SHMLock();
if (SharedObject == m_ObjectDomain)
{
goto EnsureObjectIsSharedExit;
}
//
// Grab the local shared data lock, if necessary
//
if (0 != m_pot->GetSharedDataSize())
{
m_sdlSharedData.AcquireLock(pthr, &pDataLock);
}
//
// Allocate the necessary shared data areas
//
palError = AllocateSharedDataItems(&shmObjData, &psmod);
if (NO_ERROR != palError)
{
goto EnsureObjectIsSharedExit;
}
//
// Promote the object's data and set the domain to shared
//
PromoteSharedData(shmObjData, psmod);
m_ObjectDomain = SharedObject;
EnsureObjectIsSharedExit:
if (NULL != pDataLock)
{
pDataLock->ReleaseLock(pthr, TRUE);
}
SHMRelease();
LOGEXIT("CSharedMemoryObject::EnsureObjectIsShared returns %d\n", palError);
return palError;
}
/*++
Function:
CSharedMemoryObject::CleanupForProcessShutdown
Cleanup routine called by the object manager when shutting down
Parameters:
pthr -- thread data for the calling thread
--*/
void
CSharedMemoryObject::CleanupForProcessShutdown(
CPalThread *pthr
)
{
bool fCleanupSharedState;
_ASSERTE(NULL != pthr);
ENTRY("CSharedMemoryObject::CleanupForProcessShutdown"
"(this = %p, pthr = %p)\n",
this,
pthr
);
fCleanupSharedState = DereferenceSharedData();
if (NULL != m_pot->GetObjectCleanupRoutine())
{
(*m_pot->GetObjectCleanupRoutine())(
pthr,
static_cast<IPalObject*>(this),
TRUE,
fCleanupSharedState
);
}
//
// 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();
InternalDelete(this);
pthr->ReleaseThreadReference();
LOGEXIT("CSharedMemoryObject::CleanupForProcessShutdown\n");
}
/*++
Function:
CSharedMemoryObject::AcquireObjectDestructionLock
Acquires the lock that must be held when decrementing the object's
reference count (and, if the count drops to 0, while removing the
object from the object manager's lists).
Parameters:
pthr -- thread data for the calling thread
--*/
void
CSharedMemoryObject::AcquireObjectDestructionLock(
CPalThread *pthr
)
{
_ASSERTE(NULL != pthr);
ENTRY("CSharedMemoryObject::AcquireObjectDestructionLock"
"(this = %p, pthr = $p)\n",
this,
pthr
);
InternalEnterCriticalSection(pthr, m_pcsObjListLock);
LOGEXIT("CSharedMemoryObject::AcquireObjectDestructionLock\n");
}
/*++
Function:
CSharedMemoryObject::ReleaseObjectDestructionLock
Releases the lock acquired by AcquireObjectDestructionLock
Parameters:
pthr -- thread data for the calling thread
fDestructionPending -- if TRUE, the reference count for this
object has dropped to 0; the object will be destroyed after
this routine returns
--*/
bool
CSharedMemoryObject::ReleaseObjectDestructionLock(
CPalThread *pthr,
bool fDestructionPending
)
{
bool fCleanupSharedState = FALSE;
_ASSERTE(NULL != pthr);
ENTRY("CSharedMemoryObject::ReleaseObjectDestructionLock"
"(this = %p, pthr = %p, fDestructionPending = %d\n",
this,
pthr,
fDestructionPending
);
if (fDestructionPending)
{
RemoveEntryList(&m_le);
fCleanupSharedState = DereferenceSharedData();
}
InternalLeaveCriticalSection(pthr, m_pcsObjListLock);
LOGEXIT("CSharedMemoryObject::ReleaseObjectDestructionLock returns %d\n",
fCleanupSharedState
);
return fCleanupSharedState;
}
/*++
Function:
CSharedMemoryObject::DereferenceSharedData
Called to decrement the global refcount (i.e., the count of
the number of processes that have reference to the object) when
the local reference to the object is being destroyed.
Return value:
Returns TRUE if this process needs to clean up the object's shared
data (i.e., the global refcount has dropped to 0, or the object
is in the local domain)
--*/
bool
CSharedMemoryObject::DereferenceSharedData()
{
LONG fSharedDataAlreadDereferenced;
ENTRY("CSharedMemoryObject::DereferenceSharedData(this = %p)\n", this);
fSharedDataAlreadDereferenced = InterlockedExchange(
&m_fSharedDataDereferenced,
TRUE
);
if (!fSharedDataAlreadDereferenced)
{
if (SHMNULL != m_shmod)
{
SHMObjData *psmod;
SHMLock();
psmod = SHMPTR_TO_TYPED_PTR(SHMObjData, m_shmod);
_ASSERTE(NULL != psmod);
psmod->lProcessRefCount -= 1;
if (0 == psmod->lProcessRefCount)
{
//
// No other process is using this object, so remove
// it from the shared memory named object list (if it
// had been added to it). The final cleanup will happen
// in the object's destructor
//
m_fDeleteSharedData = TRUE;
if (psmod->fAddedToList)
{
//
// This object better have a name...
//
_ASSERTE(0 != psmod->dwNameLength);
if (SHMNULL != psmod->shmPrevObj)
{
SHMObjData *psmodPrevious = SHMPTR_TO_TYPED_PTR(SHMObjData, psmod->shmPrevObj);
_ASSERTE(NULL != psmodPrevious);
psmodPrevious->shmNextObj = psmod->shmNextObj;
}
else
{
//
// This object is the head of the shared memory named object
// list -- reset that pointer now
//
if (!SHMSetInfo(SIID_NAMED_OBJECTS, psmod->shmNextObj))
{
ASSERT("Failed to set shared named object list head");
}
}
if (SHMNULL != psmod->shmNextObj)
{
SHMObjData *psmodNext = SHMPTR_TO_TYPED_PTR(SHMObjData, psmod->shmNextObj);
_ASSERTE(NULL != psmodNext);
psmodNext->shmPrevObj = psmod->shmPrevObj;
}
}
#if _DEBUG
else
{
_ASSERTE(SHMNULL == psmod->shmPrevObj);
_ASSERTE(SHMNULL == psmod->shmNextObj);
}
#endif
}
SHMRelease();
}
else if (ProcessLocalObject == m_ObjectDomain)
{
//
// If the object is local the shared data needs to be
// deleted by definition
//
m_fDeleteSharedData = TRUE;
}
}
else
{
ASSERT("Multiple calls to DereferenceSharedData\n");
}
LOGEXIT("CSharedMemoryObject::DereferenceSharedData returns %d\n",
m_fDeleteSharedData
);
return m_fDeleteSharedData;
}
/*++
Function:
CSharedMemoryObject::~CSharedMemoryObject
Destructor; should only be called from ReleaseReference
--*/
CSharedMemoryObject::~CSharedMemoryObject()
{
ENTRY("CSharedMemoryObject::~CSharedMemoryObject(this = %p)\n", this);
if (!m_fSharedDataDereferenced)
{
ASSERT("DereferenceSharedData not called before object destructor -- delete called directly?\n");
DereferenceSharedData();
}
if (NULL != m_pvSharedData && ProcessLocalObject == m_ObjectDomain)
{
InternalFree(m_pvSharedData);
}
else if (SHMNULL != m_shmod && m_fDeleteSharedData)
{
FreeSharedDataAreas(m_shmod);
}
LOGEXIT("CSharedMemoryObject::~CSharedMemoryObject\n");
}
//
// C++ standard, 18.1.5 - offsetof requires a POD (plain old data) struct or
// union. Since offsetof is a macro, gcc doesn't actually check for improper
// use of offsetof, it keys off of the -> from NULL (which is also invalid for
// non-POD types by 18.1.5)
//
// As we have numerous examples of this behavior in our codebase,
// making an offsetof which doesn't use 0.
//
// PAL_safe_offsetof is a version of offsetof that protects against an
// overridden operator&
//
#define PAL_safe_offsetof(s,m) ((size_t)((ptrdiff_t)&(char&)(((s *)64)->m))-64)
/*++
Function:
CSharedMemoryObject::GetObjectFromListLink
Given a list link returns the object that contains it. Since m_le is
protected the caller cannot perform this computation directly
Parameters:
ple -- the list entry to obtain the object for
--*/
// static
CSharedMemoryObject*
CSharedMemoryObject::GetObjectFromListLink(PLIST_ENTRY ple)
{
CSharedMemoryObject *pshmo;
_ASSERTE(NULL != ple);
ENTRY("CSharedMemoryObject::GetObjectFromListLink(ple = %p)\n", ple);
//
// Ideally we'd use CONTAINING_RECORD here, but it uses offsetof (see above
// comment
//
pshmo = reinterpret_cast<CSharedMemoryObject*>(
reinterpret_cast<size_t>(ple) - PAL_safe_offsetof(CSharedMemoryObject, m_le)
);
_ASSERTE(ple == &pshmo->m_le);
LOGEXIT("CSharedMemoryObject::GetObjectFromListLink returns %p\n", pshmo);
return pshmo;
}
/*++
Function:
CSharedMemoryObject::GetSharedData
Provides the caller access to the object's shared 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 shared data
ppvProcssSharedData -- on success, receives a pointer to the shared data
--*/
PAL_ERROR
CSharedMemoryObject::GetSharedData(
CPalThread *pthr,
LockType eLockRequest,
IDataLock **ppDataLock, // OUT
void **ppvSharedData // OUT
)
{
IDataLock *pDataLock;
_ASSERTE(NULL != pthr);
_ASSERTE(ReadLock == eLockRequest || WriteLock == eLockRequest);
_ASSERTE(NULL != ppDataLock);
_ASSERTE(NULL != ppvSharedData);
ENTRY("CSharedMemoryObject::GetSharedData"
"(this = %p, pthr = %p, eLockRequest = %d, ppDataLock = %p,"
" ppvSharedData = %p)\n",
this,
pthr,
eLockRequest,
ppDataLock,
ppvSharedData
);
_ASSERTE(0 < m_pot->GetSharedDataSize());
if (ProcessLocalObject == m_ObjectDomain)
{
//
// We need to grab the local shared data lock and re-check
// the object's domain, as there's a chance the object might
// have been promoted after we made the above check but before
// we grabbed the lock
//
m_sdlSharedData.AcquireLock(pthr, &pDataLock);
if (SharedObject == m_ObjectDomain)
{
pDataLock->ReleaseLock(pthr, FALSE);
m_ssmlSharedData.AcquireLock(pthr, &pDataLock);
}
}
else
{
//
// A shared object can never transition back to local,
// so there's no need to recheck the domain on this path
//