X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fthreads%2Fmalloc.c;h=4f8675cf90572164dbf715a35cfe33fc876e1f24;hb=2f2b3a9b0e72eb25010d1fa757687fd8a87658ef;hp=20773548d9ee2499b68a22e89e3b222694ef985c;hpb=e49798d967361536af9339202a3306fe1347d93f;p=pintos-anon diff --git a/src/threads/malloc.c b/src/threads/malloc.c index 2077354..4f8675c 100644 --- a/src/threads/malloc.c +++ b/src/threads/malloc.c @@ -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 * +malloc_or_panic (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 * @@ -161,7 +174,7 @@ calloc (size_t a, size_t b) void *p; size_t size; - /* Calculate block size. */ + /* Calculate block size and make sure it fits in size_t. */ size = a * b; if (size < a || size < b) return NULL; @@ -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 * +calloc_or_panic (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 @@ -198,7 +224,7 @@ free (void *p) } #ifndef NDEBUG - memset (b, 0xcd, d->block_size); + memset (b, 0xcc, d->block_size); #endif lock_acquire (&d->lock);