blob: 0678573383f6601f9b8452a5817986c478d59df2 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include <stdint.h>
#include <fbl/intrusive_double_list.h>
#include <fbl/unique_ptr.h>
namespace devmgr {
struct Metadata {
fbl::DoublyLinkedListNodeState<fbl::unique_ptr<Metadata>> node;
struct Node {
static fbl::DoublyLinkedListNodeState<fbl::unique_ptr<Metadata>>&
node_state(Metadata& obj) {
return obj.node;
}
};
uint32_t type;
uint32_t length;
bool has_path; // zero terminated string starts at data[length]
char* Data() { return reinterpret_cast<char*>(this + 1); }
const char* Data() const { return reinterpret_cast<const char*>(this + 1); }
static zx_status_t Create(size_t data_len, fbl::unique_ptr<Metadata>* out) {
uint8_t* buf = new uint8_t[sizeof(Metadata) + data_len];
if (!buf) {
return ZX_ERR_NO_MEMORY;
}
new (buf) Metadata();
out->reset(reinterpret_cast<Metadata*>(buf));
return ZX_OK;
}
// Implement a custom delete to deal with the allocation mechanism used in
// Create(). Since the ctor is private, all Metadata* will come from
// Create().
void operator delete(void* ptr) { delete[] reinterpret_cast<uint8_t*>(ptr); }
private:
Metadata() = default;
Metadata(const Metadata&) = delete;
Metadata& operator=(const Metadata&) = delete;
Metadata(Metadata&&) = delete;
Metadata& operator=(Metadata&&) = delete;
};
} // namespace devmgr