Skip to content
Snippets Groups Projects
Commit 420f6626 authored by PEDRO MARTINS E SA's avatar PEDRO MARTINS E SA
Browse files

Atualizacao: erro da inicializacao

parent f921af27
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -50,6 +50,8 @@ void aloca_espaco(void tam_alloc) ...@@ -50,6 +50,8 @@ void aloca_espaco(void tam_alloc)
void* alocaMem(int tam_alloc) void* alocaMem(int tam_alloc)
{ {
//se a heap n esta inicializada
if(piso_heap == 0) if(piso_heap == 0)
iniciaAlocador(); iniciaAlocador();
...@@ -58,7 +60,7 @@ void* alocaMem(int tam_alloc) ...@@ -58,7 +60,7 @@ void* alocaMem(int tam_alloc)
while (endr <= teto_heap) while (endr <= teto_heap)
{ {
//endereco livre //endereco livre
if((endr == LIVRE)) if((*endr == LIVRE))
{ {
if (8(endr) >= tam_alloc) if (8(endr) >= tam_alloc)
{ {
...@@ -72,7 +74,7 @@ void* alocaMem(int tam_alloc) ...@@ -72,7 +74,7 @@ void* alocaMem(int tam_alloc)
{ {
endr = teto_heap; endr = teto_heap;
aloca_espaco(tam_alloc); aloca_espaco(tam_alloc);
(endr) = 0; (endr) = OCUPADO;
8(endr) = tam_alloc; 8(endr) = tam_alloc;
} }
break; break;
...@@ -106,7 +108,7 @@ void arruma_heap() ...@@ -106,7 +108,7 @@ void arruma_heap()
void liberaMem(*void bloco) void liberaMem(*void bloco)
{ {
//bloco livre //bloco livre
-16(bloco) = 1; -16(bloco) = livre;
arruma_heap(); arruma_heap();
return; return;
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
str2: .string "#" str2: .string "#"
str3: .string "-" str3: .string "-"
str4: .string "*" str4: .string "*"
str5: .string "INICIANDO O ALOCADOR\n" str5: .string "INICIANDO O ALOCADOR!\n"
str6: .string "ENTRADA INVALIDA!\n"
pula_Linha: .string "\n" pula_Linha: .string "\n"
flag: .string "%d" flag: .string "%d" #############APAGARRRRRRRR##########
flaag: .string "%d, %d \n" #############APAGARRRRRRRR########## flaag: .string "%d, %d \n" #############APAGARRRRRRRR##########
oi: .string "oi\n" #############APAGARRRRRRRR########## oi: .string "oi\n" #############APAGARRRRRRRR##########
...@@ -47,9 +48,9 @@ iniciaAlocador: ...@@ -47,9 +48,9 @@ iniciaAlocador:
movq piso_heap, %rax movq piso_heap, %rax
movq $LIVRE, (%rax) #deixa livre o primeiro slot de memoria movq $LIVRE, (%rax) #deixa livre o primeiro slot de memoria
movq $ALOCADOR, %r12 movq $ALOCADOR, %r13
subq $16, %r12 subq $16, %r13
movq %r12, 8(%rax) #disponibiliza o tamanho do espaco alocado movq %r13, 8(%rax) #disponibiliza o tamanho do espaco alocado
popq %rbp popq %rbp
ret ret
...@@ -93,11 +94,19 @@ alocaMem: ...@@ -93,11 +94,19 @@ alocaMem:
subq $8, %rsp #aloca espaco para endr subq $8, %rsp #aloca espaco para endr
movq %rdi, %r12 #r12 = ponteiro alocado movq %rdi, %r12 #r12 = ponteiro alocado
movq piso_heap, %rbx cmpq $0, %r12
cmpq $0, %rbx jne fim_pegadinha_if #quando tam_alloc == 0 return
addq $8, %rsp
popq %rbp
ret
fim_pegadinha_if:
movq piso_heap, %rax
cmpq $0, %rax
jne fim_if_aloca_mem jne fim_if_aloca_mem
call iniciaAlocador call iniciaAlocador
fim_if_aloca_mem: fim_if_aloca_mem:
movq piso_heap, %rbx
movq %rbx,-8(%rbp) movq %rbx,-8(%rbp)
movq -8(%rbp), %rbx # endr = -8(rbp) movq -8(%rbp), %rbx # endr = -8(rbp)
......
#include <meu_malloc.h> #include <meu_malloc.h>
#include <stdio.h> #include <stdio.h>
int main() int main () {
{ void *a,*b,*c,*d;
void *w, *x, *y, *z;
iniciaAlocador(); a=( void * ) alocaMem(1000);
print_mapa(); print_mapa();
printf("\n"); printf("1\n");
w = alocaMem(100); b=( void * ) alocaMem(200);
print_mapa(); print_mapa();
printf("\n"); printf("2\n");
x = alocaMem(200); c=( void * ) alocaMem(300);
print_mapa(); print_mapa();
printf("\n"); printf("3\n");
y = alocaMem(300); d=( void * ) alocaMem(400);
print_mapa(); print_mapa();
printf("\n"); printf("4\n");
z = alocaMem(400); liberaMem(b);
print_mapa(); print_mapa();
printf("\n"); printf("5\n");
liberaMem(x); b=( void * ) alocaMem(50);
print_mapa(); print_mapa();
printf("6\n");
x = alocaMem(50); liberaMem(c);
print_mapa(); print_mapa();
printf("\n"); printf("7\n");
liberaMem(a);
printf("\n");
liberaMem(y);
print_mapa();
printf("\n");
liberaMem(w);
print_mapa(); print_mapa();
printf("\n"); printf("8\n");
liberaMem(x); liberaMem(b);
print_mapa(); print_mapa();
printf("\n"); printf("9\n");
liberaMem(z); liberaMem(d);
print_mapa(); print_mapa();
printf("\n"); printf("10\n");
return (0); return (0);
} }
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment