summaryrefslogtreecommitdiffstats
path: root/runtime/user/alloc.c
blob: 361e7d984480122f8adef73612a7a6e7990efa39 (plain)
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
#ifndef _ALLOC_C_
#define _ALLOC_C_

/* emulated memory allocation functions */

void *malloc(size_t size);
void free(void *ptr);

enum errorcode { ERR_NONE=0, ERR_NO_MEM };
enum errorcode _stp_errorcode = ERR_NONE;

void *__kmalloc(size_t size, gfp_t flags)
{
  return malloc(size);
}

void *_stp_alloc_percpu(size_t size)
{
	int i;
	struct percpu_data *pdata = malloc(sizeof (*pdata));
	if (!pdata)
		return NULL;

	for_each_cpu(i) {
		pdata->ptrs[i] = malloc(size);
		if (!pdata->ptrs[i])
			goto unwind_oom;
		memset(pdata->ptrs[i], 0, size);
	}

	/* Catch derefs w/o wrappers */
	return (void *) (~(unsigned long) pdata);

unwind_oom:
	while (--i >= 0) {
		free(pdata->ptrs[i]);
	}
	free(pdata);
	return NULL;
}

void _stp_free_percpu(const void *objp)
{
	int i;
	struct percpu_data *p = (struct percpu_data *) (~(unsigned long) objp);

	for_each_cpu(i)
	  free(p->ptrs[i]);
	free(p);
}

#endif /* _ALLOC_C_ */