Files
sdk/runtime/vm/memory_region.h
T
sgjesse@google.com bf67f24098 Move assert.h/assert.cc from runtime/vm to runtime/platform
The purpose of this change is twofold:

1. Source in the bin directory can now use the same assertions as
   source in the vm directory. The ASSERT macro used by the code
   in runtime/bin was just defined to use assert from the standard
   C library.
2. Moving other implementation parts from runtime/vm to
   runtime/platform (e.g. classes Monitor and Mutex) for sharing
   between runtime/bin and runtime/vm will be easier as these
   implementations rely on these assertion macros.

Created two gypi files for the platform directory. One for the
headers and one for the source. The source one is only included
when building the VM library and will be present in libdart.a
when the dart executable is linked.

All the code for asserts is still in the dart namespace.

Also re-arranged the order of includes to be alphabetically in
the files touched.

R=ager@google.com, iposva@google.com

BUG=
TEST=

Review URL: http://codereview.chromium.org//9189003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@3335 260f80e4-7a28-3924-810f-c04153c831b5
2012-01-16 12:28:10 +00:00

87 lines
2.7 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef VM_MEMORY_REGION_H_
#define VM_MEMORY_REGION_H_
#include "platform/assert.h"
#include "vm/globals.h"
namespace dart {
// Memory regions are useful for accessing memory with bounds check in
// debug mode. They can be safely passed by value and do not assume ownership
// of the region.
class MemoryRegion {
public:
MemoryRegion() : pointer_(NULL), size_(0) { }
MemoryRegion(void* pointer, uword size) : pointer_(pointer), size_(size) { }
void* pointer() const { return pointer_; }
uword size() const { return size_; }
uword size_in_bits() const { return size_ * kBitsPerByte; }
static uword pointer_offset() { return OFFSET_OF(MemoryRegion, pointer_); }
uword start() const { return reinterpret_cast<uword>(pointer_); }
uword end() const { return start() + size_; }
template<typename T> T Load(uword offset) const {
return *ComputeInternalPointer<T>(offset);
}
template<typename T> void Store(uword offset, T value) const {
*ComputeInternalPointer<T>(offset) = value;
}
template<typename T> T* PointerTo(uword offset) const {
return ComputeInternalPointer<T>(offset);
}
bool Contains(uword address) const {
return (address >= start()) && (address < end());
}
void CopyFrom(uword offset, const MemoryRegion& from) const;
// Compute a sub memory region based on an existing one.
void Subregion(const MemoryRegion& from, uword offset, uword size) {
ASSERT(from.size() >= size);
ASSERT(offset <= (from.size() - size));
pointer_ = reinterpret_cast<void*>(from.start() + offset);
size_ = size;
}
// Compute an extended memory region based on an existing one.
void Extend(const MemoryRegion& region, uword extra) {
pointer_ = region.pointer();
size_ = (region.size() + extra);
}
private:
template<typename T> T* ComputeInternalPointer(uword offset) const {
ASSERT(size() >= sizeof(T));
ASSERT(offset <= size() - sizeof(T));
return reinterpret_cast<T*>(start() + offset);
}
// Locate the bit with the given offset. Returns a pointer to the byte
// containing the bit, and sets bit_mask to the bit within that byte.
uint8_t* ComputeBitPointer(uword bit_offset, uint8_t* bit_mask) const {
uword bit_remainder = (bit_offset & (kBitsPerByte - 1));
*bit_mask = (1U << bit_remainder);
uword byte_offset = (bit_offset >> kBitsPerByteLog2);
return ComputeInternalPointer<uint8_t>(byte_offset);
}
void* pointer_;
uword size_;
DISALLOW_COPY_AND_ASSIGN(MemoryRegion);
};
} // namespace dart
#endif // VM_MEMORY_REGION_H_