Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ngx_hunk.c « core « src - github.com/nginx/nginx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 517a1af23ce282fd9136c204decb0f480c4b14d0 (plain)
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

#include <ngx_config.h>
#include <ngx_core.h>


ngx_hunk_t *ngx_create_temp_hunk(ngx_pool_t *pool, int size,
                                 int before, int after)
{
    ngx_hunk_t *h;

    ngx_test_null(h, ngx_palloc(pool, sizeof(ngx_hunk_t)), NULL);

    ngx_test_null(h->pre_start, ngx_palloc(pool, size + before + after), NULL);

    h->start = h->pos = h->last = h->pre_start + before;
    h->file_pos = h->file_last = 0;
    h->end = h->last + size;
    h->post_end = h->end + after;

    h->type = NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY;
    h->file = NULL;
    h->shadow = NULL;

    h->tag = 0;

    return h;
}

ngx_hunk_t *ngx_create_hunk_before(ngx_pool_t *pool, ngx_hunk_t *hunk, int size)
{
    ngx_hunk_t *h;

    ngx_test_null(h, ngx_palloc(pool, sizeof(ngx_hunk_t)), NULL);

    if (hunk->type & NGX_HUNK_TEMP && hunk->pos - hunk->pre_start >= size) {
        /* keep hunk->start unchanged - used in restore */
        h->pre_start = hunk->pre_start;
        h->end = h->post_end = hunk->pre_start = hunk->pos;
        h->start = h->pos = h->last = h->end - size;
        h->file_pos = h->file_last = 0;

        h->type = NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY;
        h->file = NULL;
        h->shadow = NULL;

        h->tag = 0;

    } else {
        ngx_test_null(h->pre_start, ngx_palloc(pool, size), NULL);
        h->start = h->pos = h->last = h->pre_start;
        h->end = h->post_end = h->start + size;
        h->file_pos = h->file_last = 0;

        h->type = NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY;
        h->file = NULL;
        h->shadow = NULL;

        h->tag = 0;
    }

    return h;
}

ngx_hunk_t *ngx_create_hunk_after(ngx_pool_t *pool, ngx_hunk_t *hunk, int size)
{
    ngx_hunk_t *h;

    ngx_test_null(h, ngx_palloc(pool, sizeof(ngx_hunk_t)), NULL);

    if (hunk->type & NGX_HUNK_TEMP
        && hunk->last == hunk->end
        && hunk->post_end - hunk->end >= size)
    {
        h->post_end = hunk->post_end;
        h->pre_start = h->start = h->pos = h->last = hunk->post_end =
                                                                hunk->last;
        h->file_pos = h->file_last = 0;

        h->type = NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY;
        h->file = NULL;
        h->shadow = NULL;

        h->tag = 0;

    } else {
        ngx_test_null(h->pre_start, ngx_palloc(pool, size), NULL);
        h->start = h->pos = h->last = h->pre_start;
        h->end = h->post_end = h->start + size;
        h->file_pos = h->file_last = 0;

        h->type = NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY;
        h->file = NULL;
        h->shadow = NULL;

        h->tag = 0;
    }

    return h;
}


int ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **ch, ngx_chain_t *in)
{
    ngx_chain_t  *ce, **le;

    le = ch;

    for (ce = *ch; ce; ce = ce->next) {
        le = &ce->next;
    }

    while (in) {
        ngx_test_null(ce, ngx_alloc_chain_entry(pool), NGX_ERROR);

        ce->hunk = in->hunk;
        *le = ce;
        le = &ce->next;
        in = in->next;
    }

    *le = NULL;

    return NGX_OK;
}


void ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy,
                             ngx_chain_t **out)
{
    ngx_chain_t  *te;

    if (*busy == NULL) {
        *busy = *out;

    } else {
        for (te = *busy; /* void */ ; te = te->next) {
            if (te->next == NULL) {
                te->next = *out;
                break;
            }   
        }
    }

    *out = NULL;

    while (*busy) {
        if ((*busy)->hunk->pos != (*busy)->hunk->last) {
            break;
        }

#if (HAVE_WRITE_ZEROCOPY)
        if ((*busy)->hunk->type & NGX_HUNK_ZEROCOPY_BUSY) {
            break;
        }
#endif
        if (((*busy)->hunk->type & NGX_HUNK_TEMP) == 0) {
            *busy = (*busy)->next;
            continue;
        }

        (*busy)->hunk->pos = (*busy)->hunk->last = (*busy)->hunk->start;

        te = *busy;
        *busy = (*busy)->next;
        te->next = *free;
        *free = te;
    }
}