Skip to content

Introduce MetadataBytes wrapper for compiled-in metadata accessors#4016

Open
hjanuschka wants to merge 1 commit into
google:masterfrom
hjanuschka:chromium-metadata-bytes-wrapper
Open

Introduce MetadataBytes wrapper for compiled-in metadata accessors#4016
hjanuschka wants to merge 1 commit into
google:masterfrom
hjanuschka:chromium-metadata-bytes-wrapper

Conversation

@hjanuschka

@hjanuschka hjanuschka commented Jun 20, 2026

Copy link
Copy Markdown

Summary

Introduces a small MetadataBytes RAII wrapper for the compiled-in metadata
buffers returned by the internal metadata.h, short_metadata.h and
alternate_format.h accessors.

The C-style API

int metadata_size();
const void* metadata_get();

is replaced by

MetadataBytes GetMetadata();   // and GetShortMetadata(), GetAlternateFormat()

where MetadataBytes is a move-only handle that holds either:

  1. a non-owning const void* + int size, or
  2. a std::unique_ptr<uint8_t[]> + int size (owning).

The default implementations shipped in this repo continue to wrap the
existing static const unsigned char data[] arrays with a non-owning
instance, so on-disk layout and runtime behaviour are unchanged for
upstream consumers. CppMetadataGenerator (and its tests) are updated to
emit the new API.

Backwards compatibility

These headers are library-internal (not part of the public API).

Related

Chromium CL discussion that motivated this:
https://chromium-review.googlesource.com/c/chromium/src/+/7928952

Replace the C-style `int <type>_size()` / `const void* <type>_get()` pair
in metadata.h, short_metadata.h and alternate_format.h with a single
`MetadataBytes Get<Type>()` accessor that returns a small RAII wrapper.

The default implementations shipped in this repo wrap the existing
static `data[]` arrays in a non-owning MetadataBytes, so the on-disk
layout and runtime behaviour are unchanged for upstream consumers.

The motivating use case is downstream embedders (e.g. Chromium) that
produce the metadata at runtime, for example by decompressing a
brotli-compressed blob on first use to save binary size. With the old
API such embedders had to either leak the decompressed buffer or do
gymnastics to free it after PhoneNumberUtil parsed it. With
MetadataBytes the typical call site

    MetadataBytes bytes = GetMetadata();
    collection->ParseFromArray(bytes.data(), bytes.size());

keeps the buffer alive for exactly as long as ParseFromArray() needs it
and then frees it when the wrapper goes out of scope, simply by having
the downstream-supplied implementation return an owning instance built
from a std::unique_ptr<uint8_t[]>.

CppMetadataGenerator and its tests are updated to emit the new API.
@hjanuschka hjanuschka requested a review from a team as a code owner June 20, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant