]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/src/core/vla.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / src / core / vla.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
20 */
21
22 #pragma once
23
24 #include <seastar/core/aligned_buffer.hh>
25 #include <memory>
26 #include <new>
27 #include <assert.h>
28 #include <type_traits>
29
30 namespace seastar {
31
32 // Some C APIs have a structure with a variable length array at the end.
33 // This is a helper function to help allocate it.
34 //
35 // for a structure
36 //
37 // struct xx { int a; float b[0]; };
38 //
39 // use
40 //
41 // make_struct_with_vla(&xx::b, number_of_bs);
42 //
43 // to allocate it.
44 //
45 template <class S, typename E>
46 inline
47 std::unique_ptr<S, free_deleter>
48 make_struct_with_vla(E S::*last, size_t nr) {
49 auto fake = reinterpret_cast<S*>(0);
50 size_t offset = reinterpret_cast<uintptr_t>(&(fake->*last));
51 size_t element_size = sizeof((fake->*last)[0]);
52 assert(offset == sizeof(S));
53 auto p = std::unique_ptr<char, free_deleter>(
54 reinterpret_cast<char*>(::malloc(offset + element_size * nr)));
55 auto s = std::unique_ptr<S, free_deleter>(new (p.get()) S());
56 p.release();
57 return s;
58 }
59
60 }