brk(2) and stack pointer

Shaun Jackman sjackman@gmail.com
Fri Apr 28 16:11:00 GMT 2006


Hello,

I'm implementing ARM Linux support for the newlib libc. When
implementing brk(2), is it reasonable to check that the requested
end_data_segment is below the stack pointer, or should the brk(2)
implementation allow for a stack that's been allocated below the heap
in memory?

Thanks,
Shaun

extern char _end[];
static void *curbrk = _end;

extern void *_brk(void *addr);

int brk(void *addr)
{
	void *newbrk;
	register void *stack_ptr asm ("sp");
	if (addr > stack_ptr) {
		errno = ENOMEM;
		return -1;
	}
	if (curbrk == addr)
		return 0;
	newbrk = _brk(addr);
	curbrk = newbrk;
	if (newbrk < addr) {
		errno = ENOMEM;
		return -1;
	}
	return 0;
}


More information about the Newlib mailing list