Fix compile, make slice.c respect sub_refcount

reviewable/pr8842/r1
Craig Tiller 8 years ago
parent 43a5169292
commit a82d2a36cf
  1. 11
      src/core/lib/slice/slice.c
  2. 12
      src/core/lib/slice/slice_intern.c
  3. 6
      src/core/lib/transport/metadata.c
  4. 4
      src/core/lib/transport/static_metadata.c
  5. 14
      src/core/lib/transport/static_metadata.h
  6. 16
      tools/codegen/core/gen_static_metadata.py

@ -43,7 +43,7 @@
grpc_slice grpc_empty_slice(void) { grpc_slice grpc_empty_slice(void) {
grpc_slice out; grpc_slice out;
out.refcount = 0; out.refcount = NULL;
out.data.inlined.length = 0; out.data.inlined.length = 0;
return out; return out;
} }
@ -129,6 +129,7 @@ grpc_slice grpc_slice_new_with_user_data(void *p, size_t len,
new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount)); new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount));
gpr_ref_init(&rc->refs, 1); gpr_ref_init(&rc->refs, 1);
rc->rc.vtable = &new_slice_vtable; rc->rc.vtable = &new_slice_vtable;
rc->rc.sub_refcount = &rc->rc;
rc->user_destroy = destroy; rc->user_destroy = destroy;
rc->user_data = user_data; rc->user_data = user_data;
@ -177,6 +178,7 @@ grpc_slice grpc_slice_new_with_len(void *p, size_t len,
gpr_malloc(sizeof(new_with_len_slice_refcount)); gpr_malloc(sizeof(new_with_len_slice_refcount));
gpr_ref_init(&rc->refs, 1); gpr_ref_init(&rc->refs, 1);
rc->rc.vtable = &new_with_len_vtable; rc->rc.vtable = &new_with_len_vtable;
rc->rc.sub_refcount = &rc->rc;
rc->user_destroy = destroy; rc->user_destroy = destroy;
rc->user_data = p; rc->user_data = p;
rc->user_length = len; rc->user_length = len;
@ -238,6 +240,7 @@ grpc_slice grpc_slice_malloc(size_t length) {
gpr_ref_init(&rc->refs, 1); gpr_ref_init(&rc->refs, 1);
rc->base.vtable = &malloc_vtable; rc->base.vtable = &malloc_vtable;
rc->base.sub_refcount = &rc->base;
/* Build up the slice to be returned. */ /* Build up the slice to be returned. */
/* The slices refcount points back to the allocated block. */ /* The slices refcount points back to the allocated block. */
@ -264,7 +267,7 @@ grpc_slice grpc_slice_sub_no_ref(grpc_slice source, size_t begin, size_t end) {
GPR_ASSERT(source.data.refcounted.length >= end); GPR_ASSERT(source.data.refcounted.length >= end);
/* Build the result */ /* Build the result */
subset.refcount = source.refcount; subset.refcount = source.refcount->sub_refcount;
/* Point into the source array */ /* Point into the source array */
subset.data.refcounted.bytes = source.data.refcounted.bytes + begin; subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
subset.data.refcounted.length = end - begin; subset.data.refcounted.length = end - begin;
@ -317,7 +320,7 @@ grpc_slice grpc_slice_split_tail(grpc_slice *source, size_t split) {
tail_length); tail_length);
} else { } else {
/* Build the result */ /* Build the result */
tail.refcount = source->refcount; tail.refcount = source->refcount->sub_refcount;
/* Bump the refcount */ /* Bump the refcount */
tail.refcount->vtable->ref(tail.refcount); tail.refcount->vtable->ref(tail.refcount);
/* Point into the source array */ /* Point into the source array */
@ -355,7 +358,7 @@ grpc_slice grpc_slice_split_head(grpc_slice *source, size_t split) {
GPR_ASSERT(source->data.refcounted.length >= split); GPR_ASSERT(source->data.refcounted.length >= split);
/* Build the result */ /* Build the result */
head.refcount = source->refcount; head.refcount = source->refcount->sub_refcount;
/* Bump the refcount */ /* Bump the refcount */
head.refcount->vtable->ref(head.refcount); head.refcount->vtable->ref(head.refcount);
/* Point into the source array */ /* Point into the source array */

@ -176,7 +176,7 @@ uint32_t grpc_slice_default_hash_impl(grpc_slice s) {
} }
uint32_t grpc_static_slice_hash(grpc_slice s) { uint32_t grpc_static_slice_hash(grpc_slice s) {
int id = grpc_static_metadata_index(s); int id = GRPC_STATIC_METADATA_INDEX(s);
if (id == -1) { if (id == -1) {
return grpc_slice_default_hash_impl(s); return grpc_slice_default_hash_impl(s);
} }
@ -184,8 +184,8 @@ uint32_t grpc_static_slice_hash(grpc_slice s) {
} }
int grpc_static_slice_eq(grpc_slice a, grpc_slice b) { int grpc_static_slice_eq(grpc_slice a, grpc_slice b) {
int id_a = grpc_static_metadata_index(a); int id_a = GRPC_STATIC_METADATA_INDEX(a);
int id_b = grpc_static_metadata_index(b); int id_b = GRPC_STATIC_METADATA_INDEX(b);
if (id_a == -1 || id_b == -1) { if (id_a == -1 || id_b == -1) {
return grpc_slice_default_eq_impl(a, b); return grpc_slice_default_eq_impl(a, b);
} }
@ -198,7 +198,7 @@ uint32_t grpc_slice_hash(grpc_slice s) {
} }
void grpc_slice_static_intern(grpc_slice *slice) { void grpc_slice_static_intern(grpc_slice *slice) {
if (grpc_is_static_metadata_string(*slice)) { if (GRPC_IS_STATIC_METADATA_STRING(*slice)) {
return; return;
} }
@ -217,11 +217,11 @@ void grpc_slice_static_intern(grpc_slice *slice) {
bool grpc_slice_is_interned(grpc_slice slice) { bool grpc_slice_is_interned(grpc_slice slice) {
return (slice.refcount && slice.refcount->vtable == &interned_slice_vtable) || return (slice.refcount && slice.refcount->vtable == &interned_slice_vtable) ||
grpc_is_static_metadata_string(slice); GRPC_IS_STATIC_METADATA_STRING(slice);
} }
grpc_slice grpc_slice_intern(grpc_slice slice) { grpc_slice grpc_slice_intern(grpc_slice slice) {
if (grpc_is_static_metadata_string(slice)) { if (GRPC_IS_STATIC_METADATA_STRING(slice)) {
return slice; return slice;
} }

@ -260,10 +260,10 @@ grpc_mdelem grpc_mdelem_create(
return GRPC_MAKE_MDELEM(allocated, GRPC_MDELEM_STORAGE_ALLOCATED); return GRPC_MAKE_MDELEM(allocated, GRPC_MDELEM_STORAGE_ALLOCATED);
} }
if (grpc_is_static_metadata_string(key) && if (GRPC_IS_STATIC_METADATA_STRING(key) &&
grpc_is_static_metadata_string(value)) { GRPC_IS_STATIC_METADATA_STRING(value)) {
grpc_mdelem static_elem = grpc_static_mdelem_for_static_strings( grpc_mdelem static_elem = grpc_static_mdelem_for_static_strings(
grpc_static_metadata_index(key), grpc_static_metadata_index(value)); GRPC_STATIC_METADATA_INDEX(key), GRPC_STATIC_METADATA_INDEX(value));
if (!GRPC_MDISNULL(static_elem)) { if (!GRPC_MDISNULL(static_elem)) {
return static_elem; return static_elem;
} }

@ -832,8 +832,8 @@ static const uint8_t batch_hash_to_idx[] = {
15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
grpc_metadata_batch_callouts_index grpc_batch_index_of(grpc_slice slice) { grpc_metadata_batch_callouts_index grpc_batch_index_of(grpc_slice slice) {
if (!grpc_is_static_metadata_string(slice)) return GRPC_BATCH_CALLOUTS_COUNT; if (!GRPC_IS_STATIC_METADATA_STRING(slice)) return GRPC_BATCH_CALLOUTS_COUNT;
uint32_t idx = (uint32_t)grpc_static_metadata_index(slice); uint32_t idx = (uint32_t)GRPC_STATIC_METADATA_INDEX(slice);
uint32_t hash = batch_phash(idx); uint32_t hash = batch_phash(idx);
if (hash < GPR_ARRAY_SIZE(batch_hash_to_idx) && if (hash < GPR_ARRAY_SIZE(batch_hash_to_idx) &&
batch_hash_to_idx[hash] == idx) batch_hash_to_idx[hash] == idx)

@ -247,19 +247,15 @@ extern const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT];
#define GRPC_MDSTR_IDENTITY_COMMA_DEFLATE_COMMA_GZIP \ #define GRPC_MDSTR_IDENTITY_COMMA_DEFLATE_COMMA_GZIP \
(grpc_static_slice_table[97]) (grpc_static_slice_table[97])
bool grpc_is_static_metadata_string(grpc_slice slice);
extern const grpc_slice_refcount_vtable grpc_static_metadata_vtable; extern const grpc_slice_refcount_vtable grpc_static_metadata_vtable;
extern grpc_slice_refcount extern grpc_slice_refcount
grpc_static_metadata_refcounts[GRPC_STATIC_MDSTR_COUNT]; grpc_static_metadata_refcounts[GRPC_STATIC_MDSTR_COUNT];
bool grpc_is_static_metadata_string(grpc_slice slice) { #define GRPC_IS_STATIC_METADATA_STRING(slice) \
return slice.refcount != NULL && ((slice).refcount != NULL && \
slice.refcount->vtable == &grpc_static_metadata_vtable; (slice).refcount->vtable == &grpc_static_metadata_vtable)
}
inline int grpc_static_metadata_index(grpc_slice slice) { #define GRPC_STATIC_METADATA_INDEX(static_slice) \
return (int)(slice.refcount - grpc_static_metadata_refcounts); ((int)((static_slice).refcount - grpc_static_metadata_refcounts))
}
#define GRPC_STATIC_MDELEM_COUNT 81 #define GRPC_STATIC_MDELEM_COUNT 81
extern grpc_mdelem_data grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT]; extern grpc_mdelem_data grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];

@ -343,8 +343,6 @@ for i, elem in enumerate(all_strs):
print >>H, '/* "%s" */' % elem print >>H, '/* "%s" */' % elem
print >>H, '#define %s (grpc_static_slice_table[%d])' % (mangle(elem).upper(), i) print >>H, '#define %s (grpc_static_slice_table[%d])' % (mangle(elem).upper(), i)
print >>H print >>H
print >>H, 'bool grpc_is_static_metadata_string(grpc_slice slice);'
print >>H
print >>C, 'static uint8_t g_bytes[] = {%s};' % (','.join('%d' % ord(c) for c in ''.join(all_strs))) print >>C, 'static uint8_t g_bytes[] = {%s};' % (','.join('%d' % ord(c) for c in ''.join(all_strs)))
print >>C print >>C
print >>C, 'static void static_ref(void *unused) {}' print >>C, 'static void static_ref(void *unused) {}'
@ -359,18 +357,16 @@ for i, elem in enumerate(all_strs):
print >>C, ' {&grpc_static_metadata_vtable, &static_sub_refcnt},' print >>C, ' {&grpc_static_metadata_vtable, &static_sub_refcnt},'
print >>C, '};' print >>C, '};'
print >>C print >>C
print >>H, 'bool grpc_is_static_metadata_string(grpc_slice slice) {' print >>H, '#define GRPC_IS_STATIC_METADATA_STRING(slice) \\'
print >>H, ' return slice.refcount != NULL && slice.refcount->vtable == &grpc_static_metadata_vtable;' print >>H, ' ((slice).refcount != NULL && (slice).refcount->vtable == &grpc_static_metadata_vtable)'
print >>H, '}'
print >>H print >>H
print >>C, 'const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT] = {' print >>C, 'const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT] = {'
for i, elem in enumerate(all_strs): for i, elem in enumerate(all_strs):
print >>C, slice_def(i) + ',' print >>C, slice_def(i) + ','
print >>C, '};' print >>C, '};'
print >>C print >>C
print >>H, 'inline int grpc_static_metadata_index(grpc_slice slice) {' print >>H, '#define GRPC_STATIC_METADATA_INDEX(static_slice) \\'
print >>H, ' return (int)(slice.refcount - grpc_static_metadata_refcounts);' print >>H, ' ((int)((static_slice).refcount - grpc_static_metadata_refcounts))'
print >>H, '}'
print >>H print >>H
print >>D, '# hpack fuzzing dictionary' print >>D, '# hpack fuzzing dictionary'
@ -511,8 +507,8 @@ for i, elem in enumerate( METADATA_BATCH_CALLOUTS):
print >>C, 'static const uint8_t batch_hash_to_idx[] = {%s};' % ','.join('%d' % n for n in batch_hash_to_idx) print >>C, 'static const uint8_t batch_hash_to_idx[] = {%s};' % ','.join('%d' % n for n in batch_hash_to_idx)
print >>C print >>C
print >>C, 'grpc_metadata_batch_callouts_index grpc_batch_index_of(grpc_slice slice) {' print >>C, 'grpc_metadata_batch_callouts_index grpc_batch_index_of(grpc_slice slice) {'
print >>C, ' if (!grpc_is_static_metadata_string(slice)) return GRPC_BATCH_CALLOUTS_COUNT;' print >>C, ' if (!GRPC_IS_STATIC_METADATA_STRING(slice)) return GRPC_BATCH_CALLOUTS_COUNT;'
print >>C, ' uint32_t idx = (uint32_t)grpc_static_metadata_index(slice);' print >>C, ' uint32_t idx = (uint32_t)GRPC_STATIC_METADATA_INDEX(slice);'
print >>C, ' uint32_t hash = batch_phash(idx);' print >>C, ' uint32_t hash = batch_phash(idx);'
print >>C, ' if (hash < GPR_ARRAY_SIZE(batch_hash_to_idx) && batch_hash_to_idx[hash] == idx) return (grpc_metadata_batch_callouts_index)hash;' print >>C, ' if (hash < GPR_ARRAY_SIZE(batch_hash_to_idx) && batch_hash_to_idx[hash] == idx) return (grpc_metadata_batch_callouts_index)hash;'
print >>C, ' return GRPC_BATCH_CALLOUTS_COUNT;' print >>C, ' return GRPC_BATCH_CALLOUTS_COUNT;'

Loading…
Cancel
Save