blob: 3c9c7b02855fdb1f2bc680b49c139f556d3ef2ca [file] [log] [blame]
David Garcia Quintas2425bbb2016-01-25 17:32:48 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
David Garcia Quintas2425bbb2016-01-25 17:32:48 -08004 * 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
David Garcia Quintas2425bbb2016-01-25 17:32:48 -080037#include <stddef.h>
Craig Tillerd41a4a72016-10-26 16:16:06 -070038#include <stdint.h>
39
Craig Tiller7cdad962016-11-10 08:37:21 -080040#include <grpc/impl/codegen/exec_ctx_fwd.h>
Mark D. Rothfd9f53a2017-01-06 15:33:04 -080041#include <grpc/impl/codegen/gpr_slice.h>
Craig Tillera59c16c2016-10-31 07:25:01 -070042
Craig Tiller0451c3d2016-11-17 09:34:14 -080043typedef struct grpc_slice grpc_slice;
44
Craig Tillerd41a4a72016-10-26 16:16:06 -070045/* 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
Craig Tiller0451c3d2016-11-17 09:34:14 -080058typedef struct grpc_slice_refcount_vtable {
59 void (*ref)(void *);
60 void (*unref)(grpc_exec_ctx *exec_ctx, void *);
Craig Tiller3b05e1d2016-11-21 13:46:31 -080061 int (*eq)(grpc_slice a, grpc_slice b);
62 uint32_t (*hash)(grpc_slice slice);
Craig Tiller0451c3d2016-11-17 09:34:14 -080063} grpc_slice_refcount_vtable;
64
Craig Tillerd41a4a72016-10-26 16:16:06 -070065/* 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. */
70typedef struct grpc_slice_refcount {
Craig Tiller0451c3d2016-11-17 09:34:14 -080071 const grpc_slice_refcount_vtable *vtable;
Craig Tiller43a51692016-11-23 08:03:02 -080072 /* 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 */
76 struct grpc_slice_refcount *sub_refcount;
Craig Tillerd41a4a72016-10-26 16:16:06 -070077} grpc_slice_refcount;
78
Craig Tiller618e67d2016-10-26 21:08:10 -070079#define GRPC_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1)
Craig Tillerd41a4a72016-10-26 16:16:06 -070080
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. */
Craig Tiller0451c3d2016-11-17 09:34:14 -080090struct grpc_slice {
Craig Tillerd41a4a72016-10-26 16:16:06 -070091 struct grpc_slice_refcount *refcount;
92 union {
93 struct {
94 uint8_t *bytes;
95 size_t length;
96 } refcounted;
97 struct {
98 uint8_t length;
Craig Tiller618e67d2016-10-26 21:08:10 -070099 uint8_t bytes[GRPC_SLICE_INLINED_SIZE];
Craig Tillerd41a4a72016-10-26 16:16:06 -0700100 } inlined;
101 } data;
Craig Tiller0451c3d2016-11-17 09:34:14 -0800102};
Craig Tillerd41a4a72016-10-26 16:16:06 -0700103
104#define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
105
106/* Represents an expandable array of slices, to be interpreted as a
107 single item. */
108typedef struct {
109 /* slices in the array */
110 grpc_slice *slices;
111 /* the number of slices in the array */
112 size_t count;
113 /* the number of slices allocated in the array */
114 size_t capacity;
115 /* the combined length of all slices in the array */
116 size_t length;
117 /* inlined elements to avoid allocations */
118 grpc_slice inlined[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
119} grpc_slice_buffer;
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800120
Craig Tiller618e67d2016-10-26 21:08:10 -0700121#define GRPC_SLICE_START_PTR(slice) \
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800122 ((slice).refcount ? (slice).data.refcounted.bytes \
123 : (slice).data.inlined.bytes)
Craig Tiller618e67d2016-10-26 21:08:10 -0700124#define GRPC_SLICE_LENGTH(slice) \
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800125 ((slice).refcount ? (slice).data.refcounted.length \
126 : (slice).data.inlined.length)
Craig Tiller618e67d2016-10-26 21:08:10 -0700127#define GRPC_SLICE_SET_LENGTH(slice, newlen) \
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800128 ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
129 : ((slice).data.inlined.length = (uint8_t)(newlen)))
Craig Tiller618e67d2016-10-26 21:08:10 -0700130#define GRPC_SLICE_END_PTR(slice) \
131 GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
132#define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800133
Mark D. Roth4d2ea022016-12-12 07:12:27 -0800134#ifdef GRPC_ALLOW_GPR_SLICE_FUNCTIONS
135
136/* Duplicate GPR_* definitions */
137#define GPR_SLICE_START_PTR(slice) \
138 ((slice).refcount ? (slice).data.refcounted.bytes \
139 : (slice).data.inlined.bytes)
140#define GPR_SLICE_LENGTH(slice) \
141 ((slice).refcount ? (slice).data.refcounted.length \
142 : (slice).data.inlined.length)
143#define GPR_SLICE_SET_LENGTH(slice, newlen) \
144 ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
145 : ((slice).data.inlined.length = (uint8_t)(newlen)))
146#define GPR_SLICE_END_PTR(slice) \
147 GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
148#define GPR_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
149
150#endif /* GRPC_ALLOW_GPR_SLICE_FUNCTIONS */
151
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800152#endif /* GRPC_IMPL_CODEGEN_SLICE_H */