summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_recvbuf.c
blob: 29a2b65d98c8d777845147f9a118cbe5ff6cec7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


nxt_uint_t
nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb)
{
    u_char     *last;
    size_t     size, total;
    nxt_int_t  n;
    nxt_buf_t  *b;

    total = 0;
    last = NULL;
    n = -1;

    for (b = rb->buf; b != NULL; b = b->next) {

        nxt_prefetch(b->next);

        size = b->mem.end - b->mem.free;

        if (b->mem.free != last) {

            if (++n >= rb->nmax) {
                goto done;
            }

            nxt_iobuf_set(&rb->iobuf[n], b->mem.free, size);

        } else {
            nxt_iobuf_add(&rb->iobuf[n], size);
        }

        nxt_thread_log_debug("recvbuf: %ui, %p, %uz", n,
                             nxt_iobuf_data(&rb->iobuf[n]),
                             nxt_iobuf_size(&rb->iobuf[n]));

        total += size;
        last = b->mem.end;
    }

    n++;

done:

    rb->size = total;

    return n;
}


void
nxt_recvbuf_update(nxt_buf_t *b, size_t sent)
{
    size_t  size;

    while (b != NULL && sent != 0) {

        nxt_prefetch(b->next);

        if (!nxt_buf_is_sync(b)) {

            size = b->mem.end - b->mem.free;

            if (sent < size) {
                b->mem.free += sent;
                return;
            }

            b->mem.free = b->mem.end;
            sent -= size;
        }

        b = b->next;
    }
}