aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/malloc.c156
-rw-r--r--src/malloc/aligned_alloc.c53
-rw-r--r--src/malloc/cfree.c43
-rw-r--r--src/malloc/free.c41
-rw-r--r--src/malloc/malloc_usable_size.c40
-rw-r--r--src/malloc/posix_memalign.c51
-rw-r--r--src/malloc/realloc.c53
7 files changed, 281 insertions, 156 deletions
diff --git a/src/malloc.c b/src/malloc.c
index 9b45607..694f8c4 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -221,80 +221,6 @@ void* zalloc(size_t size)
/**
- * Variant of `malloc` that extends, or shrinks, an existing allocation,
- * if beneficial and possible, or creates a new allocation with the new
- * size, copies the data, and frees the old allocation. The returned
- * pointer has an alignment usable for any compiler-independent intrinsic
- * data type, if a new pointer is returned.
- *
- * On error, `ptr` is not freed.
- *
- * @etymology Memory (realloc)ation.
- *
- * @param ptr Pointer to the beginning of the old memory allocation.
- * The process may crash if it does not point to the
- * beginning of a memory allocation on the heap.
- * However, if it is `NULL`, this function will behave
- * like `malloc`.
- * @param size The new allocation size. If zero, this function will
- * behave like `free`, and will return `NULL`.
- * @return Pointer to the beginning of the new allocation.
- * If `size` is zero, `NULL` is returned. On error `NULL`
- * is returned and `errno` is set to indicate the error.
- *
- * @throws ENOMEM The process cannot allocate more memory.
- *
- * @since Always.
- */
-void* realloc(void* ptr, size_t size)
-{
- return fast_realloc(ptr, size);
-}
-
-
-/**
- * Free a memory allocation.
- *
- * As a slibc extension, `errno` is guaranteed not to be set.
- *
- * @etymology (Free) allocated memory.
- *
- * @param ptr Pointer to the beginning of the memory allocation.
- * The process may crash if it does not point to the
- * beginning of a memory allocation on the heap.
- * However, if it is `NULL`, nothing will happen.
- *
- * @since Always.
- */
-void free(void* ptr)
-{
- fast_free(ptr);
-}
-
-
-/**
- * This function is identical to `free`.
- * Any argument beyond the first argument, is ignored.
- *
- * This function uses variadic arguments because there
- * there are multiple conflicting specifications for `cfree`.
- *
- * As a slibc extension, `errno` is guaranteed not to be set.
- *
- * @param ptr Pointer to the beginning of the memory allocation.
- * The process may crash if it does not point to the
- * beginning of a memory allocation on the heap.
- * However, if it is `NULL`, nothing will happen.
- *
- * @since Always.
- */
-void cfree(void* ptr, ...)
-{
- fast_free(ptr);
-}
-
-
-/**
* Variant of `malloc` that returns an address with a
* specified alignment.
*
@@ -350,36 +276,6 @@ void* memalign(size_t boundary, size_t size)
/**
- * `posix_memalign(p, b, n)` is equivalent to
- * `(*p = memalign(b, n), *p ? 0 : errno)`, except
- * `boundary` must also be a multiple of `sizeof(void*)`,
- * and `errno` is unspecified.
- *
- * As a GNU-compliant slibc extension, memory allocated
- * with this function can be freed with `free`.
- *
- * @etymology (POSIX)-extension: (mem)ory alignment.
- *
- * @param ptrptr Output parameter for the allocated memory.
- * @param boundary The alignment.
- * @param size The number of bytes to allocated.
- * @return Zero on success, a value for `errno` on error.
- *
- * @throws ENOMEM The process cannot allocate more memory.
- * @throws EINVAL If `boundary` is not a power-of-two multiple of `sizeof(void*)`.
- *
- * @since Always.
- */
-int posix_memalign(void** ptrptr, size_t boundary, size_t size)
-{
- if (boundary < sizeof(void*))
- return EINVAL;
- *ptrptr = memalign(boundary, size);
- return *ptrptr ? 0 : errno;
-}
-
-
-/**
* `valloc(n)` is equivalent to `memalign(sysconf(_SC_PAGESIZE), n)`.
*
* As a GNU-compliant slibc extension, memory allocated
@@ -437,55 +333,3 @@ void* pvalloc(size_t size)
return memalign(boundary, full_size);
}
-
-/**
- * This function is identical to `memalign`,
- * except it can be freed with `free`.
- *
- * Variant of `malloc` that returns an address with a
- * specified alignment.
- *
- * It is unspecified how the function works. This implemention
- * will allocate a bit of extra memory and shift the returned
- * pointer so that it is aligned.
- *
- * @etymology (Alig)ned memory (alloc)ation.
- *
- * @param boundary The alignment.
- * @param size The number of bytes to allocated.
- * @return Pointer to the beginning of the new allocation.
- * If `size` is zero, this function will either return
- * `NULL` (that is what this implement does) or return
- * a unique pointer that can later be freed with `free`.
- * `NULL` is returned on error, and `errno` is set to
- * indicate the error.
- *
- * @throws ENOMEM The process cannot allocate more memory.
- * @throws EINVAL If `boundary` is not a power of two.
- *
- * @since Always.
- */
-void* aligned_alloc(size_t boundary, size_t size)
-{
- return memalign(boundary, size);
-}
-
-
-/**
- * This function returns the allocation size of
- * a memory segment.
- *
- * `p = malloc(n), malloc_usable_size(p)` will return `n`.
- *
- * @etymology (`malloc`)-subsystem: user-(usable size) of allocation.
- *
- * @param segment The memory segment.
- * @return The size of the memory segment, 0 if `segment` is `NULL`.
- *
- * @since Always.
- */
-size_t malloc_usable_size(void* segment)
-{
- return allocsize(segment);
-}
-
diff --git a/src/malloc/aligned_alloc.c b/src/malloc/aligned_alloc.c
new file mode 100644
index 0000000..a562ca0
--- /dev/null
+++ b/src/malloc/aligned_alloc.c
@@ -0,0 +1,53 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+
+
+
+/**
+ * This function is identical to `memalign`,
+ * except it can be freed with `free`.
+ *
+ * Variant of `malloc` that returns an address with a
+ * specified alignment.
+ *
+ * It is unspecified how the function works. This implemention
+ * will allocate a bit of extra memory and shift the returned
+ * pointer so that it is aligned.
+ *
+ * @etymology (Alig)ned memory (alloc)ation.
+ *
+ * @param boundary The alignment.
+ * @param size The number of bytes to allocated.
+ * @return Pointer to the beginning of the new allocation.
+ * If `size` is zero, this function will either return
+ * `NULL` (that is what this implement does) or return
+ * a unique pointer that can later be freed with `free`.
+ * `NULL` is returned on error, and `errno` is set to
+ * indicate the error.
+ *
+ * @throws ENOMEM The process cannot allocate more memory.
+ * @throws EINVAL If `boundary` is not a power of two.
+ *
+ * @since Always.
+ */
+void* aligned_alloc(size_t boundary, size_t size)
+{
+ return memalign(boundary, size);
+}
+
diff --git a/src/malloc/cfree.c b/src/malloc/cfree.c
new file mode 100644
index 0000000..098f9ff
--- /dev/null
+++ b/src/malloc/cfree.c
@@ -0,0 +1,43 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+#include <slibc-alloc.h>
+
+
+
+/**
+ * This function is identical to `free`.
+ * Any argument beyond the first argument, is ignored.
+ *
+ * This function uses variadic arguments because there
+ * there are multiple conflicting specifications for `cfree`.
+ *
+ * As a slibc extension, `errno` is guaranteed not to be set.
+ *
+ * @param ptr Pointer to the beginning of the memory allocation.
+ * The process may crash if it does not point to the
+ * beginning of a memory allocation on the heap.
+ * However, if it is `NULL`, nothing will happen.
+ *
+ * @since Always.
+ */
+void cfree(void* ptr, ...)
+{
+ fast_free(ptr);
+}
+
diff --git a/src/malloc/free.c b/src/malloc/free.c
new file mode 100644
index 0000000..3a0ba5a
--- /dev/null
+++ b/src/malloc/free.c
@@ -0,0 +1,41 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+#include <slibc-alloc.h>
+
+
+
+/**
+ * Free a memory allocation.
+ *
+ * As a slibc extension, `errno` is guaranteed not to be set.
+ *
+ * @etymology (Free) allocated memory.
+ *
+ * @param ptr Pointer to the beginning of the memory allocation.
+ * The process may crash if it does not point to the
+ * beginning of a memory allocation on the heap.
+ * However, if it is `NULL`, nothing will happen.
+ *
+ * @since Always.
+ */
+void free(void* ptr)
+{
+ fast_free(ptr);
+}
+
diff --git a/src/malloc/malloc_usable_size.c b/src/malloc/malloc_usable_size.c
new file mode 100644
index 0000000..d44f11e
--- /dev/null
+++ b/src/malloc/malloc_usable_size.c
@@ -0,0 +1,40 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+#include <slibc-alloc.h>
+
+
+
+/**
+ * This function returns the allocation size of
+ * a memory segment.
+ *
+ * `p = malloc(n), malloc_usable_size(p)` will return `n`.
+ *
+ * @etymology (`malloc`)-subsystem: user-(usable size) of allocation.
+ *
+ * @param segment The memory segment.
+ * @return The size of the memory segment, 0 if `segment` is `NULL`.
+ *
+ * @since Always.
+ */
+size_t malloc_usable_size(void* segment)
+{
+ return allocsize(segment);
+}
+
diff --git a/src/malloc/posix_memalign.c b/src/malloc/posix_memalign.c
new file mode 100644
index 0000000..80d27c8
--- /dev/null
+++ b/src/malloc/posix_memalign.c
@@ -0,0 +1,51 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+#include <errno.h>
+
+
+
+/**
+ * `posix_memalign(p, b, n)` is equivalent to
+ * `(*p = memalign(b, n), *p ? 0 : errno)`, except
+ * `boundary` must also be a multiple of `sizeof(void*)`,
+ * and `errno` is unspecified.
+ *
+ * As a GNU-compliant slibc extension, memory allocated
+ * with this function can be freed with `free`.
+ *
+ * @etymology (POSIX)-extension: (mem)ory alignment.
+ *
+ * @param ptrptr Output parameter for the allocated memory.
+ * @param boundary The alignment.
+ * @param size The number of bytes to allocated.
+ * @return Zero on success, a value for `errno` on error.
+ *
+ * @throws ENOMEM The process cannot allocate more memory.
+ * @throws EINVAL If `boundary` is not a power-of-two multiple of `sizeof(void*)`.
+ *
+ * @since Always.
+ */
+int posix_memalign(void** ptrptr, size_t boundary, size_t size)
+{
+ if (boundary < sizeof(void*))
+ return EINVAL;
+ *ptrptr = memalign(boundary, size);
+ return *ptrptr ? 0 : errno;
+}
+
diff --git a/src/malloc/realloc.c b/src/malloc/realloc.c
new file mode 100644
index 0000000..4356c2a
--- /dev/null
+++ b/src/malloc/realloc.c
@@ -0,0 +1,53 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+#include <slibc-alloc.h>
+
+
+
+/**
+ * Variant of `malloc` that extends, or shrinks, an existing allocation,
+ * if beneficial and possible, or creates a new allocation with the new
+ * size, copies the data, and frees the old allocation. The returned
+ * pointer has an alignment usable for any compiler-independent intrinsic
+ * data type, if a new pointer is returned.
+ *
+ * On error, `ptr` is not freed.
+ *
+ * @etymology Memory (realloc)ation.
+ *
+ * @param ptr Pointer to the beginning of the old memory allocation.
+ * The process may crash if it does not point to the
+ * beginning of a memory allocation on the heap.
+ * However, if it is `NULL`, this function will behave
+ * like `malloc`.
+ * @param size The new allocation size. If zero, this function will
+ * behave like `free`, and will return `NULL`.
+ * @return Pointer to the beginning of the new allocation.
+ * If `size` is zero, `NULL` is returned. On error `NULL`
+ * is returned and `errno` is set to indicate the error.
+ *
+ * @throws ENOMEM The process cannot allocate more memory.
+ *
+ * @since Always.
+ */
+void* realloc(void* ptr, size_t size)
+{
+ return fast_realloc(ptr, size);
+}
+