Internal change.

PiperOrigin-RevId: 837829087
Change-Id: I110f2f9d1dff2aff8bcad7ac22ea8ed82323185f
This commit is contained in:
Matija Kecman
2025-11-28 06:35:11 -08:00
committed by Copybara-Service
parent 7caa3d6703
commit 69179ead48
+14 -2
View File
@@ -52,7 +52,7 @@ class WasmBuffer {
public:
// Creates a buffer with the given element count
explicit WasmBuffer(int element_count = 0) {
bytes_.resize(element_count * sizeof(T));
Resize(element_count);
}
// Creates a buffer by copying data from a (typed) array
@@ -60,13 +60,25 @@ class WasmBuffer {
return WasmBuffer<T>(array);
}
// Resizes the buffer to the given element count.
// If element count is zero the memory is released.
void Resize(int element_count) {
if (element_count == 0) {
std::vector<std::byte> empty;
bytes_.swap(empty);
} else {
bytes_.resize(element_count * sizeof(T));
}
}
// Returns the pointer to the data in the buffer
uintptr_t GetPointer() { return reinterpret_cast<uintptr_t>(bytes_.data()); }
// Returns the number of elements in the buffer
int GetElementCount() { return bytes_.size() / sizeof(T); }
// Returns a TypedArray view of the buffer
// Returns a TypedArray view of the buffer.
// Do not cache this value, bytes_.data() is invalidated on Resize!
emscripten::val GetView() {
return emscripten::val(emscripten::typed_memory_view(
bytes_.size() / sizeof(T), reinterpret_cast<const T*>(bytes_.data())));