aboutsummaryrefslogtreecommitdiffstats
path: root/needstack.c
blob: 11d8ed4c006b256d826c72cd595249d6b3d7a0b7 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libsimple_needstack(size_t n)
{
	size_t limit1, limit2, limit, used;
	uintptr_t low, high, current;
	void *currentptr;

	currentptr = __builtin_frame_address(0);
	current = (uintptr_t)currentptr;
	if (libsimple_get_stack_space(&low, &high))
		return -1;
	if (libsimple_get_stack_limit(&limit1, NULL))
		return -1;
	limit2 = (size_t)(high - low);
	limit = limit1 < limit2 ? limit1 : limit2;

	switch (libsimple_get_stack_direction()) {
	case +1:
		if (current < low)
			goto enotsup;
		used = (size_t)(current - low);
		break;
	case -1:
		if (current > high)
			goto enotsup;
		used = (size_t)(high - current);
		break;
	default:
	enotsup:
		errno = ENOTSUP;
		return -1;
	}

	/* TODO it would be nice to be able to grow the stack */
	return used <= limit && n <= limit - used;
}


#else
#include "test.h"

int
main(void)
{
	return 0;
}

#endif