summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_buf.c
blob: cb0faf7c02c85d467043ef413d3abfc54356a30c (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

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

#include <nxt_main.h>


static void nxt_buf_completion(nxt_task_t *task, void *obj, void *data);


void
nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size)
{
    b->size = NXT_BUF_MEM_SIZE;

    b->mem.start = start;
    b->mem.pos = start;
    b->mem.free = start;
    b->mem.end = nxt_pointer_to(start, size);
}


nxt_buf_t *
nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags)
{
    nxt_buf_t  *b;

    b = nxt_mp_zalloc(mp, NXT_BUF_MEM_SIZE);
    if (nxt_slow_path(b == NULL)) {
        return NULL;
    }

    b->data = mp;
    b->completion_handler = nxt_buf_completion;
    b->size = NXT_BUF_MEM_SIZE;

    if (size != 0) {
        b->mem.start = nxt_mp_alloc(mp, size);
        if (nxt_slow_path(b->mem.start == NULL)) {
            return NULL;
        }

        b->mem.pos = b->mem.start;
        b->mem.free = b->mem.start;
        b->mem.end = b->mem.start + size;
    }

    return b;
}


nxt_buf_t *
nxt_buf_file_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags)
{
    nxt_buf_t  *b;

    b = nxt_mp_zalloc(mp, NXT_BUF_FILE_SIZE);
    if (nxt_slow_path(b == NULL)) {
        return NULL;
    }

    b->data = mp;
    b->completion_handler = nxt_buf_completion;
    b->size = NXT_BUF_FILE_SIZE;
    nxt_buf_set_file(b);

    if (size != 0) {
        b->mem.start = nxt_mp_alloc(mp, size);
        if (nxt_slow_path(b->mem.start == NULL)) {
            return NULL;
        }

        b->mem.pos = b->mem.start;
        b->mem.free = b->mem.start;
        b->mem.end = b->mem.start + size;
    }

    return b;
}


nxt_buf_t *
nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size)
{
    nxt_buf_t  *b;

    b = nxt_mp_zalloc(mp, NXT_BUF_MMAP_SIZE);

    if (nxt_fast_path(b != NULL)) {
        b->data = mp;
        b->completion_handler = nxt_buf_completion;
        b->size = NXT_BUF_MMAP_SIZE;

        nxt_buf_set_file(b);
        nxt_buf_set_mmap(b);
        nxt_buf_mem_set_size(&b->mem, size);
    }

    return b;
}


nxt_buf_t *
nxt_buf_sync_alloc(nxt_mp_t *mp, nxt_uint_t flags)
{
    nxt_buf_t  *b;

    b = nxt_mp_zalloc(mp, NXT_BUF_SYNC_SIZE);

    if (nxt_fast_path(b != NULL)) {
        b->data = mp;
        b->completion_handler = nxt_buf_completion;
        b->size = NXT_BUF_SYNC_SIZE;

        nxt_buf_set_sync(b);
        b->is_nobuf = ((flags & NXT_BUF_SYNC_NOBUF) != 0);
        b->is_flush = ((flags & NXT_BUF_SYNC_FLUSH) != 0);
        b->is_last = ((flags & NXT_BUF_SYNC_LAST) != 0);
    }

    return b;
}


void
nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in)
{
    nxt_buf_t  *b, **prev;

    prev = head;

    for (b = *head; b != NULL; b = b->next) {
        prev = &b->next;
    }

    *prev = in;
}


size_t
nxt_buf_chain_length(nxt_buf_t *b)
{
    size_t  length;

    length = 0;

    while (b != NULL) {
        length += b->mem.free - b->mem.pos;
        b = b->next;
    }

    return length;
}


static void
nxt_buf_completion(nxt_task_t *task, void *obj, void *data)
{
    nxt_mp_t   *mp;
    nxt_buf_t  *b, *parent;

    b = obj;
    parent = data;

    nxt_debug(task, "buf completion: %p %p", b, b->mem.start);

    mp = b->data;
    nxt_buf_free(mp, b);

    if (parent != NULL) {
        nxt_debug(task, "parent retain:%uD", parent->retain);

        parent->retain--;

        if (parent->retain == 0) {
            parent->mem.pos = parent->mem.free;

            parent->completion_handler(task, parent, parent->parent);
        }
    }
}