Mercurial > hg > nginx-quic
annotate src/core/ngx_array.c @ 4818:28a64967e891
Removed a stale "AF_INET only" comment.
IPv6 client connections in mail modules have been supported since r2856.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Fri, 17 Aug 2012 05:14:19 +0000 |
parents | d620f497c50f |
children | 4fef8ed52389 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
4412 | 4 * Copyright (C) Nginx, Inc. |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
5 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
6 |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
100
7ebc8b7fb816
nginx-0.0.1-2003-06-03-19:42:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
92
diff
changeset
|
9 #include <ngx_core.h> |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
11 |
605 | 12 ngx_array_t * |
13 ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size) | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
14 { |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
15 ngx_array_t *a; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
16 |
501 | 17 a = ngx_palloc(p, sizeof(ngx_array_t)); |
18 if (a == NULL) { | |
485 | 19 return NULL; |
20 } | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
21 |
501 | 22 a->elts = ngx_palloc(p, n * size); |
23 if (a->elts == NULL) { | |
485 | 24 return NULL; |
25 } | |
26 | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
27 a->nelts = 0; |
485 | 28 a->size = size; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
29 a->nalloc = n; |
485 | 30 a->pool = p; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
31 |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
32 return a; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
33 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
34 |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
35 |
605 | 36 void |
37 ngx_array_destroy(ngx_array_t *a) | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
38 { |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
39 ngx_pool_t *p; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
40 |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
41 p = a->pool; |
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
42 |
2049 | 43 if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) { |
44 p->d.last -= a->size * a->nalloc; | |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
45 } |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
46 |
2049 | 47 if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) { |
48 p->d.last = (u_char *) a; | |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
49 } |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
50 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
51 |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
52 |
605 | 53 void * |
54 ngx_array_push(ngx_array_t *a) | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
55 { |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
56 void *elt, *new; |
485 | 57 size_t size; |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
58 ngx_pool_t *p; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
59 |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 if (a->nelts == a->nalloc) { |
485 | 61 |
62 /* the array is full */ | |
63 | |
64 size = a->size * a->nalloc; | |
65 | |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
0
diff
changeset
|
66 p = a->pool; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
67 |
2049 | 68 if ((u_char *) a->elts + size == p->d.last |
69 && p->d.last + a->size <= p->d.end) | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
70 { |
485 | 71 /* |
72 * the array allocation is the last in the pool | |
73 * and there is space for new allocation | |
74 */ | |
75 | |
2049 | 76 p->d.last += a->size; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
77 a->nalloc++; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
78 |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
79 } else { |
485 | 80 /* allocate a new array */ |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
81 |
501 | 82 new = ngx_palloc(p, 2 * size); |
83 if (new == NULL) { | |
485 | 84 return NULL; |
85 } | |
86 | |
87 ngx_memcpy(new, a->elts, size); | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
88 a->elts = new; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
89 a->nalloc *= 2; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
90 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
91 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
92 |
485 | 93 elt = (u_char *) a->elts + a->size * a->nelts; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
94 a->nelts++; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
95 |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
96 return elt; |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
97 } |
485 | 98 |
99 | |
605 | 100 void * |
101 ngx_array_push_n(ngx_array_t *a, ngx_uint_t n) | |
485 | 102 { |
103 void *elt, *new; | |
104 size_t size; | |
105 ngx_uint_t nalloc; | |
106 ngx_pool_t *p; | |
107 | |
108 size = n * a->size; | |
109 | |
110 if (a->nelts + n > a->nalloc) { | |
111 | |
112 /* the array is full */ | |
113 | |
114 p = a->pool; | |
115 | |
2049 | 116 if ((u_char *) a->elts + a->size * a->nalloc == p->d.last |
117 && p->d.last + size <= p->d.end) | |
485 | 118 { |
119 /* | |
120 * the array allocation is the last in the pool | |
121 * and there is space for new allocation | |
122 */ | |
123 | |
2049 | 124 p->d.last += size; |
485 | 125 a->nalloc += n; |
126 | |
127 } else { | |
128 /* allocate a new array */ | |
129 | |
130 nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc); | |
131 | |
501 | 132 new = ngx_palloc(p, nalloc * a->size); |
133 if (new == NULL) { | |
485 | 134 return NULL; |
135 } | |
136 | |
137 ngx_memcpy(new, a->elts, a->nelts * a->size); | |
138 a->elts = new; | |
139 a->nalloc = nalloc; | |
140 } | |
141 } | |
142 | |
143 elt = (u_char *) a->elts + a->size * a->nelts; | |
144 a->nelts += n; | |
145 | |
146 return elt; | |
147 } |