grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add, size_t num_to_add) { grpc_channel_args *dst = gpr_malloc(sizeof(grpc_channel_args)); size_t i; size_t src_num_args = (src == NULL) ? 0 : src->num_args; if (!src && !to_add) { dst->num_args = 0; dst->args = NULL; return dst; } dst->num_args = src_num_args + num_to_add; dst->args = gpr_malloc(sizeof(grpc_arg) * dst->num_args); for (i = 0; i < src_num_args; i++) { dst->args[i] = copy_arg(&src->args[i]); } for (i = 0; i < num_to_add; i++) { dst->args[i + src_num_args] = copy_arg(&to_add[i]); } return dst; }
grpc_channel_args *grpc_channel_args_copy_and_add_and_remove( const grpc_channel_args *src, const char **to_remove, size_t num_to_remove, const grpc_arg *to_add, size_t num_to_add) { // Figure out how many args we'll be copying. size_t num_args_to_copy = 0; if (src != NULL) { for (size_t i = 0; i < src->num_args; ++i) { if (!should_remove_arg(&src->args[i], to_remove, num_to_remove)) { ++num_args_to_copy; } } } // Create result. grpc_channel_args *dst = (grpc_channel_args *)gpr_malloc(sizeof(grpc_channel_args)); dst->num_args = num_args_to_copy + num_to_add; if (dst->num_args == 0) { dst->args = NULL; return dst; } dst->args = (grpc_arg *)gpr_malloc(sizeof(grpc_arg) * dst->num_args); // Copy args from src that are not being removed. size_t dst_idx = 0; if (src != NULL) { for (size_t i = 0; i < src->num_args; ++i) { if (!should_remove_arg(&src->args[i], to_remove, num_to_remove)) { dst->args[dst_idx++] = copy_arg(&src->args[i]); } } } // Add args from to_add. for (size_t i = 0; i < num_to_add; ++i) { dst->args[dst_idx++] = copy_arg(&to_add[i]); } GPR_ASSERT(dst_idx == dst->num_args); return dst; }
grpc_channel_args *grpc_channel_args_normalize(const grpc_channel_args *a) { grpc_arg **args = gpr_malloc(sizeof(grpc_arg *) * a->num_args); for (size_t i = 0; i < a->num_args; i++) { args[i] = &a->args[i]; } qsort(args, a->num_args, sizeof(grpc_arg *), cmp_key_stable); grpc_channel_args *b = gpr_malloc(sizeof(grpc_channel_args)); b->num_args = a->num_args; b->args = gpr_malloc(sizeof(grpc_arg) * b->num_args); for (size_t i = 0; i < a->num_args; i++) { b->args[i] = copy_arg(args[i]); } gpr_free(args); return b; }
message *msg_create(const struct message_type *mtype, variant args[]) { int i; message *msg = (message *)malloc(sizeof(message)); assert(mtype != NULL); if (mtype == NULL) { log_error("Trying to create message with type=0x0\n"); return NULL; } msg->type = mtype; msg->parameters = (variant *)(mtype->nparameters ? calloc(mtype->nparameters, sizeof(variant)) : NULL); msg->refcount = 1; for (i = 0; i != mtype->nparameters; ++i) { msg->parameters[i] = copy_arg(mtype->types[i], args[i]); } if (msg_log_create) msg_log_create(msg); return msg; }