stuff
This commit is contained in:
53
06-MemoryManager/testcases/pthread_testmemmgr.c
Normal file
53
06-MemoryManager/testcases/pthread_testmemmgr.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 4096
|
||||
#define ALLOC_SIZE 1024
|
||||
#define THREAD_COUNT 10
|
||||
|
||||
void* thread_routine(void* args)
|
||||
{
|
||||
for(int i = 0; i < 25; i++) {
|
||||
char* myptr = mymalloc_ff(ALLOC_SIZE);
|
||||
usleep(20);
|
||||
if(myptr != NULL) {
|
||||
printf("Thread %lu allocated %d byte(s): %p\n", pthread_self(), ALLOC_SIZE, myptr);
|
||||
*myptr = 'a' + i;
|
||||
printf("Thread %lu write %c\n", pthread_self(), *myptr);
|
||||
myfree(myptr);
|
||||
printf("Thread %lu freed %p\n", pthread_self(), myptr);
|
||||
} else {
|
||||
printf("Thread %lu could not allocate %d byte(s)\n", pthread_self(), ALLOC_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pthread_t mythreads[THREAD_COUNT];
|
||||
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
for(int i = 0; i < THREAD_COUNT; i++) {
|
||||
if(pthread_create(&mythreads[i], NULL, thread_routine, NULL) == -1) {
|
||||
printf("COULD NOT CREATE A THREAD\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < THREAD_COUNT; i++) {
|
||||
pthread_join(mythreads[i], NULL);
|
||||
}
|
||||
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
06-MemoryManager/testcases/tc0.c
Normal file
23
06-MemoryManager/testcases/tc0.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("TC0: init and destroy\n");
|
||||
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
35
06-MemoryManager/testcases/tc1.c
Normal file
35
06-MemoryManager/testcases/tc1.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC1: Single allocate with mymalloc_ff\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
// Allocate 10 bytes
|
||||
// shouldn't fail
|
||||
char* ptr1 = mymalloc_ff(10);
|
||||
if(ptr1 == NULL) {
|
||||
printf("ptr1 - mymalloc_ff(10) failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strncpy(ptr1, "HELLO", 10);
|
||||
printf("ptr1 is %s\n", ptr1);
|
||||
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
myfree(ptr1);
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
47
06-MemoryManager/testcases/tc10.c
Normal file
47
06-MemoryManager/testcases/tc10.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 4096
|
||||
#define MUL_COUNT 100
|
||||
|
||||
int* heap_multiply(int a, int b)
|
||||
{
|
||||
int* result = mymalloc_ff(sizeof(int));
|
||||
if(result == NULL) {
|
||||
printf("COULD NOT mymalloc_ff an int\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*result = a * b;
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int* results[MUL_COUNT];
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC10: allocate multiple integers and perform multiplications\n");
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
printf("%d -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", i, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
results[i] = heap_multiply(i, i + 2);
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
printf("%d * %d = %d\n", i, i+2, *results[i]);
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
myfree(results[i]);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
72
06-MemoryManager/testcases/tc11.c
Normal file
72
06-MemoryManager/testcases/tc11.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 4096
|
||||
#define MUL_COUNT 100
|
||||
|
||||
int* results[MUL_COUNT];
|
||||
int ids[MUL_COUNT];
|
||||
|
||||
int* heap_multiply(int a, int b)
|
||||
{
|
||||
int* result = mymalloc_ff(sizeof(int));
|
||||
if(result == NULL) {
|
||||
printf("COULD NOT mymalloc_ff an int\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*result = a * b;
|
||||
return result;
|
||||
}
|
||||
|
||||
void* thread_function(void* args)
|
||||
{
|
||||
int* localId = mymalloc_ff(sizeof(int));
|
||||
if(localId == NULL) {
|
||||
printf("%lu could not allocate space for local ID\n", pthread_self());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*localId = *(int*)args;
|
||||
printf("%d -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", *localId, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
results[*localId] = heap_multiply(*localId, *localId + 2);
|
||||
myfree(localId);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pthread_t mythreads[MUL_COUNT];
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC11: pthreads allocate multiple integers and perform multiplications\n");
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
ids[i] = i;
|
||||
if(pthread_create(&mythreads[i], NULL, thread_function, (void*)&ids[i]) == -1) {
|
||||
printf("Could not create thread %d\n", i);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
pthread_join(mythreads[i], NULL);
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
printf("%d * %d = %d\n", i, i+2, *results[i]);
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
myfree(results[i]);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
72
06-MemoryManager/testcases/tc12.c
Normal file
72
06-MemoryManager/testcases/tc12.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 4096
|
||||
#define MUL_COUNT 1
|
||||
|
||||
int* results[MUL_COUNT];
|
||||
int ids[MUL_COUNT];
|
||||
|
||||
int* heap_multiply(int a, int b)
|
||||
{
|
||||
int* result = mymalloc_ff(sizeof(int));
|
||||
if(result == NULL) {
|
||||
printf("COULD NOT mymalloc_ff an int\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*result = a * b;
|
||||
return result;
|
||||
}
|
||||
|
||||
void* thread_function(void* args)
|
||||
{
|
||||
int* localId = mymalloc_ff(sizeof(int));
|
||||
if(localId == NULL) {
|
||||
printf("%lu could not allocate space for local ID\n", pthread_self());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*localId = *(int*)args;
|
||||
printf("%d -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", *localId, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
results[*localId] = heap_multiply(*localId, *localId + 2);
|
||||
myfree(localId);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pthread_t mythreads[MUL_COUNT];
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC12: pthreads allocate single integer and perform multiplication\n");
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
ids[i] = i;
|
||||
if(pthread_create(&mythreads[i], NULL, thread_function, (void*)&ids[i]) == -1) {
|
||||
printf("Could not create thread %d\n", i);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
pthread_join(mythreads[i], NULL);
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
printf("%d * %d = %d\n", i, i+2, *results[i]);
|
||||
}
|
||||
|
||||
for(int i = 0; i < MUL_COUNT; i++) {
|
||||
myfree(results[i]);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
35
06-MemoryManager/testcases/tc2.c
Normal file
35
06-MemoryManager/testcases/tc2.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC2: Single allocate with mymalloc_bf\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
// Allocate 10 bytes
|
||||
// shouldn't fail
|
||||
char* ptr1 = mymalloc_bf(10);
|
||||
if(ptr1 == NULL) {
|
||||
printf("ptr1 - mymalloc_ff(10) failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strncpy(ptr1, "HELLO", 10);
|
||||
printf("ptr1 is %s\n", ptr1);
|
||||
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
myfree(ptr1);
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
35
06-MemoryManager/testcases/tc3.c
Normal file
35
06-MemoryManager/testcases/tc3.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC3: Single allocate with mymalloc_wf\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
// Allocate 10 bytes
|
||||
// shouldn't fail
|
||||
char* ptr1 = mymalloc_wf(10);
|
||||
if(ptr1 == NULL) {
|
||||
printf("ptr1 - mymalloc_ff(10) failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strncpy(ptr1, "HELLO", 10);
|
||||
printf("ptr1 is %s\n", ptr1);
|
||||
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
myfree(ptr1);
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
32
06-MemoryManager/testcases/tc4.c
Normal file
32
06-MemoryManager/testcases/tc4.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC4: Large allocation should fail\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
// Allocate 10 bytes
|
||||
// shouldn't fail
|
||||
char* ptr1 = mymalloc_ff(200);
|
||||
if(ptr1 != NULL) {
|
||||
printf("ptr1 - mymalloc_ff(10) succeeded\n");
|
||||
myfree(ptr1);
|
||||
}
|
||||
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
06-MemoryManager/testcases/tc5.c
Normal file
41
06-MemoryManager/testcases/tc5.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 4096
|
||||
#define ALLOC_COUNT 100
|
||||
#define ALLOC_SIZE 10
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
void* ptr[ALLOC_COUNT];
|
||||
|
||||
printf("TC5: Heap size: %d: allocation count: %d, allocation size: %d - Lots of small allocationsl\n", MY_HEAP_SIZE, ALLOC_COUNT, ALLOC_SIZE);
|
||||
|
||||
for(int i = 0 ; i < ALLOC_COUNT; i++) {
|
||||
|
||||
printf("%d - 1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", i, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
ptr[i] = mymalloc_ff(ALLOC_SIZE);
|
||||
if(ptr[i] == NULL) {
|
||||
printf("ptr[%d] - mymalloc_ff(%d) failed\n", i, ALLOC_SIZE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("%d 2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", i, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
}
|
||||
for(int i = 0; i < ALLOC_COUNT; i++) {
|
||||
printf("Freeing ptr[%d]\n", i);
|
||||
myfree(ptr[i]);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
06-MemoryManager/testcases/tc6.c
Normal file
45
06-MemoryManager/testcases/tc6.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 4096
|
||||
#define ALLOC_COUNT 100
|
||||
#define ALLOC_SIZE 10
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
void* ptr[ALLOC_COUNT];
|
||||
|
||||
printf("TC6: Heap size: %d: allocation count: %d, allocation size: %d - Lots of small allocations with interleaved frees\n", MY_HEAP_SIZE, ALLOC_COUNT, ALLOC_SIZE);
|
||||
|
||||
for(int i = 0 ; i < ALLOC_COUNT; i++) {
|
||||
|
||||
printf("%d - 1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", i, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
ptr[i] = mymalloc_ff(ALLOC_SIZE);
|
||||
if(ptr[i] == NULL) {
|
||||
printf("ptr[%d] - mymalloc_ff(%d) failed\n", i, ALLOC_SIZE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("%d 2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", i, get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
}
|
||||
for(int i = 0; i < ALLOC_COUNT; i += 2) {
|
||||
printf("Freeing ptr[%d], Fragment Count: %d\n", i, get_fragment_count());
|
||||
myfree(ptr[i]);
|
||||
}
|
||||
for(int i = 1; i < ALLOC_COUNT; i += 2) {
|
||||
myfree(ptr[i]);
|
||||
printf("Freeing ptr[%d], Fragment Count: %d\n", i, get_fragment_count());
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
06-MemoryManager/testcases/tc7.c
Normal file
62
06-MemoryManager/testcases/tc7.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC7: Test interleaved allocations FF\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
void* ptr1 = mymalloc_ff(20);
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr2 = mymalloc_ff(10);
|
||||
printf("3 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr3 = mymalloc_ff(10);
|
||||
printf("4 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
if(ptr1 == NULL) {
|
||||
printf("Unable to allocate ptr1\n");
|
||||
} else {
|
||||
myfree(ptr1);
|
||||
}
|
||||
if(ptr3 == NULL) {
|
||||
printf("Unable to allocate ptr3\n");
|
||||
} else {
|
||||
myfree(ptr3);
|
||||
}
|
||||
printf("5 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
void* ptr4 = mymalloc_ff(10);
|
||||
printf("6 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr5 = mymalloc_ff(20);
|
||||
printf("7 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
if(ptr4 == NULL) {
|
||||
printf("Unable to allocate ptr1\n");
|
||||
} else {
|
||||
myfree(ptr4);
|
||||
}
|
||||
|
||||
if(ptr2 == NULL) {
|
||||
printf("Unable to allocate ptr2\n");
|
||||
} else {
|
||||
myfree(ptr2);
|
||||
}
|
||||
if(ptr5 == NULL) {
|
||||
printf("Unable to allocate ptr5\n");
|
||||
} else {
|
||||
myfree(ptr5);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
06-MemoryManager/testcases/tc8.c
Normal file
62
06-MemoryManager/testcases/tc8.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC8: Test interleaved allocations BF\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
void* ptr1 = mymalloc_bf(20);
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr2 = mymalloc_bf(10);
|
||||
printf("3 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr3 = mymalloc_bf(10);
|
||||
printf("4 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
if(ptr1 == NULL) {
|
||||
printf("Unable to allocate ptr1\n");
|
||||
} else {
|
||||
myfree(ptr1);
|
||||
}
|
||||
if(ptr3 == NULL) {
|
||||
printf("Unable to allocate ptr3\n");
|
||||
} else {
|
||||
myfree(ptr3);
|
||||
}
|
||||
printf("5 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
void* ptr4 = mymalloc_bf(10);
|
||||
printf("6 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr5 = mymalloc_bf(20);
|
||||
printf("7 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
if(ptr4 == NULL) {
|
||||
printf("Unable to allocate ptr1\n");
|
||||
} else {
|
||||
myfree(ptr4);
|
||||
}
|
||||
|
||||
if(ptr2 == NULL) {
|
||||
printf("Unable to allocate ptr2\n");
|
||||
} else {
|
||||
myfree(ptr2);
|
||||
}
|
||||
if(ptr5 == NULL) {
|
||||
printf("Unable to allocate ptr5\n");
|
||||
} else {
|
||||
myfree(ptr5);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
06-MemoryManager/testcases/tc9.c
Normal file
62
06-MemoryManager/testcases/tc9.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("TC9: Test interleaved allocations WF\n");
|
||||
printf("1 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
void* ptr1 = mymalloc_wf(20);
|
||||
printf("2 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr2 = mymalloc_wf(10);
|
||||
printf("3 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr3 = mymalloc_wf(10);
|
||||
printf("4 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
if(ptr1 == NULL) {
|
||||
printf("Unable to allocate ptr1\n");
|
||||
} else {
|
||||
myfree(ptr1);
|
||||
}
|
||||
if(ptr3 == NULL) {
|
||||
printf("Unable to allocate ptr3\n");
|
||||
} else {
|
||||
myfree(ptr3);
|
||||
}
|
||||
printf("5 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
|
||||
void* ptr4 = mymalloc_wf(10);
|
||||
printf("6 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
void* ptr5 = mymalloc_wf(20);
|
||||
printf("7 -- Available Memory: %d, Alloc Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_allocated_space(), get_fragment_count());
|
||||
if(ptr4 == NULL) {
|
||||
printf("Unable to allocate ptr1\n");
|
||||
} else {
|
||||
myfree(ptr4);
|
||||
}
|
||||
|
||||
if(ptr2 == NULL) {
|
||||
printf("Unable to allocate ptr2\n");
|
||||
} else {
|
||||
myfree(ptr2);
|
||||
}
|
||||
if(ptr5 == NULL) {
|
||||
printf("Unable to allocate ptr5\n");
|
||||
} else {
|
||||
myfree(ptr5);
|
||||
}
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
06-MemoryManager/testcases/testmemmgr.c
Normal file
62
06-MemoryManager/testcases/testmemmgr.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "memory_manager.h"
|
||||
|
||||
#define MY_HEAP_SIZE 100
|
||||
|
||||
int main()
|
||||
{
|
||||
char my_heap[MY_HEAP_SIZE];
|
||||
mmInit(my_heap, MY_HEAP_SIZE);
|
||||
|
||||
printf("1 -- Available Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_fragment_count());
|
||||
|
||||
// Allocate 10 bytes
|
||||
// shouldn't fail
|
||||
char* ptr1 = mymalloc_ff(10);
|
||||
if(ptr1 == NULL) {
|
||||
printf("ptr1 - mymalloc_ff(10) failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strncpy(ptr1, "HELLO", 10);
|
||||
printf("ptr1 is %s\n", ptr1);
|
||||
|
||||
printf("2 -- Available Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_fragment_count());
|
||||
|
||||
// Allocate 45 bytes
|
||||
// shouldn't fail
|
||||
char* ptr2 = mymalloc_wf(45);
|
||||
if(ptr2 == NULL) {
|
||||
printf("ptr2 - mymalloc_ff(45) failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strncpy(ptr2, "GOODBYE", 45);
|
||||
printf("ptr2 is %s\n", ptr2);
|
||||
|
||||
printf("3 -- Available Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_fragment_count());
|
||||
|
||||
// Attempt to allocate 50 bytes
|
||||
// should fail
|
||||
char* ptr3 = mymalloc_bf(50);
|
||||
if(ptr3 == NULL) {
|
||||
printf("ptr3 - mymalloc_bf(50) failed\n");
|
||||
}
|
||||
printf("4 -- Available Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_fragment_count());
|
||||
|
||||
// Free the first two pointers
|
||||
myfree(ptr1);
|
||||
myfree(ptr2);
|
||||
printf("5 -- Available Memory: %d, Fragment Count: %d\n", get_remaining_space(), get_fragment_count());
|
||||
|
||||
printf("Total successful mallocs: %d\n", get_mymalloc_count());
|
||||
|
||||
// Double free, should cause a segmentation fault
|
||||
//myfree(ptr2);
|
||||
|
||||
mmDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user