00001 00006 #ifndef __SYSTEM 00007 #include "routix/system.h" 00008 #endif 00009 #include <routix/kstdio.h> 00010 #include "routix/paging.h" 00011 #include "routix/kalloc.h" 00012 #include "routix/debug.h" 00013 00015 volatile int num_mallocs = 0; 00017 volatile int num_frees = 0; 00018 00019 unsigned int umalloc_alloc = 0, umalloc_free = 0; 00020 unsigned int file_alloc = 0, file_free = 0; 00021 00022 void free (void *); 00023 00025 addr_t *_inicio,*_fin,*_sp; 00026 00027 /* Funciones relacionadas con malloc */ 00028 00029 typedef long Align; 00030 00031 typedef union header { 00032 struct { 00033 union header *ptr; 00034 unsigned size; 00035 } s; 00036 Align x; 00037 } Header; 00038 00039 //typedef union header Header; 00040 00041 static Header base; 00042 static Header *freep=NULL; 00043 00044 Header *morecore(void); 00045 00046 void *malloc (unsigned nbytes) 00047 { 00048 cli(); 00049 Header *p, *prevp; 00050 unsigned nunits; 00051 nunits = (nbytes + sizeof(Header)-1) / sizeof(Header) +1; 00052 if ((prevp = freep)==NULL) { 00053 base.s.ptr = freep = prevp = &base; 00054 base.s.size = 0; 00055 } 00056 00057 for ( p=prevp->s.ptr ; ; prevp=p , p=p->s.ptr) { 00058 if (p->s.size >= nunits) { 00059 if (p->s.size == nunits) 00060 prevp->s.ptr = p->s.ptr; 00061 else { 00062 p->s.size -= nunits; 00063 p += p->s.size; 00064 p->s.size = nunits; 00065 } 00066 freep = prevp; 00067 /*TEMP*/ num_mallocs++; 00068 sti(); 00069 return (p+1); 00070 } 00071 else if (p==freep) { 00072 if ( (p = morecore()) == NULL ) { 00073 sti(); 00074 return NULL; 00075 } 00076 } 00077 } 00078 sti(); 00079 return NULL; 00080 } 00081 00082 00083 00084 volatile word morecores=0; 00085 00086 Header *morecore(void) 00087 { 00088 Header *up; 00089 00090 up = (Header *) kmalloc_page(); 00091 if (up == NULL) { 00092 return NULL; 00093 } 00094 00095 morecores++; 00096 00097 up->s.size= PAGINA_SIZE / sizeof(Header); 00098 free ( (void *) (up+1)); 00099 return freep; 00100 } 00101 00102 00103 00104 void free (void *ap) 00105 { 00106 cli(); 00107 Header *bp, *p; 00108 00109 bp = (Header *) ap - 1; 00110 for (p=freep; ! (bp > p && bp < p->s.ptr) ; p=p->s.ptr) 00111 if ( (p >= p->s.ptr) && (bp > p || bp < p->s.ptr)) 00112 break; 00113 if ( (bp + bp->s.size) == p->s.ptr) { 00114 bp->s.size += p->s.ptr->s.size; 00115 bp->s.ptr = p->s.ptr->s.ptr; 00116 } 00117 else bp->s.ptr = p->s.ptr; 00118 00119 if ( (p+p->s.size) == bp) { 00120 p->s.size += bp->s.size; 00121 p->s.ptr = bp->s.ptr; 00122 } 00123 else p->s.ptr = bp; 00124 00125 freep = p; 00126 00127 num_frees++; 00128 sti(); 00129 } 00130 00131 00132 00133