/root/doris/be/src/util/byte_buffer.h
| Line | Count | Source | 
| 1 |  | // Licensed to the Apache Software Foundation (ASF) under one | 
| 2 |  | // or more contributor license agreements.  See the NOTICE file | 
| 3 |  | // distributed with this work for additional information | 
| 4 |  | // regarding copyright ownership.  The ASF licenses this file | 
| 5 |  | // to you under the Apache License, Version 2.0 (the | 
| 6 |  | // "License"); you may not use this file except in compliance | 
| 7 |  | // with the License.  You may obtain a copy of the License at | 
| 8 |  | // | 
| 9 |  | //   http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  | // | 
| 11 |  | // Unless required by applicable law or agreed to in writing, | 
| 12 |  | // software distributed under the License is distributed on an | 
| 13 |  | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
| 14 |  | // KIND, either express or implied.  See the License for the | 
| 15 |  | // specific language governing permissions and limitations | 
| 16 |  | // under the License. | 
| 17 |  |  | 
| 18 |  | #pragma once | 
| 19 |  |  | 
| 20 |  | #include <string.h> | 
| 21 |  |  | 
| 22 |  | #include <cstddef> | 
| 23 |  | #include <memory> | 
| 24 |  |  | 
| 25 |  | #include "common/logging.h" | 
| 26 |  | #include "common/status.h" | 
| 27 |  | #include "runtime/thread_context.h" | 
| 28 |  | #include "vec/common/allocator.h" | 
| 29 |  | #include "vec/common/allocator_fwd.h" | 
| 30 |  |  | 
| 31 |  | namespace doris { | 
| 32 |  |  | 
| 33 |  | struct ByteBuffer; | 
| 34 |  | using ByteBufferPtr = std::shared_ptr<ByteBuffer>; | 
| 35 |  |  | 
| 36 |  | struct ByteBuffer : private Allocator<false> { | 
| 37 | 9 |     static Status allocate(const size_t size, ByteBufferPtr* ptr) { | 
| 38 | 9 |         RETURN_IF_CATCH_EXCEPTION({ *ptr = ByteBufferPtr(new ByteBuffer(size)); }); | 
| 39 | 9 |         return Status::OK(); | 
| 40 | 9 |     } | 
| 41 |  |  | 
| 42 | 9 |     ~ByteBuffer() { | 
| 43 | 9 |         SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(mem_tracker_); | 
| 44 | 9 |         Allocator<false>::free(ptr, capacity); | 
| 45 | 9 |     } | 
| 46 |  |  | 
| 47 | 9 |     void put_bytes(const char* data, size_t size) { | 
| 48 | 9 |         memcpy(ptr + pos, data, size); | 
| 49 | 9 |         pos += size; | 
| 50 | 9 |     } | 
| 51 |  |  | 
| 52 | 0 |     void get_bytes(char* data, size_t size) { | 
| 53 | 0 |         memcpy(data, ptr + pos, size); | 
| 54 | 0 |         pos += size; | 
| 55 | 0 |         DCHECK(pos <= limit); | 
| 56 | 0 |     } | 
| 57 |  |  | 
| 58 | 9 |     void flip() { | 
| 59 | 9 |         limit = pos; | 
| 60 | 9 |         pos = 0; | 
| 61 | 9 |     } | 
| 62 |  |  | 
| 63 | 15 |     size_t remaining() const { return limit - pos; } | 
| 64 | 0 |     bool has_remaining() const { return limit > pos; } | 
| 65 |  |  | 
| 66 |  |     char* ptr; | 
| 67 |  |     size_t pos; | 
| 68 |  |     size_t limit; | 
| 69 |  |     size_t capacity; | 
| 70 |  |  | 
| 71 |  | private: | 
| 72 |  |     ByteBuffer(size_t capacity_) | 
| 73 | 9 |             : pos(0), | 
| 74 | 9 |               limit(capacity_), | 
| 75 | 9 |               capacity(capacity_), | 
| 76 |  |               mem_tracker_( | 
| 77 | 9 |                       doris::thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker_sptr()) { | 
| 78 | 9 |         ptr = reinterpret_cast<char*>(Allocator<false>::alloc(capacity_)); | 
| 79 | 9 |     } | 
| 80 |  |  | 
| 81 |  |     std::shared_ptr<MemTrackerLimiter> mem_tracker_; | 
| 82 |  | }; | 
| 83 |  |  | 
| 84 |  | } // namespace doris |