]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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) 20127 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 <atomic> | |
19 | #include <map> | |
20 | #include <utility> | |
21 | #include <type_traits> | |
22 | #include "include/buffer.h" | |
23 | #include "include/mempool.h" | |
24 | #include "include/spinlock.h" | |
25 | ||
26 | namespace ceph::buffer { | |
494da23a TL |
27 | inline namespace v14_2_0 { |
28 | ||
11fdf7f2 TL |
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 | std::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 | virtual bool is_shareable() const { | |
97 | // true if safe to reference/share the existing buffer copy | |
98 | // false if it is not safe to share the buffer, e.g., due to special | |
99 | // and/or registered memory that is scarce | |
100 | return true; | |
101 | } | |
102 | bool get_crc(const std::pair<size_t, size_t> &fromto, | |
103 | std::pair<uint32_t, uint32_t> *crc) const { | |
104 | std::lock_guard lg(crc_spinlock); | |
105 | if (last_crc_offset == fromto) { | |
106 | *crc = last_crc_val; | |
107 | return true; | |
108 | } | |
109 | return false; | |
110 | } | |
111 | void set_crc(const std::pair<size_t, size_t> &fromto, | |
112 | const std::pair<uint32_t, uint32_t> &crc) { | |
113 | std::lock_guard lg(crc_spinlock); | |
114 | last_crc_offset = fromto; | |
115 | last_crc_val = crc; | |
116 | } | |
117 | void invalidate_crc() { | |
118 | std::lock_guard lg(crc_spinlock); | |
119 | last_crc_offset.first = std::numeric_limits<size_t>::max(); | |
120 | last_crc_offset.second = std::numeric_limits<size_t>::max(); | |
121 | } | |
122 | }; | |
494da23a TL |
123 | |
124 | } // inline namespace v14_2_0 | |
11fdf7f2 TL |
125 | } // namespace ceph::buffer |
126 | ||
127 | #endif // CEPH_BUFFER_RAW_H |