Implementation:Alibaba MNN Protobuf Repeated Ptr Field H
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/repeated_ptr_field.h
|
| Line Count | 2014 |
| Domains | Serialization, Data_Structures |
| Key Class | RepeatedPtrField<T>
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Repeated pointer field container for message and string types. Manages an array of heap-allocated or arena-allocated objects, providing ownership semantics and efficient reuse of cleared elements.
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
RepeatedPtrField<T> is the container used by generated protobuf code for repeated fields of message, string, and bytes types. Unlike RepeatedField<T> which stores values inline, this class stores pointers to heap-allocated objects and provides:
- Object reuse -- When
Clear()is called or elements are removed, the underlying objects are not deallocated but retained in a "cleared" pool. SubsequentAdd()calls reuse these objects, avoiding repeated allocation/deallocation cycles during iterative parsing. - Arena support -- When used with an arena, all element objects are arena-allocated. Destruction becomes a no-op since the arena owns the memory.
- Ownership transfer --
AddAllocatedandReleaseLastsupport transferring ownership of elements into and out of the container. - STL compatibility -- Provides iterators,
size(),empty(),operator[], and range support. - Type erasure -- Internally uses
RepeatedPtrFieldBaseto share code across all element types, with type-specific operations viaTypeHandlertraits.
Usage
#include "google/protobuf/repeated_ptr_field.h"
Included by all generated headers that contain repeated message or string fields.
Code Reference
template <typename Element>
class RepeatedPtrField : private internal::RepeatedPtrFieldBase {
public:
RepeatedPtrField();
explicit RepeatedPtrField(Arena* arena);
~RepeatedPtrField();
// Element access.
const Element& Get(int index) const;
Element* Mutable(int index);
Element* Add();
void Add(Element&& value);
// Size management.
int size() const;
bool empty() const;
void Clear();
void Reserve(int new_size);
// Ownership transfer.
void AddAllocated(Element* value);
Element* ReleaseLast();
void UnsafeArenaAddAllocated(Element* value);
// Extract and replace all elements.
void ExtractSubrange(int start, int num, Element** elements);
// STL iterator support.
typedef internal::RepeatedPtrIterator<Element> iterator;
typedef internal::RepeatedPtrIterator<const Element> const_iterator;
iterator begin();
iterator end();
// Merge and swap.
void MergeFrom(const RepeatedPtrField& other);
void Swap(RepeatedPtrField* other);
// Delete all elements (when not using arena).
void DeleteSubrange(int start, int num);
};
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | Message/string objects | Elements added via Add() or AddAllocated()
|
| Output | Pointer array | Provides access to owned objects via iterators or index |
| Memory | Arena or heap | Arena allocation avoids per-element deallocation; cleared objects are reused |
Usage Examples
// Working with repeated message fields
RepeatedPtrField<SubMessage> field;
SubMessage* msg = field.Add();
msg->set_id(42);
msg->set_name("example");
// Iteration
for (const SubMessage& m : field) {
std::cout << m.id() << ": " << m.name() << std::endl;
}
// Ownership transfer
SubMessage* detached = field.ReleaseLast();
// caller now owns detached