GRPC Core  4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
slice.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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_IMPL_CODEGEN_SLICE_H
20 #define GRPC_IMPL_CODEGEN_SLICE_H
21 
23 
24 #include <stddef.h>
25 
28 
29 typedef struct grpc_slice grpc_slice;
30 
45  void (*ref)(void *);
46  void (*unref)(grpc_exec_ctx *exec_ctx, void *);
47  int (*eq)(grpc_slice a, grpc_slice b);
48  uint32_t (*hash)(grpc_slice slice);
50 
56 typedef struct grpc_slice_refcount {
64 
65 #define GRPC_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1)
66 
76 struct grpc_slice {
80  uint8_t *bytes;
81  size_t length;
82  } refcounted;
84  uint8_t length;
86  } inlined;
87  } data;
88 };
89 
90 #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
91 
94 typedef struct {
98 
102  size_t count;
105  size_t capacity;
107  size_t length;
111 
112 #define GRPC_SLICE_START_PTR(slice) \
113  ((slice).refcount ? (slice).data.refcounted.bytes \
114  : (slice).data.inlined.bytes)
115 #define GRPC_SLICE_LENGTH(slice) \
116  ((slice).refcount ? (slice).data.refcounted.length \
117  : (slice).data.inlined.length)
118 #define GRPC_SLICE_SET_LENGTH(slice, newlen) \
119  ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
120  : ((slice).data.inlined.length = (uint8_t)(newlen)))
121 #define GRPC_SLICE_END_PTR(slice) \
122  GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
123 #define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
124 
125 #ifdef GRPC_ALLOW_GPR_SLICE_FUNCTIONS
126 
127 /* Duplicate GPR_* definitions */
128 #define GPR_SLICE_START_PTR(slice) \
129  ((slice).refcount ? (slice).data.refcounted.bytes \
130  : (slice).data.inlined.bytes)
131 #define GPR_SLICE_LENGTH(slice) \
132  ((slice).refcount ? (slice).data.refcounted.length \
133  : (slice).data.inlined.length)
134 #define GPR_SLICE_SET_LENGTH(slice, newlen) \
135  ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
136  : ((slice).data.inlined.length = (uint8_t)(newlen)))
137 #define GPR_SLICE_END_PTR(slice) \
138  GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
139 #define GPR_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
140 
141 #endif /* GRPC_ALLOW_GPR_SLICE_FUNCTIONS */
142 
143 #endif /* GRPC_IMPL_CODEGEN_SLICE_H */
struct grpc_slice_refcount_vtable grpc_slice_refcount_vtable
Slice API.
Reference count container for grpc_slice.
Definition: slice.h:56
struct grpc_slice::grpc_slice_data::grpc_slice_inlined inlined
struct grpc_slice_refcount * sub_refcount
If a subset of this slice is taken, use this pointer for the refcount.
Definition: slice.h:62
void(* unref)(grpc_exec_ctx *exec_ctx, void *)
Definition: slice.h:46
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:76
grpc_slice * slices
slices in the array (Points to the first valid grpc_slice in the array)
Definition: slice.h:100
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice.h:94
struct grpc_slice::grpc_slice_data::grpc_slice_refcounted refcounted
uint8_t bytes[(sizeof(size_t)+sizeof(uint8_t *)-1)]
Definition: slice.h:85
void(* ref)(void *)
Definition: slice.h:45
Slice API.
Definition: slice.h:44
size_t length
the combined length of all slices in the array
Definition: slice.h:107
int(* eq)(grpc_slice a, grpc_slice b)
Definition: slice.h:47
struct grpc_slice_refcount * refcount
Definition: slice.h:77
grpc_slice * base_slices
This is for internal use only.
Definition: slice.h:97
uint32_t(* hash)(grpc_slice slice)
Definition: slice.h:48
size_t capacity
the number of slices allocated in the array.
Definition: slice.h:105
struct grpc_exec_ctx grpc_exec_ctx
Definition: exec_ctx_fwd.h:24
#define GRPC_SLICE_BUFFER_INLINE_ELEMENTS
Definition: slice.h:90
#define GRPC_SLICE_INLINED_SIZE
Definition: slice.h:65
union grpc_slice::grpc_slice_data data
struct grpc_slice_refcount grpc_slice_refcount
Reference count container for grpc_slice.
Definition: slice.h:78
size_t count
the number of slices in the array
Definition: slice.h:102
const grpc_slice_refcount_vtable * vtable
Definition: slice.h:57