From 85f88b909b46cdf6799710f7912145c96eecc3ed Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 14 May 2018 19:16:42 -0700 Subject: [PATCH] Add a swap function that swaps contents of references. --- include/refcount.h | 4 ++++ src/refcount.c | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/include/refcount.h b/include/refcount.h index c6c1ed8..bd6a046 100644 --- a/include/refcount.h +++ b/include/refcount.h @@ -83,6 +83,10 @@ void libab_ref_free(libab_ref* ref); * Copies this reference, thereby increasing the reference count. */ void libab_ref_copy(const libab_ref* ref, libab_ref* into); +/** + * Swaps the contents of two references. + */ +void libab_ref_swap(libab_ref* left, libab_ref* right); /** * Function that can be passed in to refcount to simply use free * when the refcount reaches 0. diff --git a/src/refcount.c b/src/refcount.c index 93a2963..92a638f 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -57,6 +57,13 @@ void libab_ref_copy(const libab_ref* ref, libab_ref* into) { memcpy(into, ref, sizeof(*ref)); } +void libab_ref_swap(libab_ref* left, libab_ref* right) { + libab_ref tmp; + tmp = *left; + *left = *right; + *right = tmp; +} + void libab_ref_data_free(void* data) { free(data); } void* libab_ref_get(const libab_ref* ref) {