summaryrefslogtreecommitdiffstats
path: root/libdimension/dimension/base
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-08-19 17:10:03 -0400
committerTavian Barnes <tavianator@tavianator.com>2015-10-25 11:03:56 -0400
commit7b09710392d35fb55b52031d447a542d99fc6b4b (patch)
tree270eb927ee8c52ceeb99926ebf4843704775a610 /libdimension/dimension/base
parent200c86b91ea7063d35be3bffc11c5da53c054653 (diff)
downloaddimension-7b09710392d35fb55b52031d447a542d99fc6b4b.tar.xz
Modularize the libdimension codebase.
Diffstat (limited to 'libdimension/dimension/base')
-rw-r--r--libdimension/dimension/base/array.h361
-rw-r--r--libdimension/dimension/base/common.h34
-rw-r--r--libdimension/dimension/base/compiler.h139
-rw-r--r--libdimension/dimension/base/dictionary.h87
-rw-r--r--libdimension/dimension/base/error.h118
-rw-r--r--libdimension/dimension/base/malloc.h70
-rw-r--r--libdimension/dimension/base/pool.h81
7 files changed, 890 insertions, 0 deletions
diff --git a/libdimension/dimension/base/array.h b/libdimension/dimension/base/array.h
new file mode 100644
index 0000000..a54d0e5
--- /dev/null
+++ b/libdimension/dimension/base/array.h
@@ -0,0 +1,361 @@
+/*************************************************************************
+ * Copyright (C) 2009-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
+ * Simple dynamic arrays.
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+#include <stddef.h> /* For size_t */
+#include <stdlib.h> /* For qsort() */
+#include <string.h> /* For memcpy() */
+
+/** Dynamic array type. */
+typedef struct dmnsn_array {
+ void *ptr; /**< @internal The actual memory. */
+ size_t obj_size; /**< @internal The size of each object. */
+ size_t length; /**< @internal The current size of the array. */
+ size_t capacity; /**< @internal The size of the allocated space. */
+} dmnsn_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.
+ */
+DMNSN_INLINE void
+dmnsn_init_array(dmnsn_array *array, size_t obj_size)
+{
+ 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;
+}
+
+/**
+ * Allocate an array.
+ * @param[in] type Type type of element to store in the array.
+ * @return An empty array.
+ */
+#define DMNSN_NEW_ARRAY(type) (dmnsn_new_array(sizeof(type)))
+
+/**
+ * Delete an array.
+ * @param[in,out] array The array to delete.
+ */
+DMNSN_INLINE void
+dmnsn_delete_array(dmnsn_array *array)
+{
+ if (array) {
+ dmnsn_free(array->ptr);
+ dmnsn_free(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.
+ */
+DMNSN_INLINE size_t
+dmnsn_array_size(const dmnsn_array *array)
+{
+ return array->length;
+}
+
+/**
+ * Set the size of the array.
+ * @param[in,out] array The array to resize.
+ * @param[in] length The new length of the array.
+ */
+DMNSN_INLINE void
+dmnsn_array_resize(dmnsn_array *array, size_t length)
+{
+ if (length > array->capacity) {
+ /* Resize if we don't have enough capacity */
+ array->capacity = length*2; /* We are greedy */
+ array->ptr = dmnsn_realloc(array->ptr, array->obj_size*array->capacity);
+ }
+
+ array->length = length;
+}
+
+/**
+ * Copy an array.
+ * @param[in] array The array to copy.
+ * @return A copy of the array.
+ */
+DMNSN_INLINE dmnsn_array *
+dmnsn_array_copy(const dmnsn_array *array)
+{
+ dmnsn_array *copy = dmnsn_new_array(array->obj_size);
+ dmnsn_array_resize(copy, dmnsn_array_size(array));
+ memcpy(copy->ptr, array->ptr, dmnsn_array_size(array)*array->obj_size);
+ return copy;
+}
+
+/**
+ * Split an array in half.
+ * @param[in,out] array The array to split.
+ * @return The second half of the array.
+ */
+DMNSN_INLINE dmnsn_array *
+dmnsn_array_split(dmnsn_array *array)
+{
+ dmnsn_array *half = dmnsn_new_array(array->obj_size);
+ size_t new_size = dmnsn_array_size(array)/2;
+ size_t old_size = dmnsn_array_size(array) - new_size;
+ dmnsn_array_resize(half, new_size);
+ memcpy(half->ptr, (char *)array->ptr + old_size*array->obj_size,
+ new_size*array->obj_size);
+ dmnsn_array_resize(array, old_size);
+ return half;
+}
+
+/**
+ * Get the i'th element.
+ * @param[in] array The array to access.
+ * @param[in] i The index of the element to extract.
+ * @param[out] obj The location to store the extracted object.
+ */
+DMNSN_INLINE void
+dmnsn_array_get(const dmnsn_array *array, size_t i, void *obj)
+{
+ dmnsn_assert(i < dmnsn_array_size(array), "Array index out of bounds.");
+ memcpy(obj, (char *)array->ptr + array->obj_size*i, array->obj_size);
+}
+
+/**
+ * Set the i'th object, expanding the array if necessary.
+ * @param[in,out] array The array to modify.
+ * @param[in] i The index to update.
+ * @param[in] obj The location of the object to copy into the array.
+ */
+DMNSN_INLINE void
+dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj)
+{
+ if (i >= dmnsn_array_size(array)) {
+ /* Resize if i is out of range */
+ dmnsn_array_resize(array, i + 1);
+ }
+ memcpy((char *)array->ptr + array->obj_size*i, obj, array->obj_size);
+}
+
+/**
+ * First element.
+ * @param[in] array The array to index.
+ * @return The address of the first element of the array.
+ */
+DMNSN_INLINE void *
+dmnsn_array_first(const dmnsn_array *array)
+{
+ return array->ptr;
+}
+
+/**
+ * Last element.
+ * @param[in] array The array to index.
+ * @return The address of the last element of the array.
+ */
+DMNSN_INLINE void *
+dmnsn_array_last(const dmnsn_array *array)
+{
+ return (char *)array->ptr + array->obj_size*(array->length - 1);
+}
+
+/**
+ * Arbitrary element access.
+ * @param[in] array The array to index.
+ * @param[in] i The index to access.
+ * @return The address of the i'th element of the array.
+ */
+DMNSN_INLINE void *
+dmnsn_array_at(const dmnsn_array *array, size_t i)
+{
+ dmnsn_assert(i < dmnsn_array_size(array), "Array index out of bounds.");
+ return (char *)array->ptr + array->obj_size*i;
+}
+
+/**
+ * Push an object to the end of the array.
+ * @param[in,out] array The array to append to.
+ * @param[in] obj The location of the object to push.
+ */
+DMNSN_INLINE void
+dmnsn_array_push(dmnsn_array *array, const void *obj)
+{
+ dmnsn_array_set(array, dmnsn_array_size(array), obj);
+}
+
+/**
+ * Pop an object from the end of the array.
+ * @param[in,out] array The array to pop from.
+ * @param[out] obj The location to store the extracted object.
+ */
+DMNSN_INLINE void
+dmnsn_array_pop(dmnsn_array *array, void *obj)
+{
+ size_t size = dmnsn_array_size(array);
+ dmnsn_assert(size > 0, "Array is empty.");
+ dmnsn_array_get(array, size - 1, obj); /* Copy the object */
+ dmnsn_array_resize(array, size - 1); /* Shrink the array */
+}
+
+/**
+ * Insert an item into the middle of the array. This is O(n).
+ * @param[in,out] array The array to insert to.
+ * @param[in] i The index at which to insert.
+ * @param[in] obj The object to insert.
+ */
+DMNSN_INLINE void
+dmnsn_array_insert(dmnsn_array *array, size_t i, const void *obj)
+{
+ size_t size = dmnsn_array_size(array) + 1;
+ if (i >= size)
+ size = i + 1;
+ dmnsn_array_resize(array, size);
+
+ /* Move the elements at and after `i' 1 to the right */
+ memmove((char *)array->ptr + array->obj_size*(i + 1),
+ (char *)array->ptr + array->obj_size*i,
+ array->obj_size*(size - i - 1));
+ /* Insert `obj' at `i' */
+ memcpy((char *)array->ptr + array->obj_size*i, obj, array->obj_size);
+}
+
+/**
+ * Remove an item from the middle of the array. This is O(n).
+ * @param[in,out] array The array to remove from.
+ * @param[in] i The index to remove.
+ */
+DMNSN_INLINE void
+dmnsn_array_remove(dmnsn_array *array, size_t i)
+{
+ size_t size = dmnsn_array_size(array);
+ dmnsn_assert(i < size, "Array index out of bounds.");
+ /* Move the array elements after `i' 1 to the left */
+ memmove((char *)array->ptr + array->obj_size*i,
+ (char *)array->ptr + array->obj_size*(i + 1),
+ array->obj_size*(size - i - 1));
+ /* Decrease the size by 1 */
+ dmnsn_array_resize(array, size - 1);
+}
+
+/**
+ * Apply a callback to each element of an array.
+ * @param[in,out] array The array.
+ * @param[in] callback The callback to apply to the elements.
+ */
+DMNSN_INLINE void
+dmnsn_array_apply(dmnsn_array *array, dmnsn_callback_fn *callback)
+{
+ char *i, *last = (char *)dmnsn_array_last(array);
+ for (i = (char *)dmnsn_array_first(array); i <= last; i += array->obj_size) {
+ callback((void *)i);
+ }
+}
+
+/**
+ * Callback type for array sorting.
+ * @param[in] a The first element.
+ * @param[in] b The second element.
+ * @return A negative value iff a < b, zero iff a == b, and a positive value iff
+ * a > b.
+ */
+typedef int dmnsn_array_comparator_fn(const void *a, const void *b);
+
+/**
+ * Sort an array.
+ * @param[in,out] array The array to sort.
+ * @param[in] comparator The sorting comparator to use.
+ */
+DMNSN_INLINE void
+dmnsn_array_sort(dmnsn_array *array, dmnsn_array_comparator_fn *comparator)
+{
+ qsort(array->ptr, dmnsn_array_size(array), array->obj_size, comparator);
+}
+
+/* Macros to shorten array iteration */
+
+/**
+ * Iterate over an array. For example,
+ * @code
+ * DMNSN_ARRAY_FOREACH (int *, i, array) {
+ * printf("%d\n", *i);
+ * }
+ * @endcode
+ *
+ * @param type The (pointer) type to use as an iterator.
+ * @param i The name of the iterator within the loop body.
+ * @param[in] array The array to loop over.
+ */
+#define DMNSN_ARRAY_FOREACH(type, i, array) \
+ for (type i = dmnsn_array_first(array); \
+ (size_t)(i - (type)dmnsn_array_first(array)) < dmnsn_array_size(array); \
+ ++i)
diff --git a/libdimension/dimension/base/common.h b/libdimension/dimension/base/common.h
new file mode 100644
index 0000000..b947b4a
--- /dev/null
+++ b/libdimension/dimension/base/common.h
@@ -0,0 +1,34 @@
+/*************************************************************************
+ * 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
+ * Common types and utilities.
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+/**
+ * Generic callback type.
+ * @param[in,out] ptr A pointer to an object to act on.
+ */
+typedef void dmnsn_callback_fn(void *ptr);
diff --git a/libdimension/dimension/base/compiler.h b/libdimension/dimension/base/compiler.h
new file mode 100644
index 0000000..a83f1b9
--- /dev/null
+++ b/libdimension/dimension/base/compiler.h
@@ -0,0 +1,139 @@
+/*************************************************************************
+ * Copyright (C) 2009-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
+ * Compiler abstractions.
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+/**
+ * @internal
+ * @def DMNSN_C_VERSION
+ * The C version according to \p __STDC_VERSION__ if available, otherwise 0.
+ */
+#ifdef __STDC_VERSION__
+ #define DMNSN_C_VERSION __STDC_VERSION__
+#else
+ #define DMNSN_C_VERSION 0L
+#endif
+
+/**
+ * @internal
+ * @def DMNSN_CXX_VERSION
+ * The C++ version according to \p __cplusplus if available, otherwise 0.
+ */
+#ifdef __cplusplus
+ #define DMNSN_CXX_VERSION __cplusplus
+#else
+ #define DMNSN_CXX_VERSION 0L
+#endif
+
+/**
+ * @internal
+ * Whether we're being compiled as C++.
+ */
+#define DMNSN_CXX (DMNSN_CXX_VERSION > 0)
+
+/**
+ * @internal
+ * Whether C++11 features are supported.
+ */
+#define DMNSN_CXX11 (DMNSN_CXX_VERSION >= 201103L)
+
+/**
+ * @internal
+ * Whether C99 features are supported.
+ */
+#define DMNSN_C99 (DMNSN_C_VERSION >= 199901L || DMNSN_CXX11)
+
+/**
+ * @internal
+ * Whether C11 features are supported.
+ */
+#define DMNSN_C11 (DMNSN_C_VERSION >= 201112L)
+
+/**
+ * @internal
+ * Whether GNU C features are supported.
+ */
+#define DMNSN_GNUC defined(__GNUC__)
+
+/**
+ * @def DMNSN_INLINE
+ * A portable inline specifier. Expands to the correct method of declaring
+ * inline functions for the version of C you are using.
+ */
+#ifndef DMNSN_INLINE
+ #if DMNSN_CXX
+ /* C++ inline semantics */
+ #define DMNSN_INLINE inline
+ #elif DMNSN_C99
+ /* C99 inline semantics */
+ #define DMNSN_INLINE inline
+ #elif DMNSN_GNUC
+ /* GCC inline semantics */
+ #define DMNSN_INLINE __extension__ extern __inline__
+ #else
+ /* Unknown C - mark functions static and hope the compiler is smart enough
+ to inline them */
+ #define DMNSN_INLINE static
+ #endif
+#endif
+
+/**
+ * @def DMNSN_NORETURN
+ * A portable noreturn attribute.
+ */
+#if DMNSN_CXX11
+ #define DMNSN_NORETURN [[noreturn]] void
+#elif DMNSN_C11
+ #define DMNSN_NORETURN _Noreturn void
+#elif DMNSN_GNUC
+ #define DMNSN_NORETURN __attribute__((noreturn)) void
+#else
+ #define DMNSN_NORETURN void
+#endif
+
+/**
+ * @internal
+ * @def DMNSN_FUNC
+ * @brief Expands to the name of the current function
+ */
+#if DMNSN_GNUC
+ #define DMNSN_FUNC __PRETTY_FUNCTION__
+#elif DMNSN_C99
+ #define DMNSN_FUNC __func__
+#else
+ #define DMNSN_FUNC "<unknown function>"
+#endif
+
+/**
+ * @internal
+ * An unreachable statement.
+ */
+#if DMNSN_GNUC
+ #define DMNSN_UNREACHABLE() __builtin_unreachable()
+#else
+ #define DMNSN_UNREACHABLE() ((void)0)
+#endif
diff --git a/libdimension/dimension/base/dictionary.h b/libdimension/dimension/base/dictionary.h
new file mode 100644
index 0000000..7360b45
--- /dev/null
+++ b/libdimension/dimension/base/dictionary.h
@@ -0,0 +1,87 @@
+/*************************************************************************
+ * Copyright (C) 2010-2011 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
+ * Simple associative arrays.
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+/** A string-object associative array. */
+typedef struct dmnsn_dictionary dmnsn_dictionary;
+
+/**
+ * Allocate a dictionary.
+ * @param[in] obj_size The size of the objects to store in the dictionary.
+ * @return An empty dictionary.
+ */
+dmnsn_dictionary *dmnsn_new_dictionary(size_t obj_size);
+
+/**
+ * Delete a dictionary.
+ * @param[in,out] dict The dictionary to delete.
+ */
+void dmnsn_delete_dictionary(dmnsn_dictionary *dict);
+
+/**
+ * Find an element in a dictionary.
+ * @param[in] dict The dictionary to search.
+ * @param[in] key The key to search for.
+ * @param[out] obj The location to store the found object.
+ * @return Whether the element was found.
+ */
+bool dmnsn_dictionary_get(const dmnsn_dictionary *dict, const char *key,
+ void *obj);
+
+/**
+ * Access an element in a dictionary.
+ * @param[in] dict The dictionary to search.
+ * @param[in] key The key to search for.
+ * @return A pointer to the element if found, otherwise NULL.
+ */
+void *dmnsn_dictionary_at(const dmnsn_dictionary *dict, const char *key);
+
+/**
+ * Insert a (key, value) pair into a dictionary.
+ * @param[in,out] dict The dictionary to modify.
+ * @param[in] key The key to insert.
+ * @param[in] obj The object to insert.
+ */
+void dmnsn_dictionary_insert(dmnsn_dictionary *dict, const char *key,
+ const void *obj);
+
+/**
+ * Remove a (key, value) pair from a dictionary.
+ * @param[in,out] dict The dictionary to modify.
+ * @param[in] key The key to remove.
+ * @return Whether the key existed in the dictionary.
+ */
+bool dmnsn_dictionary_remove(dmnsn_dictionary *dict, const char *key);
+
+/**
+ * Apply a callback to all elements in a dictionary.
+ * @param[in,out] dict The dictionary.
+ * @param[in] callback The callback to apply to the elements.
+ */
+void dmnsn_dictionary_apply(dmnsn_dictionary *dict,
+ dmnsn_callback_fn *callback);
diff --git a/libdimension/dimension/base/error.h b/libdimension/dimension/base/error.h
new file mode 100644
index 0000000..d039081
--- /dev/null
+++ b/libdimension/dimension/base/error.h
@@ -0,0 +1,118 @@
+/*************************************************************************
+ * Copyright (C) 2009-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
+ * Error reporting.
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+#include <stdbool.h>
+
+/**
+ * Report a warning.
+ * @param[in] str A string to print explaining the warning.
+ */
+#define dmnsn_warning(str) \
+ dmnsn_report_warning(DMNSN_FUNC, __FILE__, __LINE__, str)
+
+/**
+ * Report an error.
+ * @param[in] str A string to print explaining the error.
+ */
+#define dmnsn_error(str) \
+ dmnsn_report_error(DMNSN_FUNC, __FILE__, __LINE__, str)
+
+/**
+ * @def dmnsn_assert
+ * Make an assertion.
+ * @param[in] expr The expression to assert.
+ * @param[in] str A string to print if the assertion fails.
+ */
+#if DMNSN_DEBUG
+ #define dmnsn_assert(expr, str) \
+ do { \
+ if (!(expr)) { \
+ dmnsn_error((str)); \
+ } \
+ } while (0)
+#else
+ #define dmnsn_assert(expr, str) ((void)0)
+#endif
+
+/**
+ * @def dmnsn_unreachable
+ * Express that a line of code is unreachable.
+ * @param[in] str A string to print if the line is reached.
+ */
+#if DMNSN_DEBUG
+ #define dmnsn_unreachable(str) dmnsn_error((str))
+#else
+ #define dmnsn_unreachable(str) DMNSN_UNREACHABLE()
+#endif
+
+/**
+ * @internal
+ * Called by dmnsn_warning(); don't call directly.
+ * @param[in] func The name of the function where the error originated.
+ * @param[in] file The file where the error originated.
+ * @param[in] line The line number where the error originated.
+ * @param[in] str A string describing the error.
+ */
+void dmnsn_report_warning(const char *func, const char *file, unsigned int line, const char *str);
+
+/**
+ * @internal
+ * Called by dmnsn_error(); don't call directly.
+ * @param[in] func The name of the function where the error originated.
+ * @param[in] file The file where the error originated.
+ * @param[in] line The line number where the error originated.
+ * @param[in] str A string describing the error.
+ */
+DMNSN_NORETURN dmnsn_report_error(const char *func, const char *file, unsigned int line, const char *str);
+
+/**
+ * Treat warnings as errors.
+ * @param[in] always_die Whether to die on warnings.
+ */
+void dmnsn_die_on_warnings(bool always_die);
+
+/**
+ * Fatal error callback type. This function should never return.
+ */
+typedef void dmnsn_fatal_error_fn(void);
+
+/**
+ * Get the libdimension fatal error handler, thread-safely. The default fatal
+ * error handler terminates the current thread, or the entire program if the
+ * current thread is the main thread.
+ * @return The current fatal error handler.
+ */
+dmnsn_fatal_error_fn *dmnsn_get_fatal_error_fn(void);
+
+/**
+ * Set the libdimension fatal error handler, thread-safely.
+ * @param[in] fatal The new fatal error handler. This function must never
+ * return.
+ */
+void dmnsn_set_fatal_error_fn(dmnsn_fatal_error_fn *fatal);
diff --git a/libdimension/dimension/base/malloc.h b/libdimension/dimension/base/malloc.h
new file mode 100644
index 0000000..fcc492e
--- /dev/null
+++ b/libdimension/dimension/base/malloc.h
@@ -0,0 +1,70 @@
+/*************************************************************************
+ * Copyright (C) 2010-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
+ * Dynamic memory. dmnsn_malloc() and friends behave like their
+ * non-dmnsn_-prefixed counterparts, but never return NULL. If allocation
+ * fails, they instead call dmnsn_error().
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+#include <stddef.h> /* For size_t */
+
+/**
+ * Allocate some memory. Always use dmnsn_free() to free this memory, never
+ * free().
+ * @param[in] size The size of the memory block to allocate.
+ * @return The allocated memory area.
+ */
+void *dmnsn_malloc(size_t size);
+
+/**
+ * Allocate some memory. Always use dmnsn_free() to free this memory, never
+ * free().
+ * @param[in] type The type of the memory block to allocate.
+ * @return The allocated memory area.
+ */
+#define DMNSN_MALLOC(type) ((type *)dmnsn_malloc(sizeof(type)))
+
+/**
+ * Expand or shrink an allocation created by dmnsn_malloc().
+ * @param[in] ptr The block to resize.
+ * @param[in] size The new size.
+ * @return The resized memory area.
+ */
+void *dmnsn_realloc(void *ptr, size_t size);
+
+/**
+ * Duplicate a string.
+ * @param[in] s The string to duplicate.
+ * @return A string with the same contents as \p s, suitable for release by
+ * dmnsn_free().
+ */
+char *dmnsn_strdup(const char *s);
+
+/**
+ * Free memory allocated by dmnsn_malloc() or dmnsn_strdup().
+ * @param[in] ptr The memory block to free, or NULL.
+ */
+void dmnsn_free(void *ptr);
diff --git a/libdimension/dimension/base/pool.h b/libdimension/dimension/base/pool.h
new file mode 100644
index 0000000..9983f1d
--- /dev/null
+++ b/libdimension/dimension/base/pool.h
@@ -0,0 +1,81 @@
+/*************************************************************************
+ * 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
+ * Memory pools. Rather than more complicated garbage collection methods like
+ * reference counting, objects are allocated out of pools which are freed all at
+ * once once a scene is rendered (for example).
+ */
+
+#ifndef DMNSN_BASE_H
+#error "Please include <dimension/base.h> instead of this header directly."
+#endif
+
+#include <stddef.h> /* For size_t */
+
+/* Forward-declare dmnsn_pool. */
+typedef struct dmnsn_pool dmnsn_pool;
+
+/**
+ * Create a new memory pool.
+ * @return The new pool.
+ */
+dmnsn_pool *dmnsn_new_pool(void);
+
+/**
+ * Allocate some memory from a pool.
+ * @param[in] pool The memory pool to allocate from.
+ * @param[in] size The size of the memory block to allocate.
+ * @return The allocated memory area.
+ */
+void *dmnsn_palloc(dmnsn_pool *pool, size_t size);
+
+/**
+ * Allocate some memory from a pool.
+ * @param[in] pool The memory pool to allocate from.
+ * @param[in] size The size of the memory block to allocate.
+ * @param[in] cleanup_fn A callback to invoke before the memory is freed.
+ * @return The allocated memory area.
+ */
+void *dmnsn_palloc_tidy(dmnsn_pool *pool, size_t size, dmnsn_callback_fn *cleanup_fn);
+
+/**
+ * Allocate some memory from a pool.
+ * @param[in] pool The memory pool to allocate from.
+ * @param[in] type The type of the memory block to allocate.
+ * @return The allocated memory area.
+ */
+#define DMNSN_PALLOC(pool, type) ((type *)dmnsn_palloc((pool), sizeof(type)))
+
+/**
+ * Allocate some memory from a pool.
+ * @param[in] pool The memory pool to allocate from.
+ * @param[in] type The type of the memory block to allocate.
+ * @param[in] cleanup_fn A callback to invoke before the memory is freed.
+ * @return The allocated memory area.
+ */
+#define DMNSN_PALLOC_TIDY(pool, type, cleanup_fn) ((type *)dmnsn_palloc_tidy((pool), sizeof(type), (cleanup_fn)))
+
+/**
+ * Free a memory pool and all associated allocations.
+ * @param[in] pool The memory pool to free.
+ */
+void dmnsn_delete_pool(dmnsn_pool *pool);