From 42be2e658987225458a98a54d5a9917c8d997457 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 31 May 2014 12:40:24 -0400 Subject: array: Allow arrays to be allocated from pools. --- libdimension/Makefile.am | 1 + libdimension/array.c | 33 ++++++++++++++++++++++++++++ libdimension/dimension/array.h | 50 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 libdimension/array.c (limited to 'libdimension') 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 * + * * + * 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 * + * . * + *************************************************************************/ + +/** + * @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; } @@ -74,6 +85,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. -- cgit v1.2.3