aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib
diff options
context:
space:
mode:
authorGravatar Sanjay Ghemawat <sanjay@google.com>2016-07-21 09:32:06 -0700
committerGravatar Sanjay Ghemawat <sanjay@google.com>2016-07-21 09:32:06 -0700
commitfca59fb2aa4a0be093d12162c763b8563ada4087 (patch)
treeaf2b40d6c32ed3272d5577203c4e2f7c7fed26be /src/core/lib
parent76692bb28329bf10ca162a5e70083aa2650bcb7f (diff)
Add gpr_slice_new_with_user_data.
gpr_slice_new_with_user_data is like gpr_slice_new, but allows the caller to specify a distinct pointer to pass to the destroy function. This is useful when the data is part of a larger data structure that should be destroyed when the data is no longer needed.
Diffstat (limited to 'src/core/lib')
-rw-r--r--src/core/lib/support/slice.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/core/lib/support/slice.c b/src/core/lib/support/slice.c
index b9a7c77bda..8a2c0a9086 100644
--- a/src/core/lib/support/slice.c
+++ b/src/core/lib/support/slice.c
@@ -94,14 +94,16 @@ static void new_slice_unref(void *p) {
}
}
-gpr_slice gpr_slice_new(void *p, size_t len, void (*destroy)(void *)) {
+gpr_slice gpr_slice_new_with_user_data(void *p, size_t len,
+ void (*destroy)(void *),
+ void *user_data) {
gpr_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->user_destroy = destroy;
- rc->user_data = p;
+ rc->user_data = user_data;
slice.refcount = &rc->rc;
slice.data.refcounted.bytes = p;
@@ -109,6 +111,11 @@ gpr_slice gpr_slice_new(void *p, size_t len, void (*destroy)(void *)) {
return slice;
}
+gpr_slice gpr_slice_new(void *p, size_t len, void (*destroy)(void *)) {
+ /* Pass "p" to *destroy when the slice is no longer needed. */
+ return gpr_slice_new_with_user_data(p, len, destroy, p);
+}
+
/* gpr_slice_new_with_len support structures - we create a refcount object
extended with the user provided data pointer & destroy function */
typedef struct new_with_len_slice_refcount {