From c2627c465c6e2ab5ad2dff721dddda5d79d61bda Mon Sep 17 00:00:00 2001
From: zooko <zooko@zooko.com>
Date: Wed, 14 Nov 2007 01:43:52 +0530
Subject: [PATCH] zfec: conditionally-compile the right magic to use alloca()
 with gcc -mno-cygwin

darcs-hash:d1c2597ea94772afcbb81c50ab7ff8efd0eacd6e
---
 zfec/TODO              |  1 +
 zfec/zfec/_fecmodule.c | 16 ++++++++++++++++
 zfec/zfec/fec.c        | 17 +++++++++++++++--
 zfec/zfec/fec.h        |  8 +-------
 4 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/zfec/TODO b/zfec/TODO
index 28d0cf1..bad1951 100644
--- a/zfec/TODO
+++ b/zfec/TODO
@@ -8,6 +8,7 @@
  * Jerasure
   ** try multiplication without a lookup table? (to save cache pressure)
  * conditional compilation to handle a printf that doesn't understand %Zu
+ * try malloc() instead of alloca() (more portable, possibly better for keeping stacks aligned)
 
  * announce on lwn, p2p-hackers
  
diff --git a/zfec/zfec/_fecmodule.c b/zfec/zfec/_fecmodule.c
index 13da1c9..abe21ec 100644
--- a/zfec/zfec/_fecmodule.c
+++ b/zfec/zfec/_fecmodule.c
@@ -13,6 +13,22 @@ typedef int Py_ssize_t;
 
 #include "stdarg.h"
 
+#if defined(_MSC_VER)
+// actually, some of the flavors (i.e. Enterprise) do support restrict
+//#define restrict __restrict
+#define restrict
+#define inline __inline
+#define alloca _alloca
+#else
+#ifdef __GNUC__
+#define alloca(x) __builtin_alloca(x)
+#else
+#include <alloca.h>
+#endif
+#endif
+
+
+
 static PyObject *py_fec_error;
 
 static char fec__doc__[] = "\
diff --git a/zfec/zfec/fec.c b/zfec/zfec/fec.c
index 62f908f..761e045 100644
--- a/zfec/zfec/fec.c
+++ b/zfec/zfec/fec.c
@@ -2,13 +2,26 @@
  * zfec -- fast forward error correction library with Python interface
  */
 
+#include "fec.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
-#include <alloca.h>
 
-#include "fec.h"
+#if defined(_MSC_VER)
+// actually, some of the flavors (i.e. Enterprise) do support it
+//#define restrict __restrict
+#define restrict
+#define inline __inline
+#define alloca _alloca
+#else
+#ifdef __GNUC__
+#define alloca(x) __builtin_alloca(x)
+#else
+#include <alloca.h>
+#endif
+#endif
 
 
 /*
diff --git a/zfec/zfec/fec.h b/zfec/zfec/fec.h
index 3931017..2a7981a 100644
--- a/zfec/zfec/fec.h
+++ b/zfec/zfec/fec.h
@@ -2,13 +2,7 @@
  * zfec -- fast forward error correction library with Python interface
  */
 
-#if defined(_MSC_VER)
-// actually, some of the flavors (i.e. Enterprise) do support it
-//#define restrict __restrict
-#define restrict
-#define inline __inline
-#define alloca _alloca
-#endif
+#include <stddef.h>
 
 typedef unsigned char gf;
 
-- 
2.45.2