summaryrefslogtreecommitdiffhomepage
path: root/src/nginext/nxt_go_array.c
blob: f409cb10eba974f72fcaada3a27a07805522d3c7 (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

/*
 * Copyright (C) Max Romanov
 * Copyright (C) NGINX, Inc.
 */

#ifndef NXT_CONFIGURE

#include <stdint.h>
#include <sys/types.h>

#include <nxt_main.h>

#include "nxt_go_array.h"

void
nxt_go_array_init(nxt_array_t *array, nxt_uint_t n, size_t size)
{
    array->elts = malloc(n * size);

    if (nxt_slow_path(n != 0 && array->elts == NULL)) {
        return;
    }

    array->nelts = 0;
    array->size = size;
    array->nalloc = n;
    array->mem_pool = NULL;
}

void *
nxt_go_array_add(nxt_array_t *array)
{
    void      *p;
    uint32_t  nalloc, new_alloc;

    nalloc = array->nalloc;

    if (array->nelts == nalloc) {

        if (nalloc < 16) {
            /* Allocate new array twice larger than current. */
            new_alloc = nalloc * 2;

        } else {
            /* Allocate new array 1.5 times larger than current. */
            new_alloc = nalloc + nalloc / 2;
        }

        p = realloc(array->elts, array->size * new_alloc);

        if (nxt_slow_path(p == NULL)) {
            return NULL;
        }

        array->elts = p;
        array->nalloc = new_alloc;
    }

    p = nxt_pointer_to(array->elts, array->size * array->nelts);
    array->nelts++;

    return p;
}

#endif /* NXT_CONFIGURE */