GRPC Core  3.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 
37 #include <stddef.h>
38 #include <stdint.h>
39 
42 
43 typedef struct grpc_slice grpc_slice;
44 
45 /* Slice API
46 
47  A slice represents a contiguous reference counted array of bytes.
48  It is cheap to take references to a slice, and it is cheap to create a
49  slice pointing to a subset of another slice.
50 
51  The data-structure for slices is exposed here to allow non-gpr code to
52  build slices from whatever data they have available.
53 
54  When defining interfaces that handle slices, care should be taken to define
55  reference ownership semantics (who should call unref?) and mutability
56  constraints (is the callee allowed to modify the slice?) */
57 
59  void (*ref)(void *);
60  void (*unref)(grpc_exec_ctx *exec_ctx, void *);
61  int (*eq)(grpc_slice a, grpc_slice b);
62  uint32_t (*hash)(grpc_slice slice);
64 
65 /* Reference count container for grpc_slice. Contains function pointers to
66  increment and decrement reference counts. Implementations should cleanup
67  when the reference count drops to zero.
68  Typically client code should not touch this, and use grpc_slice_malloc,
69  grpc_slice_new, or grpc_slice_new_with_len instead. */
70 typedef struct grpc_slice_refcount {
72  /* If a subset of this slice is taken, use this pointer for the refcount.
73  Typically points back to the refcount itself, however iterning
74  implementations can use this to avoid a verification step on each hash
75  or equality check */
78 
79 #define GRPC_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1)
80 
81 /* A grpc_slice s, if initialized, represents the byte range
82  s.bytes[0..s.length-1].
83 
84  It can have an associated ref count which has a destruction routine to be run
85  when the ref count reaches zero (see grpc_slice_new() and grp_slice_unref()).
86  Multiple grpc_slice values may share a ref count.
87 
88  If the slice does not have a refcount, it represents an inlined small piece
89  of data that is copied by value. */
90 struct grpc_slice {
92  union {
93  struct {
94  uint8_t *bytes;
95  size_t length;
96  } refcounted;
97  struct {
98  uint8_t length;
100  } inlined;
101  } data;
102 };
103 
104 #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
105 
106 /* Represents an expandable array of slices, to be interpreted as a
107  single item. */
108 typedef struct {
109  /* This is for internal use only. External users (i.e any code outside grpc
110  * core) MUST NOT use this field */
112 
113  /* slices in the array (Points to the first valid grpc_slice in the array) */
115  /* the number of slices in the array */
116  size_t count;
117  /* the number of slices allocated in the array. External users (i.e any code
118  * outside grpc core) MUST NOT use this field */
119  size_t capacity;
120  /* the combined length of all slices in the array */
121  size_t length;
122  /* inlined elements to avoid allocations */
125 
126 #define GRPC_SLICE_START_PTR(slice) \
127  ((slice).refcount ? (slice).data.refcounted.bytes \
128  : (slice).data.inlined.bytes)
129 #define GRPC_SLICE_LENGTH(slice) \
130  ((slice).refcount ? (slice).data.refcounted.length \
131  : (slice).data.inlined.length)
132 #define GRPC_SLICE_SET_LENGTH(slice, newlen) \
133  ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
134  : ((slice).data.inlined.length = (uint8_t)(newlen)))
135 #define GRPC_SLICE_END_PTR(slice) \
136  GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
137 #define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
138 
139 #ifdef GRPC_ALLOW_GPR_SLICE_FUNCTIONS
140 
141 /* Duplicate GPR_* definitions */
142 #define GPR_SLICE_START_PTR(slice) \
143  ((slice).refcount ? (slice).data.refcounted.bytes \
144  : (slice).data.inlined.bytes)
145 #define GPR_SLICE_LENGTH(slice) \
146  ((slice).refcount ? (slice).data.refcounted.length \
147  : (slice).data.inlined.length)
148 #define GPR_SLICE_SET_LENGTH(slice, newlen) \
149  ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
150  : ((slice).data.inlined.length = (uint8_t)(newlen)))
151 #define GPR_SLICE_END_PTR(slice) \
152  GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
153 #define GPR_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
154 
155 #endif /* GRPC_ALLOW_GPR_SLICE_FUNCTIONS */
156 
157 #endif /* GRPC_IMPL_CODEGEN_SLICE_H */
struct grpc_slice_refcount_vtable grpc_slice_refcount_vtable
Definition: slice.h:70
struct grpc_slice_refcount * sub_refcount
Definition: slice.h:76
struct grpc_slice::@19::@21 inlined
union grpc_slice::@19 data
void(* unref)(grpc_exec_ctx *exec_ctx, void *)
Definition: slice.h:60
Definition: slice.h:90
grpc_slice * slices
Definition: slice.h:114
Definition: slice.h:108
struct grpc_slice::@19::@20 refcounted
void(* ref)(void *)
Definition: slice.h:59
Definition: slice.h:58
size_t length
Definition: slice.h:121
int(* eq)(grpc_slice a, grpc_slice b)
Definition: slice.h:61
struct grpc_slice_refcount * refcount
Definition: slice.h:91
grpc_slice * base_slices
Definition: slice.h:111
uint32_t(* hash)(grpc_slice slice)
Definition: slice.h:62
size_t capacity
Definition: slice.h:119
struct grpc_exec_ctx grpc_exec_ctx
Definition: exec_ctx_fwd.h:39
#define GRPC_SLICE_BUFFER_INLINE_ELEMENTS
Definition: slice.h:104
uint8_t * bytes
Definition: slice.h:94
#define GRPC_SLICE_INLINED_SIZE
Definition: slice.h:79
size_t length
Definition: slice.h:95
struct grpc_slice_refcount grpc_slice_refcount
uint8_t length
Definition: slice.h:98
size_t count
Definition: slice.h:116
const grpc_slice_refcount_vtable * vtable
Definition: slice.h:71