Second attempt:

1. replace alloca with malloc - that's Øyvind's patch, but I've fixed r vs. retval and I've removed one useless part.

2. add Newlib's strtok_r to replacements so that membuf.c could be build on Windows

4\/3!!
Index: src/helper/membuf.c
===================================================================
--- src/helper/membuf.c (revision 2405)
+++ src/helper/membuf.c (working copy)
@@ -23,6 +23,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "membuf.h"
 
 struct membuf {
Index: src/helper/replacements.c
===================================================================
--- src/helper/replacements.c   (revision 2405)
+++ src/helper/replacements.c   (working copy)
@@ -287,4 +287,87 @@
 
        return retcode;
 }
+
+/*
+ * strtok_r() taken from Newlib, as no strtok_r() is present for WIN32/MinGW 
platform
+ * Modified a bit to remove unused blocks associated with skip_leading_delim 
parameter
+ */
+
+/* begin Newlib copyright */
+
+/*
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* end Newlib copyright */
+
+char *strtok_r(char *s, const char *delim, char **lasts)
+{
+       register char *spanp;
+       register int c, sc;
+       char *tok;
+
+       if (s == NULL && (s = *lasts) == NULL)
+               return (NULL);
+
+       /*
+        * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
+        */
+cont:
+       c = *s++;
+       for (spanp = (char *)delim; (sc = *spanp++) != 0;)
+               if (c == sc)
+                       goto cont;
+
+       if (c == 0) {           /* no non-delimiter characters */
+               *lasts = NULL;
+               return (NULL);
+       }
+       tok = s - 1;
+
+       /*
+        * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
+        * Note that delim must have one NUL; we stop if we see that, too.
+        */
+       for (;;) {
+               c = *s++;
+               spanp = (char *)delim;
+               do {
+                       if ((sc = *spanp++) == c) {
+                               if (c == 0)
+                                       s = NULL;
+                               else
+                                       s[-1] = 0;
+                               *lasts = s;
+                               return (tok);
+                       }
+               } while (sc != 0);
+       }
+       /* NOTREACHED */
+}
 #endif
Index: src/helper/replacements.h
===================================================================
--- src/helper/replacements.h   (revision 2405)
+++ src/helper/replacements.h   (working copy)
@@ -157,6 +157,7 @@
 #endif /* IS_MINGW */
 
 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct 
timeval *tv);
+char *strtok_r(char *s, const char *delim, char **lasts);
 
 #endif  /* _WIN32 */
 
Index: src/flash/at91sam3.c
===================================================================
--- src/flash/at91sam3.c        (revision 2405)
+++ src/flash/at91sam3.c        (working copy)
@@ -2146,7 +2146,7 @@
                return ERROR_FAIL;
        }
 
-       pagebuffer = alloca(pPrivate->page_size);
+       pagebuffer = malloc(pPrivate->page_size);
 
        // what page do we start & end in?
        page_cur = offset / pPrivate->page_size;
@@ -2167,7 +2167,7 @@
                LOG_DEBUG("Special case, all in one page");
                r = sam3_page_read(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto exit_fn;
                }
 
                page_offset = (offset & (pPrivate->page_size-1));
@@ -2176,10 +2176,7 @@
                                count);
 
                r = sam3_page_write(pPrivate, page_cur, pagebuffer);
-               if (r != ERROR_OK) {
-                       return r;
-               }
-               return ERROR_OK;
+               goto exit_fn;
        }
 
        // non-aligned start
@@ -2189,7 +2186,7 @@
                // read the partial
                r = sam3_page_read(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto exit_fn;
                }
 
                // over-write with new data
@@ -2200,7 +2197,7 @@
 
                r = sam3_page_write(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto exit_fn;
                }
 
                count  -= n;
@@ -2219,7 +2216,7 @@
                   (count >= pPrivate->page_size)) {
                r = sam3_page_write(pPrivate, page_cur, buffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto exit_fn;
                }
                count    -= pPrivate->page_size;
                buffer   += pPrivate->page_size;
@@ -2232,19 +2229,23 @@
                // we have a partial page
                r = sam3_page_read(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto exit_fn;
                }
                // data goes at start
                memcpy(pagebuffer, buffer, count);
                r = sam3_page_write(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto exit_fn;
                }
                buffer += count;
                count  -= count;
        }
        LOG_DEBUG("Done!");
-       return ERROR_OK;
+
+       r=ERROR_OK;
+exit_fn:
+       free(pagebuffer);
+       return r;
 }
 
 static int
_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to