Skip to content

Commit d5fc8fc

Browse files
author
Jianchun Xu
committed
swb: replace SetArray with CopyArray
Address CR feedback.
1 parent 9958e2f commit d5fc8fc

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

lib/Common/DataStructures/BaseDictionary.h

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ namespace JsUtil
9393
Pointer(int, TAllocator) buckets;
9494
Pointer(EntryType, TAllocator) entries;
9595
PointerNoBarrier(AllocatorType) alloc;
96-
int size;
97-
uint bucketCount;
98-
int count;
99-
int freeList;
100-
int freeCount;
96+
Field(int) size;
97+
Field(uint) bucketCount;
98+
Field(int) count;
99+
Field(int) freeList;
100+
Field(int) freeCount;
101101

102102
#if PROFILE_DICTIONARY
103-
DictionaryStats *stats;
103+
PointerNoBarrier(DictionaryStats) stats;
104104
#endif
105105
enum InsertOperations
106106
{
@@ -179,11 +179,8 @@ namespace JsUtil
179179
freeList = other.freeList;
180180
freeCount = other.freeCount;
181181

182-
size_t copySize = bucketCount * sizeof(buckets[0]);
183-
js_memcpy_s(buckets, copySize, other.buckets, copySize);
184-
185-
copySize = size * sizeof(entries[0]);
186-
js_memcpy_s(entries, copySize, other.entries, copySize);
182+
CopyArray<TAllocator>(buckets, bucketCount, other.buckets, bucketCount);
183+
CopyArray<TAllocator, EntryType, ValueType>(entries, size, other.entries, size);
187184

188185
#if PROFILE_DICTIONARY
189186
stats = DictionaryStats::Create(typeid(this).name(), size);
@@ -698,11 +695,8 @@ namespace JsUtil
698695
freeList = other->freeList;
699696
freeCount = other->freeCount;
700697

701-
size_t copySize = bucketCount * sizeof(buckets[0]);
702-
js_memcpy_s(buckets, copySize, other->buckets, copySize);
703-
704-
copySize = size * sizeof(entries[0]);
705-
js_memcpy_s(entries, copySize, other->entries, copySize);
698+
CopyArray<TAllocator>(buckets, bucketCount, other->buckets, bucketCount);
699+
CopyArray<TAllocator, EntryType, ValueType>(entries, size, other->entries, size);
706700

707701
#if PROFILE_DICTIONARY
708702
stats = DictionaryStats::Create(typeid(this).name(), size);
@@ -866,8 +860,8 @@ namespace JsUtil
866860
Allocate(&newBuckets, &newEntries, initBucketCount, initSize);
867861

868862
// Allocation can throw - assign only after allocation has succeeded.
869-
SetArray<TAllocator>(this->buckets, newBuckets, initBucketCount);
870-
SetArray<TAllocator, EntryType, ValueType>(this->entries, newEntries, initSize);
863+
this->buckets = newBuckets;
864+
this->entries = newEntries;
871865
this->bucketCount = initBucketCount;
872866
this->size = initSize;
873867
Assert(this->freeCount == 0);
@@ -1007,7 +1001,7 @@ namespace JsUtil
10071001
{
10081002
// no need to rehash
10091003
newEntries = AllocateEntries(newSize);
1010-
js_memcpy_s(newEntries, sizeof(EntryType) * newSize, entries, sizeof(EntryType) * count);
1004+
CopyArray<TAllocator, EntryType, ValueType>(newEntries, newSize, entries, count);
10111005

10121006
DeleteEntries(entries, size);
10131007

@@ -1017,7 +1011,7 @@ namespace JsUtil
10171011
}
10181012

10191013
Allocate(&newBuckets, &newEntries, newBucketCount, newSize);
1020-
js_memcpy_s(newEntries, sizeof(EntryType) * newSize, entries, sizeof(EntryType) * count);
1014+
CopyArray<TAllocator, EntryType, ValueType>(newEntries, newSize, entries, count);
10211015

10221016
// When TAllocator is of type Recycler, it is possible that the Allocate above causes a collection, which
10231017
// in turn can cause entries in the dictionary to be removed - i.e. the dictionary contains weak references
@@ -1042,8 +1036,8 @@ namespace JsUtil
10421036
if (stats)
10431037
stats->Resize(newSize, /*emptyBuckets=*/ newSize - size);
10441038
#endif
1045-
SetArray<TAllocator>(this->buckets, newBuckets, newBucketCount);
1046-
SetArray<TAllocator, EntryType, ValueType>(this->entries, newEntries, newSize);
1039+
this->buckets = newBuckets;
1040+
this->entries = newEntries;
10471041
bucketCount = newBucketCount;
10481042
size = newSize;
10491043
}
@@ -1565,7 +1559,7 @@ namespace JsUtil
15651559
class SynchronizedDictionary: protected BaseDictionary<TKey, TValue, TAllocator, SizePolicy, Comparer, Entry>
15661560
{
15671561
private:
1568-
SyncObject* syncObj;
1562+
PointerNoBarrier(SyncObject) syncObj;
15691563

15701564
typedef BaseDictionary<TKey, TValue, TAllocator, SizePolicy, Comparer, Entry> Base;
15711565
public:
@@ -1817,4 +1811,3 @@ namespace JsUtil
18171811
PREVENT_COPY(SynchronizedDictionary);
18181812
};
18191813
}
1820-

