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 | |
| 40 | /* Slice API |
| 41 | |
| 42 | A slice represents a contiguous reference counted array of bytes. |
| 43 | It is cheap to take references to a slice, and it is cheap to create a |
| 44 | slice pointing to a subset of another slice. |
| 45 | |
| 46 | The data-structure for slices is exposed here to allow non-gpr code to |
| 47 | build slices from whatever data they have available. |
| 48 | |
| 49 | When defining interfaces that handle slices, care should be taken to define |
| 50 | reference ownership semantics (who should call unref?) and mutability |
| 51 | constraints (is the callee allowed to modify the slice?) */ |
| 52 | |
| 53 | /* Reference count container for grpc_slice. Contains function pointers to |
| 54 | increment and decrement reference counts. Implementations should cleanup |
| 55 | when the reference count drops to zero. |
| 56 | Typically client code should not touch this, and use grpc_slice_malloc, |
| 57 | grpc_slice_new, or grpc_slice_new_with_len instead. */ |
| 58 | typedef struct grpc_slice_refcount { |
| 59 | void (*ref)(void *); |
| 60 | void (*unref)(void *); |
| 61 | } grpc_slice_refcount; |
| 62 | |
| 63 | #define GPR_SLICE_INLINED_SIZE (sizeof(size_t) + sizeof(uint8_t *) - 1) |
| 64 | |
| 65 | /* A grpc_slice s, if initialized, represents the byte range |
| 66 | s.bytes[0..s.length-1]. |
| 67 | |
| 68 | It can have an associated ref count which has a destruction routine to be run |
| 69 | when the ref count reaches zero (see grpc_slice_new() and grp_slice_unref()). |
| 70 | Multiple grpc_slice values may share a ref count. |
| 71 | |
| 72 | If the slice does not have a refcount, it represents an inlined small piece |
| 73 | of data that is copied by value. */ |
| 74 | typedef struct grpc_slice { |
| 75 | struct grpc_slice_refcount *refcount; |
| 76 | union { |
| 77 | struct { |
| 78 | uint8_t *bytes; |
| 79 | size_t length; |
| 80 | } refcounted; |
| 81 | struct { |
| 82 | uint8_t length; |
| 83 | uint8_t bytes[GPR_SLICE_INLINED_SIZE]; |
| 84 | } inlined; |
| 85 | } data; |
| 86 | } grpc_slice; |
| 87 | |
| 88 | #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8 |
| 89 | |
| 90 | /* Represents an expandable array of slices, to be interpreted as a |
| 91 | single item. */ |
| 92 | typedef struct { |
| 93 | /* slices in the array */ |
| 94 | grpc_slice *slices; |
| 95 | /* the number of slices in the array */ |
| 96 | size_t count; |
| 97 | /* the number of slices allocated in the array */ |
| 98 | size_t capacity; |
| 99 | /* the combined length of all slices in the array */ |
| 100 | size_t length; |
| 101 | /* inlined elements to avoid allocations */ |
| 102 | grpc_slice inlined[GRPC_SLICE_BUFFER_INLINE_ELEMENTS]; |
| 103 | } grpc_slice_buffer; |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 104 | |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 105 | #define GPR_SLICE_START_PTR(slice) \ |
| 106 | ((slice).refcount ? (slice).data.refcounted.bytes \ |
| 107 | : (slice).data.inlined.bytes) |
| 108 | #define GPR_SLICE_LENGTH(slice) \ |
| 109 | ((slice).refcount ? (slice).data.refcounted.length \ |
| 110 | : (slice).data.inlined.length) |
| 111 | #define GPR_SLICE_SET_LENGTH(slice, newlen) \ |
| 112 | ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \ |
| 113 | : ((slice).data.inlined.length = (uint8_t)(newlen))) |
| 114 | #define GPR_SLICE_END_PTR(slice) \ |
| 115 | GPR_SLICE_START_PTR(slice) + GPR_SLICE_LENGTH(slice) |
| 116 | #define GPR_SLICE_IS_EMPTY(slice) (GPR_SLICE_LENGTH(slice) == 0) |
| 117 | |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 118 | #endif /* GRPC_IMPL_CODEGEN_SLICE_H */ |