--- lbbs/src/editor.c 2025/06/12 04:45:57 1.15 +++ lbbs/src/editor.c 2025/06/14 11:15:46 1.21 @@ -20,6 +20,7 @@ #include "log.h" #include "common.h" #include "str_process.h" +#include "memory_pool.h" #include #include #include @@ -28,6 +29,51 @@ #include #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" +#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 +#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) + +static MEMORY_POOL *p_mp_data_line; +static MEMORY_POOL *p_mp_editor_data; + +int editor_memory_pool_init(void) +{ + if (p_mp_data_line != NULL || p_mp_editor_data != NULL) + { + log_error("Editor mem pool already initialized\n"); + return -1; + } + + p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); + if (p_mp_data_line == NULL) + { + log_error("Memory pool init error\n"); + return -2; + } + + p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); + if (p_mp_data_line == NULL) + { + log_error("Memory pool init error\n"); + return -3; + } + + return 0; +} + +void editor_memory_pool_cleanup(void) +{ + if (p_mp_data_line != NULL) + { + memory_pool_cleanup(p_mp_data_line); + p_mp_data_line = NULL; + } + + if (p_mp_editor_data != NULL) + { + memory_pool_cleanup(p_mp_editor_data); + p_mp_editor_data = NULL; + } +} EDITOR_DATA *editor_data_load(const char *p_data) { @@ -43,10 +89,10 @@ EDITOR_DATA *editor_data_load(const char return NULL; } - p_editor_data = malloc(sizeof(EDITOR_DATA)); + p_editor_data = memory_pool_alloc(p_mp_editor_data); if (p_editor_data == NULL) { - log_error("malloc(EDITOR_DATA) error: OOM\n"); + log_error("memory_pool_alloc() error\n"); return NULL; } @@ -61,10 +107,10 @@ EDITOR_DATA *editor_data_load(const char (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) { // Allocate new data line - p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); + p_data_line = memory_pool_alloc(p_mp_data_line); if (p_data_line == NULL) { - log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); + log_error("memory_pool_alloc() error: i = %d\n", i); // Cleanup editor_data_cleanup(p_editor_data); return NULL; @@ -80,6 +126,15 @@ EDITOR_DATA *editor_data_load(const char memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); current_data_line_length += p_editor_data->display_line_lengths[i]; + + // Trim \n from last line + if (i + 1 == p_editor_data->display_line_total && + p_editor_data->display_line_lengths[i] > 0 && + p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') + { + p_editor_data->display_line_lengths[i]--; + current_data_line_length--; + } p_data_line[current_data_line_length] = '\0'; } @@ -137,17 +192,17 @@ void editor_data_cleanup(EDITOR_DATA *p_ if (p_editor_data->display_line_lengths[i] > 0 && p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') { - free(p_data_line); + memory_pool_free(p_mp_data_line, p_data_line); p_data_line = NULL; } } if (p_data_line != NULL) { - free(p_data_line); + memory_pool_free(p_mp_data_line, p_data_line); } - free(p_editor_data); + memory_pool_free(p_mp_editor_data, p_editor_data); } int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, @@ -172,6 +227,14 @@ int editor_data_insert(EDITOR_DATA *p_ed return -1; } + // Validate str + if ((str_len == 1 && str[0] <= 0) || + (str_len == 2 && (str[0] >= 0 || str[1] >= 0))) + { + log_error("Invalid input str, len=%d\n", str_len); + return -2; + } + // Get accurate offset of first character of CJK at offset position for (i = 0; i < offset; i++) { @@ -224,10 +287,10 @@ int editor_data_insert(EDITOR_DATA *p_ed } // Allocate new data line - p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); + p_data_line = memory_pool_alloc(p_mp_data_line); if (p_data_line == NULL) { - log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); + log_error("memory_pool_alloc() error\n"); return -2; } @@ -349,15 +412,17 @@ int editor_data_insert(EDITOR_DATA *p_ed if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) { - *p_offset -= p_editor_data->display_line_lengths[*p_display_line]; - - if (*p_display_line + 1 >= p_editor_data->display_line_total) + if (*p_display_line + 1 < p_editor_data->display_line_total) { - log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); + *p_offset -= p_editor_data->display_line_lengths[*p_display_line]; + (*p_display_line)++; } - else + else if (*p_display_line + 1 >= MAX_EDITOR_DATA_LINES) { - (*p_display_line)++; + len = split_line(p_editor_data->p_display_lines[*p_display_line], SCREEN_COLS - 1, &eol, &display_len); + p_editor_data->p_display_lines[*p_display_line][len] = '\0'; + p_editor_data->display_line_lengths[*p_display_line] = len; + *p_offset = len; } } @@ -423,6 +488,11 @@ int editor_data_delete(EDITOR_DATA *p_ed } } + if (offset_data_line >= len_data_line) // end-of-line + { + return 0; + } + // Check str to be deleted if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) { @@ -434,9 +504,8 @@ int editor_data_delete(EDITOR_DATA *p_ed } else { - log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", - display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1], - p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]); + log_error("Some strange character at display_line %ld, offset %ld: %d %d\n", + display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]); str_len = 1; } @@ -473,7 +542,7 @@ int editor_data_delete(EDITOR_DATA *p_ed p_data_line[offset_data_line + len_data_line] = '\0'; // Recycle next data line - free(p_editor_data->p_display_lines[display_line + 1]); + memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]); } else { @@ -506,19 +575,18 @@ int editor_data_delete(EDITOR_DATA *p_ed *p_last_updated_line = display_line + MIN(i, split_line_total - 1); - if (display_line + i < last_display_line) + if (*p_last_updated_line < last_display_line) { // Remove redundant display line after last_display_line for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) { - p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j]; - p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j]; + p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j]; + p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j]; } - (p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); - last_display_line = display_line + i; - - *p_last_updated_line = p_editor_data->display_line_total - 1; + j = p_editor_data->display_line_total; + (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); + *p_last_updated_line = MAX(j - 1, *p_last_updated_line); } return str_len; @@ -530,7 +598,7 @@ static int editor_display_key_handler(in { case 0: // Set msg snprintf(p_ctx->msg, sizeof(p_ctx->msg), - "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); + "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |"); break; } @@ -618,15 +686,16 @@ int editor_display(EDITOR_DATA *p_editor input_str[str_len] = (char)(ch - 256); str_len++; } - else + else if (str_len > 0) { + log_error("Received %d character over 127 followed by character less than 127\n", str_len); str_len = 0; } if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK ch == CR || ch == KEY_ESC) // Special character { - if (str_len == 0) + if (str_len == 0) // ch >= 32 && ch < 127 { input_str[0] = (char)ch; str_len = 1; @@ -651,12 +720,9 @@ int editor_display(EDITOR_DATA *p_editor input_str, str_len, &last_updated_line) < 0) { log_error("editor_data_insert(str_len=%d) error\n", str_len); - str_len = 0; } else { - str_len = 0; - output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); line_current -= (output_current_row - row_pos); output_current_row = (int)row_pos; @@ -687,12 +753,19 @@ int editor_display(EDITOR_DATA *p_editor col_pos = offset_out + 1; } + str_len = 0; continue; } else if (ch == KEY_DEL || ch == BACKSPACE) // Del { if (ch == BACKSPACE) { + if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden + { + input_ok = 0; + continue; + } + col_pos--; if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) { @@ -743,10 +816,12 @@ int editor_display(EDITOR_DATA *p_editor row_pos += scroll_rows; output_current_row = screen_begin_row; output_end_row = SCREEN_ROWS - 1; - clrline(output_current_row, SCREEN_ROWS); } + + clrline(output_current_row, output_end_row); } + str_len = 0; continue; } @@ -755,7 +830,7 @@ int editor_display(EDITOR_DATA *p_editor case KEY_NULL: case KEY_TIMEOUT: goto cleanup; - case Ctrl('C'): + case Ctrl('W'): loop = 0; break; case Ctrl('S'): // Start of line @@ -773,8 +848,23 @@ int editor_display(EDITOR_DATA *p_editor break; case Ctrl('B'): // Bottom of screen case KEY_CTRL_DOWN: - row_pos = SCREEN_ROWS - 1; - col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); + if (p_editor_data->display_line_total < screen_row_total) + { + row_pos = p_editor_data->display_line_total; + } + else + { + row_pos = SCREEN_ROWS - 1; + } + if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end + { + // last display line does NOT have \n in the end + col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1); + } + else + { + col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); + } break; case KEY_INS: key_insert = !key_insert; @@ -795,14 +885,14 @@ int editor_display(EDITOR_DATA *p_editor if (p_editor_data->display_line_total < screen_row_total) { row_pos = p_editor_data->display_line_total; - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); + col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; break; } line_current = p_editor_data->display_line_total - screen_row_total; output_current_row = screen_begin_row; output_end_row = SCREEN_ROWS - 1; row_pos = SCREEN_ROWS - 1; - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); + col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; clrline(output_current_row, SCREEN_ROWS); break; case KEY_LEFT: @@ -849,7 +939,8 @@ int editor_display(EDITOR_DATA *p_editor } if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end { - col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); + // last display line does NOT have \n in the end + col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; break; } line_current += (screen_row_total - (output_current_row - screen_begin_row));