Exemplo n.º 1
0
static void vector_guarantee_space(struct vector** vector)
{
    if ((*vector)->size == (*vector)->capacity) {
        int new_capacity = (*vector)->capacity * 2;
        vector_realloc(vector, new_capacity);
    }
}
Exemplo n.º 2
0
int vector_new(TALLOC_CTX *context, struct vector **out_vector)
{
	int ret = -1;
	TALLOC_CTX *temp_context;
	struct vector *vec;

	temp_context = talloc_new(NULL);
	if (temp_context == NULL) {
		_ERROR("%s: out of memory allocating temporary context for vector.\n", __FUNCTION__);
		ret = -1;
		goto out;
	}

	vec = talloc_zero(temp_context, struct vector);
	if (vec == NULL) {
		_ERROR("%s: out of memory allocating temporary context for vector.\n", __FUNCTION__);
		ret = -1;
		goto out;
	}

	vec->capacity = 1;
	vec->size = 0;

	vector_realloc(vec);

	*out_vector = (struct vector *)talloc_steal(context, vec);

	ret = 0;
out:
	talloc_free(temp_context);

	return ret;
}
Exemplo n.º 3
0
void vector_resize(struct vector* self, size_t newsize)
{
    if (newsize > self->capacity) {
        vector_realloc(self, newsize);
    }
    self->size = newsize;
}
Exemplo n.º 4
0
int cmd_help(char **arg)
{
	const char *topic = get_arg(arg);

	if (topic) {
		struct cmddb_record cmd;
		struct opdb_key key;

		if (!cmddb_get(topic, &cmd)) {
			printc("\x1b[1mCOMMAND: %s\x1b[0m\n\n%s\n",
			       cmd.name, cmd.help);
			return 0;
		}

		if (!opdb_get(topic, &key, NULL)) {
			printc("\x1b[1mOPTION: %s (%s)\x1b[0m\n\n%s\n",
			       key.name, type_text(key.type), key.help);
			return 0;
		}

		printc_err("help: unknown command: %s\n", topic);
		return -1;
	} else {
		struct vector v;

		vector_init(&v, sizeof(const char *));

		if (!cmddb_enum(push_command_name, &v)) {
			printc("Available commands:\n");
			namelist_print(&v);
			printc("\n");
		} else {
			pr_error("help: can't allocate memory for command list");
		}

		vector_realloc(&v, 0);

		if (!opdb_enum(push_option_name, &v)) {
			printc("Available options:\n");
			namelist_print(&v);
			printc("\n");
		} else {
			pr_error("help: can't allocate memory for option list");
		}

		vector_destroy(&v);

		printc("Type \"help <topic>\" for more information.\n");
		printc("Use the \"opt\" command (\"help opt\") to set "
			"options.\n");
#if defined(__Windows__) && !defined(USE_READLINE)
		printc("Press Ctrl+Z, Enter to quit.\n");
#else
		printc("Press Ctrl+D to quit.\n");
#endif
	}

	return 0;
}
Exemplo n.º 5
0
void vector_memcpy(struct vector** vector, const void* values, int size)
{
    if ((*vector)->capacity < size) {
        vector_realloc(vector, size);
    }
    memcpy((*vector)->begin, values, size * (*vector)->elem_size);
    (*vector)->size = size;
}
Exemplo n.º 6
0
int vector_push_back(struct vector *vector, void *data)
{
	if (vector->size >= vector->capacity) {
		if (vector_realloc(vector) < 0) {
			_ERROR("%s: out of memory reallocating vector array to %zu items.\n", __FUNCTION__, vector->capacity);
			return -1;
		}
	}

	vector_set(vector, vector->size++, data);

	return 0;
}
Exemplo n.º 7
0
Arquivo: test.c Projeto: esheldon/misc
void test_realloc_resize() {
    size_t n=10;
    struct vector* v = vector_new(n, sizeof(struct test));

    assert(v->size == n);
    assert(v->capacity == n);

    size_t new_n = 12;
    vector_realloc(v, new_n);
    assert(v->size == n);
    assert(v->capacity == new_n);

    new_n = 7;
    vector_realloc(v, new_n);
    assert(v->size == new_n);
    assert(v->capacity == new_n);

    size_t rsn = 6;
    vector_resize(v,rsn);
    assert(v->size == rsn);
    assert(v->capacity == new_n);

    rsn = 12;
    vector_resize(v,rsn);
    assert(v->size == rsn);
    assert(v->capacity == rsn);

    vector_clear(v);
    assert(v->size == 0);
    assert(v->capacity == rsn);

    vector_freedata(v);
    assert(v->size == 0);
    assert(v->capacity == 0);
    assert(v->d == NULL);

    v = vector_delete(v);
    assert(v == NULL);
}
Exemplo n.º 8
0
void vector_push(struct vector* self, void* val)
{
    // see if we have already filled the available data vector
    // if so, reallocate to larger storage
    if (self->size == self->capacity) {
        size_t new_capacity = _vector_new_push_capacity(self);
        vector_realloc(self, new_capacity);
    }

    memcpy(self->d+self->size*self->elsize, 
           val, 
           self->elsize);
    self->size++;
}
Exemplo n.º 9
0
void* vector_extend(struct vector* self)
{
    void* p=NULL;

    // see if we have already filled the available data vector
    // if so, reallocate to larger storage
    if (self->size == self->capacity) {
        size_t new_capacity = _vector_new_push_capacity(self);
        vector_realloc(self, new_capacity);
    }

    p = self->d+self->size*self->elsize;
    memset(p, 0, self->elsize);
    self->size++;

    return p;
}