summaryrefslogtreecommitdiffhomepage
path: root/src/go/unit/nxt_go_array.c
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2017-08-31 00:42:16 +0300
committerIgor Sysoev <igor@sysoev.ru>2017-08-31 00:42:16 +0300
commitf0e9e3ace94c82fab78ab1d4ee8c3042f3e94fdf (patch)
tree74fab38d3799598febc98aaabb0f5902d7dc5a0e /src/go/unit/nxt_go_array.c
parent61606835448554a7ee9a4431d732e1f2a9318376 (diff)
downloadunit-f0e9e3ace94c82fab78ab1d4ee8c3042f3e94fdf.tar.gz
unit-f0e9e3ace94c82fab78ab1d4ee8c3042f3e94fdf.tar.bz2
nginext has been renamed to unit.
Diffstat (limited to 'src/go/unit/nxt_go_array.c')
-rw-r--r--src/go/unit/nxt_go_array.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/go/unit/nxt_go_array.c b/src/go/unit/nxt_go_array.c
new file mode 100644
index 00000000..f409cb10
--- /dev/null
+++ b/src/go/unit/nxt_go_array.c
@@ -0,0 +1,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 */