]> granicus.if.org Git - vim/commitdiff
updated for version 7.3.007 v7.3.007
authorBram Moolenaar <Bram@vim.org>
Tue, 21 Sep 2010 14:49:37 +0000 (16:49 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 21 Sep 2010 14:49:37 +0000 (16:49 +0200)
Problem:    Python code defines global "buffer".  Re-implements a grow-array.
Solution:   Use a grow-array instead of coding the same functionality.  Handle
            out-of-memory situation properly.

src/if_py_both.h
src/version.c

index 10c614855b9acf58f54cc99dbcd18d85f13405a4..4b7f185cfaaeab3e9f24e7954217e6ddd54c6f19 100644 (file)
@@ -34,6 +34,7 @@ Python_Release_Vim(void)
 static PyObject *OutputWrite(PyObject *, PyObject *);
 static PyObject *OutputWritelines(PyObject *, PyObject *);
 
+/* Function to write a line, points to either msg() or emsg(). */
 typedef void (*writefn)(char_u *);
 static void writer(writefn fn, char_u *str, PyInt n);
 
@@ -122,52 +123,19 @@ OutputWritelines(PyObject *self, PyObject *args)
     return Py_None;
 }
 
-static char_u *buffer = NULL;
-static PyInt buffer_len = 0;
-static PyInt buffer_size = 0;
-
+/* Buffer IO, we write one whole line at a time. */
+static garray_T io_ga = {0, 0, 1, 80, NULL};
 static writefn old_fn = NULL;
 
-    static void
-buffer_ensure(PyInt n)
-{
-    PyInt new_size;
-    char_u *new_buffer;
-
-    if (n < buffer_size)
-       return;
-
-    new_size = buffer_size;
-    while (new_size < n)
-       new_size += 80;
-
-    if (new_size != buffer_size)
-    {
-       new_buffer = alloc((unsigned)new_size);
-       if (new_buffer == NULL)
-           return;
-
-       if (buffer)
-       {
-           memcpy(new_buffer, buffer, buffer_len);
-           vim_free(buffer);
-       }
-
-       buffer = new_buffer;
-       buffer_size = new_size;
-    }
-}
-
     static void
 PythonIO_Flush(void)
 {
-    if (old_fn && buffer_len)
+    if (old_fn != NULL && io_ga.ga_len > 0)
     {
-       buffer[buffer_len] = 0;
-       old_fn(buffer);
+       ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
+       old_fn((char_u *)io_ga.ga_data);
     }
-
-    buffer_len = 0;
+    io_ga.ga_len = 0;
 }
 
     static void
@@ -175,30 +143,34 @@ writer(writefn fn, char_u *str, PyInt n)
 {
     char_u *ptr;
 
-    if (fn != old_fn && old_fn != NULL)
+    /* Flush when switching output function. */
+    if (fn != old_fn)
        PythonIO_Flush();
-
     old_fn = fn;
 
+    /* Write each NL separated line.  Text after the last NL is kept for
+     * writing later. */
     while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
     {
        PyInt len = ptr - str;
 
-       buffer_ensure(buffer_len + len + 1);
+       if (ga_grow(&io_ga, len + 1) == FAIL)
+           break;
 
-       memcpy(buffer + buffer_len, str, len);
-       buffer_len += len;
-       buffer[buffer_len] = 0;
-       fn(buffer);
+       mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
+       ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
+       fn((char_u *)io_ga.ga_data);
        str = ptr + 1;
        n -= len + 1;
-       buffer_len = 0;
+       io_ga.ga_len = 0;
     }
 
-    /* Put the remaining text into the buffer for later printing */
-    buffer_ensure(buffer_len + n + 1);
-    memcpy(buffer + buffer_len, str, n);
-    buffer_len += n;
+    /* Put the remaining text into io_ga for later printing. */
+    if (n > 0 && ga_grow(&io_ga, n + 1) == OK)
+    {
+       mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
+       io_ga.ga_len += n;
+    }
 }
 
 /***************/
index b09c1f7fb3b1e136bfd08af50e26e0e9105f549d..ab8f35e8b905e084e68d70563bef1958cc1b145d 100644 (file)
@@ -714,6 +714,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    7,
 /**/
     6,
 /**/