aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/alloc.h3
-rw-r--r--src/alloc.c5
-rw-r--r--tests/liballoc_tests.c4
3 files changed, 11 insertions, 1 deletions
diff --git a/include/alloc.h b/include/alloc.h
index 6374b3a..594ef67 100644
--- a/include/alloc.h
+++ b/include/alloc.h
@@ -21,7 +21,8 @@
/*
* Returns a pointer to a new region of memory of the specified
- * size, or NULL if no memory is available.
+ * size, or NULL if no memory is available. Requests for 0 bytes of
+ * memory will also return NULL.
*/
void *allocate(size_t);
diff --git a/src/alloc.c b/src/alloc.c
index 39cbf88..32eb18a 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -44,6 +44,11 @@ void *allocate(size_t size) {
initialize();
}
+ /* special case for a size 0 allocation */
+ if (size == 0) {
+ return NULL;
+ }
+
/*
* pad the requested size to ensure alignment using
* the alignment one-liner we discussed in class
diff --git a/tests/liballoc_tests.c b/tests/liballoc_tests.c
index 16d50d6..a54998a 100644
--- a/tests/liballoc_tests.c
+++ b/tests/liballoc_tests.c
@@ -26,6 +26,10 @@ START_TEST(basic_allocate) {
size_t alignment = (size_t)memory % ALIGNMENT;
ck_assert_int_eq(alignment, 0);
+ /* allocating 0 size should return NULL */
+ void *memory2 = allocate(0);
+ ck_assert_ptr_eq(memory2, NULL);
+
/* leak the memory--we aren't testing release */
}
END_TEST