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 
22 // IWYU pragma: private, include <grpc/slice.h>
23 
24 #include <grpc/impl/codegen/port_platform.h>
25 
26 #include <stddef.h>
27 
28 #include <grpc/impl/codegen/gpr_slice.h>
29 
30 typedef struct grpc_slice grpc_slice;
31 
32 /** Slice API
33 
34    A slice represents a contiguous reference counted array of bytes.
35    It is cheap to take references to a slice, and it is cheap to create a
36    slice pointing to a subset of another slice.
37 
38    The data-structure for slices is exposed here to allow non-gpr code to
39    build slices from whatever data they have available.
40 
41    When defining interfaces that handle slices, care should be taken to define
42    reference ownership semantics (who should call unref?) and mutability
43    constraints (is the callee allowed to modify the slice?) */
44 
45 /* Inlined half of grpc_slice is allowed to expand the size of the overall type
46    by this many bytes */
47 #define GRPC_SLICE_INLINE_EXTRA_SIZE sizeof(void*)
48 
49 #define GRPC_SLICE_INLINED_SIZE \
50   (sizeof(size_t) + sizeof(uint8_t*) - 1 + GRPC_SLICE_INLINE_EXTRA_SIZE)
51 
52 struct grpc_slice_refcount;
53 /** A grpc_slice s, if initialized, represents the byte range
54    s.bytes[0..s.length-1].
55 
56    It can have an associated ref count which has a destruction routine to be run
57    when the ref count reaches zero (see grpc_slice_new() and grp_slice_unref()).
58    Multiple grpc_slice values may share a ref count.
59 
60    If the slice does not have a refcount, it represents an inlined small piece
61    of data that is copied by value. */
62 struct grpc_slice {
63   struct grpc_slice_refcount* refcount;
64   union grpc_slice_data {
65     struct grpc_slice_refcounted {
66       size_t length;
67       uint8_t* bytes;
68     } refcounted;
69     struct grpc_slice_inlined {
70       uint8_t length;
71       uint8_t bytes[GRPC_SLICE_INLINED_SIZE];
72     } inlined;
73   } data;
74 };
75 
76 #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
77 
78 /** Represents an expandable array of slices, to be interpreted as a
79    single item. */
80 typedef struct grpc_slice_buffer {
81   /** This is for internal use only. External users (i.e any code outside grpc
82    * core) MUST NOT use this field */
83   grpc_slice* base_slices;
84 
85   /** slices in the array (Points to the first valid grpc_slice in the array) */
86   grpc_slice* slices;
87   /** the number of slices in the array */
88   size_t count;
89   /** the number of slices allocated in the array. External users (i.e any code
90    * outside grpc core) MUST NOT use this field */
91   size_t capacity;
92   /** the combined length of all slices in the array */
93   size_t length;
94   /** inlined elements to avoid allocations */
95   grpc_slice inlined[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
96 } grpc_slice_buffer;
97 
98 #define GRPC_SLICE_START_PTR(slice)                 \
99   ((slice).refcount ? (slice).data.refcounted.bytes \
100                     : (slice).data.inlined.bytes)
101 #define GRPC_SLICE_LENGTH(slice)                     \
102   ((slice).refcount ? (slice).data.refcounted.length \
103                     : (slice).data.inlined.length)
104 #define GRPC_SLICE_SET_LENGTH(slice, newlen)                              \
105   ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
106                     : ((slice).data.inlined.length = (uint8_t)(newlen)))
107 #define GRPC_SLICE_END_PTR(slice) \
108   GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
109 #define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
110 
111 #ifdef GRPC_ALLOW_GPR_SLICE_FUNCTIONS
112 
113 /* Duplicate GPR_* definitions */
114 #define GPR_SLICE_START_PTR(slice)                  \
115   ((slice).refcount ? (slice).data.refcounted.bytes \
116                     : (slice).data.inlined.bytes)
117 #define GPR_SLICE_LENGTH(slice)                      \
118   ((slice).refcount ? (slice).data.refcounted.length \
119                     : (slice).data.inlined.length)
120 #define GPR_SLICE_SET_LENGTH(slice, newlen)                               \
121   ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
122                     : ((slice).data.inlined.length = (uint8_t)(newlen)))
123 #define GPR_SLICE_END_PTR(slice) \
124   GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
125 #define GPR_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
126 
127 #endif /* GRPC_ALLOW_GPR_SLICE_FUNCTIONS */
128 
129 #endif /* GRPC_IMPL_CODEGEN_SLICE_H */
130