Skip to content

Commit

Permalink
Stack allocation separated into separate functions
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
  • Loading branch information
sustrik committed May 24, 2015
1 parent 9b7011a commit 0f217ea
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ libmill_la_SOURCES = \
list.c\
slist.h\
slist.c\
stack.h\
stack.c\
tcp.c\
utils.h\
utils.c
Expand Down
34 changes: 3 additions & 31 deletions libmill.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "libmill.h"
#include "list.h"
#include "slist.h"
#include "stack.h"
#include "utils.h"

#include <assert.h>
Expand All @@ -39,16 +40,10 @@
/* Coroutines */
/******************************************************************************/

/* Size of stack for new coroutines. In bytes. */
#define MILL_STACK_SIZE 16384

/* Maximum size of an item in a channel that we can handle without
extra memory allocation. */
#define MILL_MAXINLINECHVALSIZE 128

/* Maximum number of unused cached stacks. */
#define MILL_MAX_CACHED_STACKS 64

volatile int mill_unoptimisable1 = 1;
volatile void *mill_unoptimisable2 = NULL;

Expand Down Expand Up @@ -161,11 +156,6 @@ struct mill_fdwitem {
};
static struct mill_fdwitem *wait_items = NULL;

/* A stack of unused coroutine stacks. This allows for extra-fast allocation
of a new stack. The FIFO nature of this structure minimises cache misses. */
static int num_cached_crs = 0;
static struct mill_cr *cached_crs = NULL;

/* Removes current coroutine from the queue and returns it to the caller. */
static struct mill_cr *mill_suspend() {
struct mill_cr *cr = first_cr;
Expand Down Expand Up @@ -274,17 +264,7 @@ static void mill_ctxswitch(void) {
void *mill_go_prologue(const char *created) {
if(mill_setjmp(&first_cr->ctx))
return NULL;
struct mill_cr *cr;
if(cached_crs) {
cr = cached_crs;
cached_crs = cached_crs->next;
--num_cached_crs;
}
else {
char *ptr = malloc(MILL_STACK_SIZE);
assert(ptr);
cr = (struct mill_cr*)(ptr + MILL_STACK_SIZE - sizeof(struct mill_cr));
}
struct mill_cr *cr = ((struct mill_cr*)mill_allocstack()) - 1;
mill_list_insert(&all_crs, &cr->all_crs_item, NULL);
cr->id = next_cr_id;
cr->created = created;
Expand All @@ -310,15 +290,7 @@ void mill_go_epilogue(void) {
cr->val.ptr = NULL;
}
mill_list_erase(&all_crs, &cr->all_crs_item);
if(num_cached_crs >= MILL_MAX_CACHED_STACKS) {
char *ptr = ((char*)(cr + 1)) - MILL_STACK_SIZE;
free(ptr);
}
else {
cr->next = cached_crs;
cached_crs = cr;
++num_cached_crs;
}
mill_freestack(cr + 1);
mill_ctxswitch();
}

Expand Down
66 changes: 66 additions & 0 deletions stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright (c) 2015 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

#include "slist.h"
#include "stack.h"

#include <assert.h>
#include <stdlib.h>

/* Size of stack for new coroutines. In bytes. */
#ifndef MILL_STACK_SIZE
#define MILL_STACK_SIZE 16384
#endif

/* Maximum number of unused cached stacks. */
#ifndef MILL_MAX_CACHED_STACKS
#define MILL_MAX_CACHED_STACKS 64
#endif

/* A stack of unused coroutine stacks. This allows for extra-fast allocation
of a new stack. The FIFO nature of this structure minimises cache misses.
When the stack is cached its mill_slist_item is placed on its top rather
then on the bottom. That way we minimise page misses. */
static int mill_num_cached_stacks = 0;
static struct mill_slist mill_cached_stacks = {0};

void *mill_allocstack(void) {
if(mill_slist_empty(&mill_cached_stacks))
return (void*)(mill_slist_pop(&mill_cached_stacks) + 1);
char *ptr = malloc(MILL_STACK_SIZE);
assert(ptr);
return ptr + MILL_STACK_SIZE;
}

void mill_freestack(void *stack) {
if(mill_num_cached_stacks >= MILL_MAX_CACHED_STACKS) {
char *ptr = ((char*)stack) - MILL_STACK_SIZE;
free(ptr);
return;
}
struct mill_slist_item *item = ((struct mill_slist_item*)stack) - 1;
mill_slist_push(&mill_cached_stacks, item);
++mill_num_cached_stacks;
}

35 changes: 35 additions & 0 deletions stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright (c) 2015 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

#ifndef MILL_STACK_INCLUDED
#define MILL_STACK_INCLUDED

/* Allocates new stack. Returns pointer to the *top* of the stack.
For now we assume that the stack grows downwards. */
void *mill_allocstack(void);

/* Deallocates a stack. The argument is pointer to the top of the stack. */
void mill_freestack(void *stack);

#endif

0 comments on commit 0f217ea

Please sign in to comment.