Google is committed to advancing racial equity for Black communities. See how.

LLCPP Memory Management

This document provides an overview of the tools available to manage memory when using the LLCPP bindings.

Memory ownership

LLCPP keeps references to objects using:

  • fidl::StringView for a string.
  • fidl::VectorView for a vector of objects.
  • fidl::ObjectView for a reference to an object.

These are non-owning views that only keep a reference and do not manage the object lifetime. The lifetime of the objects must be managed externally. That means that the referenced objects must outlive the views.

In particular, LLCPP generated types do not own theit out-of-line children, as defined by the FIDL wire format.

Create LLCPP object using the FidlAllocator

The FIDL allocator (fidl::FidlAllocator) can allocate LLCPP objects. It manages the lifetime of the allocated LLCPP objects (it owns the objects). As soon as the allocator is deleted, all the objects it has allocated are deallocated and their destructors are called.

The FIDL allocator is defined in lib/fidl/llcpp/fidl_allocator.h.

The objects are first allocated within a buffer which belongs to the allocator (this is a field of the allocator). The default size of the buffer is 512 bytes. A different size can be selected using fidl::FidlAllocator<size>.

When this buffer is full, the allocator allocates more buffers on the heap. Each of these buffers is 16 KiB (if it needs to allocate an object bigger, it will use a buffer which fit the bigger size).

The standard patern for using the allocator is:

  • Define a local variable allocator of type fidl::FidlAllocator.
  • Allocate objects using the allocator.
  • Send the allocated objects by making a FIDL method call or making a reply via a completer.
  • Leave the function; everything is deallocated.

Example:

fidl::FidlAllocator allocator;
fuchsia_examples::wire::User user(allocator);
user.set_age(allocator, 30);
user.set_name(allocator, allocator, "jdoe");
ASSERT_FALSE(user.IsEmpty());
ASSERT_EQ(user.age(), 30);

Unowned data

In addition to the managed allocation strategies, it is also possible to directly create pointers to memory unowned by FIDL. This is discouraged, as it is easy to accidentally create use-after-free bugs. FromExternal exists to explicitly mark pointers to FIDL-unowned memory.

To create an ObjectView from an external object using fidl::ObjectView::FromExternal.

fuchsia_examples::wire::JsonValue val;
int32_t i = 1;
val.set_int_value(fidl::ObjectView<int32_t>::FromExternal(&i));

To create a VectorView from an external collection using fidl::VectorView::FromExternal.

std::vector<uint32_t> vec = {1, 2, 3, 4};
fidl::VectorView<uint32_t> vv = fidl::VectorView<uint32_t>::FromExternal(vec);
ASSERT_EQ(vv.count(), 4UL);

To create a StringView from an external buffer using fidl::StringView::FromExternal.

const char* string = "hello";
fidl::StringView sv = fidl::StringView::FromExternal(string);
ASSERT_EQ(sv.size(), 5UL);

A StringView can also be created directly from string literals without using FromExternal.

fidl::StringView sv1 = "hello world";
fidl::StringView sv2("Hello");
ASSERT_EQ(sv1.size(), 11UL);
ASSERT_EQ(sv2.size(), 5UL);

fidl::StringView

Defined in lib/fidl/llcpp/string_view.h

Holds a reference to a variable-length string stored within the buffer. C++ wrapper of fidl_string. Does not own the memory of the contents.

fidl::StringView may be constructed by supplying the pointer and number of UTF-8 bytes (excluding trailing \0) separately. Alternatively, one could pass a C++ string literal, or any value that implements [const] char* data() and size(). The string view would borrow the contents of the container.

It is memory layout compatible with fidl_string.

fidl::VectorView<T>

Defined in lib/fidl/llcpp/vector_view.h

Holds a reference to a variable-length vector of elements stored within the buffer. C++ wrapper of fidl_vector. Does not own the memory of elements.

fidl::VectorView may be constructed by supplying the pointer and number of elements separately. Alternatively, one could pass any value that supports std::data, such as a standard container, or an array. The vector view would borrow the contents of the container.

It is memory layout compatible with fidl_vector.

fidl::Array<T, N>

Defined in lib/fidl/llcpp/array.h

Owns a fixed-length array of elements. Similar to std::array<T, N> but intended purely for in-place use.

It is memory layout compatible with FIDL arrays, and is standard-layout. The destructor closes handles if applicable e.g. it is an array of handles.