static njs_index_t
njs_generator_temp_index_get(njs_parser_t *parser)
{
- nxt_uint_t n;
- njs_index_t index, *last;
- nxt_vector_t *cache;
+ nxt_uint_t n;
+ njs_index_t index, *last;
+ nxt_array_t *cache;
cache = parser->index_cache;
if (cache != NULL && cache->items != 0) {
- last = nxt_vector_remove_last(cache);
+ last = nxt_array_remove_last(cache);
nxt_thread_log_debug("CACHE %p", *last);
njs_generator_index_release(njs_vm_t *vm, njs_parser_t *parser,
njs_index_t index)
{
- njs_index_t *last;
- nxt_vector_t *cache;
+ njs_index_t *last;
+ nxt_array_t *cache;
nxt_thread_log_debug("RELEASE %p", index);
cache = parser->index_cache;
if (cache == NULL) {
- cache = nxt_vector_create(4, sizeof(njs_value_t *), &njs_array_mem_proto,
+ cache = nxt_array_create(4, sizeof(njs_value_t *), &njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(cache == NULL)) {
return NXT_ERROR;
parser->index_cache = cache;
}
- last = nxt_vector_add(cache, &njs_array_mem_proto, vm->mem_cache_pool);
+ last = nxt_array_add(cache, &njs_array_mem_proto, vm->mem_cache_pool);
if (nxt_fast_path(last != NULL)) {
*last = index;
return NXT_OK;
} else {
/* Anonymous function. */
- value = nxt_vector_add(parser->scope_values, &njs_array_mem_proto,
- vm->mem_cache_pool);
+ value = nxt_array_add(parser->scope_values, &njs_array_mem_proto,
+ vm->mem_cache_pool);
if (nxt_slow_path(value == NULL)) {
return NJS_TOKEN_ERROR;
}
/* njs_return() size. */
fn_parser->code_size = sizeof(njs_vmcode_stop_t);
- fn_parser->arguments = nxt_vector_create(4, sizeof(njs_variable_t),
+ fn_parser->arguments = nxt_array_create(4, sizeof(njs_variable_t),
&njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(fn_parser->arguments == NULL)) {
nxt_thread_log_debug("arg: %V", name);
- arg = nxt_vector_add(fn_parser->arguments, &njs_array_mem_proto,
+ arg = nxt_array_add(fn_parser->arguments, &njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(arg == NULL)) {
return NJS_TOKEN_ERROR;
return NJS_TOKEN_ERROR;
}
- fn_parser->scope_values = nxt_vector_create(4, sizeof(njs_value_t),
+ fn_parser->scope_values = nxt_array_create(4, sizeof(njs_value_t),
&njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(fn_parser->scope_values == NULL)) {
/* njs_return() size. */
fn_parser->code_size = sizeof(njs_vmcode_stop_t);
- fn_parser->arguments = nxt_vector_create(4, sizeof(njs_variable_t),
+ fn_parser->arguments = nxt_array_create(4, sizeof(njs_variable_t),
&njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(fn_parser->arguments == NULL)) {
nxt_thread_log_debug("arg: %V", name);
- arg = nxt_vector_add(fn_parser->arguments, &njs_array_mem_proto,
+ arg = nxt_array_add(fn_parser->arguments, &njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(arg == NULL)) {
return NJS_TOKEN_ERROR;
return NJS_TOKEN_ERROR;
}
- fn_parser->scope_values = nxt_vector_create(4, sizeof(njs_value_t),
+ fn_parser->scope_values = nxt_array_create(4, sizeof(njs_value_t),
&njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(fn_parser->scope_values == NULL)) {
njs_parser_node_t *node;
/* Vector of njs_variable_t. */
- nxt_vector_t *arguments;
+ nxt_array_t *arguments;
nxt_lvlhsh_t variables_hash;
nxt_lvlhsh_t values_hash;
nxt_lvlhsh_t functions_hash;
- nxt_vector_t *index_cache;
+ nxt_array_t *index_cache;
njs_index_t index[NJS_SCOPES - NJS_INDEX_CACHE];
- nxt_vector_t *scope_values;
+ nxt_array_t *scope_values;
uint8_t scope; /* 4 bits */
uint8_t branch; /* 1 bit */
memcpy(var->name_start, name->data, name->len);
var->name_len = name->len;
- value = nxt_vector_add(parser->scope_values, &njs_array_mem_proto,
+ value = nxt_array_add(parser->scope_values, &njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_fast_path(value != NULL)) {
*value = njs_value_void;
parser->scope_offset = NJS_INDEX_GLOBAL_OFFSET;
parser->index[NJS_SCOPE_GLOBAL - NJS_INDEX_CACHE] = NJS_INDEX_GLOBAL_OFFSET;
- parser->scope_values = nxt_vector_create(4, sizeof(njs_value_t),
+ parser->scope_values = nxt_array_create(4, sizeof(njs_value_t),
&njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(parser->scope_values == NULL)) {
return NJS_ERROR;
}
- /* Empty vector to minimize tests in njs_parser_variable(). */
- parser->arguments = nxt_vector_create(0, sizeof(njs_variable_t),
+ /* Empty array to minimize tests in njs_parser_variable(). */
+ parser->arguments = nxt_array_create(0, sizeof(njs_variable_t),
&njs_array_mem_proto,
vm->mem_cache_pool);
if (nxt_slow_path(parser->arguments == NULL)) {
#include <string.h>
-nxt_vector_t *
-nxt_vector_create(nxt_uint_t items, size_t item_size,
+nxt_array_t *
+nxt_array_create(nxt_uint_t items, size_t item_size,
const nxt_mem_proto_t *proto, void *pool)
{
- nxt_vector_t *vector;
+ nxt_array_t *array;
- vector = proto->alloc(pool, sizeof(nxt_vector_t) + items * item_size);
+ array = proto->alloc(pool, sizeof(nxt_array_t) + items * item_size);
- if (nxt_fast_path(vector != NULL)) {
- vector->start = (char *) vector + sizeof(nxt_vector_t);
- vector->items = 0;
- vector->item_size = item_size;
- vector->avalaible = items;
- vector->type = NXT_VECTOR_EMBEDDED;
+ if (nxt_fast_path(array != NULL)) {
+ array->start = (char *) array + sizeof(nxt_array_t);
+ array->items = 0;
+ array->item_size = item_size;
+ array->avalaible = items;
+ array->type = NXT_ARRAY_EMBEDDED;
}
- return vector;
+ return array;
}
void *
-nxt_vector_init(nxt_vector_t *vector, nxt_uint_t items, size_t item_size,
+nxt_array_init(nxt_array_t *array, nxt_uint_t items, size_t item_size,
const nxt_mem_proto_t *proto, void *pool)
{
- vector->start = proto->alloc(pool, items * item_size);
+ array->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;
+ if (nxt_fast_path(array->start != NULL)) {
+ array->items = 0;
+ array->item_size = item_size;
+ array->avalaible = items;
+ array->type = NXT_ARRAY_INITED;
}
- return vector->start;
+ return array->start;
}
void
-nxt_vector_destroy(nxt_vector_t *vector, const nxt_mem_proto_t *proto,
- void *pool)
+nxt_array_destroy(nxt_array_t *array, const nxt_mem_proto_t *proto, void *pool)
{
- switch (vector->type) {
+ switch (array->type) {
- case NXT_VECTOR_INITED:
- proto->free(pool, vector->start);
+ case NXT_ARRAY_INITED:
+ proto->free(pool, array->start);
#if (NXT_DEBUG)
- vector->start = NULL;
- vector->items = 0;
- vector->avalaible = 0;
+ array->start = NULL;
+ array->items = 0;
+ array->avalaible = 0;
#endif
break;
- case NXT_VECTOR_DESCRETE:
- proto->free(pool, vector->start);
+ case NXT_ARRAY_DESCRETE:
+ proto->free(pool, array->start);
/* Fall through. */
- case NXT_VECTOR_EMBEDDED:
- proto->free(pool, vector);
+ case NXT_ARRAY_EMBEDDED:
+ proto->free(pool, array);
break;
}
}
void *
-nxt_vector_add(nxt_vector_t *vector, const nxt_mem_proto_t *proto, void *pool)
+nxt_array_add(nxt_array_t *array, const nxt_mem_proto_t *proto, void *pool)
{
void *item, *start, *old;
size_t size;
uint32_t n;
- n = vector->avalaible;
+ n = array->avalaible;
- if (n == vector->items) {
+ if (n == array->items) {
if (n < 16) {
- /* Allocate new vector twice as much as current. */
+ /* Allocate new array twice as much as current. */
n *= 2;
} else {
- /* Allocate new vector half as much as current. */
+ /* Allocate new array half as much as current. */
n += n / 2;
}
- size = n * vector->item_size;
+ size = n * array->item_size;
start = proto->alloc(pool, size);
if (nxt_slow_path(start == NULL)) {
return NULL;
}
- vector->avalaible = n;
- old = vector->start;
- vector->start = start;
+ array->avalaible = n;
+ old = array->start;
+ array->start = start;
memcpy(start, old, size);
- if (vector->type == NXT_VECTOR_EMBEDDED) {
- vector->type = NXT_VECTOR_DESCRETE;
+ if (array->type == NXT_ARRAY_EMBEDDED) {
+ array->type = NXT_ARRAY_DESCRETE;
} else {
proto->free(pool, old);
}
}
- item = (char *) vector->start + vector->item_size * vector->items;
+ item = (char *) array->start + array->item_size * array->items;
- vector->items++;
+ array->items++;
return item;
}
void *
-nxt_vector_zero_add(nxt_vector_t *vector, const nxt_mem_proto_t *proto,
- void *pool)
+nxt_array_zero_add(nxt_array_t *array, const nxt_mem_proto_t *proto, void *pool)
{
void *item;
- item = nxt_vector_add(vector, proto, pool);
+ item = nxt_array_add(array, proto, pool);
if (nxt_fast_path(item != NULL)) {
- memset(item, 0, vector->item_size);
+ memset(item, 0, array->item_size);
}
return item;
void
-nxt_vector_remove(nxt_vector_t *vector, void *item)
+nxt_array_remove(nxt_array_t *array, void *item)
{
u_char *next, *last, *end;
uint32_t item_size;
- item_size = vector->item_size;
- end = (u_char *) vector->start + item_size * vector->items;
+ item_size = array->item_size;
+ end = (u_char *) array->start + item_size * array->items;
last = end - item_size;
if (item != last) {
memmove(item, next, end - next);
}
- vector->items--;
+ array->items--;
}
typedef enum {
- NXT_VECTOR_INITED = 0,
- NXT_VECTOR_DESCRETE,
- NXT_VECTOR_EMBEDDED,
-} nxt_vector_type_t;
+ NXT_ARRAY_INITED = 0,
+ NXT_ARRAY_DESCRETE,
+ NXT_ARRAY_EMBEDDED,
+} nxt_array_type_t;
typedef struct {
void *start;
/*
- * A vector can hold no more than 65536 items.
+ * A array can hold no more than 65536 items.
* The item size is no more than 64K.
*/
uint16_t items;
uint16_t avalaible;
uint16_t item_size;
- nxt_vector_type_t type:8;
-} nxt_vector_t;
+ nxt_array_type_t type:8;
+} nxt_array_t;
-NXT_EXPORT nxt_vector_t *nxt_vector_create(nxt_uint_t items, size_t item_size,
+NXT_EXPORT nxt_array_t *nxt_array_create(nxt_uint_t items, size_t item_size,
const nxt_mem_proto_t *proto, void *pool);
-NXT_EXPORT void *nxt_vector_init(nxt_vector_t *vector, nxt_uint_t items,
+NXT_EXPORT void *nxt_array_init(nxt_array_t *array, nxt_uint_t items,
size_t item_size, const nxt_mem_proto_t *proto, void *pool);
-NXT_EXPORT void nxt_vector_destroy(nxt_vector_t *vector,
+NXT_EXPORT void nxt_array_destroy(nxt_array_t *array,
const nxt_mem_proto_t *proto, void *pool);
-NXT_EXPORT void *nxt_vector_add(nxt_vector_t *vector,
+NXT_EXPORT void *nxt_array_add(nxt_array_t *array, const nxt_mem_proto_t *proto,
+ void *pool);
+NXT_EXPORT void *nxt_array_zero_add(nxt_array_t *array,
const nxt_mem_proto_t *proto, void *pool);
-NXT_EXPORT void *nxt_vector_zero_add(nxt_vector_t *vector,
- const nxt_mem_proto_t *proto, void *pool);
-NXT_EXPORT void nxt_vector_remove(nxt_vector_t *vector, void *item);
+NXT_EXPORT void nxt_array_remove(nxt_array_t *array, void *item);
-#define nxt_vector_last(vector) \
+#define nxt_array_last(array) \
((void *) \
- ((char *) (vector)->start \
- + (vector)->item_size * ((vector)->items - 1)))
+ ((char *) (array)->start \
+ + (array)->item_size * ((array)->items - 1)))
-#define nxt_vector_reset(vector) \
- (vector)->items = 0;
+#define nxt_array_reset(array) \
+ (array)->items = 0;
-#define nxt_vector_is_empty(vector) \
- ((vector)->items == 0)
+#define nxt_array_is_empty(array) \
+ ((array)->items == 0)
nxt_inline void *
-nxt_vector_remove_last(nxt_vector_t *vector)
+nxt_array_remove_last(nxt_array_t *array)
{
- vector->items--;
- return (char *) vector->start + vector->item_size * vector->items;
+ array->items--;
+ return (char *) array->start + array->item_size * array->items;
}