Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty: vt: remove struct uni_screen

It contains only lines with pointers to characters (u32s). So use
simple clear 'u32 **lines' all over the code.

This avoids zero-length arrays. It also makes the allocation less
error-prone (size of the struct wasn't taken into account at all).

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20230112080136.4929-6-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
feb36abb 0c8414a6

+59 -61
+58 -59
drivers/tty/vt/vt.c
··· 320 320 * Our screen buffer is preceded by an array of line pointers so that 321 321 * scrolling only implies some pointer shuffling. 322 322 */ 323 - struct uni_screen { 324 - u32 *lines[0]; 325 - }; 326 323 327 - static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows) 324 + static u32 **vc_uniscr_alloc(unsigned int cols, unsigned int rows) 328 325 { 329 - struct uni_screen *uniscr; 326 + u32 **uni_lines; 330 327 void *p; 331 - unsigned int memsize, i, col_size = cols * sizeof(**uniscr->lines); 328 + unsigned int memsize, i, col_size = cols * sizeof(**uni_lines); 332 329 333 330 /* allocate everything in one go */ 334 331 memsize = col_size * rows; 335 - memsize += rows * sizeof(*uniscr->lines); 336 - p = vzalloc(memsize); 337 - if (!p) 332 + memsize += rows * sizeof(*uni_lines); 333 + uni_lines = vzalloc(memsize); 334 + if (!uni_lines) 338 335 return NULL; 339 336 340 337 /* initial line pointers */ 341 - uniscr = p; 342 - p = uniscr->lines + rows; 338 + p = uni_lines + rows; 343 339 for (i = 0; i < rows; i++) { 344 - uniscr->lines[i] = p; 340 + uni_lines[i] = p; 345 341 p += col_size; 346 342 } 347 - return uniscr; 343 + 344 + return uni_lines; 348 345 } 349 346 350 - static void vc_uniscr_free(struct uni_screen *uniscr) 347 + static void vc_uniscr_free(u32 **uni_lines) 351 348 { 352 - vfree(uniscr); 349 + vfree(uni_lines); 353 350 } 354 351 355 - static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr) 352 + static void vc_uniscr_set(struct vc_data *vc, u32 **new_uni_lines) 356 353 { 357 - vc_uniscr_free(vc->vc_uni_screen); 358 - vc->vc_uni_screen = new_uniscr; 354 + vc_uniscr_free(vc->vc_uni_lines); 355 + vc->vc_uni_lines = new_uni_lines; 359 356 } 360 357 361 358 static void vc_uniscr_putc(struct vc_data *vc, u32 uc) 362 359 { 363 - struct uni_screen *uniscr = vc->vc_uni_screen; 360 + u32 **uni_lines = vc->vc_uni_lines; 364 361 365 - if (uniscr) 366 - uniscr->lines[vc->state.y][vc->state.x] = uc; 362 + if (uni_lines) 363 + uni_lines[vc->state.y][vc->state.x] = uc; 367 364 } 368 365 369 366 static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr) 370 367 { 371 - struct uni_screen *uniscr = vc->vc_uni_screen; 368 + u32 **uni_lines = vc->vc_uni_lines; 372 369 373 - if (uniscr) { 374 - u32 *ln = uniscr->lines[vc->state.y]; 370 + if (uni_lines) { 371 + u32 *ln = uni_lines[vc->state.y]; 375 372 unsigned int x = vc->state.x, cols = vc->vc_cols; 376 373 377 374 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln)); ··· 378 381 379 382 static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr) 380 383 { 381 - struct uni_screen *uniscr = vc->vc_uni_screen; 384 + u32 **uni_lines = vc->vc_uni_lines; 382 385 383 - if (uniscr) { 384 - u32 *ln = uniscr->lines[vc->state.y]; 386 + if (uni_lines) { 387 + u32 *ln = uni_lines[vc->state.y]; 385 388 unsigned int x = vc->state.x, cols = vc->vc_cols; 386 389 387 390 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln)); ··· 392 395 static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x, 393 396 unsigned int nr) 394 397 { 395 - struct uni_screen *uniscr = vc->vc_uni_screen; 398 + u32 **uni_lines = vc->vc_uni_lines; 396 399 397 - if (uniscr) { 398 - u32 *ln = uniscr->lines[vc->state.y]; 400 + if (uni_lines) { 401 + u32 *ln = uni_lines[vc->state.y]; 399 402 400 403 memset32(&ln[x], ' ', nr); 401 404 } ··· 404 407 static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y, 405 408 unsigned int nr) 406 409 { 407 - struct uni_screen *uniscr = vc->vc_uni_screen; 410 + u32 **uni_lines = vc->vc_uni_lines; 408 411 409 - if (uniscr) { 412 + if (uni_lines) { 410 413 unsigned int cols = vc->vc_cols; 411 414 412 415 while (nr--) 413 - memset32(uniscr->lines[y++], ' ', cols); 416 + memset32(uni_lines[y++], ' ', cols); 414 417 } 415 418 } 416 419 417 420 static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b, 418 421 enum con_scroll dir, unsigned int nr) 419 422 { 420 - struct uni_screen *uniscr = vc->vc_uni_screen; 423 + u32 **uni_lines = vc->vc_uni_lines; 421 424 422 - if (uniscr) { 425 + if (uni_lines) { 423 426 unsigned int i, j, k, sz, d, clear; 424 427 425 428 sz = b - t; ··· 430 433 d = sz - nr; 431 434 } 432 435 for (i = 0; i < gcd(d, sz); i++) { 433 - u32 *tmp = uniscr->lines[t + i]; 436 + u32 *tmp = uni_lines[t + i]; 434 437 j = i; 435 438 while (1) { 436 439 k = j + d; ··· 438 441 k -= sz; 439 442 if (k == i) 440 443 break; 441 - uniscr->lines[t + j] = uniscr->lines[t + k]; 444 + uni_lines[t + j] = uni_lines[t + k]; 442 445 j = k; 443 446 } 444 - uniscr->lines[t + j] = tmp; 447 + uni_lines[t + j] = tmp; 445 448 } 446 449 vc_uniscr_clear_lines(vc, clear, nr); 447 450 } 448 451 } 449 452 450 - static void vc_uniscr_copy_area(struct uni_screen *dst, 453 + static void vc_uniscr_copy_area(u32 **dst_lines, 451 454 unsigned int dst_cols, 452 455 unsigned int dst_rows, 453 - struct uni_screen *src, 456 + u32 **src_lines, 454 457 unsigned int src_cols, 455 458 unsigned int src_top_row, 456 459 unsigned int src_bot_row) 457 460 { 458 461 unsigned int dst_row = 0; 459 462 460 - if (!dst) 463 + if (!dst_lines) 461 464 return; 462 465 463 466 while (src_top_row < src_bot_row) { 464 - u32 *src_line = src->lines[src_top_row]; 465 - u32 *dst_line = dst->lines[dst_row]; 467 + u32 *src_line = src_lines[src_top_row]; 468 + u32 *dst_line = dst_lines[dst_row]; 466 469 467 470 memcpy(dst_line, src_line, src_cols * sizeof(*src_line)); 468 471 if (dst_cols - src_cols) ··· 471 474 dst_row++; 472 475 } 473 476 while (dst_row < dst_rows) { 474 - u32 *dst_line = dst->lines[dst_row]; 477 + u32 *dst_line = dst_lines[dst_row]; 475 478 476 479 memset32(dst_line, ' ', dst_cols); 477 480 dst_row++; ··· 486 489 */ 487 490 int vc_uniscr_check(struct vc_data *vc) 488 491 { 489 - struct uni_screen *uniscr; 492 + u32 **uni_lines; 490 493 unsigned short *p; 491 494 int x, y, mask; 492 495 ··· 495 498 if (!vc->vc_utf) 496 499 return -ENODATA; 497 500 498 - if (vc->vc_uni_screen) 501 + if (vc->vc_uni_lines) 499 502 return 0; 500 503 501 - uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 502 - if (!uniscr) 504 + uni_lines = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 505 + if (!uni_lines) 503 506 return -ENOMEM; 504 507 505 508 /* ··· 511 514 p = (unsigned short *)vc->vc_origin; 512 515 mask = vc->vc_hi_font_mask | 0xff; 513 516 for (y = 0; y < vc->vc_rows; y++) { 514 - u32 *line = uniscr->lines[y]; 517 + u32 *line = uni_lines[y]; 515 518 for (x = 0; x < vc->vc_cols; x++) { 516 519 u16 glyph = scr_readw(p++) & mask; 517 520 line[x] = inverse_translate(vc, glyph, true); 518 521 } 519 522 } 520 523 521 - vc->vc_uni_screen = uniscr; 524 + vc->vc_uni_lines = uni_lines; 525 + 522 526 return 0; 523 527 } 524 528 ··· 531 533 void vc_uniscr_copy_line(const struct vc_data *vc, void *dest, bool viewed, 532 534 unsigned int row, unsigned int col, unsigned int nr) 533 535 { 534 - struct uni_screen *uniscr = vc->vc_uni_screen; 536 + u32 **uni_lines = vc->vc_uni_lines; 535 537 int offset = row * vc->vc_size_row + col * 2; 536 538 unsigned long pos; 537 539 538 - BUG_ON(!uniscr); 540 + BUG_ON(!uni_lines); 539 541 540 542 pos = (unsigned long)screenpos(vc, offset, viewed); 541 543 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) { ··· 546 548 */ 547 549 row = (pos - vc->vc_origin) / vc->vc_size_row; 548 550 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2; 549 - memcpy(dest, &uniscr->lines[row][col], nr * sizeof(u32)); 551 + memcpy(dest, &uni_lines[row][col], nr * sizeof(u32)); 550 552 } else { 551 553 /* 552 554 * Scrollback is active. For now let's simply backtranslate ··· 1148 1150 unsigned int new_cols, new_rows, new_row_size, new_screen_size; 1149 1151 unsigned int user; 1150 1152 unsigned short *oldscreen, *newscreen; 1151 - struct uni_screen *new_uniscr = NULL; 1153 + u32 **new_uniscr = NULL; 1152 1154 1153 1155 WARN_CONSOLE_UNLOCKED(); 1154 1156 ··· 1192 1194 if (!newscreen) 1193 1195 return -ENOMEM; 1194 1196 1195 - if (vc->vc_uni_screen) { 1197 + if (vc->vc_uni_lines) { 1196 1198 new_uniscr = vc_uniscr_alloc(new_cols, new_rows); 1197 1199 if (!new_uniscr) { 1198 1200 kfree(newscreen); ··· 1244 1246 end = old_origin + old_row_size * min(old_rows, new_rows); 1245 1247 1246 1248 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows, 1247 - vc->vc_uni_screen, rlth/2, first_copied_row, 1249 + vc->vc_uni_lines, rlth/2, first_copied_row, 1248 1250 min(old_rows, new_rows)); 1249 1251 vc_uniscr_set(vc, new_uniscr); 1250 1252 ··· 4692 4694 4693 4695 u32 screen_glyph_unicode(const struct vc_data *vc, int n) 4694 4696 { 4695 - struct uni_screen *uniscr = vc->vc_uni_screen; 4697 + u32 **uni_lines = vc->vc_uni_lines; 4696 4698 4697 - if (uniscr) 4698 - return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols]; 4699 + if (uni_lines) 4700 + return uni_lines[n / vc->vc_cols][n % vc->vc_cols]; 4701 + 4699 4702 return inverse_translate(vc, screen_glyph(vc, n * 2), true); 4700 4703 } 4701 4704 EXPORT_SYMBOL_GPL(screen_glyph_unicode);
+1 -2
include/linux/console_struct.h
··· 18 18 #include <linux/workqueue.h> 19 19 20 20 struct uni_pagedict; 21 - struct uni_screen; 22 21 23 22 #define NPAR 16 24 23 #define VC_TABSTOPS_COUNT 256U ··· 158 159 struct vc_data **vc_display_fg; /* [!] Ptr to var holding fg console for this display */ 159 160 struct uni_pagedict *uni_pagedict; 160 161 struct uni_pagedict **uni_pagedict_loc; /* [!] Location of uni_pagedict variable for this console */ 161 - struct uni_screen *vc_uni_screen; /* unicode screen content */ 162 + u32 **vc_uni_lines; /* unicode screen content */ 162 163 /* additional information is in vt_kern.h */ 163 164 }; 164 165