From 2eb564b756bb2b7c63c1510845a42491ef82fa8d Mon Sep 17 00:00:00 2001 From: Andreas Fritiofson Date: Mon, 13 Feb 2012 01:19:57 +0100 Subject: [PATCH] target: add function to get number of bytes available in working area This is a much cleaner solution to the problem of allocating as much working area as possible than what is currently being done in most/all flash drivers (which is: try an arbitrary sized chunk, if it fails, pick a smaller number, rinse and repeat). Use this function to find out how much working area is available, limit or restrict that amount at will and then simply allocate it. Change-Id: Ib7d5d0b7485aed3e0a4fad60c1bedb7dfd16146f Signed-off-by: Andreas Fritiofson Reviewed-on: http://openocd.zylin.com/446 Tested-by: jenkins Reviewed-by: Spencer Oliver --- src/target/target.c | 19 +++++++++++++++++++ src/target/target.h | 1 + 2 files changed, 20 insertions(+) diff --git a/src/target/target.c b/src/target/target.c index 26607c57..5b37ef03 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -1502,6 +1502,25 @@ void target_free_all_working_areas(struct target *target) target_free_all_working_areas_restore(target, 1); } +/* Find the largest number of bytes that can be allocated */ +uint32_t target_get_working_area_avail(struct target *target) +{ + struct working_area *c = target->working_areas; + uint32_t max_size = 0; + + if (c == NULL) + return target->working_area_size; + + while (c) { + if (c->free && max_size < c->size) + max_size = c->size; + + c = c->next; + } + + return max_size; +} + int target_arch_state(struct target *target) { int retval; diff --git a/src/target/target.h b/src/target/target.h index 680f8c32..a5ab3bdd 100644 --- a/src/target/target.h +++ b/src/target/target.h @@ -540,6 +540,7 @@ int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area); int target_free_working_area(struct target *target, struct working_area *area); void target_free_all_working_areas(struct target *target); +uint32_t target_get_working_area_avail(struct target *target); extern struct target *all_targets; -- 2.39.5