diff options
| author | kr.angelov <kr.angelov@gmail.com> | 2012-01-20 13:41:10 +0000 |
|---|---|---|
| committer | kr.angelov <kr.angelov@gmail.com> | 2012-01-20 13:41:10 +0000 |
| commit | 2eee382a62a909d5a3f2f5eda94f30fe68fd5335 (patch) | |
| tree | b0b0d513535895f244214aebf6358e172b8dce6d /src/runtime/c/gu/list.c | |
| parent | b9728357126f8b9a6311cca17d9f0dcc2a7bfb9b (diff) | |
initial import of the C runtime
Diffstat (limited to 'src/runtime/c/gu/list.c')
| -rw-r--r-- | src/runtime/c/gu/list.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/runtime/c/gu/list.c b/src/runtime/c/gu/list.c new file mode 100644 index 000000000..d98a42e41 --- /dev/null +++ b/src/runtime/c/gu/list.c @@ -0,0 +1,59 @@ +/* + * Copyright 2010 University of Helsinki. + * + * This file is part of libgu. + * + * Libgu is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Libgu is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with libgu. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <gu/list.h> +#include <gu/assert.h> +#include <string.h> + +static const int gu_list_empty = 0; + +void* gu_list_alloc(GuPool* pool, size_t base_size, size_t elem_size, + int n_elems, size_t alignment) +{ + gu_assert(n_elems >= 0); + if (n_elems == 0) { + return (void*) &gu_list_empty; + } + // XXX: use gu_flex_size, use offset of elems + void* p = gu_malloc_aligned(pool, base_size + elem_size * n_elems, + alignment); + *(int*) p = n_elems; + return p; +} + + +GU_DEFINE_KIND(GuList, abstract); + +// GU_DEFINE_TYPE(GuStrs, GuList, gu_type(GuStr)); +// GU_DEFINE_TYPE(GuStrsP, pointer, gu_type(GuStrs)); + +void* +gu_list_type_alloc(GuListType* ltype, int n_elems, GuPool* pool) +{ + return gu_list_alloc(pool, ltype->size, + gu_type_size(ltype->elem_type), + n_elems, ltype->align); +} + +void* +gu_list_type_index(GuListType* ltype, void* list, int i) +{ + uint8_t* p = list; + return &p[ltype->elems_offset + i * gu_type_size(ltype->elem_type)]; +} |
