In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use js engine zone". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use js engine zone".
Zone is also used for memory management, but it is allocated incrementally and destroyed at a time. The following is the structure diagram.
Zone.h
# ifndef V8_ZONE_H_
# define V8_ZONE_H_
Namespace v8 {namespace internal {
/ / The Zone supports very fast allocation of small chunks of
/ / memory. The chunks cannot be deallocated individually, but instead
/ / the Zone supports deallocating all chunks in one fast
/ / operation. The Zone is used to hold temporary data structures like
/ / the abstract syntax tree, which is deallocated after compilation.
/ / Note: There is no need to initialize the Zone; the first time an
/ / allocation is attempted, a segment of memory will be requested
/ / through a call to malloc ().
/ / Note: The implementation is inherently not thread safe. Do not use
/ / from multi-threaded code.
Class Zone {
Public:
/ / Allocate 'size' bytes of memory in the Zone; expands the Zone by
/ / allocating new segments of memory on demand using malloc ().
/ / allocate memory of size size
Static inline void* New (int size)
/ / Delete all objects and free all memory allocated in the Zone.
/ / release all memory at once
Static void DeleteAll ()
Private:
/ / All pointers returned from New () have this alignment.
Static const int kAlignment = kPointerSize
/ / Never allocate segments smaller than this size in bytes.
Static const int kMinimumSegmentSize = 8 * KB
/ / Never keep segments larger than this size in bytes around.
Static const int kMaximumKeptSegmentSize = 64 * KB
/ / The Zone is intentionally a singleton; you should not try to
/ / allocate instances of the class.
/ / No new can only call Zone::New
Zone () {UNREACHABLE ();}
/ / Expand the Zone to hold at least 'size' more bytes and allocate
/ / the bytes. Returns the address of the newly allocated chunk of
/ / memory in the Zone. Should only be called if there isn't enough
/ / room in the Zone already.
/ / expand memory
Static Address NewExpand (int size)
/ / The free region in the current (front) segment is represented as
/ / the half-open interval [position, limit). The 'position' variable
/ / is guaranteed to be aligned as dictated by kAlignment.
/ / manage the first address and size limit of memory
Static Address position_
Static Address limit_
}
/ / ZoneObject is an abstraction that helps define classes of objects
/ / allocated in the Zone. Use it as a base class; see ast.h.
/ / A pair of Zone encapsulated new ZoneObject (); that is, Zone::New (size)
Class ZoneObject {
Public:
/ / Allocate a new ZoneObject of 'size' bytes in the Zone.
Void* operator new (size_t size) {return Zone::New (size);}
/ / Ideally, the delete operator should be private instead of
/ / public, but unfortuately the compiler sometimes synthesizes
/ / (unused) destructors for classes derived from ZoneObject, which
/ / require the operator to be visible. MSVC requires the delete
/ / operator to be public.
/ / ZoneObjects should never be deleted individually; use
/ / Zone::DeleteAll () to delete all zone objects in one go.
/ / objects of the delete class are prohibited
Void operator delete (void*, size_t) {UNREACHABLE ();}
}
/ *
Manage allow_allocation_ fields
When new AssertNoZoneAllocation, save the current allow_allocation_
Set allow_allocation_ to false, and restore the value of allow_allocation_ after destructing
, /
Class AssertNoZoneAllocation {
Public:
AssertNoZoneAllocation (): prev_ (allow_allocation_) {
Allow_allocation_ = false
}
~ AssertNoZoneAllocation () {allow_allocation_ = prev_;}
Static bool allow_allocation () {return allow_allocation_;}
Private:
Bool prev_
Static bool allow_allocation_
}
/ / The ZoneListAllocationPolicy is used to specialize the GenericList
/ / implementation to allocate ZoneLists and their elements in the
/ / Zone.
Class ZoneListAllocationPolicy {
Public:
/ / Allocate 'size' bytes of memory in the zone.
Static void* New (int size) {return Zone::New (size);}
/ / De-allocation attempts are silently ignored.
Static void Delete (void* p) {}
}
/ / ZoneLists are growable lists with constant-time access to the
/ / elements. The list itself and all its elements are allocated in the
/ / Zone. ZoneLists cannot be deleted individually; you can delete all
/ / objects in the Zone by calling Zone::DeleteAll ().
Template
/ / ZoneList is essentially a list,ZoneListAllocationPolicy is a memory manager in list
Class ZoneList: public List {
Public:
/ / Construct a new ZoneList with the given capacity; the length is
/ / always zero. The capacity must be non-negative.
Explicit ZoneList (int capacity)
: List (capacity) {}
}
}} / / namespace v8::internal
# endif / / V8_ZONE_H_
Zone-inl.h
# ifndef V8_ZONE_INL_H_
# define V8_ZONE_INL_H_
# include "zone.h"
Namespace v8 {namespace internal {
Inline void* Zone::New (int size) {
ASSERT (AssertNoZoneAllocation::allow_allocation ())
/ / Round up the requested size to fit the alignment.
Size = RoundUp (size, kAlignment)
/ / Check if the requested size is available without expanding.
/ / current pointer position
Address result = position_
/ *
At first, both position and limit are 0, so a segment will be assigned. When you need to assign it later, if segment
If the memory in the is enough, there is no need to allocate a new segment, just the original allocation
The limit is exceeded after the size is allocated, and a segment is also allocated when the capacity is expanded.
, /
If ((position_ + = size) > limit_) result = NewExpand (size)
/ / Check that the result has the proper alignment and return it.
ASSERT (IsAddressAligned (result, kAlignment, 0))
Return reinterpret_cast (result)
}
}} / / namespace v8::internal
# endif / / V8_ZONE_INL_H_
Zone.cc
# include "v8.h"
# include "zone-inl.h"
Namespace v8 {namespace internal {
Address Zone::position_ = 0
Address Zone::limit_ = 0
Bool AssertNoZoneAllocation::allow_allocation_ = true
/ / Segments represent chunks of memory: They have starting address
/ (encoded in the this pointer) and a size in bytes. Segments are
/ / chained together forming a LIFO structure with the newest segment
/ / available as Segment::head (). Segments are allocated using malloc ()
/ / and de-allocated using free ().
Class Segment {
Public:
/ / next node
Segment* next () const {return next_;}
/ / disconnect the pointer to the next node
Void clear_next () {next_ = NULL;}
/ / Total memory size
Int size () const {return size_;}
/ / available memory size, preceded by a Segment object
Int capacity () const {return size_-sizeof (Segment);}
/ / start address of memory, that is, Segement object
Address start () const {return address (sizeof (Segment));
/ / the ending address is the first address plus size
Address end () const {return address (size_);}
/ / return the first Segment node
Static Segment* head () {return head_;}
Static void set_head (Segment* head) {head_ = head;}
/ / Creates a new segment, sets it size, and pushes it to the front
/ / of the segment chain. Returns the new segment.
/ / add a Segment
Static Segment* New (int size) {
Segment* result = reinterpret_cast (Malloced::New (size))
/ / assignment succeeded
If (result! = NULL) {
/ / insert header to insert linked list. Size is the total size allocated.
Result- > next_ = head_
Result- > size_ = size
Head_ = result
}
Return result
}
/ / Deletes the given segment. Does not touch the segment chain.
/ / release the segment node
Static void Delete (Segment* segment) {
Malloced::Delete (segment)
}
Private:
/ / Computes the address of the nth byte in this segment.
/ / first address plus n bytes
Address address (int n) const {
Return Address (this) + n
}
/ / manage the header pointers of all segment nodes
Static Segment* head_
/ / attributes of each segment node
Segment* next_
Int size_
}
Segment* Segment::head_ = NULL
Void Zone::DeleteAll () {
# ifdef DEBUG
/ / Constant byte value used for zapping dead memory in debug mode.
Static const unsigned char kZapDeadByte = 0xcd
# endif
/ / Find a segment with a suitable size to keep around.
Segment* keep = Segment::head ()
/ / to the last node or a node smaller than the kMaximumKeptSegmentSize size
While (keep! = NULL & & keep- > size () > kMaximumKeptSegmentSize) {
Keep = keep- > next ()
}
/ / Traverse the chained list of segments, zapping (in debug mode)
/ / and freeing every segment except the one we wish to keep.
Segment* current = Segment::head ()
/ / keep nodes are processed, and memory of other nodes is freed.
While (current! = NULL) {
Segment* next = current- > next ()
If (current = = keep) {
/ / Unlink the segment we wish to keep from the list.
Current- > clear_next ()
} else {
# ifdef DEBUG
/ / Zap the entire current segment (including the header).
Memset (current, kZapDeadByte, current- > size ())
# endif
Segment::Delete (current)
}
Current = next
}
/ / If we have found a segment we want to keep, we must recompute the
/ / variables' position' and 'limit' to prepare for future allocate
/ / attempts. Otherwise, we must clear the position and limit to
/ / force a new segment to be allocated on demand.
/ / update the properties, and the reserved memory will be used for the next allocation
If (keep! = NULL) {
Address start = keep- > start ()
Position_ = RoundUp (start, kAlignment)
Limit_ = keep- > end ()
# ifdef DEBUG
/ / Zap the contents of the kept segment (but not the header).
Memset (start, kZapDeadByte, keep- > capacity ())
# endif
} else {
Position_ = limit_ = 0
}
/ / Update the head segment to be the kept segment (if any).
/ / Update header pointer
Segment::set_head (keep)
}
Address Zone::NewExpand (int size) {
/ / Make sure the requested size is already properly aligned and that
/ / there isn't enough room in the Zone to satisfy the request.
ASSERT (size = = RoundDown (size, kAlignment))
ASSERT (position_ + size > limit_)
/ / Compute the new segment size. We use a 'high water mark'
/ / strategy, where we increase the segment size every time we expand
/ / except that we employ a maximum segment size when we delete. This
/ / is to avoid excessive malloc () and free () overhead.
Segment* head = Segment::head ()
Int old_size = (head = = NULL)? 0: head- > size ()
Int new_size = sizeof (Segment) + kAlignment + size + (old_size start (), kAlignment)
/ / Update the properties and use them the next time you assign them.
Position_ = result + size
Limit_ = segment- > end ()
ASSERT (position_
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.