1/*	subr_rmap.c.sav	3.1	10/14/12	*/
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/map.h"
6#include "../h/dir.h"
7#include "../h/user.h"
8#include "../h/proc.h"
9#include "../h/mtpr.h"
10#include "../h/text.h"
11
12/*
13 * Allocate 'size' units from the given
14 * map. Return the base of the allocated
15 * space.
16 * In a map, the addresses are increasing and the
17 * list is terminated by a 0 size.
18 * The swap map unit is 512 bytes.
19 * Algorithm is first-fit.
20 */
21malloc(mp, size)
22struct map *mp;
23{
24	register int a;
25	register struct map *bp;
26
27	if (size <= 0)
28		panic("malloc");
29	for (bp=mp; bp->m_size; bp++) {
30		if (bp->m_size >= size) {
31			a = bp->m_addr;
32			bp->m_addr += size;
33			if ((bp->m_size -= size) == 0) {
34				do {
35					bp++;
36					(bp-1)->m_addr = bp->m_addr;
37				} while ((bp-1)->m_size = bp->m_size);
38			}
39			if (mp == swapmap && a % CLSIZE)
40				panic("malloc swapmap");
41			return(a);
42		}
43	}
44	return(0);
45}
46
47/*
48 * Free the previously allocated space aa
49 * of size units into the specified map.
50 * Sort aa into map and combine on
51 * one or both ends if possible.
52 */
53mfree(mp, size, a)
54struct map *mp;
55register int size, a;
56{
57	register struct map *bp;
58	register int t;
59
60	if (a <= 0)
61		panic("mfree addr");
62	if (size <= 0)
63		panic("mfree size");
64	bp = mp;
65	for (; bp->m_addr<=a && bp->m_size!=0; bp++)
66		continue;
67	if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size > a)
68		panic("mfree begov");
69	if (a+size > bp->m_addr && bp->m_size)
70		panic("mfree endov");
71	if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) {
72		(bp-1)->m_size += size;
73		if (a+size == bp->m_addr) {
74			(bp-1)->m_size += bp->m_size;
75			while (bp->m_size) {
76				bp++;
77				(bp-1)->m_addr = bp->m_addr;
78				(bp-1)->m_size = bp->m_size;
79			}
80		}
81	} else {
82		if (a+size == bp->m_addr && bp->m_size) {
83			bp->m_addr -= size;
84			bp->m_size += size;
85		} else if (size) {
86			do {
87				t = bp->m_addr;
88				bp->m_addr = a;
89				a = t;
90				t = bp->m_size;
91				bp->m_size = size;
92				bp++;
93			} while (size = t);
94		}
95	}
96	if ((mp == kernelmap) && kmapwnt) {
97		kmapwnt = 0;
98		wakeup((caddr_t)kernelmap);
99	}
100}
101