diff options
author | Craig Tiller <ctiller@google.com> | 2016-11-17 09:34:14 -0800 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2016-11-17 09:34:14 -0800 |
commit | 0451c3dbfcf7dcab791a4a2b10f284ac4723da8e (patch) | |
tree | a5da5a43d137808439058745b996bf121c461cc1 /src/core | |
parent | d7f15831c6a4dcd71fe10babde7bb0b380fb1eff (diff) |
Move slice functions to vtable, implement hash
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/ext/transport/chttp2/transport/hpack_encoder.c | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/resource_quota.c | 6 | ||||
-rw-r--r-- | src/core/lib/slice/slice.c | 32 | ||||
-rw-r--r-- | src/core/lib/slice/slice_hash_table.c | 2 | ||||
-rw-r--r-- | src/core/lib/slice/slice_intern.c | 25 | ||||
-rw-r--r-- | src/core/lib/transport/static_metadata.c | 6 |
6 files changed, 53 insertions, 20 deletions
diff --git a/src/core/ext/transport/chttp2/transport/hpack_encoder.c b/src/core/ext/transport/chttp2/transport/hpack_encoder.c index 9b27071c87..651f813c7e 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_encoder.c +++ b/src/core/ext/transport/chttp2/transport/hpack_encoder.c @@ -64,7 +64,7 @@ /* don't consider adding anything bigger than this to the hpack table */ #define MAX_DECODER_SPACE_USAGE 512 -static grpc_slice_refcount terminal_slice_refcount = {NULL, NULL}; +static grpc_slice_refcount terminal_slice_refcount = {NULL}; static const grpc_slice terminal_slice = {&terminal_slice_refcount, .data.refcounted = {0, 0}}; diff --git a/src/core/lib/iomgr/resource_quota.c b/src/core/lib/iomgr/resource_quota.c index efcb0f8174..3b63773071 100644 --- a/src/core/lib/iomgr/resource_quota.c +++ b/src/core/lib/iomgr/resource_quota.c @@ -366,11 +366,13 @@ static void ru_slice_unref(grpc_exec_ctx *exec_ctx, void *p) { } } +static const grpc_slice_refcount_vtable ru_slice_vtable = { + ru_slice_ref, ru_slice_unref, grpc_slice_default_hash_impl}; + static grpc_slice ru_slice_create(grpc_resource_user *resource_user, size_t size) { ru_slice_refcount *rc = gpr_malloc(sizeof(ru_slice_refcount) + size); - rc->base.ref = ru_slice_ref; - rc->base.unref = ru_slice_unref; + rc->base.vtable = &ru_slice_vtable; gpr_ref_init(&rc->refs, 1); rc->resource_user = resource_user; rc->size = size; diff --git a/src/core/lib/slice/slice.c b/src/core/lib/slice/slice.c index dc3e6fd93d..f2c09d60e5 100644 --- a/src/core/lib/slice/slice.c +++ b/src/core/lib/slice/slice.c @@ -50,14 +50,14 @@ grpc_slice grpc_empty_slice(void) { grpc_slice grpc_slice_ref_internal(grpc_slice slice) { if (slice.refcount) { - slice.refcount->ref(slice.refcount); + slice.refcount->vtable->ref(slice.refcount); } return slice; } void grpc_slice_unref_internal(grpc_exec_ctx *exec_ctx, grpc_slice slice) { if (slice.refcount) { - slice.refcount->unref(exec_ctx, slice.refcount); + slice.refcount->vtable->unref(exec_ctx, slice.refcount); } } @@ -78,7 +78,9 @@ void grpc_slice_unref(grpc_slice slice) { static void noop_ref(void *unused) {} static void noop_unref(grpc_exec_ctx *exec_ctx, void *unused) {} -static grpc_slice_refcount noop_refcount = {noop_ref, noop_unref}; +static const grpc_slice_refcount_vtable noop_refcount_vtable = { + noop_ref, noop_unref, grpc_slice_default_hash_impl}; +static grpc_slice_refcount noop_refcount = {&noop_refcount_vtable}; grpc_slice grpc_slice_from_static_string(const char *s) { grpc_slice slice; @@ -110,14 +112,16 @@ static void new_slice_unref(grpc_exec_ctx *exec_ctx, void *p) { } } +static const grpc_slice_refcount_vtable new_slice_vtable = { + new_slice_ref, new_slice_unref, grpc_slice_default_hash_impl}; + grpc_slice grpc_slice_new_with_user_data(void *p, size_t len, void (*destroy)(void *), void *user_data) { grpc_slice slice; new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount)); gpr_ref_init(&rc->refs, 1); - rc->rc.ref = new_slice_ref; - rc->rc.unref = new_slice_unref; + rc->rc.vtable = &new_slice_vtable; rc->user_destroy = destroy; rc->user_data = user_data; @@ -155,14 +159,16 @@ static void new_with_len_unref(grpc_exec_ctx *exec_ctx, void *p) { } } +static const grpc_slice_refcount_vtable new_with_len_vtable = { + new_with_len_ref, new_with_len_unref, grpc_slice_default_hash_impl}; + grpc_slice grpc_slice_new_with_len(void *p, size_t len, void (*destroy)(void *, size_t)) { grpc_slice slice; new_with_len_slice_refcount *rc = gpr_malloc(sizeof(new_with_len_slice_refcount)); gpr_ref_init(&rc->refs, 1); - rc->rc.ref = new_with_len_ref; - rc->rc.unref = new_with_len_unref; + rc->rc.vtable = &new_with_len_vtable; rc->user_destroy = destroy; rc->user_data = p; rc->user_length = len; @@ -200,6 +206,9 @@ static void malloc_unref(grpc_exec_ctx *exec_ctx, void *p) { } } +static const grpc_slice_refcount_vtable malloc_vtable = { + malloc_ref, malloc_unref, grpc_slice_default_hash_impl}; + grpc_slice grpc_slice_malloc(size_t length) { grpc_slice slice; @@ -219,8 +228,7 @@ grpc_slice grpc_slice_malloc(size_t length) { this reference. */ gpr_ref_init(&rc->refs, 1); - rc->base.ref = malloc_ref; - rc->base.unref = malloc_unref; + rc->base.vtable = &malloc_vtable; /* Build up the slice to be returned. */ /* The slices refcount points back to the allocated block. */ @@ -273,7 +281,7 @@ grpc_slice grpc_slice_sub(grpc_slice source, size_t begin, size_t end) { } else { subset = grpc_slice_sub_no_ref(source, begin, end); /* Bump the refcount */ - subset.refcount->ref(subset.refcount); + subset.refcount->vtable->ref(subset.refcount); } return subset; } @@ -302,7 +310,7 @@ grpc_slice grpc_slice_split_tail(grpc_slice *source, size_t split) { /* Build the result */ tail.refcount = source->refcount; /* Bump the refcount */ - tail.refcount->ref(tail.refcount); + tail.refcount->vtable->ref(tail.refcount); /* Point into the source array */ tail.data.refcounted.bytes = source->data.refcounted.bytes + split; tail.data.refcounted.length = tail_length; @@ -340,7 +348,7 @@ grpc_slice grpc_slice_split_head(grpc_slice *source, size_t split) { /* Build the result */ head.refcount = source->refcount; /* Bump the refcount */ - head.refcount->ref(head.refcount); + head.refcount->vtable->ref(head.refcount); /* Point into the source array */ head.data.refcounted.bytes = source->data.refcounted.bytes; head.data.refcounted.length = split; diff --git a/src/core/lib/slice/slice_hash_table.c b/src/core/lib/slice/slice_hash_table.c index 7e6f705164..10b41343ea 100644 --- a/src/core/lib/slice/slice_hash_table.c +++ b/src/core/lib/slice/slice_hash_table.c @@ -40,7 +40,7 @@ #include "src/core/lib/slice/slice_internal.h" #include "src/core/lib/transport/metadata.h" -static grpc_slice_refcount terminal_slice_refcount = {NULL, NULL}; +static grpc_slice_refcount terminal_slice_refcount = {NULL}; static const grpc_slice terminal_slice = {&terminal_slice_refcount, .data.refcounted = {0, 0}}; diff --git a/src/core/lib/slice/slice_intern.c b/src/core/lib/slice/slice_intern.c index 8f48a36101..a7e17527c3 100644 --- a/src/core/lib/slice/slice_intern.c +++ b/src/core/lib/slice/slice_intern.c @@ -100,6 +100,18 @@ static void interned_slice_unref(grpc_exec_ctx *exec_ctx, void *p) { } } +static uint32_t interned_slice_hash(void *p, grpc_slice slice) { + interned_slice_refcount *s = p; + if (slice.data.refcounted.bytes == (uint8_t *)(s + 1) && + slice.data.refcounted.length == s->length) { + return s->hash; + } + return grpc_slice_default_hash_impl(p, slice); +} + +static const grpc_slice_refcount_vtable interned_slice_vtable = { + interned_slice_ref, interned_slice_unref, interned_slice_hash}; + static void grow_shard(slice_shard *shard) { size_t capacity = shard->capacity * 2; size_t i; @@ -135,6 +147,16 @@ static grpc_slice materialize(interned_slice_refcount *s) { return slice; } +uint32_t grpc_slice_default_hash_impl(void *unused_refcnt, grpc_slice s) { + return gpr_murmur_hash3(GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s), + g_hash_seed); +} + +uint32_t grpc_slice_hash(grpc_slice s) { + return s.refcount == NULL ? grpc_slice_default_hash_impl(NULL, s) + : s.refcount->vtable->hash(s.refcount, s); +} + grpc_slice grpc_slice_intern(grpc_slice slice) { interned_slice_refcount *s; uint32_t hash = gpr_murmur_hash3(GRPC_SLICE_START_PTR(slice), @@ -168,8 +190,7 @@ grpc_slice grpc_slice_intern(grpc_slice slice) { gpr_atm_rel_store(&s->refcnt, 1); s->length = GRPC_SLICE_LENGTH(slice); s->hash = hash; - s->base.ref = interned_slice_ref; - s->base.unref = interned_slice_unref; + s->base.vtable = &interned_slice_vtable; s->bucket_next = shard->strs[idx]; shard->strs[idx] = s; memcpy(s + 1, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice)); diff --git a/src/core/lib/transport/static_metadata.c b/src/core/lib/transport/static_metadata.c index 0eb5cd5d74..b30b8a046b 100644 --- a/src/core/lib/transport/static_metadata.c +++ b/src/core/lib/transport/static_metadata.c @@ -114,10 +114,12 @@ static uint8_t g_raw_bytes[] = { static void static_ref(void *unused) {} static void static_unref(grpc_exec_ctx *exec_ctx, void *unused) {} -static grpc_slice_refcount g_refcnt = {static_ref, static_unref}; +static const grpc_slice_refcount_vtable static_vtable = { + static_ref, static_unref, grpc_slice_default_hash_impl}; +static grpc_slice_refcount g_refcnt = {&static_vtable}; bool grpc_is_static_metadata_string(grpc_slice slice) { - return slice.refcount != NULL && slice.refcount->ref == static_ref; + return slice.refcount != NULL && slice.refcount->vtable == &static_vtable; } const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT] = { |