Skip to content

Commit fef68cc

Browse files
committed
Bug 1666746 - pt 1. Add an IsAllocated method r=froydnj
Adding this method makes it clearer what code is doing when it checks that the storage has been allocated. Differential Revision: https://phabricator.services.mozilla.com/D89564
1 parent 98e2fea commit fef68cc

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

xpcom/ds/PLDHashTable.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ PLDHashTable::~PLDHashTable() {
282282
AutoDestructorOp op(mChecker);
283283
#endif
284284

285-
if (!mEntryStore.Get()) {
285+
if (!mEntryStore.IsAllocated()) {
286286
return;
287287
}
288288

@@ -318,7 +318,7 @@ MOZ_ALWAYS_INLINE auto PLDHashTable::SearchTable(const void* aKey,
318318
PLDHashNumber aKeyHash,
319319
Success&& aSuccess,
320320
Failure&& aFailure) const {
321-
MOZ_ASSERT(mEntryStore.Get());
321+
MOZ_ASSERT(mEntryStore.IsAllocated());
322322
NS_ASSERTION(!(aKeyHash & kCollisionFlag), "!(aKeyHash & kCollisionFlag)");
323323

324324
// Compute the primary hash address.
@@ -389,7 +389,7 @@ MOZ_ALWAYS_INLINE auto PLDHashTable::SearchTable(const void* aKey,
389389
// to keys, which means callers can use complex key types more easily.
390390
MOZ_ALWAYS_INLINE auto PLDHashTable::FindFreeSlot(PLDHashNumber aKeyHash) const
391391
-> Slot {
392-
MOZ_ASSERT(mEntryStore.Get());
392+
MOZ_ASSERT(mEntryStore.IsAllocated());
393393
NS_ASSERTION(!(aKeyHash & kCollisionFlag), "!(aKeyHash & kCollisionFlag)");
394394

395395
// Compute the primary hash address.
@@ -423,7 +423,7 @@ MOZ_ALWAYS_INLINE auto PLDHashTable::FindFreeSlot(PLDHashNumber aKeyHash) const
423423
}
424424

425425
bool PLDHashTable::ChangeTable(int32_t aDeltaLog2) {
426-
MOZ_ASSERT(mEntryStore.Get());
426+
MOZ_ASSERT(mEntryStore.IsAllocated());
427427

428428
// Look, but don't touch, until we succeed in getting new entry store.
429429
int32_t oldLog2 = kPLDHashNumberBits - mHashShift;
@@ -471,7 +471,7 @@ bool PLDHashTable::ChangeTable(int32_t aDeltaLog2) {
471471

472472
MOZ_ALWAYS_INLINE PLDHashNumber
473473
PLDHashTable::ComputeKeyHash(const void* aKey) const {
474-
MOZ_ASSERT(mEntryStore.Get());
474+
MOZ_ASSERT(mEntryStore.IsAllocated());
475475

476476
PLDHashNumber keyHash = mozilla::ScrambleHashCode(mOps->hashKey(aKey));
477477

@@ -489,7 +489,7 @@ PLDHashEntryHdr* PLDHashTable::Search(const void* aKey) const {
489489
AutoReadOp op(mChecker);
490490
#endif
491491

492-
if (!mEntryStore.Get()) {
492+
if (!mEntryStore.IsAllocated()) {
493493
return nullptr;
494494
}
495495

@@ -506,13 +506,13 @@ PLDHashEntryHdr* PLDHashTable::Add(const void* aKey,
506506
#endif
507507

508508
// Allocate the entry storage if it hasn't already been allocated.
509-
if (!mEntryStore.Get()) {
509+
if (!mEntryStore.IsAllocated()) {
510510
uint32_t nbytes;
511511
// We already checked this in the constructor, so it must still be true.
512512
MOZ_RELEASE_ASSERT(
513513
SizeOfEntryStore(CapacityFromHashShift(), mEntrySize, &nbytes));
514514
mEntryStore.Set((char*)calloc(1, nbytes), &mGeneration);
515-
if (!mEntryStore.Get()) {
515+
if (!mEntryStore.IsAllocated()) {
516516
return nullptr;
517517
}
518518
}
@@ -566,7 +566,7 @@ PLDHashEntryHdr* PLDHashTable::Add(const void* aKey,
566566
PLDHashEntryHdr* PLDHashTable::Add(const void* aKey) {
567567
PLDHashEntryHdr* entry = Add(aKey, fallible);
568568
if (!entry) {
569-
if (!mEntryStore.Get()) {
569+
if (!mEntryStore.IsAllocated()) {
570570
// We OOM'd while allocating the initial entry storage.
571571
uint32_t nbytes;
572572
(void)SizeOfEntryStore(CapacityFromHashShift(), mEntrySize, &nbytes);
@@ -587,7 +587,7 @@ void PLDHashTable::Remove(const void* aKey) {
587587
AutoWriteOp op(mChecker);
588588
#endif
589589

590-
if (!mEntryStore.Get()) {
590+
if (!mEntryStore.IsAllocated()) {
591591
return;
592592
}
593593

@@ -623,7 +623,7 @@ void PLDHashTable::RawRemove(Slot& aSlot) {
623623
// active, which doesn't fit well into how Checker's mState variable works.
624624
MOZ_ASSERT(mChecker.IsWritable());
625625

626-
MOZ_ASSERT(mEntryStore.Get());
626+
MOZ_ASSERT(mEntryStore.IsAllocated());
627627

628628
MOZ_ASSERT(aSlot.IsLive());
629629

xpcom/ds/PLDHashTable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ class PLDHashTable {
323323
}
324324

325325
char* Get() const { return mEntryStore; }
326+
bool IsAllocated() const { return !!mEntryStore; }
326327

327328
Slot SlotForIndex(uint32_t aIndex, uint32_t aEntrySize,
328329
uint32_t aCapacity) const {
@@ -427,7 +428,7 @@ class PLDHashTable {
427428
// This can be zero if no elements have been added yet, in which case the
428429
// entry storage will not have yet been allocated.
429430
uint32_t Capacity() const {
430-
return mEntryStore.Get() ? CapacityFromHashShift() : 0;
431+
return mEntryStore.IsAllocated() ? CapacityFromHashShift() : 0;
431432
}
432433

433434
uint32_t EntrySize() const { return mEntrySize; }

0 commit comments

Comments
 (0)