]> git.ozlabs.org Git - ccan/blobdiff - ccan/coroutine/coroutine.h
tal: allow notifiers on NULL.
[ccan] / ccan / coroutine / coroutine.h
index 1d746f493f1c7f1b0c653e05e7850446ab8a0e36..54b5198d1f6e597194b9ae2b22c0490ea4883fdb 100644 (file)
  * Describes a stack suitable for executing a coroutine.  This
  * structure is always contained within the stack it describes.
  */
-struct coroutine_stack;
+struct coroutine_stack {
+       uint64_t magic;
+       size_t size;
+       int valgrind_id;
+};
 
 /**
  * struct coroutine_state
@@ -37,7 +41,7 @@ struct coroutine_state;
  * Number of bytes of a stack which coroutine needs for its own
  * tracking information.
  */
-#define COROUTINE_STK_OVERHEAD         (sizeof(uint64_t) + sizeof(size_t))
+#define COROUTINE_STK_OVERHEAD sizeof(struct coroutine_stack)
 
 /**
  * COROUTINE_MIN_STKSZ - Minimum coroutine stack size
@@ -49,10 +53,16 @@ struct coroutine_state;
 #define COROUTINE_MIN_STKSZ            2048
 
 /**
- * COROUTINE_STACK_MAGIC - Magic number for coroutine stacks
+ * COROUTINE_STACK_MAGIC_BUF - Magic number for coroutine stacks in a user
+ *                             supplied buffer
  */
-#define COROUTINE_STACK_MAGIC          0xc040c040574c574c
+#define COROUTINE_STACK_MAGIC_BUF      0xc040c040574cb00f
 
+/**
+ * COROUTINE_STACK_MAGIC_ALLOC - Magic number for coroutine stacks
+ *                               allocated by this module
+ */
+#define COROUTINE_STACK_MAGIC_ALLOC    0xc040c040574ca110
 
 /**
  * coroutine_stack_init - Prepare a coroutine stack in an existing buffer
@@ -71,6 +81,23 @@ struct coroutine_state;
 struct coroutine_stack *coroutine_stack_init(void *buf, size_t bufsize,
                                             size_t metasize);
 
+/**
+ * coroutine_stack_alloc - Allocate a coroutine stack
+ * @totalsize: total size to allocate
+ * @metasize: size of metadata to add to the stack (not including
+ *            coroutine internal overhead)
+ *
+ * Allocates a coroutine stack of size @totalsize, including both
+ * internal overhead (COROUTINE_STK_OVERHEAD) and metadata of size
+ * @metasize.  Where available this will also create a guard page, so
+ * that overruning the stack will result in an immediate crash, rather
+ * than data corruption.
+ *
+ * This will fail if the totalsize < (COROUTINE_MIN_STKSZ +
+ * COROUTINE_STK_OVERHEAD + metasize).
+ */
+struct coroutine_stack *coroutine_stack_alloc(size_t bufsize, size_t metasize);
+
 /**
  * coroutine_stack_release - Stop using a coroutine stack
  * @stack: coroutine stack to release