]> git.proxmox.com Git - ceph.git/blob - ceph/src/include/buffer_raw.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / include / buffer_raw.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2017 Red Hat, Inc.
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef CEPH_BUFFER_RAW_H
16 #define CEPH_BUFFER_RAW_H
17
18 #include <map>
19 #include <utility>
20 #include <type_traits>
21 #include "common/ceph_atomic.h"
22 #include "include/buffer.h"
23 #include "include/mempool.h"
24 #include "include/spinlock.h"
25
26 namespace ceph::buffer {
27 inline namespace v15_2_0 {
28
29 class raw {
30 public:
31 // In the future we might want to have a slab allocator here with few
32 // embedded slots. This would allow to avoid the "if" in dtor of ptr_node.
33 std::aligned_storage<sizeof(ptr_node),
34 alignof(ptr_node)>::type bptr_storage;
35 char *data;
36 unsigned len;
37 ceph::atomic<unsigned> nref { 0 };
38 int mempool;
39
40 std::pair<size_t, size_t> last_crc_offset {std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};
41 std::pair<uint32_t, uint32_t> last_crc_val;
42
43 mutable ceph::spinlock crc_spinlock;
44
45 explicit raw(unsigned l, int mempool=mempool::mempool_buffer_anon)
46 : data(nullptr), len(l), nref(0), mempool(mempool) {
47 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
48 }
49 raw(char *c, unsigned l, int mempool=mempool::mempool_buffer_anon)
50 : data(c), len(l), nref(0), mempool(mempool) {
51 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
52 }
53 virtual ~raw() {
54 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
55 -1, -(int)len);
56 }
57
58 void _set_len(unsigned l) {
59 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
60 -1, -(int)len);
61 len = l;
62 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
63 }
64
65 void reassign_to_mempool(int pool) {
66 if (pool == mempool) {
67 return;
68 }
69 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
70 -1, -(int)len);
71 mempool = pool;
72 mempool::get_pool(mempool::pool_index_t(pool)).adjust_count(1, len);
73 }
74
75 void try_assign_to_mempool(int pool) {
76 if (mempool == mempool::mempool_buffer_anon) {
77 reassign_to_mempool(pool);
78 }
79 }
80
81 private:
82 // no copying.
83 // cppcheck-suppress noExplicitConstructor
84 raw(const raw &other) = delete;
85 const raw& operator=(const raw &other) = delete;
86 public:
87 char *get_data() {
88 return data;
89 }
90 virtual raw* clone_empty() = 0;
91 ceph::unique_leakable_ptr<raw> clone() {
92 raw* const c = clone_empty();
93 memcpy(c->data, data, len);
94 return ceph::unique_leakable_ptr<raw>(c);
95 }
96 bool get_crc(const std::pair<size_t, size_t> &fromto,
97 std::pair<uint32_t, uint32_t> *crc) const {
98 std::lock_guard lg(crc_spinlock);
99 if (last_crc_offset == fromto) {
100 *crc = last_crc_val;
101 return true;
102 }
103 return false;
104 }
105 void set_crc(const std::pair<size_t, size_t> &fromto,
106 const std::pair<uint32_t, uint32_t> &crc) {
107 std::lock_guard lg(crc_spinlock);
108 last_crc_offset = fromto;
109 last_crc_val = crc;
110 }
111 void invalidate_crc() {
112 std::lock_guard lg(crc_spinlock);
113 last_crc_offset.first = std::numeric_limits<size_t>::max();
114 last_crc_offset.second = std::numeric_limits<size_t>::max();
115 }
116 };
117
118 } // inline namespace v15_2_0
119 } // namespace ceph::buffer
120
121 #endif // CEPH_BUFFER_RAW_H