From 4763d8162aafa2f2d8640a952127947c6c7a5787 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 27 Sep 2010 19:36:27 +0000 Subject: [PATCH] New functions by Michael Walle for resizing registers on the fly git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1851 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/ChangeLog | 6 ++++++ urjtag/include/urjtag/data_register.h | 7 +++++++ urjtag/include/urjtag/tap_register.h | 1 + urjtag/src/part/data_register.c | 11 ++++++++++ urjtag/src/tap/register.c | 29 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+) diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index 3e428dbf..6b15f7d0 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -15,6 +15,12 @@ * include/urjtag/bitops.h: New generic bit operations by Michael Walle. * include/urjtag/Makefile.am (pkginclude_HEADERS): Add bitops.h + * include/urjtag/data_register.h, src/part/data_register.c + (urj_part_data_register_realloc): New func by Michael Walle for resizing + registers on the fly. + * include/urjtag/tap_register.h, src/tap/register.c + (urj_tap_register_realloc): Likewise. + 2010-08-30 Mike Frysinger * configure.ac: Clean up libusb detection to prefer 1.0 over 0.1, and accept diff --git a/urjtag/include/urjtag/data_register.h b/urjtag/include/urjtag/data_register.h index e9323a4b..1759f6c7 100644 --- a/urjtag/include/urjtag/data_register.h +++ b/urjtag/include/urjtag/data_register.h @@ -40,6 +40,13 @@ struct URJ_DATA_REGISTER urj_data_register_t *urj_part_data_register_alloc (const char *name, int len); void urj_part_data_register_free (urj_data_register_t *dr); +/** + * change the length of a data_register while preserving its contents + * + * @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error + */ +int urj_part_data_register_realloc (urj_data_register_t *dr, int len); + /** * allocate a data register and initialize the relevant part parts * diff --git a/urjtag/include/urjtag/tap_register.h b/urjtag/include/urjtag/tap_register.h index 81a35866..6d46631e 100644 --- a/urjtag/include/urjtag/tap_register.h +++ b/urjtag/include/urjtag/tap_register.h @@ -36,6 +36,7 @@ struct URJ_TAP_REGISTER }; urj_tap_register_t *urj_tap_register_alloc (int len); +urj_tap_register_t *urj_tap_register_realloc (urj_tap_register_t *tr, int new_len); urj_tap_register_t *urj_tap_register_duplicate (const urj_tap_register_t *tr); void urj_tap_register_free (urj_tap_register_t *tr); urj_tap_register_t *urj_tap_register_fill (urj_tap_register_t *tr, int val); diff --git a/urjtag/src/part/data_register.c b/urjtag/src/part/data_register.c index 9a6fc298..4b1f3278 100644 --- a/urjtag/src/part/data_register.c +++ b/urjtag/src/part/data_register.c @@ -133,3 +133,14 @@ urj_part_data_register_define (urj_part_t *part, const char *name, int len) return URJ_STATUS_OK; } + +int +urj_part_data_register_realloc (urj_data_register_t *dr, int new_len) +{ + if (urj_tap_register_realloc (dr->in, new_len) == NULL) + return URJ_STATUS_FAIL; + if (urj_tap_register_realloc (dr->out, new_len) == NULL) + return URJ_STATUS_FAIL; + + return URJ_STATUS_OK; +} diff --git a/urjtag/src/tap/register.c b/urjtag/src/tap/register.c index 0c012c8b..33bdd469 100644 --- a/urjtag/src/tap/register.c +++ b/urjtag/src/tap/register.c @@ -78,6 +78,35 @@ urj_tap_register_alloc (int len) return tr; } +urj_tap_register_t * +urj_tap_register_realloc (urj_tap_register_t *tr, int new_len) +{ + if (!tr) + return urj_tap_register_alloc (new_len); + + if (new_len < 1) + { + urj_error_set (URJ_ERROR_INVALID, "new_len < 1"); + return NULL; + } + + tr->data = realloc (tr->data, new_len); + + if (!tr->data) + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "realloc(%d) fails", + new_len); + return NULL; + } + + if (tr->len < new_len) + memset (tr->data + tr->len, 0, (new_len - tr->len)); + + tr->len = new_len; + + return tr; +} + urj_tap_register_t * urj_tap_register_duplicate (const urj_tap_register_t *tr) {