]> git.proxmox.com Git - ceph.git/blame - ceph/src/include/buffer_raw.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / include / buffer_raw.h
CommitLineData
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 *
9f95a23c 6 * Copyright (C) 2017 Red Hat, Inc.
11fdf7f2
TL
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
11fdf7f2
TL
18#include <map>
19#include <utility>
20#include <type_traits>
9f95a23c 21#include "common/ceph_atomic.h"
11fdf7f2
TL
22#include "include/buffer.h"
23#include "include/mempool.h"
24#include "include/spinlock.h"
25
26namespace ceph::buffer {
9f95a23c 27inline namespace v15_2_0 {
494da23a 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;
f67539c2 35 protected:
11fdf7f2
TL
36 char *data;
37 unsigned len;
f67539c2 38 public:
9f95a23c 39 ceph::atomic<unsigned> nref { 0 };
11fdf7f2
TL
40 int mempool;
41
42 std::pair<size_t, size_t> last_crc_offset {std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};
43 std::pair<uint32_t, uint32_t> last_crc_val;
44
45 mutable ceph::spinlock crc_spinlock;
46
47 explicit raw(unsigned l, int mempool=mempool::mempool_buffer_anon)
48 : data(nullptr), len(l), nref(0), mempool(mempool) {
49 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
50 }
51 raw(char *c, unsigned l, int mempool=mempool::mempool_buffer_anon)
52 : data(c), len(l), nref(0), mempool(mempool) {
53 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
54 }
55 virtual ~raw() {
56 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
57 -1, -(int)len);
58 }
59
60 void _set_len(unsigned l) {
61 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
62 -1, -(int)len);
63 len = l;
64 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
65 }
66
67 void reassign_to_mempool(int pool) {
68 if (pool == mempool) {
69 return;
70 }
71 mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
72 -1, -(int)len);
73 mempool = pool;
74 mempool::get_pool(mempool::pool_index_t(pool)).adjust_count(1, len);
75 }
76
77 void try_assign_to_mempool(int pool) {
78 if (mempool == mempool::mempool_buffer_anon) {
79 reassign_to_mempool(pool);
80 }
81 }
82
83private:
84 // no copying.
85 // cppcheck-suppress noExplicitConstructor
86 raw(const raw &other) = delete;
87 const raw& operator=(const raw &other) = delete;
88public:
f67539c2 89 char *get_data() const {
11fdf7f2
TL
90 return data;
91 }
f67539c2
TL
92 unsigned get_len() const {
93 return len;
94 }
11fdf7f2
TL
95 virtual raw* clone_empty() = 0;
96 ceph::unique_leakable_ptr<raw> clone() {
97 raw* const c = clone_empty();
98 memcpy(c->data, data, len);
99 return ceph::unique_leakable_ptr<raw>(c);
100 }
11fdf7f2
TL
101 bool get_crc(const std::pair<size_t, size_t> &fromto,
102 std::pair<uint32_t, uint32_t> *crc) const {
103 std::lock_guard lg(crc_spinlock);
104 if (last_crc_offset == fromto) {
105 *crc = last_crc_val;
106 return true;
107 }
108 return false;
109 }
110 void set_crc(const std::pair<size_t, size_t> &fromto,
111 const std::pair<uint32_t, uint32_t> &crc) {
112 std::lock_guard lg(crc_spinlock);
113 last_crc_offset = fromto;
114 last_crc_val = crc;
115 }
116 void invalidate_crc() {
117 std::lock_guard lg(crc_spinlock);
118 last_crc_offset.first = std::numeric_limits<size_t>::max();
119 last_crc_offset.second = std::numeric_limits<size_t>::max();
120 }
121 };
494da23a 122
9f95a23c 123} // inline namespace v15_2_0
11fdf7f2
TL
124} // namespace ceph::buffer
125
126#endif // CEPH_BUFFER_RAW_H