Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_HEAP_H_
6 : #define V8_WASM_HEAP_H_
7 :
8 : #include <list>
9 :
10 : #include "src/base/macros.h"
11 : #include "src/vector.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 : namespace wasm {
16 :
17 : // Sorted, disjoint and non-overlapping memory ranges. A range is of the
18 : // form [start, end). So there's no [start, end), [end, other_end),
19 : // because that should have been reduced to [start, other_end).
20 : using AddressRange = std::pair<Address, Address>;
21 : class V8_EXPORT_PRIVATE DisjointAllocationPool final {
22 : public:
23 : enum ExtractionMode : bool { kAny = false, kContiguous = true };
24 : DisjointAllocationPool() {}
25 :
26 : explicit DisjointAllocationPool(Address, Address);
27 :
28 : DisjointAllocationPool(DisjointAllocationPool&& other) = default;
29 : DisjointAllocationPool& operator=(DisjointAllocationPool&& other) = default;
30 :
31 : // Merge the ranges of the parameter into this object. Ordering is
32 : // preserved. The assumption is that the passed parameter is
33 : // not intersecting this object - for example, it was obtained
34 : // from a previous Allocate{Pool}.
35 : void Merge(DisjointAllocationPool&&);
36 :
37 : // Allocate a contiguous range of size {size}. Return an empty pool on
38 : // failure.
39 : DisjointAllocationPool Allocate(size_t size) {
40 2 : return Extract(size, kContiguous);
41 : }
42 :
43 : // Allocate a sub-pool of size {size}. Return an empty pool on failure.
44 : DisjointAllocationPool AllocatePool(size_t size) {
45 4 : return Extract(size, kAny);
46 : }
47 :
48 : bool IsEmpty() const { return ranges_.empty(); }
49 : const std::list<AddressRange>& ranges() const { return ranges_; }
50 :
51 : private:
52 : // Extract out a total of {size}. By default, the return may
53 : // be more than one range. If kContiguous is passed, the return
54 : // will be one range. If the operation fails, this object is
55 : // unchanged, and the return {IsEmpty()}
56 : DisjointAllocationPool Extract(size_t size, ExtractionMode mode);
57 :
58 : std::list<AddressRange> ranges_;
59 :
60 : DISALLOW_COPY_AND_ASSIGN(DisjointAllocationPool)
61 : };
62 :
63 : } // namespace wasm
64 : } // namespace internal
65 : } // namespace v8
66 : #endif
|