1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #ifndef GRPC_SRC_CORE_LIB_SLICE_SLICE_INTERNAL_H
20 #define GRPC_SRC_CORE_LIB_SLICE_SLICE_INTERNAL_H
21
22 #include <grpc/slice.h>
23 #include <grpc/support/port_platform.h>
24 #include <stdint.h>
25
26 #include <cstddef>
27 #include <string>
28
29 #include "absl/hash/hash.h"
30 #include "absl/log/check.h"
31 #include "absl/strings/string_view.h"
32 #include "src/core/util/memory.h"
33
34 // Returns a pointer to the first slice in the slice buffer without giving
35 // ownership to or a reference count on that slice.
grpc_slice_buffer_peek_first(grpc_slice_buffer * sb)36 inline grpc_slice* grpc_slice_buffer_peek_first(grpc_slice_buffer* sb) {
37 DCHECK_GT(sb->count, 0u);
38 return &sb->slices[0];
39 }
40
41 // Removes the first slice from the slice buffer.
42 void grpc_slice_buffer_remove_first(grpc_slice_buffer* sb);
43
44 // Calls grpc_slice_sub with the given parameters on the first slice.
45 void grpc_slice_buffer_sub_first(grpc_slice_buffer* sb, size_t begin,
46 size_t end);
47
48 // if slice matches a static slice, returns the static slice
49 // otherwise returns the passed in slice (without reffing it)
50 // used for surface boundaries where we might receive an un-interned static
51 // string
52 grpc_slice grpc_slice_maybe_static_intern(grpc_slice slice,
53 bool* returned_slice_is_different);
54 uint32_t grpc_static_slice_hash(grpc_slice s);
55 int grpc_static_slice_eq(grpc_slice a, grpc_slice b);
56
57 grpc_slice grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char> p,
58 size_t len);
59 grpc_slice grpc_slice_from_moved_string(grpc_core::UniquePtr<char> p);
60 grpc_slice grpc_slice_from_cpp_string(std::string str);
61
62 // Returns the memory used by this slice, not counting the slice structure
63 // itself. This means that inlined and slices from static strings will return
64 // 0. All other slices will return the size of the allocated chars.
65 size_t grpc_slice_memory_usage(grpc_slice s);
66
67 grpc_slice grpc_slice_split_tail_maybe_ref_no_inline(
68 grpc_slice* source, size_t split, grpc_slice_ref_whom ref_whom);
69
70 grpc_slice grpc_slice_split_tail_no_inline(grpc_slice* source, size_t split);
71
72 grpc_slice grpc_slice_split_head_no_inline(grpc_slice* source, size_t split);
73
74 namespace grpc_core {
75
76 // Converts grpc_slice to absl::string_view.
StringViewFromSlice(const grpc_slice & slice)77 inline absl::string_view StringViewFromSlice(const grpc_slice& slice) {
78 return absl::string_view(
79 reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)),
80 GRPC_SLICE_LENGTH(slice));
81 }
82
83 } // namespace grpc_core
84
grpc_slice_hash(const grpc_slice & s)85 inline uint32_t grpc_slice_hash(const grpc_slice& s) {
86 return absl::HashOf(grpc_core::StringViewFromSlice(s));
87 }
88
89 namespace grpc_core {
90 struct SliceHash {
operatorSliceHash91 std::size_t operator()(const grpc_slice& slice) const {
92 return grpc_slice_hash(slice);
93 }
94 };
95 } // namespace grpc_core
96
97 inline bool operator==(const grpc_slice& s1, const grpc_slice& s2) {
98 return grpc_slice_eq(s1, s2);
99 }
100
101 #endif // GRPC_SRC_CORE_LIB_SLICE_SLICE_INTERNAL_H
102