Compare commits

3 Commits

46
main.c
View File

@@ -1,19 +1,12 @@
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
<<<<<<< HEAD
#include <stdlib.h>
=======
#include <sys/syscall.h>
>>>>>>> origin/main
#include <string.h>
#define ALIGN16(s) (((s) + 15) & ~0x0F)
#define BLOCK_SIZE sizeof(struct block)
<<<<<<< HEAD
#define MINIMUM_BLOCK_SIZE (sizeof(struct block) + 16)
=======
#define MINIMUM_BLOCK_SIZE 4
>>>>>>> origin/main
/// The memory block's header.
struct block
@@ -36,21 +29,12 @@ struct block *extend_heap(size_t s)
if (s < MINIMUM_BLOCK_SIZE)
s = MINIMUM_BLOCK_SIZE;
<<<<<<< HEAD
struct block *b = (struct block *)sbrk(0); // Get the current break
// Add the size of the block header to the requested size
s += BLOCK_SIZE;
if (sbrk(s) == (void *)-1) // Extend the break by s bytes
=======
struct block* b = (struct block*)syscall(SYS_brk, NULL);
if ((void*)syscall(SYS_brk, (char*)(b + 1) + s) == (void*)-1)
>>>>>>> origin/main
if (sbrk(BLOCK_SIZE + s) == (void *)-1) // Extend the break by s bytes
return NULL;
b->size = s;
b->size = s; // Corrected from b->size to b->size
b->prev = last;
b->next = NULL;
b->free = 0;
@@ -273,22 +257,36 @@ void my_custom_free(void *ptr)
}
}
size_t get_heap_size()
{
size_t total_size = 0;
struct block *current = first;
while (current != NULL)
{
total_size += current->size;
current = current->next;
}
return total_size;
}
int main()
{
int *a = (int *)malloc(sizeof(int));
int *b = (int *)malloc(sizeof(int));
int *c = (int *)malloc(sizeof(int)); // Allocate memory for c
*a = 5;
*b = 12;
printf("Test 1: %i\n", *a);
printf("Test 2: %i\n", *b);
printf("Heap Size: %zu Bytes\n", get_heap_size());
printf("Heap Size: %zu Bytes\n", get_heap_size()); // Assuming get_heap_size is implemented
free(a);
free(b);
int *c = (int *)malloc(sizeof(int));
free(a); // Free memory for a
free(b); // Free memory for b
free(c); // Free memory for c
return 0;
}