]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2017 Red Hat, Inc.
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.
15 #ifndef CEPH_BUFFER_RAW_H
16 #define CEPH_BUFFER_RAW_H
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"
26 namespace ceph::buffer
{
27 inline namespace v15_2_0
{
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
;
37 ceph::atomic
<unsigned> nref
{ 0 };
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
;
43 mutable ceph::spinlock crc_spinlock
;
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
);
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
);
54 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(
58 void _set_len(unsigned l
) {
59 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(
62 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(1, len
);
65 void reassign_to_mempool(int pool
) {
66 if (pool
== mempool
) {
69 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(
72 mempool::get_pool(mempool::pool_index_t(pool
)).adjust_count(1, len
);
75 void try_assign_to_mempool(int pool
) {
76 if (mempool
== mempool::mempool_buffer_anon
) {
77 reassign_to_mempool(pool
);
83 // cppcheck-suppress noExplicitConstructor
84 raw(const raw
&other
) = delete;
85 const raw
& operator=(const raw
&other
) = delete;
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
);
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
) {
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
;
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();
118 } // inline namespace v15_2_0
119 } // namespace ceph::buffer
121 #endif // CEPH_BUFFER_RAW_H