uint32_t ResourceQueue::EvictBefore(uint64_t aOffset, ErrorResult& aRv) { SBR_DEBUG("EvictBefore(%llu)", aOffset); uint32_t evicted = 0; while (ResourceItem* item = ResourceAt(0)) { SBR_DEBUG("item=%p length=%d offset=%llu", item, item->mData->Length(), mOffset); if (item->mData->Length() + mOffset >= aOffset) { if (aOffset <= mOffset) { break; } uint32_t offset = aOffset - mOffset; mOffset += offset; evicted += offset; RefPtr<MediaByteBuffer> data = new MediaByteBuffer; if (!data->AppendElements(item->mData->Elements() + offset, item->mData->Length() - offset, fallible)) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return 0; } item->mData = data; break; } mOffset += item->mData->Length(); evicted += item->mData->Length(); delete PopFront(); } return evicted; }
void ResourcesContainer::MakeEmpty() { for (int32 i = 0; ResourceItem *item = ResourceAt(i); i++) delete item; fResources.MakeEmpty(); SetModified(false); }
bool ResourcesContainer::IsModified() const { bool isModified = fIsModified; int32 count = CountResources(); for (int32 i = 0; !isModified && i < count; i++) isModified |= ResourceAt(i)->IsModified(); return isModified; }
void ResourcesContainer::SetModified(bool modified) { fIsModified = modified; // If unmodified, set the resource item's modified flag as well. if (!modified) { int32 count = CountResources(); for (int32 i = 0; i < count; i++) ResourceAt(i)->SetModified(false); } }
int32 ResourcesContainer::IndexOf(type_code type, int32 id) const { int32 index = -1; int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { ResourceItem *item = ResourceAt(i); if (item->Type() == type && item->ID() == id) index = i; } return index; }
int32 ResourcesContainer::IndexOf(const void *data) const { int32 index = -1; if (data) { int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { if (ResourceAt(i)->Data() == data) index = i; } } return index; }
size_t ResourceQueue::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { // Calculate the size of the internal deque. size_t size = nsDeque::SizeOfExcludingThis(aMallocSizeOf); // Sum the ResourceItems. for (uint32_t i = 0; i < uint32_t(GetSize()); ++i) { const ResourceItem* item = ResourceAt(i); size += item->SizeOfIncludingThis(aMallocSizeOf); } return size; }
uint32_t ResourceQueue::EvictAll() { SBR_DEBUG("EvictAll()"); uint32_t evicted = 0; while (ResourceItem* item = ResourceAt(0)) { SBR_DEBUG("item=%p length=%d offset=%llu", item, item->mData->Length(), mOffset); mOffset += item->mData->Length(); evicted += item->mData->Length(); delete PopFront(); } return evicted; }
int32 ResourcesContainer::IndexOfType(type_code type, int32 typeIndex) const { int32 index = -1; int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { ResourceItem *item = ResourceAt(i); if (item->Type() == type) { if (typeIndex == 0) index = i; typeIndex--; } } return index; }
void ResourceQueue::Dump(const char* aPath) { for (uint32_t i = 0; i < uint32_t(GetSize()); ++i) { ResourceItem* item = ResourceAt(i); char buf[255]; PR_snprintf(buf, sizeof(buf), "%s/%08u.bin", aPath, i); FILE* fp = fopen(buf, "wb"); if (!fp) { return; } fwrite(item->mData->Elements(), item->mData->Length(), 1, fp); fclose(fp); } }
// IndexOf int32 ResourcesContainer::IndexOf(type_code type, const char *name) const { int32 index = -1; int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { ResourceItem *item = ResourceAt(i); const char *itemName = item->Name(); if (item->Type() == type && (name == NULL && itemName == NULL || name != NULL && itemName != NULL && !strcmp(name, itemName))) { index = i; } } return index; }
void ResourceQueue::CopyData(uint64_t aOffset, uint32_t aCount, char* aDest) { uint32_t offset = 0; uint32_t start = GetAtOffset(aOffset, &offset); uint32_t end = std::min(GetAtOffset(aOffset + aCount, nullptr) + 1, uint32_t(GetSize())); for (uint32_t i = start; i < end; ++i) { ResourceItem* item = ResourceAt(i); uint32_t bytes = std::min(aCount, uint32_t(item->mData->Length() - offset)); if (bytes != 0) { memcpy(aDest, &(*item->mData)[offset], bytes); offset = 0; aCount -= bytes; aDest += bytes; } } }
uint32_t ResourceQueue::GetAtOffset(uint64_t aOffset, uint32_t *aResourceOffset) { MOZ_RELEASE_ASSERT(aOffset >= mOffset); uint64_t offset = mOffset; for (uint32_t i = 0; i < uint32_t(GetSize()); ++i) { ResourceItem* item = ResourceAt(i); // If the item contains the start of the offset we want to // break out of the loop. if (item->mData->Length() + offset > aOffset) { if (aResourceOffset) { *aResourceOffset = aOffset - offset; } return i; } offset += item->mData->Length(); } return GetSize(); }