1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
3 * This file is open source software, licensed to you under the terms
4 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
5 * distributed with this work for additional information regarding copyright
6 * ownership. You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 * Copyright (C) 2014 Cloudius Systems, Ltd.
23 * Ceph - scalable distributed file system
25 * Copyright (C) 2015 XSky <haomai@xsky.com>
27 * Author: Haomai Wang <haomaiwang@gmail.com>
29 * This is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU Lesser General Public
31 * License version 2.1, as published by the Free Software
32 * Foundation. See file COPYING.
43 constexpr size_t Packet::internal_data_size;
44 constexpr size_t Packet::default_nr_frags;
46 void Packet::linearize(size_t at_frag, size_t desired_size) {
47 _impl->unuse_internal_data();
49 size_t accum_size = 0;
50 while (accum_size < desired_size) {
51 accum_size += _impl->frags[at_frag + nr_frags].size;
54 char *new_frag = new char[accum_size];
56 for (size_t i = 0; i < nr_frags; ++i) {
57 auto& f = _impl->frags[at_frag + i];
58 p = std::copy(f.base, f.base + f.size, p);
60 // collapse nr_frags into one fragment
61 std::copy(_impl->frags + at_frag + nr_frags, _impl->frags + _impl->_nr_frags,
62 _impl->frags + at_frag + 1);
63 _impl->_nr_frags -= nr_frags - 1;
64 _impl->frags[at_frag] = fragment{new_frag, accum_size};
65 if (at_frag == 0 && desired_size == len()) {
66 // We can drop the old buffer safely
67 auto x = std::move(_impl->_deleter);
68 _impl->_deleter = make_deleter([new_frag] { delete []new_frag; });
71 [new_frag](deleter &d) { delete []new_frag; }, std::move(_impl->_deleter));
72 _impl->_deleter = make_deleter(std::move(del));
76 class C_free_on_cpu : public EventCallback {
78 std::function<void()> cb;
80 C_free_on_cpu(deleter &&d, std::function<void()> &&c):
81 del(std::move(d)), cb(std::move(c)) {}
82 void do_request(int fd) {
83 // deleter needs to be moved from lambda capture to be destroyed here
84 // otherwise deleter destructor will be called on a cpu that called
85 // create_external_event when work_item is destroyed.
86 deleter xxx(std::move(del));
92 Packet Packet::free_on_cpu(EventCenter *center, std::function<void()> cb)
95 [center, cb] (deleter &del) mutable {
96 center->dispatch_event_external(new C_free_on_cpu(std::move(del), std::move(cb)));
97 }, std::move(_impl->_deleter));
98 // make new deleter that runs old deleter on an origin cpu
99 _impl->_deleter = make_deleter(deleter(), std::move(del));
101 return Packet(impl::copy(_impl.get()));
104 std::ostream& operator<<(std::ostream& os, const Packet& p) {
107 for (auto&& frag : p.fragments()) {
112 if (std::all_of(frag.base, frag.base + frag.size, [] (int c) { return c >= 9 && c <= 0x7f; })) {
114 for (auto p = frag.base; p != frag.base + frag.size; ++p) {
118 } else if (c == '\r') {
120 } else if (c == '\n') {
122 } else if (c == '\t') {
126 os << "\\x" << (b / 16) << (b % 16);
133 for (auto p = frag.base; p != frag.base + frag.size; ++p) {