boa tarde, tenho um exercicio onde preciso implementar a função void *myMalloc(unsigned int bytesToAllocate) e a função int myFree(void *address). Tenho também que implementar void debugBlockList() para mostrar todo o conteúdo da lista de blocos.
Até agora fiz o que está no código em baixo, mas estou com RUNTIME ERROR em alguns testes de verificação do código noutros o output que me dá não é o correto...
Alguém me podia ajudar????
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#define BLOCK_SIZE 12 //sizeof(struct s_block)
#define align4(x) (((((x)-1)>>2)>>2))
typedef struct s_block *t_block;
struct s_block {
size_t size; // current size of block
t_block next; // pointer to next block
t_block prev;
int free; // flag indicating that the block is free or occupied; 0 or 1.
void *ptr;
char data[1];
};
t_block head = NULL; // points to the beginning of the list
t_block tail = NULL; // points to the last element of the list
t_block find_block(t_block *last, size_t size) {
t_block b = head;
while (b && !(b->free && b->size >= size)){
*last = b;
b = b->next;
}
return b;
}
t_block extend_heap(t_block last, size_t s) {
int sb;
t_block b;
b = sbrk (0);
sb = (int)sbrk( BLOCK_SIZE + s);
if (sb < 0) return (NULL );
b->size = s;
b->next = NULL;
b->prev = last;
b->ptr = b->data;
if (last) last ->next = b;
b->free = 0;
return (b);
}
/* Get the block from and addr */
t_block get_block (void *p) {
char *tmp;
tmp = p;
return (p = tmp -= BLOCK_SIZE );
}
/* Valid addr for free */
int valid_addr (void *p) {
if (head) {
if ((t_block)p>head && p<sbrk(0)) {
return (p == ( get_block(p))->ptr );
}
}
return (0);
}
void split_block(t_block b, size_t s){
t_block new;
new = (t_block )(b->data + s);
new ->size = b->size - s - BLOCK_SIZE ;
new ->next = b->next;
new ->prev = b;
new ->free = 1;
new -> ptr = new->data;
b->size = s;
b->next = new;
if(new->next) new->next->prev = new;
}
t_block fusion(t_block b){
if (b->next && b->next ->free ){
b->size += BLOCK_SIZE + b->next ->size;
b->next = b->next ->next;
if (b->next)
b->next ->prev = b;
}
return (b);
}
void debugBlockList() {
t_block p = head;
printf("current block list:\n");
while(p!=NULL){
printf("address: %lu - %u bytes (%s)\n", (unsigned long)(p), p->size, p->free?"free":"in use");
p=p->next;
}
}
void *myMalloc(size_t size) {
t_block b,last;
size_t s = align4(size);
if(head) {
last = head;
b = find_block(&last,s);
if(b) {
/* can we split */
if ((b->size - s) >= ( BLOCK_SIZE + 4))
split_block(b,s);
b->free =0;
} else {
/* No fitting block , extend the heap */
b = extend_heap(last ,s);
if (!b)
return(NULL );
}
} else {
/* first time */
b = extend_heap(NULL ,s);
if (!b)
return(NULL );
head = b;
}
return(b);
}
void myFree(void *ptr) {
t_block b;
if ( valid_addr (ptr)) {
b = get_block (ptr);
b->free = 1;
/* fusion with previous if possible */
if(b->prev && b->prev ->free)
b = fusion(b->prev );
/* then fusion with next */
if (b->next)
fusion(b);
else {
/* free the end of the heap */
if (b->prev)
b->prev ->next = NULL;
else
/* No more block !*/
head = NULL;
brk(b);
}
}
}