From 135866231da43233a9264d52a655205b17b257b5 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 25 Mar 2016 14:56:34 +0800 Subject: [PATCH] Rework line allocation. --- line.c | 26 ++++++++++++-------------- line.h | 28 +++++++++++++--------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/line.c b/line.c index 7e3e47c..cfa636f 100644 --- a/line.c +++ b/line.c @@ -28,8 +28,6 @@ int tabwidth = 8 ; /* column span of a tab */ -#define BLOCK_SIZE 16 /* Line block chunk size. */ - static int ldelnewline( void) ; /* The editor holds deleted text chunks in the struct kill buffer. The @@ -175,21 +173,21 @@ int forwchar(int f, int n) * a pointer to the new block, or NULL if there isn't any memory left. Print a * message in the message line if no space. */ -struct line *lalloc(int used) -{ - struct line *lp; - int size; +line_p lalloc( int used) { +#define BLOCK_SIZE 16 /* Line block chunk size. */ + line_p lp ; + int size ; - size = (used + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1); - if (size == 0) /* Assume that is an empty. */ - size = BLOCK_SIZE; /* Line is for type-in. */ - if ((lp = (struct line *)malloc(sizeof(struct line) + size)) == NULL) { + size = used + BLOCK_SIZE - used % BLOCK_SIZE ; + lp = (line_p) malloc( offsetof( struct line, l_text) + size) ; + if( lp == NULL) { mloutstr( "(OUT OF MEMORY)") ; - return NULL; + return NULL ; } - lp->l_size = size; - lp->l_used = used; - return lp; + + lp->l_size = size ; + lp->l_used = used ; + return lp ; } /* diff --git a/line.h b/line.h index 07ef3a7..0e1445b 100644 --- a/line.h +++ b/line.h @@ -4,23 +4,21 @@ #include "retcode.h" #include "utf8.h" -#define NLINE 256 /* # of bytes, input line */ - /* - * All text is kept in circularly linked lists of "struct line" structures. These - * begin at the header line (which is the blank line beyond the end of the - * buffer). This line is pointed to by the "struct buffer". Each line contains a the + * All text is kept in circularly linked lists of "struct line" structures. + * These begin at the header line (which is the blank line beyond the end of the + * buffer). This line is pointed to by the "struct buffer". Each line contains a * number of bytes in the line (the "used" size), the size of the text array, * and the text. The end of line is not stored as a byte; it's implied. Future * additions will include update hints, and a list of marks into the line. */ -struct line { - struct line *l_fp; /* Link to the next line */ - struct line *l_bp; /* Link to the previous line */ - int l_size; /* Allocated size */ - int l_used; /* Used size */ - char l_text[1]; /* A bunch of characters. */ -}; +typedef struct line { + struct line *l_fp ; /* Forward link to the next line */ + struct line *l_bp ; /* Backward link to the previous line */ + int l_size ; /* Allocated size */ + int l_used ; /* Used size */ + char l_text[ 1] ; /* A bunch of characters */ +} *line_p ; #define lforw(lp) ((lp)->l_fp) #define lback(lp) ((lp)->l_bp) @@ -28,14 +26,14 @@ struct line { #define lputc(lp, n, c) ((lp)->l_text[(n)]=(c)) #define llength(lp) ((lp)->l_used) -extern int tabwidth ; +extern int tabwidth ; /* Map to $tab, default to 8, can be set to [1, .. */ char *getkill( void) ; int backchar( int f, int n) ; int forwchar( int f, int n) ; -void lfree( struct line *lp) ; +void lfree( line_p lp) ; void lchange( int flag) ; int insspace( int f, int n) ; int linstr( char *instr) ; @@ -49,7 +47,7 @@ char *getctext( void) ; void kdelete( void) ; int kinsert( int c) ; int yank( int f, int n) ; -struct line *lalloc( int) ; /* Allocate a line. */ +line_p lalloc( int minsize) ; /* Allocate a line of at least minsize chars. */ boolean rdonly( void) ; /* Read Only error message */