瀏覽代碼

implement error avoiding malloc()

Jan Klemkow 6 年之前
父節點
當前提交
fb79c20e36
共有 1 個文件被更改,包括 25 次插入4 次删除
  1. 25 4
      scroll.c

+ 25 - 4
scroll.c

@@ -106,6 +106,27 @@ reset(void)
 		die("tcsetattr:");
 }
 
+/* error avoiding malloc */
+void *
+eamalloc(size_t size)
+{
+	void *mem;
+
+	while ((mem = malloc(size)) == NULL) {
+		struct line *line = TAILQ_LAST(&head, tailhead);
+
+		if (line == NULL)
+			return NULL;
+
+		TAILQ_REMOVE(&head, line, entries);
+		free(line->buf);
+		free(line);
+	}
+
+	return mem;
+}
+
+
 /* Count string length w/o ansi esc sequences. */
 size_t
 strelen(const char *buf, size_t size)
@@ -144,16 +165,16 @@ strelen(const char *buf, size_t size)
 void
 addline(char *buf, size_t size)
 {
-	struct line *line = malloc(sizeof *line);
+	struct line *line = eamalloc(sizeof *line);
 
 	if (line == NULL)
-		die("malloc:");
+		die("eamalloc:");
 
 	line->size = size;
 	line->len = strelen(buf, size);
-	line->buf = malloc(size);
+	line->buf = eamalloc(size);
 	if (line->buf == NULL)
-		die("malloc:");
+		die("eamalloc:");
 	memcpy(line->buf, buf, size);
 
 	bottom = line;