-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathngx_slab_array.c
104 lines (71 loc) · 1.63 KB
/
ngx_slab_array.c
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
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_slab_array.h"
ngx_slab_array_t *
ngx_slab_array_create(ngx_slab_pool_t *p, ngx_uint_t n, size_t size)
{
ngx_slab_array_t *a;
a = ngx_slab_alloc(p, sizeof(ngx_slab_array_t));
if (a == NULL) {
return NULL;
}
if (ngx_slab_array_init(a, p, n, size) != NGX_OK) {
return NULL;
}
return a;
}
void
ngx_slab_array_destroy(ngx_slab_array_t *a)
{
ngx_slab_free(a->pool, a->elts);
}
void *
ngx_slab_array_push(ngx_slab_array_t *a)
{
void *elt, *new;
size_t size;
ngx_slab_pool_t *p;
if (a->nelts == a->nalloc) {
/* the array is full */
size = a->size * a->nalloc;
p = a->pool;
/* allocate a new array */
new = ngx_slab_alloc(p, 2 * size);
if (new == NULL) {
return NULL;
}
ngx_memcpy(new, a->elts, size);
a->elts = new;
a->nalloc *= 2;
}
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts++;
return elt;
}
void *
ngx_slab_array_push_n(ngx_slab_array_t *a, ngx_uint_t n)
{
void *elt, *new;
ngx_uint_t nalloc;
ngx_slab_pool_t *p;
if (a->nelts + n > a->nalloc) {
/* the array is full */
p = a->pool;
/* allocate a new array */
nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
new = ngx_slab_alloc(p, nalloc * a->size);
if (new == NULL) {
return NULL;
}
ngx_memcpy(new, a->elts, a->nelts * a->size);
a->elts = new;
a->nalloc = nalloc;
}
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts += n;
return elt;
}