malloclab/mm_ts.c
CS3214 Class Account fab3f24f1c Last set of updates for Spring 2016
- added vary_size -s option
- switch to clock_gettime() based accounting
- tuned AVG_LIBC_THRUPUT
- introduced THREAD_SAFE flag to separate mt results
2016-04-10 00:14:52 -04:00

48 lines
1.2 KiB
C

/*
* Thread-safety wrapper.
* To be included in mm.c - you may assume this file exists, or else
* (better!) implement your own version.
*
* Generally, #including .c files is fragile and not good style.
* This is just a stop-gap solution.
*/
#include <pthread.h>
#ifdef THREAD_SAFE
static pthread_mutex_t malloc_lock = PTHREAD_MUTEX_INITIALIZER;
void *_mm_malloc_thread_unsafe(size_t size);
void _mm_free_thread_unsafe(void *bp);
void *_mm_realloc_thread_unsafe(void *ptr, size_t size);
void *mm_malloc(size_t size)
{
pthread_mutex_lock(&malloc_lock);
void * p = _mm_malloc_thread_unsafe(size);
pthread_mutex_unlock(&malloc_lock);
return p;
}
void mm_free(void *bp)
{
pthread_mutex_lock(&malloc_lock);
_mm_free_thread_unsafe(bp);
pthread_mutex_unlock(&malloc_lock);
}
void *mm_realloc(void *ptr, size_t size)
{
pthread_mutex_lock(&malloc_lock);
void * p = _mm_realloc_thread_unsafe(ptr, size);
pthread_mutex_unlock(&malloc_lock);
return p;
}
#define mm_malloc _mm_malloc_thread_unsafe
#define mm_free _mm_free_thread_unsafe
#define mm_realloc _mm_realloc_thread_unsafe
#else
/* If THREAD_SAFE is not defined, we leave it as is in order
* to avoid the locking overhead. */
#endif /* THREAD_SAFE */