@@ -144,33 +144,40 @@ void DiskStorage::MarkAsFree(DiskSegment segment) {
144144 alloc_.Free (segment.offset , segment.length );
145145}
146146
147- std::error_code DiskStorage::Stash (io::Bytes bytes, StashCb cb ) {
148- DCHECK_GT (bytes. length (), 0u ) ;
147+ io::Result< std::pair< size_t , UringBuf>> DiskStorage::PrepareStash ( size_t length ) {
148+ using namespace nonstd ;
149149
150- size_t len = bytes.size ();
151- int64_t offset = alloc_.Malloc (len);
150+ int64_t offset = alloc_.Malloc (length);
151+ if (offset >= 0 )
152+ return std::make_pair (offset, PrepareBuf (length));
152153
153154 // If we don't have enough space, request grow and return to avoid blocking
154155 if (offset < 0 ) {
155156 auto ec = RequestGrow (-offset);
156- return ec ? ec : make_error_code (errc::operation_would_block);
157+ return make_unexpected ( ec ? ec : make_error_code (errc::operation_would_block) );
157158 }
158159
159- UringBuf buf = PrepareBuf (len);
160- memcpy (buf.bytes .data (), bytes.data (), bytes.length ());
160+ offset = alloc_.Malloc (length);
161+ if (offset < 0 ) // we can't fit it even after resizing
162+ return make_unexpected (make_error_code (errc::file_too_large));
163+
164+ return std::make_pair (offset, PrepareBuf (length));
165+ }
161166
162- auto io_cb = [this , cb, offset, buf, len](int io_res) {
167+ void DiskStorage::Stash (DiskSegment segment, UringBuf buf, StashCb cb) {
168+ auto io_cb = [this , cb, buf, segment](int io_res) {
163169 if (io_res < 0 ) {
164- MarkAsFree ({ size_t (offset), len} );
165- cb (nonstd::make_unexpected ( error_code{-io_res, std::system_category ()}) );
170+ MarkAsFree (segment );
171+ cb (error_code{-io_res, std::system_category ()});
166172 } else {
167- cb (DiskSegment{ size_t (offset), len });
173+ cb ({ });
168174 }
169175 ReturnBuf (buf);
170176 pending_ops_--;
171177 };
172178
173179 pending_ops_++;
180+ size_t offset = segment.offset ;
174181 if (buf.buf_idx )
175182 backing_file_->WriteFixedAsync (buf.bytes , offset, *buf.buf_idx , std::move (io_cb));
176183 else
@@ -183,8 +190,6 @@ std::error_code DiskStorage::Stash(io::Bytes bytes, StashCb cb) {
183190 auto ec = RequestGrow (256_MB);
184191 LOG_IF (ERROR, ec && ec != errc::file_too_large) << " Could not call grow :" << ec.message ();
185192 }
186-
187- return {}; // Must succeed after the operation was scheduled to run cleanup
188193}
189194
190195DiskStorage::Stats DiskStorage::GetStats () const {
0 commit comments