David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 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 | |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 37 | #include <stddef.h> |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 38 | #include <stdint.h> |
| 39 | |
Craig Tiller | 7cdad96 | 2016-11-10 08:37:21 -0800 | [diff] [blame] | 40 | #include <grpc/impl/codegen/exec_ctx_fwd.h> |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 41 | |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 42 | typedef struct grpc_slice grpc_slice; |
| 43 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 44 | /* Slice API |
| 45 | |
| 46 | A slice represents a contiguous reference counted array of bytes. |
| 47 | It is cheap to take references to a slice, and it is cheap to create a |
| 48 | slice pointing to a subset of another slice. |
| 49 | |
| 50 | The data-structure for slices is exposed here to allow non-gpr code to |
| 51 | build slices from whatever data they have available. |
| 52 | |
| 53 | When defining interfaces that handle slices, care should be taken to define |
| 54 | reference ownership semantics (who should call unref?) and mutability |
| 55 | constraints (is the callee allowed to modify the slice?) */ |
| 56 | |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 57 | typedef struct grpc_slice_refcount_vtable { |
| 58 | void (*ref)(void *); |
| 59 | void (*unref)(grpc_exec_ctx *exec_ctx, void *); |
Craig Tiller | 3b05e1d | 2016-11-21 13:46:31 -0800 | [diff] [blame] | 60 | int (*eq)(grpc_slice a, grpc_slice b); |
| 61 | uint32_t (*hash)(grpc_slice slice); |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 62 | } grpc_slice_refcount_vtable; |
| 63 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 64 | /* Reference count container for grpc_slice. Contains function pointers to |
| 65 | increment and decrement reference counts. Implementations should cleanup |
| 66 | when the reference count drops to zero. |
| 67 | Typically client code should not touch this, and use grpc_slice_malloc, |
| 68 | grpc_slice_new, or grpc_slice_new_with_len instead. */ |
| 69 | typedef struct grpc_slice_refcount { |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 70 | const grpc_slice_refcount_vtable *vtable; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 71 | } grpc_slice_refcount; |
| 72 | |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 73 | #define GRPC_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1) |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 74 | |
| 75 | /* A grpc_slice s, if initialized, represents the byte range |
| 76 | s.bytes[0..s.length-1]. |
| 77 | |
| 78 | It can have an associated ref count which has a destruction routine to be run |
| 79 | when the ref count reaches zero (see grpc_slice_new() and grp_slice_unref()). |
| 80 | Multiple grpc_slice values may share a ref count. |
| 81 | |
| 82 | If the slice does not have a refcount, it represents an inlined small piece |
| 83 | of data that is copied by value. */ |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 84 | struct grpc_slice { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 85 | struct grpc_slice_refcount *refcount; |
| 86 | union { |
| 87 | struct { |
| 88 | uint8_t *bytes; |
| 89 | size_t length; |
| 90 | } refcounted; |
| 91 | struct { |
| 92 | uint8_t length; |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 93 | uint8_t bytes[GRPC_SLICE_INLINED_SIZE]; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 94 | } inlined; |
| 95 | } data; |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 96 | }; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 97 | |
| 98 | #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8 |
| 99 | |
| 100 | /* Represents an expandable array of slices, to be interpreted as a |
| 101 | single item. */ |
| 102 | typedef struct { |
| 103 | /* slices in the array */ |
| 104 | grpc_slice *slices; |
| 105 | /* the number of slices in the array */ |
| 106 | size_t count; |
| 107 | /* the number of slices allocated in the array */ |
| 108 | size_t capacity; |
| 109 | /* the combined length of all slices in the array */ |
| 110 | size_t length; |
| 111 | /* inlined elements to avoid allocations */ |
| 112 | grpc_slice inlined[GRPC_SLICE_BUFFER_INLINE_ELEMENTS]; |
| 113 | } grpc_slice_buffer; |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 114 | |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 115 | #define GRPC_SLICE_START_PTR(slice) \ |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 116 | ((slice).refcount ? (slice).data.refcounted.bytes \ |
| 117 | : (slice).data.inlined.bytes) |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 118 | #define GRPC_SLICE_LENGTH(slice) \ |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 119 | ((slice).refcount ? (slice).data.refcounted.length \ |
| 120 | : (slice).data.inlined.length) |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 121 | #define GRPC_SLICE_SET_LENGTH(slice, newlen) \ |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 122 | ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \ |
| 123 | : ((slice).data.inlined.length = (uint8_t)(newlen))) |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 124 | #define GRPC_SLICE_END_PTR(slice) \ |
| 125 | GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice) |
| 126 | #define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0) |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 127 | |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 128 | #endif /* GRPC_IMPL_CODEGEN_SLICE_H */ |