11/* * Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU.
22 *
3- * Copyright 2024 -present Space and Time Labs, Inc.
3+ * Copyright 2025 -present Space and Time Labs, Inc.
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
1717#include " sxt/base/device/pinned_buffer.h"
1818
1919#include " sxt/base/device/pinned_buffer_pool.h"
20+ #include " sxt/base/error/assert.h"
2021
2122namespace sxt ::basdv {
2223// --------------------------------------------------------------------------------------------------
23- // consructor
24+ // constructor
2425// --------------------------------------------------------------------------------------------------
25- pinned_buffer::pinned_buffer () noexcept : handle_{get_pinned_buffer_pool ()->acquire_handle ()} {}
26-
27- pinned_buffer::pinned_buffer (pinned_buffer&& ptr) noexcept : handle_{ptr.handle_ } {
28- ptr.handle_ = nullptr ;
26+ pinned_buffer::pinned_buffer (size_t size) noexcept
27+ : handle_{get_pinned_buffer_pool ()->acquire_handle ()}, size_{size} {
28+ SXT_RELEASE_ASSERT (size_ <= this ->capacity ());
2929}
3030
31+ pinned_buffer::pinned_buffer (pinned_buffer&& other) noexcept
32+ : handle_{std::exchange (other.handle_ , nullptr )}, size_{std::exchange (other.size_ , 0 )} {}
33+
3134// --------------------------------------------------------------------------------------------------
3235// destructor
3336// --------------------------------------------------------------------------------------------------
@@ -40,17 +43,54 @@ pinned_buffer::~pinned_buffer() noexcept {
4043// --------------------------------------------------------------------------------------------------
4144// operator=
4245// --------------------------------------------------------------------------------------------------
43- pinned_buffer& pinned_buffer::operator =(pinned_buffer&& ptr) noexcept {
44- if (handle_ != nullptr ) {
45- get_pinned_buffer_pool ()->release_handle (handle_);
46- }
47- handle_ = ptr.handle_ ;
48- ptr.handle_ = nullptr ;
46+ pinned_buffer& pinned_buffer::operator =(pinned_buffer&& other) noexcept {
47+ this ->reset ();
48+ handle_ = std::exchange (other.handle_ , nullptr );
49+ size_ = std::exchange (other.size_ , 0 );
4950 return *this ;
5051}
5152
5253// --------------------------------------------------------------------------------------------------
53- // size
54+ // capacity
5455// --------------------------------------------------------------------------------------------------
55- size_t pinned_buffer::size () noexcept { return pinned_buffer_size; }
56+ size_t pinned_buffer::capacity () noexcept { return pinned_buffer_size; }
57+
58+ // --------------------------------------------------------------------------------------------------
59+ // resize
60+ // --------------------------------------------------------------------------------------------------
61+ void pinned_buffer::resize (size_t size) noexcept {
62+ SXT_RELEASE_ASSERT (size <= this ->capacity ());
63+ if (handle_ == nullptr ) {
64+ handle_ = get_pinned_buffer_pool ()->acquire_handle ();
65+ }
66+ size_ = size;
67+ }
68+
69+ // --------------------------------------------------------------------------------------------------
70+ // fill
71+ // --------------------------------------------------------------------------------------------------
72+ basct::cspan<std::byte> pinned_buffer::fill_from_host (basct::cspan<std::byte> src) noexcept {
73+ if (src.empty ()) {
74+ return src;
75+ }
76+ if (handle_ == nullptr ) {
77+ handle_ = get_pinned_buffer_pool ()->acquire_handle ();
78+ }
79+ auto n = std::min (src.size (), this ->capacity () - size_);
80+ std::copy_n (src.data (), n, static_cast <std::byte*>(handle_->ptr ) + size_);
81+ size_ += n;
82+ return src.subspan (n);
83+ }
84+
85+ // --------------------------------------------------------------------------------------------------
86+ // reset
87+ // --------------------------------------------------------------------------------------------------
88+ void pinned_buffer::reset () noexcept {
89+ if (handle_ == nullptr ) {
90+ return ;
91+ }
92+ get_pinned_buffer_pool ()->release_handle (handle_);
93+ handle_ = nullptr ;
94+ size_ = 0 ;
95+ }
5696} // namespace sxt::basdv
0 commit comments