-
Notifications
You must be signed in to change notification settings - Fork 0
/
pspace.c
51 lines (44 loc) · 1.05 KB
/
pspace.c
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
#include "pspace.h"
#include <stdint.h>
#include <jemalloc/jemalloc.h>
#include <string.h>
field_t pspace_get(const pspace_t *p, uint32_t paddr) {
paddr = paddr % p->len;
return paddr == 0 ? p->lastresult : p->mem[paddr];
}
void pspace_set(pspace_t *p, uint32_t paddr, field_t val) {
paddr = paddr % p->len;
if (paddr == 0) {
p->lastresult = val;
} else {
p->mem[paddr] = val;
}
}
void pspace_clear(pspace_t *p) {
p->lastresult = 0;
memset(p->mem, 0, sizeof(field_t) * p->len);
}
void pspace_free(pspace_t *p) {
if (p) {
if (p->ownmem) {
free(p->ownmem);
}
free(p);
}
}
pspace_t *pspace_alloc(uint32_t pspacesize) {
pspace_t *p;
if ((p = (pspace_t *)calloc(1, sizeof(pspace_t)))) {
p->len = pspacesize;
if (!(p->ownmem = (field_t *)calloc(p->len, sizeof(field_t)))) {
pspace_free(p);
p = NULL;
}
p->mem = p->ownmem;
}
return p;
}
void pspace_share(const pspace_t *shared, pspace_t *sharer) {
sharer->mem = shared->mem;
}
void pspace_privatise(pspace_t *p) { p->mem = p->ownmem; }