aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/gpr
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lib/gpr')
-rw-r--r--src/core/lib/gpr/alloc.cc14
-rw-r--r--src/core/lib/gpr/arena.cc27
-rw-r--r--src/core/lib/gpr/atm.cc3
-rw-r--r--src/core/lib/gpr/avl.cc300
-rw-r--r--src/core/lib/gpr/cmdline.cc330
-rw-r--r--src/core/lib/gpr/cpu_linux.cc6
-rw-r--r--src/core/lib/gpr/cpu_posix.cc3
-rw-r--r--src/core/lib/gpr/env.h2
-rw-r--r--src/core/lib/gpr/env_linux.cc2
-rw-r--r--src/core/lib/gpr/env_windows.cc8
-rw-r--r--src/core/lib/gpr/fork.cc2
-rw-r--r--src/core/lib/gpr/host_port.cc9
-rw-r--r--src/core/lib/gpr/host_port.h43
-rw-r--r--src/core/lib/gpr/log.cc9
-rw-r--r--src/core/lib/gpr/log_linux.cc2
-rw-r--r--src/core/lib/gpr/murmur_hash.cc8
-rw-r--r--src/core/lib/gpr/string.cc35
-rw-r--r--src/core/lib/gpr/string_posix.cc6
-rw-r--r--src/core/lib/gpr/subprocess_posix.cc99
-rw-r--r--src/core/lib/gpr/subprocess_windows.cc126
-rw-r--r--src/core/lib/gpr/sync_posix.cc14
-rw-r--r--src/core/lib/gpr/thd_posix.cc6
-rw-r--r--src/core/lib/gpr/time.cc10
-rw-r--r--src/core/lib/gpr/time_posix.cc4
-rw-r--r--src/core/lib/gpr/tls.h68
-rw-r--r--src/core/lib/gpr/tls_gcc.h50
-rw-r--r--src/core/lib/gpr/tls_msvc.h50
-rw-r--r--src/core/lib/gpr/tls_pthread.cc2
-rw-r--r--src/core/lib/gpr/tls_pthread.h54
-rw-r--r--src/core/lib/gpr/useful.h65
30 files changed, 418 insertions, 939 deletions
diff --git a/src/core/lib/gpr/alloc.cc b/src/core/lib/gpr/alloc.cc
index 000b7dcb25..e0d25963ed 100644
--- a/src/core/lib/gpr/alloc.cc
+++ b/src/core/lib/gpr/alloc.cc
@@ -50,43 +50,39 @@ void gpr_set_allocation_functions(gpr_allocation_functions functions) {
}
void* gpr_malloc(size_t size) {
+ GPR_TIMER_SCOPE("gpr_malloc", 0);
void* p;
if (size == 0) return nullptr;
- GPR_TIMER_BEGIN("gpr_malloc", 0);
p = g_alloc_functions.malloc_fn(size);
if (!p) {
abort();
}
- GPR_TIMER_END("gpr_malloc", 0);
return p;
}
void* gpr_zalloc(size_t size) {
+ GPR_TIMER_SCOPE("gpr_zalloc", 0);
void* p;
if (size == 0) return nullptr;
- GPR_TIMER_BEGIN("gpr_zalloc", 0);
p = g_alloc_functions.zalloc_fn(size);
if (!p) {
abort();
}
- GPR_TIMER_END("gpr_zalloc", 0);
return p;
}
void gpr_free(void* p) {
- GPR_TIMER_BEGIN("gpr_free", 0);
+ GPR_TIMER_SCOPE("gpr_free", 0);
g_alloc_functions.free_fn(p);
- GPR_TIMER_END("gpr_free", 0);
}
void* gpr_realloc(void* p, size_t size) {
+ GPR_TIMER_SCOPE("gpr_realloc", 0);
if ((size == 0) && (p == nullptr)) return nullptr;
- GPR_TIMER_BEGIN("gpr_realloc", 0);
p = g_alloc_functions.realloc_fn(p, size);
if (!p) {
abort();
}
- GPR_TIMER_END("gpr_realloc", 0);
return p;
}
@@ -99,4 +95,4 @@ void* gpr_malloc_aligned(size_t size, size_t alignment) {
return (void*)ret;
}
-void gpr_free_aligned(void* ptr) { gpr_free(((void**)ptr)[-1]); }
+void gpr_free_aligned(void* ptr) { gpr_free((static_cast<void**>(ptr))[-1]); }
diff --git a/src/core/lib/gpr/arena.cc b/src/core/lib/gpr/arena.cc
index 687592a140..2d514df68b 100644
--- a/src/core/lib/gpr/arena.cc
+++ b/src/core/lib/gpr/arena.cc
@@ -23,7 +23,6 @@
#include <grpc/support/alloc.h>
#include <grpc/support/atm.h>
#include <grpc/support/log.h>
-#include <grpc/support/useful.h>
// TODO(roth): We currently assume that all callers need alignment of 16
// bytes, which may be wrong in some cases. As part of converting the
@@ -52,8 +51,8 @@ static void* zalloc_aligned(size_t size) {
gpr_arena* gpr_arena_create(size_t initial_size) {
initial_size = ROUND_UP_TO_ALIGNMENT_SIZE(initial_size);
- gpr_arena* a = (gpr_arena*)zalloc_aligned(
- ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(gpr_arena)) + initial_size);
+ gpr_arena* a = static_cast<gpr_arena*>(zalloc_aligned(
+ ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(gpr_arena)) + initial_size));
a->initial_zone.size_end = initial_size;
return a;
}
@@ -67,23 +66,25 @@ size_t gpr_arena_destroy(gpr_arena* arena) {
gpr_free_aligned(z);
z = next_z;
}
- return (size_t)size;
+ return static_cast<size_t>(size);
}
void* gpr_arena_alloc(gpr_arena* arena, size_t size) {
size = ROUND_UP_TO_ALIGNMENT_SIZE(size);
- size_t start =
- (size_t)gpr_atm_no_barrier_fetch_add(&arena->size_so_far, size);
+ size_t start = static_cast<size_t>(
+ gpr_atm_no_barrier_fetch_add(&arena->size_so_far, size));
zone* z = &arena->initial_zone;
while (start > z->size_end) {
zone* next_z = (zone*)gpr_atm_acq_load(&z->next_atm);
if (next_z == nullptr) {
- size_t next_z_size = (size_t)gpr_atm_no_barrier_load(&arena->size_so_far);
- next_z = (zone*)zalloc_aligned(ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(zone)) +
- next_z_size);
+ size_t next_z_size =
+ static_cast<size_t>(gpr_atm_no_barrier_load(&arena->size_so_far));
+ next_z = static_cast<zone*>(zalloc_aligned(
+ ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(zone)) + next_z_size));
next_z->size_begin = z->size_end;
next_z->size_end = z->size_end + next_z_size;
- if (!gpr_atm_rel_cas(&z->next_atm, (gpr_atm)NULL, (gpr_atm)next_z)) {
+ if (!gpr_atm_rel_cas(&z->next_atm, static_cast<gpr_atm>(NULL),
+ (gpr_atm)next_z)) {
gpr_free_aligned(next_z);
next_z = (zone*)gpr_atm_acq_load(&z->next_atm);
}
@@ -96,7 +97,9 @@ void* gpr_arena_alloc(gpr_arena* arena, size_t size) {
GPR_ASSERT(start >= z->size_begin);
GPR_ASSERT(start + size <= z->size_end);
char* ptr = (z == &arena->initial_zone)
- ? (char*)arena + ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(gpr_arena))
- : (char*)z + ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(zone));
+ ? reinterpret_cast<char*>(arena) +
+ ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(gpr_arena))
+ : reinterpret_cast<char*>(z) +
+ ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(zone));
return ptr + start - z->size_begin;
}
diff --git a/src/core/lib/gpr/atm.cc b/src/core/lib/gpr/atm.cc
index 15bfe52d64..3d0b430348 100644
--- a/src/core/lib/gpr/atm.cc
+++ b/src/core/lib/gpr/atm.cc
@@ -17,7 +17,8 @@
*/
#include <grpc/support/atm.h>
-#include <grpc/support/useful.h>
+
+#include "src/core/lib/gpr/useful.h"
gpr_atm gpr_atm_no_barrier_clamped_add(gpr_atm* value, gpr_atm delta,
gpr_atm min, gpr_atm max) {
diff --git a/src/core/lib/gpr/avl.cc b/src/core/lib/gpr/avl.cc
deleted file mode 100644
index 0b67a21f2f..0000000000
--- a/src/core/lib/gpr/avl.cc
+++ /dev/null
@@ -1,300 +0,0 @@
-/*
- *
- * Copyright 2015 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <grpc/support/avl.h>
-
-#include <assert.h>
-#include <stdlib.h>
-
-#include <grpc/support/alloc.h>
-#include <grpc/support/string_util.h>
-#include <grpc/support/useful.h>
-
-gpr_avl gpr_avl_create(const gpr_avl_vtable* vtable) {
- gpr_avl out;
- out.vtable = vtable;
- out.root = nullptr;
- return out;
-}
-
-static gpr_avl_node* ref_node(gpr_avl_node* node) {
- if (node) {
- gpr_ref(&node->refs);
- }
- return node;
-}
-
-static void unref_node(const gpr_avl_vtable* vtable, gpr_avl_node* node,
- void* user_data) {
- if (node == nullptr) {
- return;
- }
- if (gpr_unref(&node->refs)) {
- vtable->destroy_key(node->key, user_data);
- vtable->destroy_value(node->value, user_data);
- unref_node(vtable, node->left, user_data);
- unref_node(vtable, node->right, user_data);
- gpr_free(node);
- }
-}
-
-static long node_height(gpr_avl_node* node) {
- return node == nullptr ? 0 : node->height;
-}
-
-#ifndef NDEBUG
-static long calculate_height(gpr_avl_node* node) {
- return node == nullptr ? 0
- : 1 + GPR_MAX(calculate_height(node->left),
- calculate_height(node->right));
-}
-
-static gpr_avl_node* assert_invariants(gpr_avl_node* n) {
- if (n == nullptr) return nullptr;
- assert_invariants(n->left);
- assert_invariants(n->right);
- assert(calculate_height(n) == n->height);
- assert(labs(node_height(n->left) - node_height(n->right)) <= 1);
- return n;
-}
-#else
-static gpr_avl_node* assert_invariants(gpr_avl_node* n) { return n; }
-#endif
-
-gpr_avl_node* new_node(void* key, void* value, gpr_avl_node* left,
- gpr_avl_node* right) {
- gpr_avl_node* node = (gpr_avl_node*)gpr_malloc(sizeof(*node));
- gpr_ref_init(&node->refs, 1);
- node->key = key;
- node->value = value;
- node->left = assert_invariants(left);
- node->right = assert_invariants(right);
- node->height = 1 + GPR_MAX(node_height(left), node_height(right));
- return node;
-}
-
-static gpr_avl_node* get(const gpr_avl_vtable* vtable, gpr_avl_node* node,
- void* key, void* user_data) {
- long cmp;
-
- if (node == nullptr) {
- return nullptr;
- }
-
- cmp = vtable->compare_keys(node->key, key, user_data);
- if (cmp == 0) {
- return node;
- } else if (cmp > 0) {
- return get(vtable, node->left, key, user_data);
- } else {
- return get(vtable, node->right, key, user_data);
- }
-}
-
-void* gpr_avl_get(gpr_avl avl, void* key, void* user_data) {
- gpr_avl_node* node = get(avl.vtable, avl.root, key, user_data);
- return node ? node->value : nullptr;
-}
-
-int gpr_avl_maybe_get(gpr_avl avl, void* key, void** value, void* user_data) {
- gpr_avl_node* node = get(avl.vtable, avl.root, key, user_data);
- if (node != nullptr) {
- *value = node->value;
- return 1;
- }
- return 0;
-}
-
-static gpr_avl_node* rotate_left(const gpr_avl_vtable* vtable, void* key,
- void* value, gpr_avl_node* left,
- gpr_avl_node* right, void* user_data) {
- gpr_avl_node* n = new_node(vtable->copy_key(right->key, user_data),
- vtable->copy_value(right->value, user_data),
- new_node(key, value, left, ref_node(right->left)),
- ref_node(right->right));
- unref_node(vtable, right, user_data);
- return n;
-}
-
-static gpr_avl_node* rotate_right(const gpr_avl_vtable* vtable, void* key,
- void* value, gpr_avl_node* left,
- gpr_avl_node* right, void* user_data) {
- gpr_avl_node* n =
- new_node(vtable->copy_key(left->key, user_data),
- vtable->copy_value(left->value, user_data), ref_node(left->left),
- new_node(key, value, ref_node(left->right), right));
- unref_node(vtable, left, user_data);
- return n;
-}
-
-static gpr_avl_node* rotate_left_right(const gpr_avl_vtable* vtable, void* key,
- void* value, gpr_avl_node* left,
- gpr_avl_node* right, void* user_data) {
- /* rotate_right(..., rotate_left(left), right) */
- gpr_avl_node* n =
- new_node(vtable->copy_key(left->right->key, user_data),
- vtable->copy_value(left->right->value, user_data),
- new_node(vtable->copy_key(left->key, user_data),
- vtable->copy_value(left->value, user_data),
- ref_node(left->left), ref_node(left->right->left)),
- new_node(key, value, ref_node(left->right->right), right));
- unref_node(vtable, left, user_data);
- return n;
-}
-
-static gpr_avl_node* rotate_right_left(const gpr_avl_vtable* vtable, void* key,
- void* value, gpr_avl_node* left,
- gpr_avl_node* right, void* user_data) {
- /* rotate_left(..., left, rotate_right(right)) */
- gpr_avl_node* n =
- new_node(vtable->copy_key(right->left->key, user_data),
- vtable->copy_value(right->left->value, user_data),
- new_node(key, value, left, ref_node(right->left->left)),
- new_node(vtable->copy_key(right->key, user_data),
- vtable->copy_value(right->value, user_data),
- ref_node(right->left->right), ref_node(right->right)));
- unref_node(vtable, right, user_data);
- return n;
-}
-
-static gpr_avl_node* rebalance(const gpr_avl_vtable* vtable, void* key,
- void* value, gpr_avl_node* left,
- gpr_avl_node* right, void* user_data) {
- switch (node_height(left) - node_height(right)) {
- case 2:
- if (node_height(left->left) - node_height(left->right) == -1) {
- return assert_invariants(
- rotate_left_right(vtable, key, value, left, right, user_data));
- } else {
- return assert_invariants(
- rotate_right(vtable, key, value, left, right, user_data));
- }
- case -2:
- if (node_height(right->left) - node_height(right->right) == 1) {
- return assert_invariants(
- rotate_right_left(vtable, key, value, left, right, user_data));
- } else {
- return assert_invariants(
- rotate_left(vtable, key, value, left, right, user_data));
- }
- default:
- return assert_invariants(new_node(key, value, left, right));
- }
-}
-
-static gpr_avl_node* add_key(const gpr_avl_vtable* vtable, gpr_avl_node* node,
- void* key, void* value, void* user_data) {
- long cmp;
- if (node == nullptr) {
- return new_node(key, value, nullptr, nullptr);
- }
- cmp = vtable->compare_keys(node->key, key, user_data);
- if (cmp == 0) {
- return new_node(key, value, ref_node(node->left), ref_node(node->right));
- } else if (cmp > 0) {
- return rebalance(vtable, vtable->copy_key(node->key, user_data),
- vtable->copy_value(node->value, user_data),
- add_key(vtable, node->left, key, value, user_data),
- ref_node(node->right), user_data);
- } else {
- return rebalance(
- vtable, vtable->copy_key(node->key, user_data),
- vtable->copy_value(node->value, user_data), ref_node(node->left),
- add_key(vtable, node->right, key, value, user_data), user_data);
- }
-}
-
-gpr_avl gpr_avl_add(gpr_avl avl, void* key, void* value, void* user_data) {
- gpr_avl_node* old_root = avl.root;
- avl.root = add_key(avl.vtable, avl.root, key, value, user_data);
- assert_invariants(avl.root);
- unref_node(avl.vtable, old_root, user_data);
- return avl;
-}
-
-static gpr_avl_node* in_order_head(gpr_avl_node* node) {
- while (node->left != nullptr) {
- node = node->left;
- }
- return node;
-}
-
-static gpr_avl_node* in_order_tail(gpr_avl_node* node) {
- while (node->right != nullptr) {
- node = node->right;
- }
- return node;
-}
-
-static gpr_avl_node* remove_key(const gpr_avl_vtable* vtable,
- gpr_avl_node* node, void* key,
- void* user_data) {
- long cmp;
- if (node == nullptr) {
- return nullptr;
- }
- cmp = vtable->compare_keys(node->key, key, user_data);
- if (cmp == 0) {
- if (node->left == nullptr) {
- return ref_node(node->right);
- } else if (node->right == nullptr) {
- return ref_node(node->left);
- } else if (node->left->height < node->right->height) {
- gpr_avl_node* h = in_order_head(node->right);
- return rebalance(
- vtable, vtable->copy_key(h->key, user_data),
- vtable->copy_value(h->value, user_data), ref_node(node->left),
- remove_key(vtable, node->right, h->key, user_data), user_data);
- } else {
- gpr_avl_node* h = in_order_tail(node->left);
- return rebalance(vtable, vtable->copy_key(h->key, user_data),
- vtable->copy_value(h->value, user_data),
- remove_key(vtable, node->left, h->key, user_data),
- ref_node(node->right), user_data);
- }
- } else if (cmp > 0) {
- return rebalance(vtable, vtable->copy_key(node->key, user_data),
- vtable->copy_value(node->value, user_data),
- remove_key(vtable, node->left, key, user_data),
- ref_node(node->right), user_data);
- } else {
- return rebalance(
- vtable, vtable->copy_key(node->key, user_data),
- vtable->copy_value(node->value, user_data), ref_node(node->left),
- remove_key(vtable, node->right, key, user_data), user_data);
- }
-}
-
-gpr_avl gpr_avl_remove(gpr_avl avl, void* key, void* user_data) {
- gpr_avl_node* old_root = avl.root;
- avl.root = remove_key(avl.vtable, avl.root, key, user_data);
- assert_invariants(avl.root);
- unref_node(avl.vtable, old_root, user_data);
- return avl;
-}
-
-gpr_avl gpr_avl_ref(gpr_avl avl, void* user_data) {
- ref_node(avl.root);
- return avl;
-}
-
-void gpr_avl_unref(gpr_avl avl, void* user_data) {
- unref_node(avl.vtable, avl.root, user_data);
-}
-
-int gpr_avl_is_empty(gpr_avl avl) { return avl.root == nullptr; }
diff --git a/src/core/lib/gpr/cmdline.cc b/src/core/lib/gpr/cmdline.cc
deleted file mode 100644
index 4118f9a72a..0000000000
--- a/src/core/lib/gpr/cmdline.cc
+++ /dev/null
@@ -1,330 +0,0 @@
-/*
- *
- * Copyright 2015 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <grpc/support/cmdline.h>
-
-#include <limits.h>
-#include <stdio.h>
-#include <string.h>
-
-#include <grpc/support/alloc.h>
-#include <grpc/support/log.h>
-#include <grpc/support/string_util.h>
-#include "src/core/lib/gpr/string.h"
-
-typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype;
-
-typedef struct arg {
- const char* name;
- const char* help;
- argtype type;
- void* value;
- struct arg* next;
-} arg;
-
-struct gpr_cmdline {
- const char* description;
- arg* args;
- const char* argv0;
-
- const char* extra_arg_name;
- const char* extra_arg_help;
- void (*extra_arg)(void* user_data, const char* arg);
- void* extra_arg_user_data;
-
- int (*state)(gpr_cmdline* cl, char* arg);
- arg* cur_arg;
-
- int survive_failure;
-};
-
-static int normal_state(gpr_cmdline* cl, char* arg);
-
-gpr_cmdline* gpr_cmdline_create(const char* description) {
- gpr_cmdline* cl = (gpr_cmdline*)gpr_zalloc(sizeof(gpr_cmdline));
-
- cl->description = description;
- cl->state = normal_state;
-
- return cl;
-}
-
-void gpr_cmdline_set_survive_failure(gpr_cmdline* cl) {
- cl->survive_failure = 1;
-}
-
-void gpr_cmdline_destroy(gpr_cmdline* cl) {
- while (cl->args) {
- arg* a = cl->args;
- cl->args = a->next;
- gpr_free(a);
- }
- gpr_free(cl);
-}
-
-static void add_arg(gpr_cmdline* cl, const char* name, const char* help,
- argtype type, void* value) {
- arg* a;
-
- for (a = cl->args; a; a = a->next) {
- GPR_ASSERT(0 != strcmp(a->name, name));
- }
-
- a = (arg*)gpr_zalloc(sizeof(arg));
- a->name = name;
- a->help = help;
- a->type = type;
- a->value = value;
- a->next = cl->args;
- cl->args = a;
-}
-
-void gpr_cmdline_add_int(gpr_cmdline* cl, const char* name, const char* help,
- int* value) {
- add_arg(cl, name, help, ARGTYPE_INT, value);
-}
-
-void gpr_cmdline_add_flag(gpr_cmdline* cl, const char* name, const char* help,
- int* value) {
- add_arg(cl, name, help, ARGTYPE_BOOL, value);
-}
-
-void gpr_cmdline_add_string(gpr_cmdline* cl, const char* name, const char* help,
- const char** value) {
- add_arg(cl, name, help, ARGTYPE_STRING, value);
-}
-
-void gpr_cmdline_on_extra_arg(
- gpr_cmdline* cl, const char* name, const char* help,
- void (*on_extra_arg)(void* user_data, const char* arg), void* user_data) {
- GPR_ASSERT(!cl->extra_arg);
- GPR_ASSERT(on_extra_arg);
-
- cl->extra_arg = on_extra_arg;
- cl->extra_arg_user_data = user_data;
- cl->extra_arg_name = name;
- cl->extra_arg_help = help;
-}
-
-/* recursively descend argument list, adding the last element
- to s first - so that arguments are added in the order they were
- added to the list by api calls */
-static void add_args_to_usage(gpr_strvec* s, arg* a) {
- char* tmp;
-
- if (!a) return;
- add_args_to_usage(s, a->next);
-
- switch (a->type) {
- case ARGTYPE_BOOL:
- gpr_asprintf(&tmp, " [--%s|--no-%s]", a->name, a->name);
- gpr_strvec_add(s, tmp);
- break;
- case ARGTYPE_STRING:
- gpr_asprintf(&tmp, " [--%s=string]", a->name);
- gpr_strvec_add(s, tmp);
- break;
- case ARGTYPE_INT:
- gpr_asprintf(&tmp, " [--%s=int]", a->name);
- gpr_strvec_add(s, tmp);
- break;
- }
-}
-
-char* gpr_cmdline_usage_string(gpr_cmdline* cl, const char* argv0) {
- /* TODO(ctiller): make this prettier */
- gpr_strvec s;
- char* tmp;
- const char* name = strrchr(argv0, '/');
-
- if (name) {
- name++;
- } else {
- name = argv0;
- }
-
- gpr_strvec_init(&s);
-
- gpr_asprintf(&tmp, "Usage: %s", name);
- gpr_strvec_add(&s, tmp);
- add_args_to_usage(&s, cl->args);
- if (cl->extra_arg) {
- gpr_asprintf(&tmp, " [%s...]", cl->extra_arg_name);
- gpr_strvec_add(&s, tmp);
- }
- gpr_strvec_add(&s, gpr_strdup("\n"));
-
- tmp = gpr_strvec_flatten(&s, nullptr);
- gpr_strvec_destroy(&s);
- return tmp;
-}
-
-static int print_usage_and_die(gpr_cmdline* cl) {
- char* usage = gpr_cmdline_usage_string(cl, cl->argv0);
- fprintf(stderr, "%s", usage);
- gpr_free(usage);
- if (!cl->survive_failure) {
- exit(1);
- }
- return 0;
-}
-
-static int extra_state(gpr_cmdline* cl, char* str) {
- if (!cl->extra_arg) {
- return print_usage_and_die(cl);
- }
- cl->extra_arg(cl->extra_arg_user_data, str);
- return 1;
-}
-
-static arg* find_arg(gpr_cmdline* cl, char* name) {
- arg* a;
-
- for (a = cl->args; a; a = a->next) {
- if (0 == strcmp(a->name, name)) {
- break;
- }
- }
-
- if (!a) {
- fprintf(stderr, "Unknown argument: %s\n", name);
- return nullptr;
- }
-
- return a;
-}
-
-static int value_state(gpr_cmdline* cl, char* str) {
- long intval;
- char* end;
-
- GPR_ASSERT(cl->cur_arg);
-
- switch (cl->cur_arg->type) {
- case ARGTYPE_INT:
- intval = strtol(str, &end, 0);
- if (*end || intval < INT_MIN || intval > INT_MAX) {
- fprintf(stderr, "expected integer, got '%s' for %s\n", str,
- cl->cur_arg->name);
- return print_usage_and_die(cl);
- }
- *(int*)cl->cur_arg->value = (int)intval;
- break;
- case ARGTYPE_BOOL:
- if (0 == strcmp(str, "1") || 0 == strcmp(str, "true")) {
- *(int*)cl->cur_arg->value = 1;
- } else if (0 == strcmp(str, "0") || 0 == strcmp(str, "false")) {
- *(int*)cl->cur_arg->value = 0;
- } else {
- fprintf(stderr, "expected boolean, got '%s' for %s\n", str,
- cl->cur_arg->name);
- return print_usage_and_die(cl);
- }
- break;
- case ARGTYPE_STRING:
- *(char**)cl->cur_arg->value = str;
- break;
- }
-
- cl->state = normal_state;
- return 1;
-}
-
-static int normal_state(gpr_cmdline* cl, char* str) {
- char* eq = nullptr;
- char* tmp = nullptr;
- char* arg_name = nullptr;
- int r = 1;
-
- if (0 == strcmp(str, "-help") || 0 == strcmp(str, "--help") ||
- 0 == strcmp(str, "-h")) {
- return print_usage_and_die(cl);
- }
-
- cl->cur_arg = nullptr;
-
- if (str[0] == '-') {
- if (str[1] == '-') {
- if (str[2] == 0) {
- /* handle '--' to move to just extra args */
- cl->state = extra_state;
- return 1;
- }
- str += 2;
- } else {
- str += 1;
- }
- /* first byte of str is now past the leading '-' or '--' */
- if (str[0] == 'n' && str[1] == 'o' && str[2] == '-') {
- /* str is of the form '--no-foo' - it's a flag disable */
- str += 3;
- cl->cur_arg = find_arg(cl, str);
- if (cl->cur_arg == nullptr) {
- return print_usage_and_die(cl);
- }
- if (cl->cur_arg->type != ARGTYPE_BOOL) {
- fprintf(stderr, "%s is not a flag argument\n", str);
- return print_usage_and_die(cl);
- }
- *(int*)cl->cur_arg->value = 0;
- return 1; /* early out */
- }
- eq = strchr(str, '=');
- if (eq != nullptr) {
- /* copy the string into a temp buffer and extract the name */
- tmp = arg_name = (char*)gpr_malloc((size_t)(eq - str + 1));
- memcpy(arg_name, str, (size_t)(eq - str));
- arg_name[eq - str] = 0;
- } else {
- arg_name = str;
- }
- cl->cur_arg = find_arg(cl, arg_name);
- if (cl->cur_arg == nullptr) {
- return print_usage_and_die(cl);
- }
- if (eq != nullptr) {
- /* str was of the type --foo=value, parse the value */
- r = value_state(cl, eq + 1);
- } else if (cl->cur_arg->type != ARGTYPE_BOOL) {
- /* flag types don't have a '--foo value' variant, other types do */
- cl->state = value_state;
- } else {
- /* flag parameter: just set the value */
- *(int*)cl->cur_arg->value = 1;
- }
- } else {
- r = extra_state(cl, str);
- }
-
- gpr_free(tmp);
- return r;
-}
-
-int gpr_cmdline_parse(gpr_cmdline* cl, int argc, char** argv) {
- int i;
-
- GPR_ASSERT(argc >= 1);
- cl->argv0 = argv[0];
-
- for (i = 1; i < argc; i++) {
- if (!cl->state(cl, argv[i])) {
- return 0;
- }
- }
- return 1;
-}
diff --git a/src/core/lib/gpr/cpu_linux.cc b/src/core/lib/gpr/cpu_linux.cc
index 21b1a71dc9..4782f9f742 100644
--- a/src/core/lib/gpr/cpu_linux.cc
+++ b/src/core/lib/gpr/cpu_linux.cc
@@ -45,7 +45,7 @@ static void init_num_cpus() {
#endif
/* This must be signed. sysconf returns -1 when the number cannot be
determined */
- ncpus = (int)sysconf(_SC_NPROCESSORS_ONLN);
+ ncpus = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
if (ncpus < 1) {
gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1");
ncpus = 1;
@@ -55,7 +55,7 @@ static void init_num_cpus() {
unsigned gpr_cpu_num_cores(void) {
static gpr_once once = GPR_ONCE_INIT;
gpr_once_init(&once, init_num_cpus);
- return (unsigned)ncpus;
+ return static_cast<unsigned>(ncpus);
}
unsigned gpr_cpu_current_cpu(void) {
@@ -71,7 +71,7 @@ unsigned gpr_cpu_current_cpu(void) {
gpr_log(GPR_ERROR, "Error determining current CPU: %s\n", strerror(errno));
return 0;
}
- return (unsigned)cpu;
+ return static_cast<unsigned>(cpu);
#endif
}
diff --git a/src/core/lib/gpr/cpu_posix.cc b/src/core/lib/gpr/cpu_posix.cc
index bca14a0c12..7a77f7ab64 100644
--- a/src/core/lib/gpr/cpu_posix.cc
+++ b/src/core/lib/gpr/cpu_posix.cc
@@ -29,7 +29,8 @@
#include <grpc/support/cpu.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
-#include <grpc/support/useful.h>
+
+#include "src/core/lib/gpr/useful.h"
static long ncpus = 0;
diff --git a/src/core/lib/gpr/env.h b/src/core/lib/gpr/env.h
index 7f35104be3..b31e20b7d2 100644
--- a/src/core/lib/gpr/env.h
+++ b/src/core/lib/gpr/env.h
@@ -29,7 +29,7 @@
variable exists). */
char* gpr_getenv(const char* name);
-/* Sets the the environment with the specified name to the specified value. */
+/* Sets the environment with the specified name to the specified value. */
void gpr_setenv(const char* name, const char* value);
/* This is a version of gpr_getenv that does not produce any output if it has to
diff --git a/src/core/lib/gpr/env_linux.cc b/src/core/lib/gpr/env_linux.cc
index 17902c3d0b..fadc42f22f 100644
--- a/src/core/lib/gpr/env_linux.cc
+++ b/src/core/lib/gpr/env_linux.cc
@@ -34,9 +34,9 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-#include <grpc/support/useful.h>
#include "src/core/lib/gpr/string.h"
+#include "src/core/lib/gpr/useful.h"
const char* gpr_getenv_silent(const char* name, char** dst) {
const char* insecure_func_used = nullptr;
diff --git a/src/core/lib/gpr/env_windows.cc b/src/core/lib/gpr/env_windows.cc
index 9ca1e02d03..cf8ed60d8f 100644
--- a/src/core/lib/gpr/env_windows.cc
+++ b/src/core/lib/gpr/env_windows.cc
@@ -22,14 +22,14 @@
#include <windows.h>
-#include "src/core/lib/gpr/env.h"
-#include "src/core/lib/gpr/string.h"
-#include "src/core/lib/gpr/string_windows.h"
-
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
+#include "src/core/lib/gpr/env.h"
+#include "src/core/lib/gpr/string.h"
+#include "src/core/lib/gpr/string_windows.h"
+
const char* gpr_getenv_silent(const char* name, char** dst) {
*dst = gpr_getenv(name);
return NULL;
diff --git a/src/core/lib/gpr/fork.cc b/src/core/lib/gpr/fork.cc
index 92023f4350..4651d22595 100644
--- a/src/core/lib/gpr/fork.cc
+++ b/src/core/lib/gpr/fork.cc
@@ -21,9 +21,9 @@
#include <string.h>
#include <grpc/support/alloc.h>
-#include <grpc/support/useful.h>
#include "src/core/lib/gpr/env.h"
+#include "src/core/lib/gpr/useful.h"
/*
* NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
diff --git a/src/core/lib/gpr/host_port.cc b/src/core/lib/gpr/host_port.cc
index 29178279d3..5a03a16296 100644
--- a/src/core/lib/gpr/host_port.cc
+++ b/src/core/lib/gpr/host_port.cc
@@ -16,13 +16,14 @@
*
*/
-#include <grpc/support/host_port.h>
+#include "src/core/lib/gpr/host_port.h"
#include <string.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
+
#include "src/core/lib/gpr/string.h"
int gpr_join_host_port(char** out, const char* host, int port) {
@@ -61,7 +62,7 @@ int gpr_split_host_port(const char* name, char** host, char** port) {
return 0;
}
host_start = name + 1;
- host_len = (size_t)(rbracket - host_start);
+ host_len = static_cast<size_t>(rbracket - host_start);
if (memchr(host_start, ':', host_len) == nullptr) {
/* Require all bracketed hosts to contain a colon, because a hostname or
IPv4 address should never use brackets. */
@@ -72,7 +73,7 @@ int gpr_split_host_port(const char* name, char** host, char** port) {
if (colon != nullptr && strchr(colon + 1, ':') == nullptr) {
/* Exactly 1 colon. Split into host:port. */
host_start = name;
- host_len = (size_t)(colon - name);
+ host_len = static_cast<size_t>(colon - name);
port_start = colon + 1;
} else {
/* 0 or 2+ colons. Bare hostname or IPv6 litearal. */
@@ -83,7 +84,7 @@ int gpr_split_host_port(const char* name, char** host, char** port) {
}
/* Allocate return values. */
- *host = (char*)gpr_malloc(host_len + 1);
+ *host = static_cast<char*>(gpr_malloc(host_len + 1));
memcpy(*host, host_start, host_len);
(*host)[host_len] = '\0';
diff --git a/src/core/lib/gpr/host_port.h b/src/core/lib/gpr/host_port.h
new file mode 100644
index 0000000000..0bf0960f82
--- /dev/null
+++ b/src/core/lib/gpr/host_port.h
@@ -0,0 +1,43 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_GPR_HOST_PORT_H
+#define GRPC_CORE_LIB_GPR_HOST_PORT_H
+
+#include <grpc/support/port_platform.h>
+
+/** Given a host and port, creates a newly-allocated string of the form
+ "host:port" or "[ho:st]:port", depending on whether the host contains colons
+ like an IPv6 literal. If the host is already bracketed, then additional
+ brackets will not be added.
+
+ Usage is similar to gpr_asprintf: returns the number of bytes written
+ (excluding the final '\0'), and *out points to a string which must later be
+ destroyed using gpr_free().
+
+ In the unlikely event of an error, returns -1 and sets *out to NULL. */
+int gpr_join_host_port(char** out, const char* host, int port);
+
+/** Given a name in the form "host:port" or "[ho:st]:port", split into hostname
+ and port number, into newly allocated strings, which must later be
+ destroyed using gpr_free().
+ Return 1 on success, 0 on failure. Guarantees *host and *port == NULL on
+ failure. */
+int gpr_split_host_port(const char* name, char** host, char** port);
+
+#endif /* GRPC_CORE_LIB_GPR_HOST_PORT_H */
diff --git a/src/core/lib/gpr/log.cc b/src/core/lib/gpr/log.cc
index 19c0f6c34d..410096c0f7 100644
--- a/src/core/lib/gpr/log.cc
+++ b/src/core/lib/gpr/log.cc
@@ -45,7 +45,8 @@ const char* gpr_log_severity_string(gpr_log_severity severity) {
void gpr_log_message(const char* file, int line, gpr_log_severity severity,
const char* message) {
- if ((gpr_atm)severity < gpr_atm_no_barrier_load(&g_min_severity_to_print)) {
+ if (static_cast<gpr_atm>(severity) <
+ gpr_atm_no_barrier_load(&g_min_severity_to_print)) {
return;
}
@@ -70,11 +71,11 @@ void gpr_log_verbosity_init() {
gpr_atm min_severity_to_print = GPR_LOG_SEVERITY_ERROR;
if (verbosity != nullptr) {
if (gpr_stricmp(verbosity, "DEBUG") == 0) {
- min_severity_to_print = (gpr_atm)GPR_LOG_SEVERITY_DEBUG;
+ min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_DEBUG);
} else if (gpr_stricmp(verbosity, "INFO") == 0) {
- min_severity_to_print = (gpr_atm)GPR_LOG_SEVERITY_INFO;
+ min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_INFO);
} else if (gpr_stricmp(verbosity, "ERROR") == 0) {
- min_severity_to_print = (gpr_atm)GPR_LOG_SEVERITY_ERROR;
+ min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_ERROR);
}
gpr_free(verbosity);
}
diff --git a/src/core/lib/gpr/log_linux.cc b/src/core/lib/gpr/log_linux.cc
index 6b1f1c71e4..d743eedf38 100644
--- a/src/core/lib/gpr/log_linux.cc
+++ b/src/core/lib/gpr/log_linux.cc
@@ -67,7 +67,7 @@ void gpr_default_log(gpr_log_func_args* args) {
static __thread long tid = 0;
if (tid == 0) tid = gettid();
- timer = (time_t)now.tv_sec;
+ timer = static_cast<time_t>(now.tv_sec);
final_slash = strrchr(args->file, '/');
if (final_slash == nullptr)
display_file = args->file;
diff --git a/src/core/lib/gpr/murmur_hash.cc b/src/core/lib/gpr/murmur_hash.cc
index 3f5e04d211..01a7290c67 100644
--- a/src/core/lib/gpr/murmur_hash.cc
+++ b/src/core/lib/gpr/murmur_hash.cc
@@ -36,7 +36,7 @@ uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed) {
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
- const uint8_t* keyptr = (const uint8_t*)key;
+ const uint8_t* keyptr = static_cast<const uint8_t*>(key);
const size_t bsize = sizeof(k1);
const size_t nblocks = len / bsize;
@@ -58,10 +58,10 @@ uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed) {
/* tail */
switch (len & 3) {
case 3:
- k1 ^= ((uint32_t)keyptr[2]) << 16;
+ k1 ^= (static_cast<uint32_t>(keyptr[2])) << 16;
/* fallthrough */
case 2:
- k1 ^= ((uint32_t)keyptr[1]) << 8;
+ k1 ^= (static_cast<uint32_t>(keyptr[1])) << 8;
/* fallthrough */
case 1:
k1 ^= keyptr[0];
@@ -72,7 +72,7 @@ uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed) {
};
/* finalization */
- h1 ^= (uint32_t)len;
+ h1 ^= static_cast<uint32_t>(len);
FMIX32(h1);
return h1;
}
diff --git a/src/core/lib/gpr/string.cc b/src/core/lib/gpr/string.cc
index 919d957d1b..5a16377e49 100644
--- a/src/core/lib/gpr/string.cc
+++ b/src/core/lib/gpr/string.cc
@@ -28,7 +28,8 @@
#include <grpc/support/log.h>
#include <grpc/support/port_platform.h>
#include <grpc/support/string_util.h>
-#include <grpc/support/useful.h>
+
+#include "src/core/lib/gpr/useful.h"
char* gpr_strdup(const char* src) {
char* dst;
@@ -39,7 +40,7 @@ char* gpr_strdup(const char* src) {
}
len = strlen(src) + 1;
- dst = (char*)gpr_malloc(len);
+ dst = static_cast<char*>(gpr_malloc(len));
memcpy(dst, src, len);
@@ -60,7 +61,7 @@ static dump_out dump_out_create(void) {
static void dump_out_append(dump_out* out, char c) {
if (out->length == out->capacity) {
out->capacity = GPR_MAX(8, 2 * out->capacity);
- out->data = (char*)gpr_realloc(out->data, out->capacity);
+ out->data = static_cast<char*>(gpr_realloc(out->data, out->capacity));
}
out->data[out->length++] = c;
}
@@ -68,7 +69,7 @@ static void dump_out_append(dump_out* out, char c) {
static void hexdump(dump_out* out, const char* buf, size_t len) {
static const char* hex = "0123456789abcdef";
- const uint8_t* const beg = (const uint8_t*)buf;
+ const uint8_t* const beg = reinterpret_cast<const uint8_t*>(buf);
const uint8_t* const end = beg + len;
const uint8_t* cur;
@@ -80,7 +81,7 @@ static void hexdump(dump_out* out, const char* buf, size_t len) {
}
static void asciidump(dump_out* out, const char* buf, size_t len) {
- const uint8_t* const beg = (const uint8_t*)buf;
+ const uint8_t* const beg = reinterpret_cast<const uint8_t*>(buf);
const uint8_t* const end = beg + len;
const uint8_t* cur;
int out_was_empty = (out->length == 0);
@@ -89,7 +90,7 @@ static void asciidump(dump_out* out, const char* buf, size_t len) {
dump_out_append(out, '\'');
}
for (cur = beg; cur != end; ++cur) {
- dump_out_append(out, (char)(isprint(*cur) ? *(char*)cur : '.'));
+ dump_out_append(out, (isprint(*cur) ? *(char*)cur : '.'));
}
if (!out_was_empty) {
dump_out_append(out, '\'');
@@ -117,7 +118,7 @@ int gpr_parse_bytes_to_uint32(const char* buf, size_t len, uint32_t* result) {
for (i = 0; i < len; i++) {
if (buf[i] < '0' || buf[i] > '9') return 0; /* bad char */
- new_val = 10 * out + (uint32_t)(buf[i] - '0');
+ new_val = 10 * out + static_cast<uint32_t>(buf[i] - '0');
if (new_val < out) return 0; /* overflow */
out = new_val;
}
@@ -147,7 +148,7 @@ int gpr_ltoa(long value, char* string) {
sign = value < 0 ? -1 : 1;
while (value) {
- string[i++] = (char)('0' + sign * (value % 10));
+ string[i++] = static_cast<char>('0' + sign * (value % 10));
value /= 10;
}
if (sign < 0) string[i++] = '-';
@@ -168,7 +169,7 @@ int int64_ttoa(int64_t value, char* string) {
sign = value < 0 ? -1 : 1;
while (value) {
- string[i++] = (char)('0' + sign * (value % 10));
+ string[i++] = static_cast<char>('0' + sign * (value % 10));
value /= 10;
}
if (sign < 0) string[i++] = '-';
@@ -181,13 +182,13 @@ int gpr_parse_nonnegative_int(const char* value) {
char* end;
long result = strtol(value, &end, 0);
if (*end != '\0' || result < 0 || result > INT_MAX) return -1;
- return (int)result;
+ return static_cast<int>(result);
}
char* gpr_leftpad(const char* str, char flag, size_t length) {
const size_t str_length = strlen(str);
const size_t out_length = str_length > length ? str_length : length;
- char* out = (char*)gpr_malloc(out_length + 1);
+ char* out = static_cast<char*>(gpr_malloc(out_length + 1));
memset(out, flag, out_length - str_length);
memcpy(out + out_length - str_length, str, str_length);
out[out_length] = 0;
@@ -211,7 +212,7 @@ char* gpr_strjoin_sep(const char** strs, size_t nstrs, const char* sep,
if (nstrs > 0) {
out_length += sep_len * (nstrs - 1); /* separators */
}
- out = (char*)gpr_malloc(out_length);
+ out = static_cast<char*>(gpr_malloc(out_length));
out_length = 0;
for (i = 0; i < nstrs; i++) {
const size_t slen = strlen(strs[i]);
@@ -242,7 +243,8 @@ void gpr_strvec_destroy(gpr_strvec* sv) {
void gpr_strvec_add(gpr_strvec* sv, char* str) {
if (sv->count == sv->capacity) {
sv->capacity = GPR_MAX(sv->capacity + 8, sv->capacity * 2);
- sv->strs = (char**)gpr_realloc(sv->strs, sizeof(char*) * sv->capacity);
+ sv->strs = static_cast<char**>(
+ gpr_realloc(sv->strs, sizeof(char*) * sv->capacity));
}
sv->strs[sv->count++] = str;
}
@@ -264,12 +266,13 @@ int gpr_stricmp(const char* a, const char* b) {
static void add_string_to_split(const char* beg, const char* end, char*** strs,
size_t* nstrs, size_t* capstrs) {
- char* out = (char*)gpr_malloc((size_t)(end - beg) + 1);
- memcpy(out, beg, (size_t)(end - beg));
+ char* out =
+ static_cast<char*>(gpr_malloc(static_cast<size_t>(end - beg) + 1));
+ memcpy(out, beg, static_cast<size_t>(end - beg));
out[end - beg] = 0;
if (*nstrs == *capstrs) {
*capstrs = GPR_MAX(8, 2 * *capstrs);
- *strs = (char**)gpr_realloc(*strs, sizeof(*strs) * *capstrs);
+ *strs = static_cast<char**>(gpr_realloc(*strs, sizeof(*strs) * *capstrs));
}
(*strs)[*nstrs] = out;
++*nstrs;
diff --git a/src/core/lib/gpr/string_posix.cc b/src/core/lib/gpr/string_posix.cc
index 8b818e39b9..d32775fb3b 100644
--- a/src/core/lib/gpr/string_posix.cc
+++ b/src/core/lib/gpr/string_posix.cc
@@ -43,8 +43,8 @@ int gpr_asprintf(char** strp, const char* format, ...) {
}
/* Allocate a new buffer, with space for the NUL terminator. */
- strp_buflen = (size_t)ret + 1;
- if ((*strp = (char*)gpr_malloc(strp_buflen)) == nullptr) {
+ strp_buflen = static_cast<size_t>(ret) + 1;
+ if ((*strp = static_cast<char*>(gpr_malloc(strp_buflen))) == nullptr) {
/* This shouldn't happen, because gpr_malloc() calls abort(). */
return -1;
}
@@ -59,7 +59,7 @@ int gpr_asprintf(char** strp, const char* format, ...) {
va_start(args, format);
ret = vsnprintf(*strp, strp_buflen, format, args);
va_end(args);
- if ((size_t)ret == strp_buflen - 1) {
+ if (static_cast<size_t>(ret) == strp_buflen - 1) {
return ret;
}
diff --git a/src/core/lib/gpr/subprocess_posix.cc b/src/core/lib/gpr/subprocess_posix.cc
deleted file mode 100644
index dc046b6499..0000000000
--- a/src/core/lib/gpr/subprocess_posix.cc
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- *
- * Copyright 2015 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <grpc/support/port_platform.h>
-
-#ifdef GPR_POSIX_SUBPROCESS
-
-#include <grpc/support/subprocess.h>
-
-#include <assert.h>
-#include <errno.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <unistd.h>
-
-#include <grpc/support/alloc.h>
-#include <grpc/support/log.h>
-
-struct gpr_subprocess {
- int pid;
- bool joined;
-};
-
-const char* gpr_subprocess_binary_extension() { return ""; }
-
-gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
- gpr_subprocess* r;
- int pid;
- char** exec_args;
-
- pid = fork();
- if (pid == -1) {
- return nullptr;
- } else if (pid == 0) {
- exec_args = (char**)gpr_malloc(((size_t)argc + 1) * sizeof(char*));
- memcpy(exec_args, argv, (size_t)argc * sizeof(char*));
- exec_args[argc] = nullptr;
- execv(exec_args[0], exec_args);
- /* if we reach here, an error has occurred */
- gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
- _exit(1);
- return nullptr;
- } else {
- r = (gpr_subprocess*)gpr_zalloc(sizeof(gpr_subprocess));
- r->pid = pid;
- return r;
- }
-}
-
-void gpr_subprocess_destroy(gpr_subprocess* p) {
- if (!p->joined) {
- kill(p->pid, SIGKILL);
- gpr_subprocess_join(p);
- }
- gpr_free(p);
-}
-
-int gpr_subprocess_join(gpr_subprocess* p) {
- int status;
-retry:
- if (waitpid(p->pid, &status, 0) == -1) {
- if (errno == EINTR) {
- goto retry;
- }
- gpr_log(GPR_ERROR, "waitpid failed for pid %d: %s", p->pid,
- strerror(errno));
- return -1;
- }
- p->joined = true;
- return status;
-}
-
-void gpr_subprocess_interrupt(gpr_subprocess* p) {
- if (!p->joined) {
- kill(p->pid, SIGINT);
- }
-}
-
-#endif /* GPR_POSIX_SUBPROCESS */
diff --git a/src/core/lib/gpr/subprocess_windows.cc b/src/core/lib/gpr/subprocess_windows.cc
deleted file mode 100644
index 1947d475e3..0000000000
--- a/src/core/lib/gpr/subprocess_windows.cc
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- *
- * Copyright 2016 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <grpc/support/port_platform.h>
-
-#ifdef GPR_WINDOWS_SUBPROCESS
-
-#include <string.h>
-#include <tchar.h>
-#include <windows.h>
-
-#include <grpc/support/alloc.h>
-#include <grpc/support/log.h>
-#include <grpc/support/subprocess.h>
-#include "src/core/lib/gpr/string.h"
-#include "src/core/lib/gpr/string_windows.h"
-
-struct gpr_subprocess {
- PROCESS_INFORMATION pi;
- int joined;
- int interrupted;
-};
-
-const char* gpr_subprocess_binary_extension() { return ".exe"; }
-
-gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
- gpr_subprocess* r;
-
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
-
- char* args = gpr_strjoin_sep(argv, (size_t)argc, " ", NULL);
- TCHAR* args_tchar;
-
- args_tchar = gpr_char_to_tchar(args);
- gpr_free(args);
-
- memset(&si, 0, sizeof(si));
- si.cb = sizeof(si);
- memset(&pi, 0, sizeof(pi));
-
- if (!CreateProcess(NULL, args_tchar, NULL, NULL, FALSE,
- CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) {
- gpr_free(args_tchar);
- return NULL;
- }
- gpr_free(args_tchar);
-
- r = (gpr_subprocess*)gpr_malloc(sizeof(gpr_subprocess));
- memset(r, 0, sizeof(*r));
- r->pi = pi;
- return r;
-}
-
-void gpr_subprocess_destroy(gpr_subprocess* p) {
- if (p) {
- if (!p->joined) {
- gpr_subprocess_interrupt(p);
- gpr_subprocess_join(p);
- }
- if (p->pi.hProcess) {
- CloseHandle(p->pi.hProcess);
- }
- if (p->pi.hThread) {
- CloseHandle(p->pi.hThread);
- }
- gpr_free(p);
- }
-}
-
-int gpr_subprocess_join(gpr_subprocess* p) {
- DWORD dwExitCode;
- if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
- if (dwExitCode == STILL_ACTIVE) {
- if (WaitForSingleObject(p->pi.hProcess, INFINITE) == WAIT_OBJECT_0) {
- p->joined = 1;
- goto getExitCode;
- }
- return -1; // failed to join
- } else {
- goto getExitCode;
- }
- } else {
- return -1; // failed to get exit code
- }
-
-getExitCode:
- if (p->interrupted) {
- return 0;
- }
- if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
- return (int)dwExitCode;
- } else {
- return -1; // failed to get exit code
- }
-}
-
-void gpr_subprocess_interrupt(gpr_subprocess* p) {
- DWORD dwExitCode;
- if (GetExitCodeProcess(p->pi.hProcess, &dwExitCode)) {
- if (dwExitCode == STILL_ACTIVE) {
- gpr_log(GPR_INFO, "sending ctrl-break");
- GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p->pi.dwProcessId);
- p->joined = 1;
- p->interrupted = 1;
- }
- }
- return;
-}
-
-#endif /* GPR_WINDOWS_SUBPROCESS */
diff --git a/src/core/lib/gpr/sync_posix.cc b/src/core/lib/gpr/sync_posix.cc
index c3f6b10463..848d23730c 100644
--- a/src/core/lib/gpr/sync_posix.cc
+++ b/src/core/lib/gpr/sync_posix.cc
@@ -43,23 +43,19 @@ void gpr_mu_lock(gpr_mu* mu) {
#ifdef GPR_LOW_LEVEL_COUNTERS
GPR_ATM_INC_COUNTER(gpr_mu_locks);
#endif
- GPR_TIMER_BEGIN("gpr_mu_lock", 0);
+ GPR_TIMER_SCOPE("gpr_mu_lock", 0);
GPR_ASSERT(pthread_mutex_lock(mu) == 0);
- GPR_TIMER_END("gpr_mu_lock", 0);
}
void gpr_mu_unlock(gpr_mu* mu) {
- GPR_TIMER_BEGIN("gpr_mu_unlock", 0);
+ GPR_TIMER_SCOPE("gpr_mu_unlock", 0);
GPR_ASSERT(pthread_mutex_unlock(mu) == 0);
- GPR_TIMER_END("gpr_mu_unlock", 0);
}
int gpr_mu_trylock(gpr_mu* mu) {
- int err;
- GPR_TIMER_BEGIN("gpr_mu_trylock", 0);
- err = pthread_mutex_trylock(mu);
+ GPR_TIMER_SCOPE("gpr_mu_trylock", 0);
+ int err = pthread_mutex_trylock(mu);
GPR_ASSERT(err == 0 || err == EBUSY);
- GPR_TIMER_END("gpr_mu_trylock", 0);
return err == 0;
}
@@ -88,7 +84,7 @@ int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
#else
abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
#endif // GPR_LINUX
- abs_deadline_ts.tv_sec = (time_t)abs_deadline.tv_sec;
+ abs_deadline_ts.tv_sec = static_cast<time_t>(abs_deadline.tv_sec);
abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
}
diff --git a/src/core/lib/gpr/thd_posix.cc b/src/core/lib/gpr/thd_posix.cc
index cfff0df6de..e8730b9c66 100644
--- a/src/core/lib/gpr/thd_posix.cc
+++ b/src/core/lib/gpr/thd_posix.cc
@@ -26,12 +26,12 @@
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
#include <grpc/support/thd.h>
-#include <grpc/support/useful.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "src/core/lib/gpr/fork.h"
+#include "src/core/lib/gpr/useful.h"
static gpr_mu g_mu;
static gpr_cv g_cv;
@@ -49,7 +49,7 @@ static void dec_thd_count();
/* Body of every thread started via gpr_thd_new. */
static void* thread_body(void* v) {
- struct thd_arg a = *(struct thd_arg*)v;
+ struct thd_arg a = *static_cast<struct thd_arg*>(v);
free(v);
if (a.name != nullptr) {
#if GPR_APPLE_PTHREAD_NAME
@@ -77,7 +77,7 @@ int gpr_thd_new(gpr_thd_id* t, const char* thd_name,
pthread_t p;
/* don't use gpr_malloc as we may cause an infinite recursion with
* the profiling code */
- struct thd_arg* a = (struct thd_arg*)malloc(sizeof(*a));
+ struct thd_arg* a = static_cast<struct thd_arg*>(malloc(sizeof(*a)));
GPR_ASSERT(a != nullptr);
a->body = thd_body;
a->arg = arg;
diff --git a/src/core/lib/gpr/time.cc b/src/core/lib/gpr/time.cc
index 6903674d75..39ebeb4339 100644
--- a/src/core/lib/gpr/time.cc
+++ b/src/core/lib/gpr/time.cc
@@ -81,8 +81,9 @@ static gpr_timespec to_seconds_from_sub_second_time(int64_t time_in_units,
units_per_sec) -
1;
}
- out.tv_nsec = (int32_t)((time_in_units - out.tv_sec * units_per_sec) *
- GPR_NS_PER_SEC / units_per_sec);
+ out.tv_nsec =
+ static_cast<int32_t>((time_in_units - out.tv_sec * units_per_sec) *
+ GPR_NS_PER_SEC / units_per_sec);
out.clock_type = type;
}
return out;
@@ -216,12 +217,13 @@ int32_t gpr_time_to_millis(gpr_timespec t) {
care?) */
return -2147483647;
} else {
- return (int32_t)(t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS);
+ return static_cast<int32_t>(t.tv_sec * GPR_MS_PER_SEC +
+ t.tv_nsec / GPR_NS_PER_MS);
}
}
double gpr_timespec_to_micros(gpr_timespec t) {
- return (double)t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3;
+ return static_cast<double>(t.tv_sec) * GPR_US_PER_SEC + t.tv_nsec * 1e-3;
}
gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type) {
diff --git a/src/core/lib/gpr/time_posix.cc b/src/core/lib/gpr/time_posix.cc
index 9c7e86b080..09171c9c48 100644
--- a/src/core/lib/gpr/time_posix.cc
+++ b/src/core/lib/gpr/time_posix.cc
@@ -37,7 +37,7 @@ static struct timespec timespec_from_gpr(gpr_timespec gts) {
/* fine to assert, as this is only used in gpr_sleep_until */
GPR_ASSERT(gts.tv_sec <= INT32_MAX && gts.tv_sec >= INT32_MIN);
}
- rv.tv_sec = (time_t)gts.tv_sec;
+ rv.tv_sec = static_cast<time_t>(gts.tv_sec);
rv.tv_nsec = gts.tv_nsec;
return rv;
}
@@ -52,7 +52,7 @@ static gpr_timespec gpr_from_timespec(struct timespec ts,
*/
gpr_timespec rv;
rv.tv_sec = ts.tv_sec;
- rv.tv_nsec = (int32_t)ts.tv_nsec;
+ rv.tv_nsec = static_cast<int32_t>(ts.tv_nsec);
rv.clock_type = clock_type;
return rv;
}
diff --git a/src/core/lib/gpr/tls.h b/src/core/lib/gpr/tls.h
new file mode 100644
index 0000000000..aee8f4d941
--- /dev/null
+++ b/src/core/lib/gpr/tls.h
@@ -0,0 +1,68 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_GPR_TLS_H
+#define GRPC_CORE_LIB_GPR_TLS_H
+
+#include <grpc/support/port_platform.h>
+
+/** Thread local storage.
+
+ A minimal wrapper that should be implementable across many compilers,
+ and implementable efficiently across most modern compilers.
+
+ Thread locals have type intptr_t.
+
+ Declaring a thread local variable 'foo':
+ GPR_TLS_DECL(foo);
+ Thread locals always have static scope.
+
+ Declaring a thread local class variable 'foo':
+ GPR_TLS_CLASS_DECL(foo);
+
+ Defining the thread local class variable:
+ GPR_TLS_CLASS_DEF(foo);
+
+ Initializing a thread local (must be done at library initialization
+ time):
+ gpr_tls_init(&foo);
+
+ Destroying a thread local:
+ gpr_tls_destroy(&foo);
+
+ Setting a thread local (returns new_value):
+ gpr_tls_set(&foo, new_value);
+
+ Accessing a thread local:
+ current_value = gpr_tls_get(&foo);
+
+ ALL functions here may be implemented as macros. */
+
+#ifdef GPR_GCC_TLS
+#include "src/core/lib/gpr/tls_gcc.h"
+#endif
+
+#ifdef GPR_MSVC_TLS
+#include "src/core/lib/gpr/tls_msvc.h"
+#endif
+
+#ifdef GPR_PTHREAD_TLS
+#include "src/core/lib/gpr/tls_pthread.h"
+#endif
+
+#endif /* GRPC_CORE_LIB_GPR_TLS_H */
diff --git a/src/core/lib/gpr/tls_gcc.h b/src/core/lib/gpr/tls_gcc.h
new file mode 100644
index 0000000000..14c59eca55
--- /dev/null
+++ b/src/core/lib/gpr/tls_gcc.h
@@ -0,0 +1,50 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_GPR_TLS_GCC_H
+#define GRPC_CORE_LIB_GPR_TLS_GCC_H
+
+#include <stdbool.h>
+
+#include <grpc/support/log.h>
+
+/** Thread local storage based on gcc compiler primitives.
+ #include tls.h to use this - and see that file for documentation */
+
+struct gpr_gcc_thread_local {
+ intptr_t value;
+};
+
+#define GPR_TLS_DECL(name) \
+ static __thread struct gpr_gcc_thread_local name = {0}
+
+#define GPR_TLS_CLASS_DECL(name) \
+ static __thread struct gpr_gcc_thread_local name
+
+#define GPR_TLS_CLASS_DEF(name) __thread struct gpr_gcc_thread_local name = {0}
+
+#define gpr_tls_init(tls) \
+ do { \
+ } while (0)
+#define gpr_tls_destroy(tls) \
+ do { \
+ } while (0)
+#define gpr_tls_set(tls, new_value) (((tls)->value) = (new_value))
+#define gpr_tls_get(tls) ((tls)->value)
+
+#endif /* GRPC_CORE_LIB_GPR_TLS_GCC_H */
diff --git a/src/core/lib/gpr/tls_msvc.h b/src/core/lib/gpr/tls_msvc.h
new file mode 100644
index 0000000000..a6cc4174be
--- /dev/null
+++ b/src/core/lib/gpr/tls_msvc.h
@@ -0,0 +1,50 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_GPR_TLS_MSVC_H
+#define GRPC_CORE_LIB_GPR_TLS_MSVC_H
+
+/** Thread local storage based on ms visual c compiler primitives.
+ #include tls.h to use this - and see that file for documentation */
+
+struct gpr_msvc_thread_local {
+ intptr_t value;
+};
+
+/** Use GPR_TLS_DECL to declare tls static variables outside a class */
+#define GPR_TLS_DECL(name) \
+ static __declspec(thread) struct gpr_msvc_thread_local name = {0}
+
+/** Use GPR_TLS_CLASS_DECL to declare tls static variable members of a class.
+ * GPR_TLS_CLASS_DEF needs to be called to define this member. */
+#define GPR_TLS_CLASS_DECL(name) \
+ static __declspec(thread) struct gpr_msvc_thread_local name
+
+#define GPR_TLS_CLASS_DEF(name) \
+ __declspec(thread) struct gpr_msvc_thread_local name = {0}
+
+#define gpr_tls_init(tls) \
+ do { \
+ } while (0)
+#define gpr_tls_destroy(tls) \
+ do { \
+ } while (0)
+#define gpr_tls_set(tls, new_value) (((tls)->value) = (new_value))
+#define gpr_tls_get(tls) ((tls)->value)
+
+#endif /* GRPC_CORE_LIB_GPR_TLS_MSVC_H */
diff --git a/src/core/lib/gpr/tls_pthread.cc b/src/core/lib/gpr/tls_pthread.cc
index ebeef2a8c2..2e5b306909 100644
--- a/src/core/lib/gpr/tls_pthread.cc
+++ b/src/core/lib/gpr/tls_pthread.cc
@@ -20,7 +20,7 @@
#ifdef GPR_PTHREAD_TLS
-#include <grpc/support/tls.h>
+#include "src/core/lib/gpr/tls.h"
intptr_t gpr_tls_set(struct gpr_pthread_thread_local* tls, intptr_t value) {
GPR_ASSERT(0 == pthread_setspecific(tls->key, (void*)value));
diff --git a/src/core/lib/gpr/tls_pthread.h b/src/core/lib/gpr/tls_pthread.h
new file mode 100644
index 0000000000..9202653dcb
--- /dev/null
+++ b/src/core/lib/gpr/tls_pthread.h
@@ -0,0 +1,54 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_GPR_TLS_PTHREAD_H
+#define GRPC_CORE_LIB_GPR_TLS_PTHREAD_H
+
+#include <grpc/support/log.h> /* for GPR_ASSERT */
+#include <pthread.h>
+
+/** Thread local storage based on pthread library calls.
+ #include tls.h to use this - and see that file for documentation */
+
+struct gpr_pthread_thread_local {
+ pthread_key_t key;
+};
+
+/** Use GPR_TLS_DECL to declare tls static variables outside a class */
+#define GPR_TLS_DECL(name) static struct gpr_pthread_thread_local name = {0}
+
+/** Use GPR_TLS_CLASS_DECL to declare tls static variable members of a class.
+ * GPR_TLS_CLASS_DEF needs to be called to define this member. */
+#define GPR_TLS_CLASS_DECL(name) static struct gpr_pthread_thread_local name
+
+/** Use GPR_TLS_CLASS_DEF to declare tls static variable members of a class.
+ * GPR_TLS_CLASS_DEF needs to be called to define this member. */
+#define GPR_TLS_CLASS_DEF(name) struct gpr_pthread_thread_local name = {0}
+
+#define gpr_tls_init(tls) GPR_ASSERT(0 == pthread_key_create(&(tls)->key, NULL))
+#define gpr_tls_destroy(tls) pthread_key_delete((tls)->key)
+#define gpr_tls_get(tls) ((intptr_t)pthread_getspecific((tls)->key))
+#ifdef __cplusplus
+extern "C" {
+#endif
+intptr_t gpr_tls_set(struct gpr_pthread_thread_local* tls, intptr_t value);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GRPC_CORE_LIB_GPR_TLS_PTHREAD_H */
diff --git a/src/core/lib/gpr/useful.h b/src/core/lib/gpr/useful.h
new file mode 100644
index 0000000000..a4e73b9a61
--- /dev/null
+++ b/src/core/lib/gpr/useful.h
@@ -0,0 +1,65 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_GPR_USEFUL_H
+#define GRPC_CORE_LIB_GPR_USEFUL_H
+
+/** useful macros that don't belong anywhere else */
+
+#define GPR_MIN(a, b) ((a) < (b) ? (a) : (b))
+#define GPR_MAX(a, b) ((a) > (b) ? (a) : (b))
+#define GPR_CLAMP(a, min, max) ((a) < (min) ? (min) : (a) > (max) ? (max) : (a))
+/** rotl, rotr assume x is unsigned */
+#define GPR_ROTL(x, n) (((x) << (n)) | ((x) >> (sizeof(x) * 8 - (n))))
+#define GPR_ROTR(x, n) (((x) >> (n)) | ((x) << (sizeof(x) * 8 - (n))))
+
+#define GPR_ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
+
+#define GPR_SWAP(type, a, b) \
+ do { \
+ type x = a; \
+ a = b; \
+ b = x; \
+ } while (0)
+
+/** Set the \a n-th bit of \a i (a mutable pointer). */
+#define GPR_BITSET(i, n) ((*(i)) |= (1u << (n)))
+
+/** Clear the \a n-th bit of \a i (a mutable pointer). */
+#define GPR_BITCLEAR(i, n) ((*(i)) &= ~(1u << (n)))
+
+/** Get the \a n-th bit of \a i */
+#define GPR_BITGET(i, n) (((i) & (1u << (n))) != 0)
+
+#define GPR_INTERNAL_HEXDIGIT_BITCOUNT(x) \
+ ((x) - (((x) >> 1) & 0x77777777) - (((x) >> 2) & 0x33333333) - \
+ (((x) >> 3) & 0x11111111))
+
+/** Returns number of bits set in bitset \a i */
+#define GPR_BITCOUNT(i) \
+ (((GPR_INTERNAL_HEXDIGIT_BITCOUNT(i) + \
+ (GPR_INTERNAL_HEXDIGIT_BITCOUNT(i) >> 4)) & \
+ 0x0f0f0f0f) % \
+ 255)
+
+#define GPR_ICMP(a, b) ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
+
+#define GPR_HASH_POINTER(x, range) \
+ ((((size_t)x) >> 4) ^ (((size_t)x) >> 9) ^ (((size_t)x) >> 14)) % (range)
+
+#endif /* GRPC_CORE_LIB_GPR_USEFUL_H */