sys/malloc.c

02800 #include "../h/param.h" 02801 #include "../h/systm.h" 02802 #include "../h/map.h" 02803 02804 /* 02805 * Allocate 'size' units from the given 02806 * map. Return the base of the allocated 02807 * space. 02808 * In a map, the addresses are increasing and the 02809 * list is terminated by a 0 size. 02810 * The core map unit is 64 bytes; the swap map unit 02811 * is 512 bytes. 02812 * Algorithm is first-fit. 02813 */ 02814 malloc(mp, size) 02815 struct map *mp; 02816 { 02817 register unsigned int a; 02818 register struct map *bp; 02819 02820 for (bp=mp; bp->m_size; bp++) { 02821 if (bp->m_size >= size) { 02822 a = bp->m_addr; 02823 bp->m_addr += size; 02824 if ((bp->m_size -= size) == 0) { 02825 do { 02826 bp++; 02827 (bp-1)->m_addr = bp->m_addr; 02828 } while ((bp-1)->m_size = bp->m_size); 02829 } 02830 return(a); 02831 } 02832 } 02833 return(0); 02834 } 02835 02836 /* 02837 * Free the previously allocated space aa 02838 * of size units into the specified map. 02839 * Sort aa into map and combine on 02840 * one or both ends if possible. 02841 */ 02842 mfree(mp, size, a) 02843 struct map *mp; 02844 register int a; 02845 { 02846 register struct map *bp; 02847 register unsigned int t; 02848 02849 if ((bp = mp)==coremap && runin) { 02850 runin = 0; 02851 wakeup((caddr_t)&runin); /* Wake scheduler when freeing core */ 02852 } 02853 for (; bp->m_addr<=a && bp->m_size!=0; bp++); 02854 if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) { 02855 (bp-1)->m_size += size; 02856 if (a+size == bp->m_addr) { 02857 (bp-1)->m_size += bp->m_size; 02858 while (bp->m_size) { 02859 bp++; 02860 (bp-1)->m_addr = bp->m_addr; 02861 (bp-1)->m_size = bp->m_size; 02862 } 02863 } 02864 } else { 02865 if (a+size == bp->m_addr && bp->m_size) { 02866 bp->m_addr -= size; 02867 bp->m_size += size; 02868 } else if (size) { 02869 do { 02870 t = bp->m_addr; 02871 bp->m_addr = a; 02872 a = t; 02873 t = bp->m_size; 02874 bp->m_size = size; 02875 bp++; 02876 } while (size = t); 02877 } 02878 } 02879 } 02880 02881 02882 02883 02884 02885 02886 02887 02888 02889 02890 02891 02892 02893 02894 02895 02896 02897 02898 02899