1 // Copyright 2018 The Fuchsia 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 #include "slice-extent.h"
6 
7 #include <fbl/alloc_checker.h>
8 #include <fbl/unique_ptr.h>
9 #include <zircon/assert.h>
10 
11 namespace fvm {
12 
Split(size_t vslice)13 fbl::unique_ptr<SliceExtent> SliceExtent::Split(size_t vslice) {
14     ZX_DEBUG_ASSERT(start() <= vslice);
15     ZX_DEBUG_ASSERT(vslice < end());
16     fbl::AllocChecker ac;
17     fbl::unique_ptr<SliceExtent> new_extent(new (&ac) SliceExtent(vslice + 1));
18     if (!ac.check()) {
19         return nullptr;
20     }
21     new_extent->pslices_.reserve(end() - vslice, &ac);
22     if (!ac.check()) {
23         return nullptr;
24     }
25     for (size_t vs = vslice + 1; vs < end(); vs++) {
26         ZX_ASSERT(new_extent->push_back(get(vs)));
27     }
28     while (!is_empty() && vslice + 1 != end()) {
29         pop_back();
30     }
31     return new_extent;
32 }
33 
Merge(const SliceExtent & other)34 bool SliceExtent::Merge(const SliceExtent& other) {
35     ZX_DEBUG_ASSERT(end() == other.start());
36     fbl::AllocChecker ac;
37     pslices_.reserve(other.size(), &ac);
38     if (!ac.check()) {
39         return false;
40     }
41 
42     for (size_t vs = other.start(); vs < other.end(); vs++) {
43         ZX_ASSERT(push_back(other.get(vs)));
44     }
45     return true;
46 }
47 
48 } // namespace fvm
49