]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer: move buffer::raw to new header
authorCasey Bodley <cbodley@redhat.com>
Fri, 3 Nov 2017 23:06:31 +0000 (19:06 -0400)
committerKefu Chai <kchai@redhat.com>
Fri, 8 Jun 2018 12:58:15 +0000 (20:58 +0800)
this allows new raw buffer types to live outside of buffer.cc, and
potentially outside of libcommon entirely

Signed-off-by: Casey Bodley <cbodley@redhat.com>
src/common/buffer.cc
src/include/buffer_raw.h [new file with mode: 0644]

index 5220130a5dcc93956e8d786324666d43d3a74af4..b60fbe99ff50be8796a7b99e3e2d82017804571a 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <sys/uio.h>
 
+#include "include/buffer_raw.h"
 #include "include/compat.h"
 #include "include/mempool.h"
 #include "armor.h"
@@ -166,109 +167,6 @@ using namespace ceph;
   buffer::error_code::error_code(int error) :
     buffer::malformed_input(cpp_strerror(error).c_str()), code(error) {}
 
-  class buffer::raw {
-  public:
-    char *data;
-    unsigned len;
-    std::atomic<unsigned> nref { 0 };
-    int mempool;
-
-    std::pair<size_t, size_t> last_crc_offset {std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};
-    std::pair<uint32_t, uint32_t> last_crc_val;
-
-    mutable ceph::spinlock crc_spinlock;
-
-    explicit raw(unsigned l, int mempool=mempool::mempool_buffer_anon)
-      : data(NULL), len(l), nref(0), mempool(mempool) {
-      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
-    }
-    raw(char *c, unsigned l, int mempool=mempool::mempool_buffer_anon)
-      : data(c), len(l), nref(0), mempool(mempool) {
-      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
-    }
-    virtual ~raw() {
-      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
-       -1, -(int)len);
-    }
-
-    void _set_len(unsigned l) {
-      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
-       -1, -(int)len);
-      len = l;
-      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
-    }
-
-    void reassign_to_mempool(int pool) {
-      if (pool == mempool) {
-       return;
-      }
-      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
-       -1, -(int)len);
-      mempool = pool;
-      mempool::get_pool(mempool::pool_index_t(pool)).adjust_count(1, len);
-    }
-
-    void try_assign_to_mempool(int pool) {
-      if (mempool == mempool::mempool_buffer_anon) {
-       reassign_to_mempool(pool);
-      }
-    }
-
-private:
-    // no copying.
-    // cppcheck-suppress noExplicitConstructor
-    raw(const raw &other) = delete;
-    const raw& operator=(const raw &other) = delete;
-public:
-    virtual char *get_data() {
-      return data;
-    }
-    virtual raw* clone_empty() = 0;
-    raw *clone() {
-      raw *c = clone_empty();
-      memcpy(c->data, data, len);
-      return c;
-    }
-    virtual bool can_zero_copy() const {
-      return false;
-    }
-    virtual int zero_copy_to_fd(int fd, loff_t *offset) {
-      return -ENOTSUP;
-    }
-    virtual bool is_page_aligned() {
-      return ((long)data & ~CEPH_PAGE_MASK) == 0;
-    }
-    bool is_n_page_sized() {
-      return (len & ~CEPH_PAGE_MASK) == 0;
-    }
-    virtual bool is_shareable() {
-      // true if safe to reference/share the existing buffer copy
-      // false if it is not safe to share the buffer, e.g., due to special
-      // and/or registered memory that is scarce
-      return true;
-    }
-    bool get_crc(const pair<size_t, size_t> &fromto,
-         pair<uint32_t, uint32_t> *crc) const {
-      std::lock_guard<decltype(crc_spinlock)> lg(crc_spinlock);
-      if (last_crc_offset == fromto) {
-        *crc = last_crc_val;
-        return true;
-      }
-      return false;
-    }
-    void set_crc(const pair<size_t, size_t> &fromto,
-         const pair<uint32_t, uint32_t> &crc) {
-      std::lock_guard<decltype(crc_spinlock)> lg(crc_spinlock);
-      last_crc_offset = fromto;
-      last_crc_val = crc;
-    }
-    void invalidate_crc() {
-      std::lock_guard<decltype(crc_spinlock)> lg(crc_spinlock);
-      last_crc_offset.first = std::numeric_limits<size_t>::max();
-      last_crc_offset.second = std::numeric_limits<size_t>::max();
-    }
-  };
-
   /*
    * raw_combined is always placed within a single allocation along
    * with the data buffer.  the data goes at the beginning, and
diff --git a/src/include/buffer_raw.h b/src/include/buffer_raw.h
new file mode 100644 (file)
index 0000000..0c6513c
--- /dev/null
@@ -0,0 +1,130 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 20127 Red Hat, Inc.
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#ifndef CEPH_BUFFER_RAW_H
+#define CEPH_BUFFER_RAW_H
+
+#include <atomic>
+#include <map>
+#include <utility>
+#include "include/buffer.h"
+#include "include/mempool.h"
+#include "include/spinlock.h"
+
+namespace ceph::buffer {
+  class raw {
+  public:
+    char *data;
+    unsigned len;
+    std::atomic<unsigned> nref { 0 };
+    int mempool;
+
+    std::pair<size_t, size_t> last_crc_offset {std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};
+    std::pair<uint32_t, uint32_t> last_crc_val;
+
+    mutable ceph::spinlock crc_spinlock;
+
+    explicit raw(unsigned l, int mempool=mempool::mempool_buffer_anon)
+      : data(NULL), len(l), nref(0), mempool(mempool) {
+      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
+    }
+    raw(char *c, unsigned l, int mempool=mempool::mempool_buffer_anon)
+      : data(c), len(l), nref(0), mempool(mempool) {
+      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
+    }
+    virtual ~raw() {
+      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
+       -1, -(int)len);
+    }
+
+    void _set_len(unsigned l) {
+      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
+       -1, -(int)len);
+      len = l;
+      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
+    }
+
+    void reassign_to_mempool(int pool) {
+      if (pool == mempool) {
+       return;
+      }
+      mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(
+       -1, -(int)len);
+      mempool = pool;
+      mempool::get_pool(mempool::pool_index_t(pool)).adjust_count(1, len);
+    }
+
+    void try_assign_to_mempool(int pool) {
+      if (mempool == mempool::mempool_buffer_anon) {
+       reassign_to_mempool(pool);
+      }
+    }
+
+private:
+    // no copying.
+    // cppcheck-suppress noExplicitConstructor
+    raw(const raw &other) = delete;
+    const raw& operator=(const raw &other) = delete;
+public:
+    virtual char *get_data() {
+      return data;
+    }
+    virtual raw* clone_empty() = 0;
+    raw *clone() {
+      raw *c = clone_empty();
+      memcpy(c->data, data, len);
+      return c;
+    }
+    virtual bool can_zero_copy() const {
+      return false;
+    }
+    virtual int zero_copy_to_fd(int fd, loff_t *offset) {
+      return -ENOTSUP;
+    }
+    virtual bool is_page_aligned() {
+      return ((long)data & ~CEPH_PAGE_MASK) == 0;
+    }
+    bool is_n_page_sized() {
+      return (len & ~CEPH_PAGE_MASK) == 0;
+    }
+    virtual bool is_shareable() {
+      // true if safe to reference/share the existing buffer copy
+      // false if it is not safe to share the buffer, e.g., due to special
+      // and/or registered memory that is scarce
+      return true;
+    }
+    bool get_crc(const std::pair<size_t, size_t> &fromto,
+                std::pair<uint32_t, uint32_t> *crc) const {
+      std::lock_guard lg(crc_spinlock);
+      if (last_crc_offset == fromto) {
+        *crc = last_crc_val;
+        return true;
+      }
+      return false;
+    }
+    void set_crc(const std::pair<size_t, size_t> &fromto,
+                const std::pair<uint32_t, uint32_t> &crc) {
+      std::lock_guard lg(crc_spinlock);
+      last_crc_offset = fromto;
+      last_crc_val = crc;
+    }
+    void invalidate_crc() {
+      std::lock_guard lg(crc_spinlock);
+      last_crc_offset.first = std::numeric_limits<size_t>::max();
+      last_crc_offset.second = std::numeric_limits<size_t>::max();
+    }
+  };
+} // namespace ceph::buffer
+
+#endif // CEPH_BUFFER_RAW_H