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 <fbl/alloc_checker.h>
6 #include <lib/fzl/vmar-manager.h>
7 
8 #include <utility>
9 
10 namespace fzl {
11 
Create(size_t size,fbl::RefPtr<VmarManager> parent,zx_vm_option_t options)12 fbl::RefPtr<VmarManager> VmarManager::Create(size_t size,
13                                              fbl::RefPtr<VmarManager> parent,
14                                              zx_vm_option_t options) {
15     if (!size || (parent && !parent->vmar().is_valid())) {
16         return nullptr;
17     }
18 
19     fbl::AllocChecker ac;
20     fbl::RefPtr<VmarManager> ret = fbl::AdoptRef(new (&ac) VmarManager());
21 
22     if (!ac.check()) {
23         return nullptr;
24     }
25 
26     zx_status_t res;
27     zx_handle_t p = parent ? parent->vmar().get() : zx::vmar::root_self()->get();
28     uintptr_t child_addr;
29 
30     res = zx_vmar_allocate(p, options, 0, size, ret->vmar_.reset_and_get_address(), &child_addr);
31     if (res != ZX_OK) {
32         return nullptr;
33     }
34 
35     ret->parent_ = std::move(parent);
36     ret->start_ = reinterpret_cast<void*>(child_addr);
37     ret->size_ = size;
38 
39     return ret;
40 }
41 
42 } // namespace fzl
43