GRPC Core  3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
slice.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
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_SLICE_H
35 #define GRPC_SLICE_H
36 
38 #include <grpc/support/sync.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* Increment the refcount of s. Requires slice is initialized.
45  Returns s. */
47 
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
50  initialized. If s is ultimately derived from a call to grpc_slice_new(start,
51  len, dest) where dest!=NULL , then (*dest)(start) is called, else if s is
52  ultimately derived from a call to grpc_slice_new_with_len(start, len, dest)
53  where dest!=NULL , then (*dest)(start, len). Requires s initialized. */
55 
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. */
59 GPRAPI grpc_slice grpc_slice_new(void *p, size_t len, void (*destroy)(void *));
60 
61 /* Equivalent to grpc_slice_new, but with a separate pointer that is
62  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. */
66  void (*destroy)(void *),
67  void *user_data);
68 
69 /* Equivalent to grpc_slice_new, but with a two argument destroy function that
70  also takes the slice length. */
71 GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len,
72  void (*destroy)(void *, size_t));
73 
74 /* Equivalent to grpc_slice_new(malloc(len), len, free), but saves one malloc()
75  call.
76  Aborts if malloc() fails. */
77 GPRAPI grpc_slice grpc_slice_malloc(size_t length);
78 
79 /* Intern a slice:
80 
81  The return value for two invocations of this function with the same sequence
82  of bytes is a slice which points to the same memory. */
84 
85 /* Create a slice by copying a string.
86  Does not preserve null terminators.
87  Equivalent to:
88  size_t len = strlen(source);
89  grpc_slice slice = grpc_slice_malloc(len);
90  memcpy(slice->data, source, len); */
92 
93 /* Create a slice by copying a buffer.
94  Equivalent to:
95  grpc_slice slice = grpc_slice_malloc(len);
96  memcpy(slice->data, source, len); */
97 GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len);
98 
99 /* Create a slice pointing to constant memory */
101 
102 /* Create a slice pointing to constant memory */
103 GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len);
104 
105 /* Return a result slice derived from s, which shares a ref count with s, where
106  result.data==s.data+begin, and result.length==end-begin.
107  The ref count of s is increased by one.
108  Requires s initialized, begin <= end, begin <= s.length, and
109  end <= source->length. */
110 GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end);
111 
112 /* The same as grpc_slice_sub, but without altering the ref count */
113 GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end);
114 
115 /* Splits s into two: modifies s to be s[0:split], and returns a new slice,
116  sharing a refcount with s, that contains s[split:s.length].
117  Requires s intialized, split <= s.length */
119 
120 /* Splits s into two: modifies s to be s[split:s.length], and returns a new
121  slice, sharing a refcount with s, that contains s[0:split].
122  Requires s intialized, split <= s.length */
124 
126 
129 
131 
132 /* Returns <0 if a < b, ==0 if a == b, >0 if a > b
133  The order is arbitrary, and is not guaranteed to be stable across different
134  versions of the API. */
136 GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b);
137 GPRAPI int grpc_slice_buf_cmp(grpc_slice a, const void *b, size_t blen);
138 
139 /* return non-zero if the first blen bytes of a are equal to b */
140 GPRAPI int grpc_slice_buf_start_eq(grpc_slice a, const void *b, size_t blen);
141 
142 /* return the index of the last instance of \a c in \a s, or -1 if not found */
143 GPRAPI int grpc_slice_rchr(grpc_slice s, char c);
144 GPRAPI int grpc_slice_chr(grpc_slice s, char c);
145 
146 /* return the index of the first occurance of \a needle in \a haystack, or -1 if
147  * it's not found */
148 GPRAPI int grpc_slice_slice(grpc_slice haystack, grpc_slice needle);
149 
150 GPRAPI uint32_t grpc_slice_hash(grpc_slice s);
151 
152 /* Do two slices point at the same memory, with the same length
153  If a or b is inlined, actually compares data */
155 
156 /* Return a slice pointing to newly allocated memory that has the same contents
157  * as \a s */
159 
160 /* Return a copy of slice as a C string. Offers no protection against embedded
161  NULL's. Returned string must be freed with gpr_free. */
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif /* GRPC_SLICE_H */
GPRAPI grpc_slice grpc_slice_dup(grpc_slice a)
GPRAPI grpc_slice grpc_slice_split_head(grpc_slice *s, size_t split)
GPRAPI grpc_slice grpc_empty_slice(void)
GPRAPI grpc_slice grpc_slice_new(void *p, size_t len, void(*destroy)(void *))
GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split)
Definition: slice.h:90
#define GPRAPI
Definition: port_platform.h:410
GPRAPI int grpc_slice_slice(grpc_slice haystack, grpc_slice needle)
GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end)
GPRAPI uint32_t grpc_slice_default_hash_impl(grpc_slice s)
GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b)
GPRAPI uint32_t grpc_slice_hash(grpc_slice s)
GPRAPI int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b)
GPRAPI int grpc_slice_rchr(grpc_slice s, char c)
GPRAPI grpc_slice grpc_slice_intern(grpc_slice slice)
GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end)
GPRAPI int grpc_slice_buf_cmp(grpc_slice a, const void *b, size_t blen)
GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len, void(*destroy)(void *, size_t))
GPRAPI void grpc_slice_unref(grpc_slice s)
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
GPRAPI int grpc_slice_default_eq_impl(grpc_slice a, grpc_slice b)
GPRAPI int grpc_slice_chr(grpc_slice s, char c)
GPRAPI grpc_slice grpc_slice_ref(grpc_slice s)
GPRAPI grpc_slice grpc_slice_new_with_user_data(void *p, size_t len, void(*destroy)(void *), void *user_data)
GPRAPI grpc_slice grpc_slice_malloc(size_t length)
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b)
GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b)
GPRAPI char * grpc_slice_to_c_string(grpc_slice s)
GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len)
GPRAPI int grpc_slice_buf_start_eq(grpc_slice a, const void *b, size_t blen)