~kris/9p

9hist

ref: a8e3f548086e4e1b75a0b3ca75b7fe58aa06a334 9hist/pc/bbmalloc.c -rw-r--r-- 1.5 KiB
a8e3f548 — David du Colombier Plan 9 from Bell Labs 1994-01-26 32 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"

#define LINEWORDS	(0)

/*
 * Allocate memory for use in kernel bitblts.
 * The allocated memory must have a flushed instruction
 * cache, and the data cache must be flushed by bbdflush().
 * To avoid the need for frequent cache flushes, the memory
 * is allocated out of an arena, and the i-cache is only
 * flushed when it has to be reused
 *
 * This code will have to be interlocked if we ever get
 * a multiprocessor with a bitmapped display.
 */

/* a 0->3 bitblt can take 900 words */
enum {
	narena=2,	/* put interrupt time stuff in separate arena */
	nbbarena=4096	/* number of words in an arena */
};

static ulong	bbarena[narena][nbbarena+LINEWORDS];
static ulong	*bbcur[narena] = {&bbarena[0][0], &bbarena[1][0]};
static ulong	*bblast[narena] = {0, 0};

#define INTLEVEL(v)	(((v)&(1<<9)) == 0)
void *
bbmalloc(int nbytes)
{
	int nw, a;
	int s;
	ulong *ans;

	nw = nbytes/sizeof(long);
	s = splhi();
	a = INTLEVEL(s)? 1 : 0;
	if(bbcur[a] + nw > &bbarena[a][nbbarena])
		ans = bbarena[a];
	else
		ans = bbcur[a];
	bbcur[a] = ans + nw + (LINEWORDS);
	splx(s);

	bblast[a] = ans;
	return ans;
}

void
bbfree(void *p, int n)
{
	int a, s;

	s = splhi();
	a = INTLEVEL(s)? 1 : 0;
	if(p == bblast[a])
		bbcur[a] = (ulong *)(((char *)bblast[a]) + n + LINEWORDS*sizeof(long));
	splx(s);
}

int
bbonstack(void)
{
	/*if(u)
		return 1;*/
	return 0;
}

void
bbexec(void (*memstart)(void), int len, int onstack)
{
	memstart();
	if(onstack)
		return;
	bbfree(memstart, len);
}