Get rid of unneeded malloc in as_text_generic

This commit is contained in:
Kovid Goyal
2020-09-22 09:57:45 +05:30
parent e4d353b105
commit e0f5c39297
5 changed files with 8 additions and 13 deletions

View File

@@ -463,7 +463,7 @@ as_text(HistoryBuf *self, PyObject *args) {
GetLineWrapper glw = {.self=self};
glw.line.xnum = self->xnum;
ANSIBuf output = {0};
PyObject *ans = as_text_generic(args, &glw, get_line_wrapper, self->count, self->xnum, &output);
PyObject *ans = as_text_generic(args, &glw, get_line_wrapper, self->count, &output);
free(output.buf);
return ans;
}

View File

@@ -442,7 +442,7 @@ get_line(void *x, int y) {
static PyObject*
as_text(LineBuf *self, PyObject *args) {
ANSIBuf output = {0};
PyObject* ans = as_text_generic(args, self, get_line, self->ynum, self->xnum, &output);
PyObject* ans = as_text_generic(args, self, get_line, self->ynum, &output);
free(output.buf);
return ans;
}

View File

@@ -762,21 +762,16 @@ mark_text_in_line(PyObject *marker, Line *line) {
}
PyObject*
as_text_generic(PyObject *args, void *container, get_line_func get_line, index_type lines, index_type columns, ANSIBuf *ansibuf) {
as_text_generic(PyObject *args, void *container, get_line_func get_line, index_type lines, ANSIBuf *ansibuf) {
PyObject *callback;
int as_ansi = 0, insert_wrap_markers = 0;
if (!PyArg_ParseTuple(args, "O|pp", &callback, &as_ansi, &insert_wrap_markers)) return NULL;
PyObject *ret = NULL, *t = NULL;
Py_UCS4 *buf = NULL;
PyObject *nl = PyUnicode_FromString("\n");
PyObject *cr = PyUnicode_FromString("\r");
PyObject *sgr_reset = PyUnicode_FromString("\x1b[m");
const GPUCell *prev_cell = NULL;
if (nl == NULL || cr == NULL) goto end;
if (as_ansi) {
buf = malloc(sizeof(Py_UCS4) * columns * 100);
if (buf == NULL) { PyErr_NoMemory(); goto end; }
}
for (index_type y = 0; y < lines; y++) {
Line *line = get_line(container, y);
if (!line->continued && y > 0) {
@@ -811,7 +806,7 @@ as_text_generic(PyObject *args, void *container, get_line_func get_line, index_t
}
}
end:
Py_CLEAR(nl); Py_CLEAR(cr); Py_CLEAR(sgr_reset); free(buf);
Py_CLEAR(nl); Py_CLEAR(cr); Py_CLEAR(sgr_reset);
if (PyErr_Occurred()) return NULL;
Py_RETURN_NONE;
}

View File

@@ -110,4 +110,4 @@ void historybuf_refresh_sprite_positions(HistoryBuf *self);
void historybuf_clear(HistoryBuf *self);
void mark_text_in_line(PyObject *marker, Line *line);
bool line_has_mark(Line *, attrs_type mark);
PyObject* as_text_generic(PyObject *args, void *container, get_line_func get_line, index_type lines, index_type columns, ANSIBuf *ansibuf);
PyObject* as_text_generic(PyObject *args, void *container, get_line_func get_line, index_type lines, ANSIBuf *ansibuf);

View File

@@ -2030,17 +2030,17 @@ static Line* get_range_line(void *x, int y) { return range_line_(x, y); }
static PyObject*
as_text(Screen *self, PyObject *args) {
return as_text_generic(args, self, get_visual_line, self->lines, self->columns, &self->as_ansi_buf);
return as_text_generic(args, self, get_visual_line, self->lines, &self->as_ansi_buf);
}
static PyObject*
as_text_non_visual(Screen *self, PyObject *args) {
return as_text_generic(args, self, get_range_line, self->lines, self->columns, &self->as_ansi_buf);
return as_text_generic(args, self, get_range_line, self->lines, &self->as_ansi_buf);
}
static inline PyObject*
as_text_generic_wrapper(Screen *self, PyObject *args, get_line_func get_line) {
return as_text_generic(args, self, get_line, self->lines, self->columns, &self->as_ansi_buf);
return as_text_generic(args, self, get_line, self->lines, &self->as_ansi_buf);
}
static PyObject*