svf: try to reallocate buffers if they're not enough 99/2099/2
authorPaul Fertser <fercerpav@gmail.com>
Wed, 9 Apr 2014 05:58:07 +0000 (09:58 +0400)
committerPaul Fertser <fercerpav@gmail.com>
Mon, 14 Apr 2014 18:21:11 +0000 (18:21 +0000)
On larger FPGAs the default buffer size is not nearly enough;
automatically reallocate it as needed.

Change-Id: Ic8071e8cceddeef897ce76996c029250fd22662b
Signed-off-by: Paul Fertser <fercerpav@gmail.com>
Reviewed-on: http://openocd.zylin.com/2099
Tested-by: jenkins
Reviewed-by: Kenneth Lorthioir <ibelimb@gmail.com>
Reviewed-by: Freddie Chopin <freddie.chopin@gmail.com>
Reviewed-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
src/svf/svf.c

index 89ea80dc94dfd024c08676275ca0ba31ba48466d..4107aa2c0eb26d1bda6535698996448f6707395b 100644 (file)
@@ -241,6 +241,30 @@ static long svf_total_lines;
 static int svf_percentage;
 static int svf_last_printed_percentage = -1;
 
+static int svf_realloc_buffers(size_t len)
+{
+       void *ptr;
+
+       ptr = realloc(svf_tdi_buffer, len);
+       if (!ptr)
+               return ERROR_FAIL;
+       svf_tdi_buffer = ptr;
+
+       ptr = realloc(svf_tdo_buffer, len);
+       if (!ptr)
+               return ERROR_FAIL;
+       svf_tdo_buffer = ptr;
+
+       ptr = realloc(svf_mask_buffer, len);
+       if (!ptr)
+               return ERROR_FAIL;
+       svf_mask_buffer = ptr;
+
+       svf_buffer_size = len;
+
+       return ERROR_OK;
+}
+
 static void svf_free_xxd_para(struct svf_xxr_para *para)
 {
        if (NULL != para) {
@@ -383,25 +407,10 @@ COMMAND_HANDLER(handle_svf_command)
        /* in case current command cannot be committed, and next command is a bit scan command */
        /* here is 32K bits for this big scan command, it should be enough */
        /* buffer will be reallocated if buffer size is not enough */
-       svf_tdi_buffer = malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
-       if (NULL == svf_tdi_buffer) {
-               LOG_ERROR("not enough memory");
+       if (svf_realloc_buffers(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT) != ERROR_OK) {
                ret = ERROR_FAIL;
                goto free_all;
        }
-       svf_tdo_buffer = malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
-       if (NULL == svf_tdo_buffer) {
-               LOG_ERROR("not enough memory");
-               ret = ERROR_FAIL;
-               goto free_all;
-       }
-       svf_mask_buffer = malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
-       if (NULL == svf_mask_buffer) {
-               LOG_ERROR("not enough memory");
-               ret = ERROR_FAIL;
-               goto free_all;
-       }
-       svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
 
        memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
 
@@ -1078,47 +1087,11 @@ XXR_common:
                                i = svf_para.hdr_para.len + svf_para.sdr_para.len +
                                                svf_para.tdr_para.len;
                                if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
-#if 1
-                                       /* simply print error message */
-                                       LOG_ERROR("buffer is not enough, report to author");
-                                       return ERROR_FAIL;
-#else
-                                       uint8_t *buffer_tmp;
-
                                        /* reallocate buffer */
-                                       buffer_tmp = malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
+                                       if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
                                                LOG_ERROR("not enough memory");
                                                return ERROR_FAIL;
                                        }
-                                       memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
-                                       /* svf_tdi_buffer isn't NULL here */
-                                       free(svf_tdi_buffer);
-                                       svf_tdi_buffer = buffer_tmp;
-
-                                       buffer_tmp = malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
-                                       /* svf_tdo_buffer isn't NULL here */
-                                       free(svf_tdo_buffer);
-                                       svf_tdo_buffer = buffer_tmp;
-
-                                       buffer_tmp = malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
-                                       /* svf_mask_buffer isn't NULL here */
-                                       free(svf_mask_buffer);
-                                       svf_mask_buffer = buffer_tmp;
-
-                                       buffer_tmp = NULL;
-                                       svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
-#endif
                                }
 
                                /* assemble dr data */
@@ -1205,47 +1178,10 @@ XXR_common:
                                i = svf_para.hir_para.len + svf_para.sir_para.len +
                                                svf_para.tir_para.len;
                                if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
-#if 1
-                                       /* simply print error message */
-                                       LOG_ERROR("buffer is not enough, report to author");
-                                       return ERROR_FAIL;
-#else
-                                       uint8_t *buffer_tmp;
-
-                                       /* reallocate buffer */
-                                       buffer_tmp = malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
-                                       /* svf_tdi_buffer isn't NULL here */
-                                       free(svf_tdi_buffer);
-                                       svf_tdi_buffer = buffer_tmp;
-
-                                       buffer_tmp = malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
+                                       if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
                                                LOG_ERROR("not enough memory");
                                                return ERROR_FAIL;
                                        }
-                                       memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
-                                       /* svf_tdo_buffer isn't NULL here */
-                                       free(svf_tdo_buffer);
-                                       svf_tdo_buffer = buffer_tmp;
-
-                                       buffer_tmp = malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
-                                       /* svf_mask_buffer isn't NULL here */
-                                       free(svf_mask_buffer);
-                                       svf_mask_buffer = buffer_tmp;
-
-                                       buffer_tmp = NULL;
-                                       svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
-#endif
                                }
 
                                /* assemble ir data */

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)