| 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_CORE_LIB_SLICE_SLICE_INTERNAL_H |
| 20 |
#define GRPC_CORE_LIB_SLICE_SLICE_INTERNAL_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stdint.h> |
| 25 |
|
| 26 |
#include <cstddef> |
| 27 |
#include <string> |
| 28 |
|
| 29 |
#include "absl/hash/hash.h" |
| 30 |
#include "absl/strings/string_view.h" |
| 31 |
|
| 32 |
#include <grpc/slice.h> |
| 33 |
#include <grpc/support/log.h> |
| 34 |
|
| 35 |
#include "src/core/lib/gprpp/memory.h" |
| 36 |
|
| 37 |
// Returns a pointer to the first slice in the slice buffer without giving |
| 38 |
// ownership to or a reference count on that slice. |
| 39 |
inline grpc_slice* grpc_slice_buffer_peek_first(grpc_slice_buffer* sb) { |
| 40 |
GPR_DEBUG_ASSERT(sb->count > 0); |
| 41 |
return &sb->slices[0]; |
| 42 |
} |
| 43 |
|
| 44 |
// Removes the first slice from the slice buffer. |
| 45 |
void grpc_slice_buffer_remove_first(grpc_slice_buffer* sb); |
| 46 |
|
| 47 |
// Calls grpc_slice_sub with the given parameters on the first slice. |
| 48 |
void grpc_slice_buffer_sub_first(grpc_slice_buffer* sb, size_t begin, |
| 49 |
size_t end); |
| 50 |
|
| 51 |
// if slice matches a static slice, returns the static slice |
| 52 |
// otherwise returns the passed in slice (without reffing it) |
| 53 |
// used for surface boundaries where we might receive an un-interned static |
| 54 |
// string |
| 55 |
grpc_slice grpc_slice_maybe_static_intern(grpc_slice slice, |
| 56 |
bool* returned_slice_is_different); |
| 57 |
uint32_t grpc_static_slice_hash(grpc_slice s); |
| 58 |
int grpc_static_slice_eq(grpc_slice a, grpc_slice b); |
| 59 |
|
| 60 |
grpc_slice grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char> p, |
| 61 |
size_t len); |
| 62 |
grpc_slice grpc_slice_from_moved_string(grpc_core::UniquePtr<char> p); |
| 63 |
grpc_slice grpc_slice_from_cpp_string(std::string str); |
| 64 |
|
| 65 |
// Returns the memory used by this slice, not counting the slice structure |
| 66 |
// itself. This means that inlined and slices from static strings will return |
| 67 |
// 0. All other slices will return the size of the allocated chars. |
| 68 |
size_t grpc_slice_memory_usage(grpc_slice s); |
| 69 |
|
| 70 |
namespace grpc_core { |
| 71 |
|
| 72 |
// Converts grpc_slice to absl::string_view. |
| 73 |
inline absl::string_view StringViewFromSlice(const grpc_slice& slice) { |
| 74 |
return absl::string_view( |
| 75 |
reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)), |
| 76 |
GRPC_SLICE_LENGTH(slice)); |
| 77 |
} |
| 78 |
|
| 79 |
} // namespace grpc_core |
| 80 |
|
| 81 |
inline uint32_t grpc_slice_hash(const grpc_slice& s) { |
| 82 |
return absl::HashOf(grpc_core::StringViewFromSlice(s)); |
| 83 |
} |
| 84 |
|
| 85 |
namespace grpc_core { |
| 86 |
struct SliceHash { |
| 87 |
std::size_t operator()(const grpc_slice& slice) const { |
| 88 |
return grpc_slice_hash(slice); |
| 89 |
} |
| 90 |
}; |
| 91 |
} // namespace grpc_core |
| 92 |
|
| 93 |
inline bool operator==(const grpc_slice& s1, const grpc_slice& s2) { |
| 94 |
return grpc_slice_eq(s1, s2); |
| 95 |
} |
| 96 |
|
| 97 |
#endif /* GRPC_CORE_LIB_SLICE_SLICE_INTERNAL_H */ |