+/* Tell the split stack code whether it has to block signals while
+ manipulating the stack. This is for programs in which some threads
+ block all signals. If a thread already blocks signals, there is no
+ need for the split stack code to block them as well. If NEW is not
+ NULL, then if *NEW is non-zero signals will be blocked while
+ splitting the stack, otherwise they will not. If OLD is not NULL,
+ *OLD will be set to the old value. */
+
+void
+__splitstack_block_signals (int *new, int *old)
+{
+ if (old != NULL)
+ *old = __morestack_initial_sp.dont_block_signals ? 0 : 1;
+ if (new != NULL)
+ __morestack_initial_sp.dont_block_signals = *new ? 0 : 1;
+}
+
+/* The offsets into the arrays used by __splitstack_getcontext and
+ __splitstack_setcontext. */
+
+enum __splitstack_context_offsets
+{
+ MORESTACK_SEGMENTS = 0,
+ CURRENT_SEGMENT = 1,
+ CURRENT_STACK = 2,
+ STACK_GUARD = 3,
+ INITIAL_SP = 4,
+ INITIAL_SP_LEN = 5,
+ BLOCK_SIGNALS = 6,
+
+ NUMBER_OFFSETS = 10
+};
+
+/* Get the current split stack context. This may be used for
+ coroutine switching, similar to getcontext. The argument should
+ have at least 10 void *pointers for extensibility, although we
+ don't currently use all of them. This would normally be called
+ immediately before a call to getcontext or swapcontext or
+ setjmp. */
+
+void
+__splitstack_getcontext (void *context[NUMBER_OFFSETS])
+{
+ memset (context, 0, NUMBER_OFFSETS * sizeof (void *));
+ context[MORESTACK_SEGMENTS] = (void *) __morestack_segments;
+ context[CURRENT_SEGMENT] = (void *) __morestack_current_segment;
+ context[CURRENT_STACK] = (void *) &context;
+ context[STACK_GUARD] = __morestack_get_guard ();
+ context[INITIAL_SP] = (void *) __morestack_initial_sp.sp;
+ context[INITIAL_SP_LEN] = (void *) (uintptr_type) __morestack_initial_sp.len;
+ context[BLOCK_SIGNALS] = (void *) __morestack_initial_sp.dont_block_signals;
+}
+
+/* Set the current split stack context. The argument should be a
+ context previously passed to __splitstack_getcontext. This would
+ normally be called immediately after a call to getcontext or
+ swapcontext or setjmp if something jumped to it. */
+
+void
+__splitstack_setcontext (void *context[NUMBER_OFFSETS])
+{
+ __morestack_segments = (struct stack_segment *) context[MORESTACK_SEGMENTS];
+ __morestack_current_segment =
+ (struct stack_segment *) context[CURRENT_SEGMENT];
+ __morestack_set_guard (context[STACK_GUARD]);
+ __morestack_initial_sp.sp = context[INITIAL_SP];
+ __morestack_initial_sp.len = (size_t) context[INITIAL_SP_LEN];
+ __morestack_initial_sp.dont_block_signals =
+ (uintptr_type) context[BLOCK_SIGNALS];
+}
+
+/* Create a new split stack context. This will allocate a new stack
+ segment which may be used by a coroutine. STACK_SIZE is the
+ minimum size of the new stack. The caller is responsible for
+ actually setting the stack pointer. This would normally be called
+ before a call to makecontext, and the returned stack pointer and
+ size would be used to set the uc_stack field. A function called
+ via makecontext on a stack created by __splitstack_makecontext may
+ not return. Note that the returned pointer points to the lowest
+ address in the stack space, and thus may not be the value to which
+ to set the stack pointer. */
+
+void *
+__splitstack_makecontext (size_t stack_size, void *context[NUMBER_OFFSETS],
+ size_t *size)
+{
+ struct stack_segment *segment;
+ void *initial_sp;
+
+ memset (context, 0, NUMBER_OFFSETS * sizeof (void *));
+ segment = allocate_segment (stack_size);
+ context[MORESTACK_SEGMENTS] = segment;
+ context[CURRENT_SEGMENT] = segment;
+#ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
+ initial_sp = (void *) ((char *) (segment + 1) + segment->size);
+#else
+ initial_sp = (void *) (segment + 1);
+#endif
+ context[STACK_GUARD] = __morestack_make_guard (initial_sp, segment->size);
+ context[INITIAL_SP] = NULL;
+ context[INITIAL_SP_LEN] = 0;
+ *size = segment->size;
+ return (void *) (segment + 1);
+}
+
+/* Given an existing split stack context, reset it back to the start
+ of the stack. Return the stack pointer and size, appropriate for
+ use with makecontext. This may be used if a coroutine exits, in
+ order to reuse the stack segments for a new coroutine. */
+
+void *
+__splitstack_resetcontext (void *context[10], size_t *size)
+{
+ struct stack_segment *segment;
+ void *initial_sp;
+ size_t initial_size;
+ void *ret;
+
+ /* Reset the context assuming that MORESTACK_SEGMENTS, INITIAL_SP
+ and INITIAL_SP_LEN are correct. */
+
+ segment = context[MORESTACK_SEGMENTS];
+ context[CURRENT_SEGMENT] = segment;
+ context[CURRENT_STACK] = NULL;
+ if (segment == NULL)
+ {
+ initial_sp = context[INITIAL_SP];
+ initial_size = (uintptr_type) context[INITIAL_SP_LEN];
+ ret = initial_sp;
+#ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
+ ret = (void *) ((char *) ret - initial_size);
+#endif
+ }
+ else
+ {
+#ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
+ initial_sp = (void *) ((char *) (segment + 1) + segment->size);
+#else
+ initial_sp = (void *) (segment + 1);
+#endif
+ initial_size = segment->size;
+ ret = (void *) (segment + 1);
+ }
+ context[STACK_GUARD] = __morestack_make_guard (initial_sp, initial_size);
+ context[BLOCK_SIGNALS] = NULL;
+ *size = initial_size;
+ return ret;
+}
+
+/* Release all the memory associated with a splitstack context. This
+ may be used if a coroutine exits and the associated stack should be
+ freed. */
+
+void
+__splitstack_releasecontext (void *context[10])
+{
+ __morestack_release_segments (((struct stack_segment **)
+ &context[MORESTACK_SEGMENTS]),
+ 1);
+}
+
+/* Like __splitstack_block_signals, but operating on CONTEXT, rather
+ than on the current state. */
+
+void
+__splitstack_block_signals_context (void *context[NUMBER_OFFSETS], int *new,
+ int *old)
+{
+ if (old != NULL)
+ *old = ((uintptr_type) context[BLOCK_SIGNALS]) != 0 ? 0 : 1;
+ if (new != NULL)
+ context[BLOCK_SIGNALS] = (void *) (uintptr_type) (*new ? 0 : 1);
+}
+
+/* Find the stack segments associated with a split stack context.
+ This will return the address of the first stack segment and set
+ *STACK_SIZE to its size. It will set next_segment, next_sp, and
+ initial_sp which may be passed to __splitstack_find to find the
+ remaining segments. */
+
+void *
+__splitstack_find_context (void *context[NUMBER_OFFSETS], size_t *stack_size,
+ void **next_segment, void **next_sp,
+ void **initial_sp)
+{
+ void *sp;
+ struct stack_segment *segment;
+
+ *initial_sp = context[INITIAL_SP];
+
+ sp = context[CURRENT_STACK];
+ if (sp == NULL)
+ {
+ /* Most likely this context was created but was never used. The
+ value 2 is a code used by __splitstack_find to mean that we
+ have reached the end of the list of stacks. */
+ *next_segment = (void *) (uintptr_type) 2;
+ *next_sp = NULL;
+ *initial_sp = NULL;
+ return NULL;
+ }
+
+ segment = context[CURRENT_SEGMENT];
+ if (segment == NULL)
+ {
+ /* Most likely this context was saved by a thread which was not
+ created using __splistack_makecontext and which has never
+ split the stack. The value 1 is a code used by
+ __splitstack_find to look at the initial stack. */
+ segment = (struct stack_segment *) (uintptr_type) 1;
+ }
+
+ return __splitstack_find (segment, sp, stack_size, next_segment, next_sp,
+ initial_sp);
+}
+