lib/Common/DataStructures/DefaultContainerLockPolicy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Js
1414
{
1515
public:
1616
template <class SyncObject>
17-
NoLock(SyncObject*)
17+
NoLock(const SyncObject&)
1818
{
1919
// No lock, do nothing.
2020
}

lib/Common/Memory/RecyclerPointers.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,33 @@ void WriteBarrier(T * address, size_t count)
108108
return _ArrayWriteBarrier<Policy>::WriteBarrier(address, count);
109109
}
110110

111-
// Set an array field to an allocated array.
112-
//
113-
// Will trigger write barrier on the array content When the field is of WriteBarrierPtr
114-
// type, and Allocator / element type determines write barrier required.
111+
// Copy array content. Triggers write barrier on the dst array content if if
112+
// Allocator and element type determines write barrier is needed.
115113
//
116114
template <class Allocator, class T, class PolicyType = T>
117-
void SetArray(T*& dst, T* src, size_t srcCount)
115+
void CopyArray(T* dst, size_t dstCount, const T* src, size_t srcCount)
118116
{
119-
dst = src;
117+
js_memcpy_s(dst, sizeof(T) * dstCount, src, sizeof(T) * srcCount);
118+
WriteBarrier<T, Allocator, PolicyType>(dst, dstCount);
120119
}
121120
template <class Allocator, class T, class PolicyType = T>
122-
void SetArray(NoWriteBarrierPtr<T>& dst, T* src, size_t srcCount)
121+
void CopyArray(const NoWriteBarrierPtr<T>& dst, size_t dstCount, const NoWriteBarrierPtr<T>& src, size_t srcCount)
123122
{
124-
dst = src;
123+
return CopyArray<Allocator, T, PolicyType>((T*)dst, dstCount, (const T*)src, srcCount);
125124
}
126125
template <class Allocator, class T, class PolicyType = T>
127-
void SetArray(WriteBarrierPtr<T>& dst, T* src, size_t srcCount)
126+
void CopyArray(const WriteBarrierPtr<T>& dst, size_t dstCount, const WriteBarrierPtr<T>& src, size_t srcCount)
128127
{
129-
dst = src;
130-
WriteBarrier<T, Allocator, PolicyType>(dst, srcCount);
128+
return CopyArray<Allocator, T, PolicyType>((T*)dst, dstCount, (const T*)src, srcCount);
131129
}
132130

131+
133132
template <typename T>
134133
class NoWriteBarrierField
135134
{
136135
public:
137136
NoWriteBarrierField() {}
138-
NoWriteBarrierField(T const& value) : value(value) {}
137+
explicit NoWriteBarrierField(T const& value) : value(value) {}
139138

140139
// Getters
141140
operator T const&() const { return value; }
@@ -160,7 +159,7 @@ class NoWriteBarrierPtr
160159
{
161160
public:
162161
NoWriteBarrierPtr() {}
163-
NoWriteBarrierPtr(T * value) : value(value) {}
162+
explicit NoWriteBarrierPtr(T * value) : value(value) {}
164163

165164
// Getters
166165
T * operator->() const { return this->value; }
@@ -209,7 +208,7 @@ class WriteBarrierPtr
209208
{
210209
public:
211210
WriteBarrierPtr() {}
212-
WriteBarrierPtr(T * ptr)
211+
explicit WriteBarrierPtr(T * ptr)
213212
{
214213
// WriteBarrier
215214
NoWriteBarrierSet(ptr);

0 commit comments

Comments
 (0)