From 6451d35e929c80fe4249761959186b382c7c5558 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Mon, 27 Nov 2017 16:32:54 +0000 Subject: [PATCH] Fix block refcounts to use saturating arithmetic. --- blocks_runtime.m | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/blocks_runtime.m b/blocks_runtime.m index 844d487..74e2163 100644 --- a/blocks_runtime.m +++ b/blocks_runtime.m @@ -60,8 +60,10 @@ static int increment24(int *ref) { int old = *ref; int val = old & BLOCK_REFCOUNT_MASK; - // FIXME: We should gracefully handle refcount overflow, but for now we - // just give up + if (val == BLOCK_REFCOUNT_MASK) + { + return val; + } assert(val < BLOCK_REFCOUNT_MASK); if (!__sync_bool_compare_and_swap(ref, old, old+1)) { @@ -74,8 +76,10 @@ static int decrement24(int *ref) { int old = *ref; int val = old & BLOCK_REFCOUNT_MASK; - // FIXME: We should gracefully handle refcount overflow, but for now we - // just give up + if (val == BLOCK_REFCOUNT_MASK) + { + return val; + } assert(val > 0); if (!__sync_bool_compare_and_swap(ref, old, old-1)) {