diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-05-31 12:40:24 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-05-31 12:40:24 -0400 |
commit | 42be2e658987225458a98a54d5a9917c8d997457 (patch) | |
tree | 6d659b9d348d05226969c070eb1add020eb2671b /libdimension | |
parent | c9c85a530402541518dd585de682bb0ecdf7cf7b (diff) | |
download | dimension-42be2e658987225458a98a54d5a9917c8d997457.tar.xz |
array: Allow arrays to be allocated from pools.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/Makefile.am | 1 | ||||
-rw-r--r-- | libdimension/array.c | 33 | ||||
-rw-r--r-- | libdimension/dimension/array.h | 50 |
3 files changed, 79 insertions, 5 deletions
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index fb14a2e..5cb3e0d 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -65,6 +65,7 @@ nobase_include_HEADERS = dimension.h \ lib_LTLIBRARIES = libdimension.la libdimension_la_SOURCES = $(nobase_include_HEADERS) \ + array.c \ bvh.c \ bvh.h \ camera.c \ diff --git a/libdimension/array.c b/libdimension/array.c new file mode 100644 index 0000000..deea035 --- /dev/null +++ b/libdimension/array.c @@ -0,0 +1,33 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library 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. * + * * + * The Dimension Library 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 this program. If not, see * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +/** + * @file + * Non-inline array functions. + */ + +#include "dimension.h" + +void +dmnsn_array_cleanup(void *ptr) +{ + dmnsn_array *array = ptr; + dmnsn_free(array->ptr); +} diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h index 6a8249f..9261a0e 100644 --- a/libdimension/dimension/array.h +++ b/libdimension/dimension/array.h @@ -36,21 +36,32 @@ typedef struct dmnsn_array { } dmnsn_array; /** - * Allocate an array. + * @internal + * Initialize a new array. + * @param[out] array The array to initialize. * @param[in] obj_size The size of the objects to store in the array. - * @return An empty array. */ -DMNSN_INLINE dmnsn_array * -dmnsn_new_array(size_t obj_size) +DMNSN_INLINE void +dmnsn_init_array(dmnsn_array *array, size_t obj_size) { - dmnsn_array *array = (dmnsn_array *)dmnsn_malloc(sizeof(dmnsn_array)); array->obj_size = obj_size; array->length = 0; array->capacity = 2; /* Start with capacity of 2 */ /* Allocate the memory */ array->ptr = dmnsn_malloc(array->capacity*array->obj_size); +} +/** + * Allocate an array. + * @param[in] obj_size The size of the objects to store in the array. + * @return An empty array. + */ +DMNSN_INLINE dmnsn_array * +dmnsn_new_array(size_t obj_size) +{ + dmnsn_array *array = DMNSN_MALLOC(dmnsn_array); + dmnsn_init_array(array, obj_size); return array; } @@ -75,6 +86,35 @@ dmnsn_delete_array(dmnsn_array *array) } /** + * @internal + * Free a pool-allocated array. + * @param[in,out] ptr The array to clean up. + */ +void dmnsn_array_cleanup(void *ptr); + +/** + * Allocate an array from a pool. + * @param[in] pool The memory pool to allocate from. + * @param[in] obj_size The size of the objects to store in the array. + * @return An empty array. + */ +DMNSN_INLINE dmnsn_array * +dmnsn_palloc_array(dmnsn_pool *pool, size_t obj_size) +{ + dmnsn_array *array = DMNSN_PALLOC_TIDY(pool, dmnsn_array, dmnsn_array_cleanup); + dmnsn_init_array(array, obj_size); + return array; +} + +/** + * Allocate an array from a pool. + * @param[in] pool The memory pool to allocate from. + * @param[in] type Type type of element to store in the array. + * @return An empty array. + */ +#define DMNSN_PALLOC_ARRAY(pool, type) (dmnsn_palloc_array(pool, sizeof(type))) + +/** * Get the size of the array. * @param[in] array The array in question. * @return The number of elements in the array. |