summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_vector.c
blob: 4737248cfe1407c67d35d2483c6f1f21e6a5796e (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

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

#include <nxt_main.h>


nxt_vector_t *
nxt_vector_create(nxt_uint_t items, size_t item_size,
    const nxt_mem_proto_t *proto, void *pool)
{
    nxt_vector_t  *vector;

    vector = proto->alloc(pool, sizeof(nxt_vector_t) + items * item_size);

    if (nxt_fast_path(vector != NULL)) {
        vector->start = nxt_pointer_to(vector, sizeof(nxt_vector_t));
        vector->items = 0;
        vector->item_size = item_size;
        vector->avalaible = items;
        vector->type = NXT_VECTOR_EMBEDDED;
    }

    return vector;
}


void *
nxt_vector_init(nxt_vector_t *vector, nxt_uint_t items, size_t item_size,
    const nxt_mem_proto_t *proto, void *pool)
{
    vector->start = proto->alloc(pool, items * item_size);

    if (nxt_fast_path(vector->start != NULL)) {
        vector->items = 0;
        vector->item_size = item_size;
        vector->avalaible = items;
        vector->type = NXT_VECTOR_INITED;
    }

    return vector->start;
}


void
nxt_vector_destroy(nxt_vector_t *vector, const nxt_mem_proto_t *proto,
    void *pool)
{
    switch (vector->type) {

    case NXT_VECTOR_INITED:
        proto->free(pool, vector->start);
#if (NXT_DEBUG)
        vector->start = NULL;
        vector->items = 0;
        vector->avalaible = 0;
#endif
        break;

    case NXT_VECTOR_DESCRETE:
        proto->free(pool, vector->start);

        /* Fall through. */

    case NXT_VECTOR_EMBEDDED:
        proto->free(pool, vector);
        break;
    }
}


void *
nxt_vector_add(nxt_vector_t *vector, const nxt_mem_proto_t *proto, void *pool)
{
    void      *item, *start, *old;
    size_t    size;
    uint32_t  n;

    n = vector->avalaible;

    if (n == vector->items) {

        if (n < 16) {
            /* Allocate new vector twice as much as current. */
            n *= 2;

        } else {
            /* Allocate new vector half as much as current. */
            n += n / 2;
        }

        size = n * vector->item_size;

        start = proto->alloc(pool, size);
        if (nxt_slow_path(start == NULL)) {
            return NULL;
        }

        vector->avalaible = n;
        old = vector->start;
        vector->start = start;

        nxt_memcpy(start, old, size);

        if (vector->type == NXT_VECTOR_EMBEDDED) {
            vector->type = NXT_VECTOR_DESCRETE;

        } else {
            proto->free(pool, old);
        }
    }

    item = nxt_pointer_to(vector->start, vector->item_size * vector->items);

    vector->items++;

    return item;
}


void *
nxt_vector_zero_add(nxt_vector_t *vector, const nxt_mem_proto_t *proto,
    void *pool)
{
    void  *item;

    item = nxt_vector_add(vector, proto, pool);

    if (nxt_fast_path(item != NULL)) {
        nxt_memzero(item, vector->item_size);
    }

    return item;
}


void
nxt_vector_remove(nxt_vector_t *vector, void *item)
{
    u_char    *next, *last, *end;
    uint32_t  item_size;

    item_size = vector->item_size;
    end = nxt_pointer_to(vector->start, item_size * vector->items);
    last = end - item_size;

    if (item != last) {
        next = nxt_pointer_to(item, item_size);

        nxt_memmove(item, next, end - next);
    }

    vector->items--;
}