blob: ef60ce1220c66b2a0e8fe3a117597c46d0b03a39 [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 Tillera59c16c2016-10-31 07:25:01 -070040typedef struct grpc_exec_ctx grpc_exec_ctx;
41
Craig Tillerd41a4a72016-10-26 16:16:06 -070042/* Slice API
43
44 A slice represents a contiguous reference counted array of bytes.
45 It is cheap to take references to a slice, and it is cheap to create a
46 slice pointing to a subset of another slice.
47
48 The data-structure for slices is exposed here to allow non-gpr code to
49 build slices from whatever data they have available.
50
51 When defining interfaces that handle slices, care should be taken to define
52 reference ownership semantics (who should call unref?) and mutability
53 constraints (is the callee allowed to modify the slice?) */
54
55/* Reference count container for grpc_slice. Contains function pointers to
56 increment and decrement reference counts. Implementations should cleanup
57 when the reference count drops to zero.
58 Typically client code should not touch this, and use grpc_slice_malloc,
59 grpc_slice_new, or grpc_slice_new_with_len instead. */
60typedef struct grpc_slice_refcount {
61 void (*ref)(void *);
Craig Tillera59c16c2016-10-31 07:25:01 -070062 void (*unref)(grpc_exec_ctx *exec_ctx, void *);
Craig Tillerd41a4a72016-10-26 16:16:06 -070063} grpc_slice_refcount;
64
Craig Tiller618e67d2016-10-26 21:08:10 -070065#define GRPC_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1)
Craig Tillerd41a4a72016-10-26 16:16:06 -070066
67/* A grpc_slice s, if initialized, represents the byte range
68 s.bytes[0..s.length-1].
69
70 It can have an associated ref count which has a destruction routine to be run
71 when the ref count reaches zero (see grpc_slice_new() and grp_slice_unref()).
72 Multiple grpc_slice values may share a ref count.
73
74 If the slice does not have a refcount, it represents an inlined small piece
75 of data that is copied by value. */
76typedef struct grpc_slice {
77 struct grpc_slice_refcount *refcount;
78 union {
79 struct {
80 uint8_t *bytes;
81 size_t length;
82 } refcounted;
83 struct {
84 uint8_t length;
Craig Tiller618e67d2016-10-26 21:08:10 -070085 uint8_t bytes[GRPC_SLICE_INLINED_SIZE];
Craig Tillerd41a4a72016-10-26 16:16:06 -070086 } inlined;
87 } data;
88} grpc_slice;
89
90#define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
91
92/* Represents an expandable array of slices, to be interpreted as a
93 single item. */
94typedef struct {
95 /* slices in the array */
96 grpc_slice *slices;
97 /* the number of slices in the array */
98 size_t count;
99 /* the number of slices allocated in the array */
100 size_t capacity;
101 /* the combined length of all slices in the array */
102 size_t length;
103 /* inlined elements to avoid allocations */
104 grpc_slice inlined[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
105} grpc_slice_buffer;
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800106
Craig Tiller618e67d2016-10-26 21:08:10 -0700107#define GRPC_SLICE_START_PTR(slice) \
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800108 ((slice).refcount ? (slice).data.refcounted.bytes \
109 : (slice).data.inlined.bytes)
Craig Tiller618e67d2016-10-26 21:08:10 -0700110#define GRPC_SLICE_LENGTH(slice) \
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800111 ((slice).refcount ? (slice).data.refcounted.length \
112 : (slice).data.inlined.length)
Craig Tiller618e67d2016-10-26 21:08:10 -0700113#define GRPC_SLICE_SET_LENGTH(slice, newlen) \
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800114 ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
115 : ((slice).data.inlined.length = (uint8_t)(newlen)))
Craig Tiller618e67d2016-10-26 21:08:10 -0700116#define GRPC_SLICE_END_PTR(slice) \
117 GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
118#define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800119
David Garcia Quintas2425bbb2016-01-25 17:32:48 -0800120#endif /* GRPC_IMPL_CODEGEN_SLICE_H */