From b12562978b1ba90dfa20df32cd4e27e04b240cb0 Mon Sep 17 00:00:00 2001 From: Inga Stotland Date: Wed, 15 Aug 2018 13:16:52 -0700 Subject: [PATCH] tools/mesh: Fix reclaiming address range The computations for reclaiming released unicast addresses back into address pool were off by 1. Also, the adjacent pools need to be merged, if the released range fills the gap between two address pools. Change-Id: If420d6a914067880250d78f6de955a071fcf5fe7 Signed-off-by: Anupam Roy --- tools/mesh/net.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/tools/mesh/net.c b/tools/mesh/net.c index d1b3f79..f1ef23a 100644 --- a/tools/mesh/net.c +++ b/tools/mesh/net.c @@ -730,7 +730,7 @@ static int match_address_range(const void *a, const void *b) uint16_t max = range >> 16; uint16_t min = range & 0xffff; - return ((max - min) >= (num_elements - 1)) ? 0 : -1; + return ((max - min + 1) >= num_elements) ? 0 : -1; } @@ -749,13 +749,14 @@ uint16_t net_obtain_address(uint8_t num_eles) addr = min; min += num_eles; - if (min > max) + if (min > max) { net.address_pool = g_list_delete_link(net.address_pool, l); - else { + } else { range = min + (max << 16); l->data = GUINT_TO_POINTER(range); } + return addr; } @@ -775,23 +776,42 @@ void net_release_address(uint16_t addr, uint8_t num_elements) GList *l; uint32_t range; + if (addr == UNASSIGNED_ADDRESS) + return; + for (l = net.address_pool; l != NULL; l = l->next) { uint16_t max; uint16_t min; + GList *l1 = l->next; range = GPOINTER_TO_UINT(l->data); max = range >> 16; min = range & 0xffff; - if (min == (addr + num_elements + 1)) + if (min == (addr + num_elements)) min = addr; - else if (addr && max == (addr - 1)) - max = addr + num_elements + 1; + else if (max == (addr - 1)) + max = addr + num_elements; else continue; + /* Check if range pools need to be merged */ + if (l1) { + uint16_t min1; + + range = GPOINTER_TO_UINT(l1->data); + min1 = range & 0xffff; + + if (min1 == (max + 1)) { + max = range >> 16; + l->next = l1->next; + net.address_pool = g_list_delete_link( + net.address_pool, l1); + } + } + range = min + (max << 16); l->data = GUINT_TO_POINTER(range); return; -- 2.7.4