feat(libwebrtc): implement rtc::CopyOnWriteBuffer #16485
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16485"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-06T20:59:10Z.
What
Implements libwebrtc
rtc::CopyOnWriteBuffer's five exports, which threw throughNotImplemented_nid_no_patch:CopyOnWriteBuffer(size_t)(_ZN3rtc17CopyOnWriteBufferC1Em)sR-f7IaOCzcCopyOnWriteBuffer(size_t, size_t)(_ZN3rtc17CopyOnWriteBufferC1Emm)dxzEsYnE9lgCopyOnWriteBuffer(const CopyOnWriteBuffer&)(_ZN3rtc17CopyOnWriteBufferC1ERKS0_)ZRgDNVxmGck~CopyOnWriteBuffer()(_ZN3rtc17CopyOnWriteBufferD1Ev)jjig4x39rfkUnshareAndEnsureCapacity(size_t)(_ZN3rtc17CopyOnWriteBuffer24UnshareAndEnsureCapacityEm)wak9ck3EZGESources, all at M134 8d78f5de:
copy_on_write_buffer.ccand its header (RefCountedBuffer = FinalRefCountedObject<Buffer>, line 295);buffer.h(BufferT(size, capacity), lines 91–96);ref_counted_object.h(FinalRefCountedObject, lines 60–84);ref_counter.h(lines 29, 48 and 65).At M127 (e0b28a6a), the other end of the debt file's revision range, the
.cc,buffer.handref_counter.hare byte-identical. The header differs only by M134's added constbegin()/end().src/CopyOnWriteBuffer.cppstarts withcopy_on_write_buffer.cc's notice copied byte for byte, plus the WebRTC BSD licence. The bodies follow upstream:BufferT(size, size)only whensize > 0. The size-and-capacity constructor allocates when either is non-zero, with capacitymax(size, capacity)and the bytes left uninitialised, asnew T[capacity_]leaves them.delete thison aFinalRefCountedObject<BufferT<uint8_t, false>>does (no zeroing).UnshareAndEnsureCapacity: returns early when the buffer is unshared andnew_capacity <= capacity() - offset_. Otherwise it allocates a new buffer, copiessize_bytes fromoffset_, adds the new reference before dropping the old one (asscoped_refptrassignment does), and resetsoffset_to 0.ABI. Guest code inlines most of
CopyOnWriteBuffer, includingAddRef,ReleaseandHasOneRef, so the layout and allocator have to match what guest code does:Layouts:
CopyOnWriteBuffer:buffer_,offset_,size_at offsets 0, 8 and 16 (24 bytes);FinalRefCountedObject<Buffer>, which has no virtual functions:size_,capacity_,data_and the atomicref_count_at 0, 8, 16 and 24 (32 bytes).include/CopyOnWriteBuffer.hppchecks both withstatic_asserts.Reference count: updates use the memory orders
ref_counter.huses: relaxed increment, acq_rel decrement, acquire load forHasOneRef.Allocator: the backing object comes from the guest's
operator new/operator delete(_Znwm/_ZdlPv) and its bytes fromoperator new[]/operator delete[](_Znam/_ZdaPv), as libc'sStdioExtras.cppprovides them. So a buffer this module creates can be freed by guest-inlineddelete this, and the reverse. libc's sizedoperator deletefrees through the same heap, so either form of the guest's deletion works.Departures from upstream:
RTC_DCHECKs are debug-only and left out. A null object, or unsharing a buffer with no backing storage, throws where upstream would crash.Debt: a new entry under "Public source implementations" in
docs/dev/debt/libwebrtc.mdrecords these choices and that nothing is compared with the console. The inventory count is recounted fromExport.cpp: it becomes 330, althoughmainlists 339, because that figure was already 4 too high onmain. All five exports had names, so the unnamed figure stays 60.Tested
guest_webrtc_copy_on_write_buffer(core/libs/prx/libwebrtc/tests/CopyOnWriteBuffer.cpp), with the guest allocator intercepted so every allocation and free is accounted for:UnshareAndEnsureCapacity: a no-op when unshared and large enough, including from a slice; growth; detaching a shared buffer, after which modifying one copy leaves the other unchanged; detaching a slice copies from its offset and resets it.main's library, 18 existing tests pass and the new test fails with_ZN3rtc17CopyOnWriteBufferC1Em_nid_postfix not implemented;public_headerspasses (407 libraries, 30,759 exports).UnshareAndEnsureCapacityfails the new test.ctest -R webrtcpasses 19/19 at 284ba89852, with no warnings in the new files;guest_webrtc_copy_on_write_bufferpasses 20 consecutive repeats.python3 tools/check_conventions.py --base origin/main: all conventions checks passed.python3 tools/pr_overlap.py --branch feat/libwebrtc-copy-on-write-buffer: no open PR implements these exports. #15154 listsCopyOnWriteBufferamong the families it leaves unimplemented, and keeps only the CMake conflict it already has withmain.Export.cpp, because it removes the stub directly after aCopyOnWriteBufferconstructor.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code