1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/security/credentials/credentials.h"
22
23 #include <grpc/support/alloc.h>
24
25 #include <string.h>
26
27 #include "src/core/lib/slice/slice_internal.h"
28
mdelem_list_ensure_capacity(grpc_credentials_mdelem_array * list,size_t additional_space_needed)29 static void mdelem_list_ensure_capacity(grpc_credentials_mdelem_array* list,
30 size_t additional_space_needed) {
31 size_t target_size = list->size + additional_space_needed;
32 // Find the next power of two greater than the target size (i.e.,
33 // whenever we add more space, we double what we already have).
34 size_t new_size = 2;
35 while (new_size < target_size) {
36 new_size *= 2;
37 }
38 list->md = static_cast<grpc_mdelem*>(
39 gpr_realloc(list->md, sizeof(grpc_mdelem) * new_size));
40 }
41
grpc_credentials_mdelem_array_add(grpc_credentials_mdelem_array * list,grpc_mdelem md)42 void grpc_credentials_mdelem_array_add(grpc_credentials_mdelem_array* list,
43 grpc_mdelem md) {
44 mdelem_list_ensure_capacity(list, 1);
45 list->md[list->size++] = GRPC_MDELEM_REF(md);
46 }
47
grpc_credentials_mdelem_array_append(grpc_credentials_mdelem_array * dst,grpc_credentials_mdelem_array * src)48 void grpc_credentials_mdelem_array_append(grpc_credentials_mdelem_array* dst,
49 grpc_credentials_mdelem_array* src) {
50 mdelem_list_ensure_capacity(dst, src->size);
51 for (size_t i = 0; i < src->size; ++i) {
52 dst->md[dst->size++] = GRPC_MDELEM_REF(src->md[i]);
53 }
54 }
55
grpc_credentials_mdelem_array_destroy(grpc_credentials_mdelem_array * list)56 void grpc_credentials_mdelem_array_destroy(
57 grpc_credentials_mdelem_array* list) {
58 for (size_t i = 0; i < list->size; ++i) {
59 GRPC_MDELEM_UNREF(list->md[i]);
60 }
61 gpr_free(list->md);
62 }
63