Add xmalloc(), xcalloc() functions.
[pintos-anon] / src / threads / malloc.c
index 8e6b4591ea87ecae09337ae5d741c89a68bdcb9a..9c83e3d438f780199166c3c92aa0893aa842f98f 100644 (file)
@@ -153,6 +153,19 @@ malloc (size_t size)
   return b;
 }
 
+/* Obtains and returns a new block of at least SIZE bytes.
+   Panics if memory is not available.  It is unacceptable for the
+   kernel to panic in normal operation, so this function should
+   only be used during kernel initialization. */
+void *
+xmalloc (size_t size) 
+{
+  void *p = malloc (size);
+  if (p == NULL && size > 0)
+    PANIC ("memory exhausted");
+  return p;
+}
+
 /* Allocates and return A times B bytes initialized to zeroes.
    Returns a null pointer if memory is not available. */
 void *
@@ -174,6 +187,19 @@ calloc (size_t a, size_t b)
   return p;
 }
 
+/* Allocates and return A times B bytes initialized to zeroes.
+   Panics if memory is not available.  It is unacceptable for the
+   kernel to panic in normal operation, so this function should
+   only be used during kernel initialization. */
+void *
+xcalloc (size_t a, size_t b) 
+{
+  void *p = calloc (a, b);
+  if (p == NULL && a > 0 && b > 0)
+    PANIC ("memory exhausted");
+  return p;
+}
+
 /* Frees block P, which must have been previously allocated with
    malloc() or calloc(). */
 void