blob: 86a455b42c2715a07f80f8f48639152277b3dd79 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
Craig Tiller5b676a62016-10-26 21:09:29 -070034#ifndef GRPC_SLICE_H
35#define GRPC_SLICE_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
David Garcia Quintas2425bbb2016-01-25 17:32:48 -080037#include <grpc/impl/codegen/slice.h>
David Garcia Quintas61887dc2016-07-27 23:17:34 -070038#include <grpc/support/sync.h>
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* Increment the refcount of s. Requires slice is initialized.
45 Returns s. */
Craig Tillerd41a4a72016-10-26 16:16:06 -070046GPRAPI grpc_slice grpc_slice_ref(grpc_slice s);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070047
48/* Decrement the ref count of s. If the ref count of s reaches zero, all
49 slices sharing the ref count are destroyed, and considered no longer
Craig Tillerd41a4a72016-10-26 16:16:06 -070050 initialized. If s is ultimately derived from a call to grpc_slice_new(start,
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070051 len, dest) where dest!=NULL , then (*dest)(start) is called, else if s is
Craig Tillerd41a4a72016-10-26 16:16:06 -070052 ultimately derived from a call to grpc_slice_new_with_len(start, len, dest)
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070053 where dest!=NULL , then (*dest)(start, len). Requires s initialized. */
Craig Tillerd41a4a72016-10-26 16:16:06 -070054GPRAPI void grpc_slice_unref(grpc_slice s);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070055
56/* Create a slice pointing at some data. Calls malloc to allocate a refcount
57 for the object, and arranges that destroy will be called with the pointer
58 passed in at destruction. */
Craig Tillerd41a4a72016-10-26 16:16:06 -070059GPRAPI grpc_slice grpc_slice_new(void *p, size_t len, void (*destroy)(void *));
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070060
Craig Tillerd41a4a72016-10-26 16:16:06 -070061/* Equivalent to grpc_slice_new, but with a separate pointer that is
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070062 passed to the destroy function. This function can be useful when
63 the data is part of a larger structure that must be destroyed when
64 the data is no longer needed. */
Craig Tillerd41a4a72016-10-26 16:16:06 -070065GPRAPI grpc_slice grpc_slice_new_with_user_data(void *p, size_t len,
66 void (*destroy)(void *),
67 void *user_data);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070068
Craig Tillerd41a4a72016-10-26 16:16:06 -070069/* Equivalent to grpc_slice_new, but with a two argument destroy function that
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070070 also takes the slice length. */
Craig Tillerd41a4a72016-10-26 16:16:06 -070071GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len,
72 void (*destroy)(void *, size_t));
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070073
Craig Tillerd41a4a72016-10-26 16:16:06 -070074/* Equivalent to grpc_slice_new(malloc(len), len, free), but saves one malloc()
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070075 call.
76 Aborts if malloc() fails. */
Craig Tillerd41a4a72016-10-26 16:16:06 -070077GPRAPI grpc_slice grpc_slice_malloc(size_t length);
Craig Tiller423d6fd2017-04-12 13:15:45 -070078GPRAPI grpc_slice grpc_slice_malloc_large(size_t length);
79
80#define GRPC_SLICE_MALLOC(len) \
81 ((len) <= GRPC_SLICE_INLINED_SIZE \
82 ? (grpc_slice){.refcount = NULL, \
83 .data.inlined = {.length = (uint8_t)(len)}} \
84 : grpc_slice_malloc_large((len)))
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070085
Craig Tiller7c70b6c2017-01-23 07:48:42 -080086/* Intern a slice:
87
88 The return value for two invocations of this function with the same sequence
89 of bytes is a slice which points to the same memory. */
90GPRAPI grpc_slice grpc_slice_intern(grpc_slice slice);
91
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070092/* Create a slice by copying a string.
93 Does not preserve null terminators.
94 Equivalent to:
95 size_t len = strlen(source);
Craig Tillerd41a4a72016-10-26 16:16:06 -070096 grpc_slice slice = grpc_slice_malloc(len);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070097 memcpy(slice->data, source, len); */
Craig Tillerd41a4a72016-10-26 16:16:06 -070098GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -070099
100/* Create a slice by copying a buffer.
101 Equivalent to:
Craig Tillerd41a4a72016-10-26 16:16:06 -0700102 grpc_slice slice = grpc_slice_malloc(len);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700103 memcpy(slice->data, source, len); */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700104GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700105
106/* Create a slice pointing to constant memory */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700107GPRAPI grpc_slice grpc_slice_from_static_string(const char *source);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700108
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800109/* Create a slice pointing to constant memory */
110GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len);
111
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700112/* Return a result slice derived from s, which shares a ref count with s, where
113 result.data==s.data+begin, and result.length==end-begin.
114 The ref count of s is increased by one.
115 Requires s initialized, begin <= end, begin <= s.length, and
116 end <= source->length. */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700117GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700118
Craig Tillerd41a4a72016-10-26 16:16:06 -0700119/* The same as grpc_slice_sub, but without altering the ref count */
120GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700121
122/* Splits s into two: modifies s to be s[0:split], and returns a new slice,
123 sharing a refcount with s, that contains s[split:s.length].
124 Requires s intialized, split <= s.length */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700125GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700126
Craig Tiller255ea132017-04-12 17:18:56 -0700127/* The same as grpc_slice_split_tail, but with an option to skip altering
128 * refcounts (grpc_slice_split_tail_maybe_ref(..., true) is equivalent to
129 * grpc_slice_split_tail(...)) */
130GPRAPI grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice *s, size_t split,
Craig Tillera3583b22017-04-13 06:42:47 -0700131 int inc_refs);
Craig Tiller423d6fd2017-04-12 13:15:45 -0700132
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700133/* Splits s into two: modifies s to be s[split:s.length], and returns a new
134 slice, sharing a refcount with s, that contains s[0:split].
135 Requires s intialized, split <= s.length */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700136GPRAPI grpc_slice grpc_slice_split_head(grpc_slice *s, size_t split);
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700137
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800138GPRAPI grpc_slice grpc_empty_slice(void);
139
140GPRAPI uint32_t grpc_slice_default_hash_impl(grpc_slice s);
141GPRAPI int grpc_slice_default_eq_impl(grpc_slice a, grpc_slice b);
142
143GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b);
Craig Tiller0451c3d2016-11-17 09:34:14 -0800144
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700145/* Returns <0 if a < b, ==0 if a == b, >0 if a > b
146 The order is arbitrary, and is not guaranteed to be stable across different
147 versions of the API. */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700148GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b);
149GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800150GPRAPI int grpc_slice_buf_cmp(grpc_slice a, const void *b, size_t blen);
151
152/* return non-zero if the first blen bytes of a are equal to b */
153GPRAPI int grpc_slice_buf_start_eq(grpc_slice a, const void *b, size_t blen);
154
155/* return the index of the last instance of \a c in \a s, or -1 if not found */
156GPRAPI int grpc_slice_rchr(grpc_slice s, char c);
157GPRAPI int grpc_slice_chr(grpc_slice s, char c);
158
159/* return the index of the first occurance of \a needle in \a haystack, or -1 if
160 * it's not found */
161GPRAPI int grpc_slice_slice(grpc_slice haystack, grpc_slice needle);
162
163GPRAPI uint32_t grpc_slice_hash(grpc_slice s);
Craig Tillere52bbb12016-11-10 15:34:06 -0800164
Craig Tillerb121fc72016-11-03 15:22:59 -0700165/* Do two slices point at the same memory, with the same length
166 If a or b is inlined, actually compares data */
Craig Tillerd57a1482016-11-16 14:45:10 -0800167GPRAPI int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b);
Craig Tillerb121fc72016-11-03 15:22:59 -0700168
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800169/* Return a slice pointing to newly allocated memory that has the same contents
170 * as \a s */
171GPRAPI grpc_slice grpc_slice_dup(grpc_slice a);
172
173/* Return a copy of slice as a C string. Offers no protection against embedded
174 NULL's. Returned string must be freed with gpr_free. */
175GPRAPI char *grpc_slice_to_c_string(grpc_slice s);
176
David Garcia Quintase8fd66b2016-07-27 22:05:37 -0700177#ifdef __cplusplus
178}
179#endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180
Craig Tiller5b676a62016-10-26 21:09:29 -0700181#endif /* GRPC_SLICE_H */