comparison src/channel.c @ 9487:69ed2c9d34a6 v7.4.2024

commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 10 22:11:16 2016 +0200 patch 7.4.2024 Problem: More buf_valid() calls can be optimized. Solution: Use bufref_valid() instead.
author Christian Brabandt <cb@256bit.org>
date Sun, 10 Jul 2016 22:15:06 +0200
parents 8f904a323b3f
children 404de131cff9
comparison
equal deleted inserted replaced
9486:b931ed8a6782 9487:69ed2c9d34a6
360 /* If there is a close callback it may still need to be invoked. */ 360 /* If there is a close callback it may still need to be invoked. */
361 if (channel->ch_close_cb != NULL) 361 if (channel->ch_close_cb != NULL)
362 return TRUE; 362 return TRUE;
363 363
364 /* If reading from or a buffer it's still useful. */ 364 /* If reading from or a buffer it's still useful. */
365 if (channel->ch_part[PART_IN].ch_buffer != NULL) 365 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
366 return TRUE; 366 return TRUE;
367 367
368 /* If there is no callback then nobody can get readahead. If the fd is 368 /* If there is no callback then nobody can get readahead. If the fd is
369 * closed and there is no readahead then the callback won't be called. */ 369 * closed and there is no readahead then the callback won't be called. */
370 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD 370 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
377 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL 377 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
378 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL; 378 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
379 return (channel->ch_callback != NULL && (has_sock_msg 379 return (channel->ch_callback != NULL && (has_sock_msg
380 || has_out_msg || has_err_msg)) 380 || has_out_msg || has_err_msg))
381 || ((channel->ch_part[PART_OUT].ch_callback != NULL 381 || ((channel->ch_part[PART_OUT].ch_callback != NULL
382 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg) 382 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
383 && has_out_msg)
383 || ((channel->ch_part[PART_ERR].ch_callback != NULL 384 || ((channel->ch_part[PART_ERR].ch_callback != NULL
384 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg); 385 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
386 && has_err_msg);
385 } 387 }
386 388
387 /* 389 /*
388 * Close a channel and free all its resources. 390 * Close a channel and free all its resources.
389 */ 391 */
1044 1046
1045 if (job->jv_in_buf != NULL) 1047 if (job->jv_in_buf != NULL)
1046 { 1048 {
1047 chanpart_T *in_part = &channel->ch_part[PART_IN]; 1049 chanpart_T *in_part = &channel->ch_part[PART_IN];
1048 1050
1049 in_part->ch_buffer = job->jv_in_buf; 1051 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
1050 ch_logs(channel, "reading from buffer '%s'", 1052 ch_logs(channel, "reading from buffer '%s'",
1051 (char *)in_part->ch_buffer->b_ffname); 1053 (char *)in_part->ch_bufref.br_buf->b_ffname);
1052 if (options->jo_set & JO_IN_TOP) 1054 if (options->jo_set & JO_IN_TOP)
1053 { 1055 {
1054 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT)) 1056 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1055 { 1057 {
1056 /* Special mode: send last-but-one line when appending a line 1058 /* Special mode: send last-but-one line when appending a line
1057 * to the buffer. */ 1059 * to the buffer. */
1058 in_part->ch_buffer->b_write_to_channel = TRUE; 1060 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
1059 in_part->ch_buf_append = TRUE; 1061 in_part->ch_buf_append = TRUE;
1060 in_part->ch_buf_top = 1062 in_part->ch_buf_top =
1061 in_part->ch_buffer->b_ml.ml_line_count + 1; 1063 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
1062 } 1064 }
1063 else 1065 else
1064 in_part->ch_buf_top = options->jo_in_top; 1066 in_part->ch_buf_top = options->jo_in_top;
1065 } 1067 }
1066 else 1068 else
1067 in_part->ch_buf_top = 1; 1069 in_part->ch_buf_top = 1;
1068 if (options->jo_set & JO_IN_BOT) 1070 if (options->jo_set & JO_IN_BOT)
1069 in_part->ch_buf_bot = options->jo_in_bot; 1071 in_part->ch_buf_bot = options->jo_in_bot;
1070 else 1072 else
1071 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count; 1073 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
1072 } 1074 }
1073 } 1075 }
1074 1076
1075 /* 1077 /*
1076 * Find a buffer matching "name" or create a new one. 1078 * Find a buffer matching "name" or create a new one.
1227 } 1229 }
1228 else 1230 else
1229 { 1231 {
1230 ch_logs(channel, "writing out to buffer '%s'", 1232 ch_logs(channel, "writing out to buffer '%s'",
1231 (char *)buf->b_ffname); 1233 (char *)buf->b_ffname);
1232 channel->ch_part[PART_OUT].ch_buffer = buf; 1234 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1233 } 1235 }
1234 } 1236 }
1235 } 1237 }
1236 1238
1237 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER 1239 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1242 1244
1243 /* writing err to a buffer. Default mode is NL. */ 1245 /* writing err to a buffer. Default mode is NL. */
1244 if (!(opt->jo_set & JO_ERR_MODE)) 1246 if (!(opt->jo_set & JO_ERR_MODE))
1245 channel->ch_part[PART_ERR].ch_mode = MODE_NL; 1247 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1246 if (opt->jo_io[PART_ERR] == JIO_OUT) 1248 if (opt->jo_io[PART_ERR] == JIO_OUT)
1247 buf = channel->ch_part[PART_OUT].ch_buffer; 1249 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1248 else if (opt->jo_set & JO_ERR_BUF) 1250 else if (opt->jo_set & JO_ERR_BUF)
1249 { 1251 {
1250 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]); 1252 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1251 if (buf == NULL) 1253 if (buf == NULL)
1252 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]); 1254 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1264 } 1266 }
1265 else 1267 else
1266 { 1268 {
1267 ch_logs(channel, "writing err to buffer '%s'", 1269 ch_logs(channel, "writing err to buffer '%s'",
1268 (char *)buf->b_ffname); 1270 (char *)buf->b_ffname);
1269 channel->ch_part[PART_ERR].ch_buffer = buf; 1271 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1270 } 1272 }
1271 } 1273 }
1272 } 1274 }
1273 1275
1274 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT]; 1276 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1405 static void 1407 static void
1406 channel_write_in(channel_T *channel) 1408 channel_write_in(channel_T *channel)
1407 { 1409 {
1408 chanpart_T *in_part = &channel->ch_part[PART_IN]; 1410 chanpart_T *in_part = &channel->ch_part[PART_IN];
1409 linenr_T lnum; 1411 linenr_T lnum;
1410 buf_T *buf = in_part->ch_buffer; 1412 buf_T *buf = in_part->ch_bufref.br_buf;
1411 int written = 0; 1413 int written = 0;
1412 1414
1413 if (buf == NULL || in_part->ch_buf_append) 1415 if (buf == NULL || in_part->ch_buf_append)
1414 return; /* no buffer or using appending */ 1416 return; /* no buffer or using appending */
1415 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL) 1417 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
1416 { 1418 {
1417 /* buffer was wiped out or unloaded */ 1419 /* buffer was wiped out or unloaded */
1418 in_part->ch_buffer = NULL; 1420 in_part->ch_bufref.br_buf = NULL;
1419 return; 1421 return;
1420 } 1422 }
1421 1423
1422 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot 1424 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1423 && lnum <= buf->b_ml.ml_line_count; ++lnum) 1425 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1435 1437
1436 in_part->ch_buf_top = lnum; 1438 in_part->ch_buf_top = lnum;
1437 if (lnum > buf->b_ml.ml_line_count) 1439 if (lnum > buf->b_ml.ml_line_count)
1438 { 1440 {
1439 /* Writing is done, no longer need the buffer. */ 1441 /* Writing is done, no longer need the buffer. */
1440 in_part->ch_buffer = NULL; 1442 in_part->ch_bufref.br_buf = NULL;
1441 ch_log(channel, "Finished writing all lines to channel"); 1443 ch_log(channel, "Finished writing all lines to channel");
1442 } 1444 }
1443 else 1445 else
1444 ch_logn(channel, "Still %d more lines to write", 1446 ch_logn(channel, "Still %d more lines to write",
1445 buf->b_ml.ml_line_count - lnum + 1); 1447 buf->b_ml.ml_line_count - lnum + 1);
1457 for (channel = first_channel; channel != NULL; channel = channel->ch_next) 1459 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1458 for (part = PART_SOCK; part <= PART_IN; ++part) 1460 for (part = PART_SOCK; part <= PART_IN; ++part)
1459 { 1461 {
1460 chanpart_T *ch_part = &channel->ch_part[part]; 1462 chanpart_T *ch_part = &channel->ch_part[part];
1461 1463
1462 if (ch_part->ch_buffer == buf) 1464 if (ch_part->ch_bufref.br_buf == buf)
1463 { 1465 {
1464 ch_logs(channel, "%s buffer has been wiped out", 1466 ch_logs(channel, "%s buffer has been wiped out",
1465 part_names[part]); 1467 part_names[part]);
1466 ch_part->ch_buffer = NULL; 1468 ch_part->ch_bufref.br_buf = NULL;
1467 } 1469 }
1468 } 1470 }
1469 } 1471 }
1470 1472
1471 /* 1473 /*
1478 1480
1479 for (channel = first_channel; channel != NULL; channel = channel->ch_next) 1481 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1480 { 1482 {
1481 chanpart_T *in_part = &channel->ch_part[PART_IN]; 1483 chanpart_T *in_part = &channel->ch_part[PART_IN];
1482 1484
1483 if (in_part->ch_buffer != NULL) 1485 if (in_part->ch_bufref.br_buf != NULL)
1484 { 1486 {
1485 if (in_part->ch_buf_append) 1487 if (in_part->ch_buf_append)
1486 channel_write_new_lines(in_part->ch_buffer); 1488 channel_write_new_lines(in_part->ch_bufref.br_buf);
1487 else 1489 else
1488 channel_write_in(channel); 1490 channel_write_in(channel);
1489 } 1491 }
1490 } 1492 }
1491 } 1493 }
1505 { 1507 {
1506 chanpart_T *in_part = &channel->ch_part[PART_IN]; 1508 chanpart_T *in_part = &channel->ch_part[PART_IN];
1507 linenr_T lnum; 1509 linenr_T lnum;
1508 int written = 0; 1510 int written = 0;
1509 1511
1510 if (in_part->ch_buffer == buf && in_part->ch_buf_append) 1512 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
1511 { 1513 {
1512 if (in_part->ch_fd == INVALID_FD) 1514 if (in_part->ch_fd == INVALID_FD)
1513 continue; /* pipe was closed */ 1515 continue; /* pipe was closed */
1514 found_one = TRUE; 1516 found_one = TRUE;
1515 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count; 1517 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
2310 buffer->b_write_to_channel = TRUE; 2312 buffer->b_write_to_channel = TRUE;
2311 for (ch = first_channel; ch != NULL; ch = ch->ch_next) 2313 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2312 { 2314 {
2313 chanpart_T *in_part = &ch->ch_part[PART_IN]; 2315 chanpart_T *in_part = &ch->ch_part[PART_IN];
2314 2316
2315 if (in_part->ch_buffer == buffer) 2317 if (in_part->ch_bufref.br_buf == buffer)
2316 in_part->ch_buf_bot = buffer->b_ml.ml_line_count; 2318 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2317 } 2319 }
2318 } 2320 }
2319 } 2321 }
2320 2322
2372 { 2374 {
2373 callback = channel->ch_callback; 2375 callback = channel->ch_callback;
2374 partial = channel->ch_partial; 2376 partial = channel->ch_partial;
2375 } 2377 }
2376 2378
2377 buffer = channel->ch_part[part].ch_buffer; 2379 buffer = channel->ch_part[part].ch_bufref.br_buf;
2378 if (buffer != NULL && !buf_valid(buffer)) 2380 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
2379 { 2381 {
2380 /* buffer was wiped out */ 2382 /* buffer was wiped out */
2381 channel->ch_part[part].ch_buffer = NULL; 2383 channel->ch_part[part].ch_bufref.br_buf = NULL;
2382 buffer = NULL; 2384 buffer = NULL;
2383 } 2385 }
2384 2386
2385 if (ch_mode == MODE_JSON || ch_mode == MODE_JS) 2387 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2386 { 2388 {
2832 2834
2833 for (ch = first_channel; ch != NULL; ch = ch->ch_next) 2835 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2834 { 2836 {
2835 chanpart_T *in_part = &ch->ch_part[PART_IN]; 2837 chanpart_T *in_part = &ch->ch_part[PART_IN];
2836 2838
2837 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL) 2839 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
2838 { 2840 {
2839 FD_SET((int)in_part->ch_fd, wfds); 2841 FD_SET((int)in_part->ch_fd, wfds);
2840 if ((int)in_part->ch_fd >= maxfd) 2842 if ((int)in_part->ch_fd >= maxfd)
2841 maxfd = (int)in_part->ch_fd + 1; 2843 maxfd = (int)in_part->ch_fd + 1;
2842 } 2844 }
2855 2857
2856 for (ch = first_channel; ch != NULL; ch = ch->ch_next) 2858 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2857 { 2859 {
2858 chanpart_T *in_part = &ch->ch_part[PART_IN]; 2860 chanpart_T *in_part = &ch->ch_part[PART_IN];
2859 2861
2860 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL) 2862 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
2861 { 2863 {
2862 in_part->ch_poll_idx = nfd; 2864 in_part->ch_poll_idx = nfd;
2863 fds[nfd].fd = in_part->ch_fd; 2865 fds[nfd].fd = in_part->ch_fd;
2864 fds[nfd].events = POLLOUT; 2866 fds[nfd].events = POLLOUT;
2865 ++nfd; 2867 ++nfd;
3641 idx = in_part->ch_poll_idx; 3643 idx = in_part->ch_poll_idx;
3642 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT)) 3644 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3643 { 3645 {
3644 if (in_part->ch_buf_append) 3646 if (in_part->ch_buf_append)
3645 { 3647 {
3646 if (in_part->ch_buffer != NULL) 3648 if (in_part->ch_bufref.br_buf != NULL)
3647 channel_write_new_lines(in_part->ch_buffer); 3649 channel_write_new_lines(in_part->ch_bufref.br_buf);
3648 } 3650 }
3649 else 3651 else
3650 channel_write_in(channel); 3652 channel_write_in(channel);
3651 --ret; 3653 --ret;
3652 } 3654 }
3719 if (ret > 0 && in_part->ch_fd != INVALID_FD 3721 if (ret > 0 && in_part->ch_fd != INVALID_FD
3720 && FD_ISSET(in_part->ch_fd, wfds)) 3722 && FD_ISSET(in_part->ch_fd, wfds))
3721 { 3723 {
3722 if (in_part->ch_buf_append) 3724 if (in_part->ch_buf_append)
3723 { 3725 {
3724 if (in_part->ch_buffer != NULL) 3726 if (in_part->ch_bufref.br_buf != NULL)
3725 channel_write_new_lines(in_part->ch_buffer); 3727 channel_write_new_lines(in_part->ch_bufref.br_buf);
3726 } 3728 }
3727 else 3729 else
3728 channel_write_in(channel); 3730 channel_write_in(channel);
3729 --ret; 3731 --ret;
3730 } 3732 }