From da4d1ea3c8a6c93e277ee7af9486d2ddffed2b70 Mon Sep 17 00:00:00 2001
From: whh8b <whh8b@git.zephyr-software.com>
Date: Wed, 17 Feb 2016 04:13:18 +0000
Subject: [PATCH] Add GetFreeRanges() support.

---
 include/zipr_mem_space.h |  2 ++
 src/memory_space.cpp     | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/zipr_mem_space.h b/include/zipr_mem_space.h
index ccc7096..8e46e41 100644
--- a/include/zipr_mem_space.h
+++ b/include/zipr_mem_space.h
@@ -51,6 +51,7 @@ class ZiprMemorySpace_t : public MemorySpace_t
 		void MergeFreeRange(RangeAddress_t addr);
 		RangeSet_t::iterator FindFreeRange(RangeAddress_t addr);
 		Range_t GetFreeRange(int size);
+		std::list<Range_t> GetFreeRanges(size_t size = 0);
 		std::pair<RangeSet_t::const_iterator,RangeSet_t::const_iterator>
 			GetNearbyFreeRanges(const RangeAddress_t hint, size_t count = 0);
 		void AddFreeRange(Range_t newRange);
@@ -105,6 +106,7 @@ class ZiprMemorySpace_t : public MemorySpace_t
 		RangeAddress_t max_plopped;
 		RangeAddress_t min_plopped;
 		ZiprBooleanOption_t m_verbose;
+		static bool SortRangeBySize(const Range_t &a, const Range_t &b);
 };
 
 #endif
diff --git a/src/memory_space.cpp b/src/memory_space.cpp
index 4788d3c..1d8f831 100644
--- a/src/memory_space.cpp
+++ b/src/memory_space.cpp
@@ -197,6 +197,26 @@ Range_t ZiprMemorySpace_t::GetLargeRange(void)
 	return Range_t(0,0);
 }
 
+bool ZiprMemorySpace_t::SortRangeBySize(const Range_t &a, const Range_t &b)
+{
+	return (a.GetEnd() - a.GetStart()) <= (b.GetEnd() - b.GetStart());
+}
+
+std::list<Range_t> ZiprMemorySpace_t::GetFreeRanges(size_t size)
+{
+	list<Range_t> result;
+	for( RangeSet_t::iterator it=free_ranges.begin();
+		it!=free_ranges.end();
+		++it)
+	{
+		Range_t r=*it;
+		if(r.GetEnd() - r.GetStart() >= (unsigned) size)
+			result.push_back(r);
+	}
+	result.sort(SortRangeBySize);
+	return result;
+}
+
 Range_t ZiprMemorySpace_t::GetFreeRange(int size)
 {
 	vector<Range_t> v;
-- 
GitLab