blob: 93d42df876ac007b9b72b7f65ef4820f5a7d0cfb [file] [log] [blame]
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "memory_allocator_reporter.h"
#include "starboard/once.h"
namespace {
// Control to initialize s_instance.
SbOnceControl s_instance_control = SB_ONCE_INITIALIZER;
MemoryAllocatorReporter* s_instance = NULL;
void Initialize() {
s_instance = new MemoryAllocatorReporter();
}
void* OffsetPointer(void* base, ssize_t offset) {
uintptr_t base_as_int = reinterpret_cast<uintptr_t>(base);
return reinterpret_cast<void*>(base_as_int + offset);
}
} // namespace
AllocationMetadata* AllocationMetadata::GetMetadataFromUserAddress(void* ptr) {
if (ptr == NULL) {
return NULL;
}
// The metadata lives just in front of the data.
void* meta_addr =
OffsetPointer(ptr, -static_cast<ssize_t>(sizeof(AllocationMetadata)));
return reinterpret_cast<AllocationMetadata*>(meta_addr);
}
void* AllocationMetadata::GetUserAddressFromBaseAddress(void* base_ptr) {
void* adjusted_base =
OffsetPointer(base_ptr, static_cast<ssize_t>(sizeof(AllocationMetadata)));
return adjusted_base;
}
void AllocationMetadata::SetSizeToBaseAddress(void* base_ptr, size_t size) {
if (base_ptr) {
AllocationMetadata* metadata =
reinterpret_cast<AllocationMetadata*>(base_ptr);
metadata->set_size_requested(size);
}
}
void MemoryAllocatorReporter::UpdateAllocatedBytes(ssize_t bytes) {
starboard::ScopedLock lock(mutex_);
current_bytes_allocated_ += bytes;
}
ssize_t MemoryAllocatorReporter::GetCurrentBytesAllocated() {
starboard::ScopedLock lock(mutex_);
return current_bytes_allocated_;
}
void MemoryAllocatorReporter::UpdateMappedBytes(ssize_t bytes) {
starboard::ScopedLock lock(mutex_);
current_bytes_mapped_ += bytes;
}
ssize_t MemoryAllocatorReporter::GetCurrentBytesMapped() {
starboard::ScopedLock lock(mutex_);
return current_bytes_mapped_;
}
// static
MemoryAllocatorReporter* MemoryAllocatorReporter::Get() {
SbOnce(&s_instance_control, &Initialize);
return s_instance;
}