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, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_IMPL_CODEGEN_SLICE_H
35 #define GRPC_IMPL_CODEGEN_SLICE_H
36 
38 
39 #include <stddef.h>
40 
43 
44 typedef struct grpc_slice grpc_slice;
45 
60  void (*ref)(void *);
61  void (*unref)(grpc_exec_ctx *exec_ctx, void *);
62  int (*eq)(grpc_slice a, grpc_slice b);
63  uint32_t (*hash)(grpc_slice slice);
65 
71 typedef struct grpc_slice_refcount {
79 
80 #define GRPC_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1)
81 
91 struct grpc_slice {
93  union {
94  struct {
95  uint8_t *bytes;
96  size_t length;
97  } refcounted;
98  struct {
99  uint8_t length;
101  } inlined;
102  } data;
103 };
104 
105 #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
106 
109 typedef struct {
113 
117  size_t count;
120  size_t capacity;
122  size_t length;
126 
127 #define GRPC_SLICE_START_PTR(slice) \
128  ((slice).refcount ? (slice).data.refcounted.bytes \
129  : (slice).data.inlined.bytes)
130 #define GRPC_SLICE_LENGTH(slice) \
131  ((slice).refcount ? (slice).data.refcounted.length \
132  : (slice).data.inlined.length)
133 #define GRPC_SLICE_SET_LENGTH(slice, newlen) \
134  ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
135  : ((slice).data.inlined.length = (uint8_t)(newlen)))
136 #define GRPC_SLICE_END_PTR(slice) \
137  GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
138 #define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
139 
140 #ifdef GRPC_ALLOW_GPR_SLICE_FUNCTIONS
141 
142 /* Duplicate GPR_* definitions */
143 #define GPR_SLICE_START_PTR(slice) \
144  ((slice).refcount ? (slice).data.refcounted.bytes \
145  : (slice).data.inlined.bytes)
146 #define GPR_SLICE_LENGTH(slice) \
147  ((slice).refcount ? (slice).data.refcounted.length \
148  : (slice).data.inlined.length)
149 #define GPR_SLICE_SET_LENGTH(slice, newlen) \
150  ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
151  : ((slice).data.inlined.length = (uint8_t)(newlen)))
152 #define GPR_SLICE_END_PTR(slice) \
153  GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
154 #define GPR_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
155 
156 #endif /* GRPC_ALLOW_GPR_SLICE_FUNCTIONS */
157 
158 #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:71
struct grpc_slice_refcount * sub_refcount
If a subset of this slice is taken, use this pointer for the refcount.
Definition: slice.h:77
struct grpc_slice::@19::@21 inlined
union grpc_slice::@19 data
void(* unref)(grpc_exec_ctx *exec_ctx, void *)
Definition: slice.h:61
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:91
grpc_slice * slices
slices in the array (Points to the first valid grpc_slice in the array)
Definition: slice.h:115
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice.h:109
struct grpc_slice::@19::@20 refcounted
void(* ref)(void *)
Definition: slice.h:60
Slice API.
Definition: slice.h:59
size_t length
the combined length of all slices in the array
Definition: slice.h:122
int(* eq)(grpc_slice a, grpc_slice b)
Definition: slice.h:62
struct grpc_slice_refcount * refcount
Definition: slice.h:92
grpc_slice * base_slices
This is for internal use only.
Definition: slice.h:112
uint32_t(* hash)(grpc_slice slice)
Definition: slice.h:63
size_t capacity
the number of slices allocated in the array.
Definition: slice.h:120
struct grpc_exec_ctx grpc_exec_ctx
Definition: exec_ctx_fwd.h:39
#define GRPC_SLICE_BUFFER_INLINE_ELEMENTS
Definition: slice.h:105
uint8_t * bytes
Definition: slice.h:95
#define GRPC_SLICE_INLINED_SIZE
Definition: slice.h:80
size_t length
Definition: slice.h:96
struct grpc_slice_refcount grpc_slice_refcount
Reference count container for grpc_slice.
uint8_t length
Definition: slice.h:99
size_t count
the number of slices in the array
Definition: slice.h:117
const grpc_slice_refcount_vtable * vtable
Definition: slice.h:72