odyssey/src/mm_context_stack.c

37 lines
737 B
C
Raw Normal View History

/*
* machinarium.
*
* cooperative multitasking engine.
*/
#include <machinarium.h>
#include <machinarium_private.h>
2017-05-16 14:31:01 +00:00
#ifdef HAVE_VALGRIND
# include <valgrind/valgrind.h>
#endif
int mm_contextstack_create(mm_contextstack_t *stack, size_t size)
{
stack->size = ((size * sizeof (void*) + 4096 - 1) / 4096) * 4096;
stack->pointer = malloc(stack->size);
if (stack->pointer == NULL)
return -1;
2017-05-16 14:31:01 +00:00
#ifdef HAVE_VALGRIND
stack->valgrind_stack =
VALGRIND_STACK_REGISTER(stack->pointer, stack->pointer + stack->size);
2017-05-16 14:31:01 +00:00
#endif
return 0;
}
void mm_contextstack_free(mm_contextstack_t *stack)
{
if (stack->pointer == NULL)
return;
2017-05-16 14:31:01 +00:00
#ifdef HAVE_VALGRIND
VALGRIND_STACK_DEREGISTER(stack->valgrind_stack);
#endif
free(stack->pointer);
}