aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/gpr
diff options
context:
space:
mode:
authorGravatar Vijay Pai <vpai@google.com>2018-01-25 13:24:03 -0800
committerGravatar Vijay Pai <vpai@google.com>2018-02-02 09:16:44 -0800
commitd4d0a30c6f569e031d0bfaa31f0410b855f362a3 (patch)
tree84f3a69aaf14cdf2e0c6d4204b752f5555da6f27 /src/core/lib/gpr
parente5b0a504167fbc6277d034709aa29ea07fa09a00 (diff)
Privatize useful.h and avl.h
Diffstat (limited to 'src/core/lib/gpr')
-rw-r--r--src/core/lib/gpr/arena.cc1
-rw-r--r--src/core/lib/gpr/atm.cc3
-rw-r--r--src/core/lib/gpr/avl.cc5
-rw-r--r--src/core/lib/gpr/avl.h92
-rw-r--r--src/core/lib/gpr/cpu_posix.cc3
-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/string.cc3
-rw-r--r--src/core/lib/gpr/thd_posix.cc2
-rw-r--r--src/core/lib/gpr/useful.h65
11 files changed, 173 insertions, 13 deletions
diff --git a/src/core/lib/gpr/arena.cc b/src/core/lib/gpr/arena.cc
index 687592a140..5a8d7ff6ec 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
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
index 0b67a21f2f..9d3470fe0a 100644
--- a/src/core/lib/gpr/avl.cc
+++ b/src/core/lib/gpr/avl.cc
@@ -16,14 +16,15 @@
*
*/
-#include <grpc/support/avl.h>
+#include "src/core/lib/gpr/avl.h"
#include <assert.h>
#include <stdlib.h>
#include <grpc/support/alloc.h>
#include <grpc/support/string_util.h>
-#include <grpc/support/useful.h>
+
+#include "src/core/lib/gpr/useful.h"
gpr_avl gpr_avl_create(const gpr_avl_vtable* vtable) {
gpr_avl out;
diff --git a/src/core/lib/gpr/avl.h b/src/core/lib/gpr/avl.h
new file mode 100644
index 0000000000..f3ab202bb7
--- /dev/null
+++ b/src/core/lib/gpr/avl.h
@@ -0,0 +1,92 @@
+/*
+ *
+ * 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_AVL_H
+#define GRPC_CORE_LIB_GPR_AVL_H
+
+#include <grpc/support/sync.h>
+
+/** internal node of an AVL tree */
+typedef struct gpr_avl_node {
+ gpr_refcount refs;
+ void* key;
+ void* value;
+ struct gpr_avl_node* left;
+ struct gpr_avl_node* right;
+ long height;
+} gpr_avl_node;
+
+/** vtable for the AVL tree
+ * The optional user_data is propagated from the top level gpr_avl_XXX API.
+ * From the same API call, multiple vtable functions may be called multiple
+ * times.
+ */
+typedef struct gpr_avl_vtable {
+ /** destroy a key */
+ void (*destroy_key)(void* key, void* user_data);
+ /** copy a key, returning new value */
+ void* (*copy_key)(void* key, void* user_data);
+ /** compare key1, key2; return <0 if key1 < key2,
+ >0 if key1 > key2, 0 if key1 == key2 */
+ long (*compare_keys)(void* key1, void* key2, void* user_data);
+ /** destroy a value */
+ void (*destroy_value)(void* value, void* user_data);
+ /** copy a value */
+ void* (*copy_value)(void* value, void* user_data);
+} gpr_avl_vtable;
+
+/** "pointer" to an AVL tree - this is a reference
+ counted object - use gpr_avl_ref to add a reference,
+ gpr_avl_unref when done with a reference */
+typedef struct gpr_avl {
+ const gpr_avl_vtable* vtable;
+ gpr_avl_node* root;
+} gpr_avl;
+
+/** Create an immutable AVL tree. */
+gpr_avl gpr_avl_create(const gpr_avl_vtable* vtable);
+/** Add a reference to an existing tree - returns
+ the tree as a convenience. The optional user_data will be passed to vtable
+ functions. */
+gpr_avl gpr_avl_ref(gpr_avl avl, void* user_data);
+/** Remove a reference to a tree - destroying it if there
+ are no references left. The optional user_data will be passed to vtable
+ functions. */
+void gpr_avl_unref(gpr_avl avl, void* user_data);
+/** Return a new tree with (key, value) added to avl.
+ implicitly unrefs avl to allow easy chaining.
+ if key exists in avl, the new tree's key entry updated
+ (i.e. a duplicate is not created). The optional user_data will be passed to
+ vtable functions. */
+gpr_avl gpr_avl_add(gpr_avl avl, void* key, void* value, void* user_data);
+/** Return a new tree with key deleted
+ implicitly unrefs avl to allow easy chaining. The optional user_data will be
+ passed to vtable functions. */
+gpr_avl gpr_avl_remove(gpr_avl avl, void* key, void* user_data);
+/** Lookup key, and return the associated value.
+ Does not mutate avl.
+ Returns NULL if key is not found. The optional user_data will be passed to
+ vtable functions.*/
+void* gpr_avl_get(gpr_avl avl, void* key, void* user_data);
+/** Return 1 if avl contains key, 0 otherwise; if it has the key, sets *value to
+ its value. The optional user_data will be passed to vtable functions. */
+int gpr_avl_maybe_get(gpr_avl avl, void* key, void** value, void* user_data);
+/** Return 1 if avl is empty, 0 otherwise */
+int gpr_avl_is_empty(gpr_avl avl);
+
+#endif /* GRPC_CORE_LIB_GPR_AVL_H */
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_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/string.cc b/src/core/lib/gpr/string.cc
index 919d957d1b..6b360e4013 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;
diff --git a/src/core/lib/gpr/thd_posix.cc b/src/core/lib/gpr/thd_posix.cc
index cfff0df6de..683fd4575e 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;
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 */