# New Ticket Created by  Leopold Toetsch 
# Please include the string:  [perl #22455]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=22455 >


Attached patch tests if either posix_memalign or memalign is present.
Additionally the platform interface prototypes are moved to a separate 
header file.

I did test it with 2 different libc's one providing posix_memalign and 
one with memalign.

Missing is a patch for interpreter.c where Parrot_memalign should be 
Parrot_memalign_if_possible, which maps to malloc if no memalign was found.
Finally freeing memaligned memory should be done via 
Parrot_memalign_free which on reasonable systems is just free, but ...

Comments welcome as well as adjustments for other platforms.
leo


-- attachment  1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/58565/43555/00eca4/config-memalign.patch

--- parrot/config/auto/memalign.pl      Mon Feb 10 11:03:55 2003
+++ parrot-leo/config/auto/memalign.pl  Mon Jun  2 12:04:19 2003
@@ -16,10 +16,22 @@
     }
     cc_clean();

+    my $test2 = 0;
+
+    cc_gen('config/auto/memalign/test_c2.in');
+    eval { cc_build(); };
+    unless ($@ || cc_run() !~ /ok/) {
+       $test2 = 1;
+    }
+    cc_clean();
+
+    my $f;
     Configure::Data->set(
-       memalign          => $test
+       memalign          => ($f = $test2 ? 'posix_memalign' :
+                            $test ?  'memalign' :
+                            undef)
     );
-    print $test ? " (Yep) " : " (no) "
+    print $test ? " (Yep:$f) " : " (no) "
 }

 1;
--- parrot/config/auto/memalign/test_c.in       Mon Feb 10 11:03:53 2003
+++ parrot-leo/config/auto/memalign/test_c.in   Mon Jun  2 11:32:23 2003
@@ -2,7 +2,7 @@
  * test for memalign function
  *
  * This file is automatically generated by Configure
- * from testcomputedgoto_c.in.
+ * from test_c.in.
  */
 #include <malloc.h>
 #include <stdio.h>
--- /dev/null   Fri Feb 28 14:27:28 2003
+++ parrot-leo/config/auto/memalign/test_c2.in  Mon Jun  2 11:32:23 2003
@@ -0,0 +1,18 @@
+/*
+ * test for posix_memalign function
+ *
+ * This file is automatically generated by Configure
+ * from test_c.in.
+ */
+#include <malloc.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+       void * p;
+       size_t s = 256;
+       int i = posix_memalign(&p, s, s);
+       puts(((int)p & 0xff) == 0 ? "ok" : "nix");
+       return i;
+}
--- parrot/config/gen/feature_h/feature_h.in    Thu May  1 10:06:05 2003
+++ parrot-leo/config/gen/feature_h/feature_h.in        Mon Jun  2 12:04:01 2003
@@ -11,15 +11,19 @@
        print OUT <<EOP;
 /* from config/auto/memalign */
 EOP
-if (${memalign}) {
-       print OUT <<EOP;
-#include <malloc.h>
-#define Parrot_memalign(a, s) memalign((a), (s))
-EOP
-} else {
-       print OUT <<EOP;
-#define Parrot_memalign(a, s) mem_sys_allocate(s)
-EOP
+if (${memalign} eq 'posix_memalign') {
+  print OUT <<'END'
+#define HAS_POSIX_MEMALIGN
+#define HAS_SOME_MEMALIGN
+
+END
+}
+elsif (${memalign} eq 'memalign') {
+  print OUT <<'END'
+#define HAS_MEMALIGN
+#define HAS_SOME_MEMALIGN
+
+END
 }

        print OUT <<EOP;
@@ -38,6 +42,7 @@
 #ifdef I386
 #  define HAS_JIT_FCOMIP
 #endif
+
 END
 }

--- parrot/config/gen/platform.pl       Tue Sep  3 10:20:58 2002
+++ parrot-leo/config/gen/platform.pl   Mon Jun  2 11:24:34 2003
@@ -17,6 +17,8 @@

   copy_if_diff("config/gen/platform/$platform.c", "platform.c");
   copy_if_diff("config/gen/platform/$platform.h", "include/parrot/platform.h");
+  copy_if_diff("config/gen/platform/platform_interface.h",
+                 "include/parrot/platform_interface.h");
 }

 1;
--- parrot/config/gen/platform/ansi.h   Thu Jul 18 10:02:02 2002
+++ parrot-leo/config/gen/platform/ansi.h       Mon Jun  2 11:25:28 2003
@@ -16,20 +16,12 @@
 ** Miscellaneous:
 */

-void Parrot_sleep(unsigned int seconds);
-INTVAL Parrot_intval_time(void);
-FLOATVAL Parrot_floatval_time(void);
-void Parrot_setenv(const char *name, const char *value);


 /*
 ** Dynamic Loading:
 */

-void *Parrot_dlopen(const char *filename);
-const char *Parrot_dlerror(void);
-void *Parrot_dlsym(void *handle, const char *symbol);
-int Parrot_dlclose(void *handle);

 /*
  * Local variables:
--- parrot/config/gen/platform/darwin.h Thu Jul 18 10:02:02 2002
+++ parrot-leo/config/gen/platform/darwin.h     Mon Jun  2 11:25:28 2003
@@ -1,5 +1,5 @@
 /*
-** platform.h [generic version]
+** platform.h [darwin version]
 */


@@ -16,22 +16,12 @@
 ** Miscellaneous:
 */

-void Parrot_sleep(unsigned int seconds);
-INTVAL Parrot_intval_time(void);
-FLOATVAL Parrot_floatval_time(void);
-void Parrot_setenv(const char *name, const char *value);
-

 /*
 ** Dynamic Loading:
 */

 #define PARROT_DLOPEN_FLAGS RTLD_LAZY
-
-void *Parrot_dlopen(const char *filename);
-const char *Parrot_dlerror(void);
-void *Parrot_dlsym(void *handle, const char *symbol);
-int Parrot_dlclose(void *handle);

 /*
  * Local variables:
--- parrot/config/gen/platform/generic.c        Sat Jan  4 11:00:14 2003
+++ parrot-leo/config/gen/platform/generic.c    Mon Jun  2 11:51:09 2003
@@ -125,6 +125,37 @@
 #endif
 }

+#if defined(HAS_POSIX_MEMALIGN)
+#include <stdlib.h>
+
+void *
+Parrot_memalign(size_t align, size_t size)
+{
+    void *p;
+    int i = posix_memalign(p, align, size);
+    return i == 0 ? p : NULL;
+}
+
+#  define Parrot_memalign_if_possible(a, s) Parrot_memalign((a), (s))
+
+#elif defined(HAS_MEMALIGN)
+#include <malloc.h>
+
+void *
+Parrot_memalign(size_t align, size_t size)
+{
+    return memalign(align, size);
+}
+
+void *
+Parrot_memalign_if_possible(size_t align, size_t size)
+{
+    return memalign(align, size);
+}
+
+#endif
+
+#define Parrot_free_memalign(p) free(p)
 /*
  * Local variables:
  * c-indentation-style: bsd
--- parrot/config/gen/platform/generic.h        Thu Jul 18 10:02:02 2002
+++ parrot-leo/config/gen/platform/generic.h    Mon Jun  2 11:25:28 2003
@@ -16,22 +16,12 @@
 ** Miscellaneous:
 */

-void Parrot_sleep(unsigned int seconds);
-INTVAL Parrot_intval_time(void);
-FLOATVAL Parrot_floatval_time(void);
-void Parrot_setenv(const char *name, const char *value);
-

 /*
 ** Dynamic Loading:
 */

 #define PARROT_DLOPEN_FLAGS RTLD_LAZY
-
-void *Parrot_dlopen(const char *filename);
-const char *Parrot_dlerror(void);
-void *Parrot_dlsym(void *handle, const char *symbol);
-int Parrot_dlclose(void *handle);

 /*
  * Local variables:
--- /dev/null   Fri Feb 28 14:27:28 2003
+++ parrot-leo/config/gen/platform/platform_interface.h Mon Jun  2 11:25:28 2003
@@ -0,0 +1,51 @@
+#ifndef PLATFORM_INTERFACE_H_GUARD
+#define PLATFORM_INTERFACE_H_GUARD
+/*
+** platform_interface.h
+*/
+#include "feature.h"
+
+/*
+** I/O:
+*/
+
+/*
+** Memory:
+*/
+
+void *Parrot_memalign(size_t align, size_t size);
+void *Parrot_memalign_if_possible(size_t align, size_t size);
+void Parrot_free_memalign(void *);
+
+#if !defined(HAS_MEMALIGN) && !defined(HAS_POSIX_MEMALIGN)
+#  define Parrot_memalign_if_possible(a, s) malloc(a)
+#endif
+
+/*
+** Miscellaneous:
+*/
+
+void Parrot_sleep(unsigned int seconds);
+INTVAL Parrot_intval_time(void);
+FLOATVAL Parrot_floatval_time(void);
+void Parrot_setenv(const char *name, const char *value);
+
+/*
+** Dynamic Loading:
+*/
+
+void *Parrot_dlopen(const char *filename);
+const char *Parrot_dlerror(void);
+void *Parrot_dlsym(void *handle, const char *symbol);
+int Parrot_dlclose(void *handle);
+
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vim: expandtab shiftwidth=4:
+ */
+#endif
--- parrot/config/gen/platform/win32.h  Thu May  1 10:06:06 2003
+++ parrot-leo/config/gen/platform/win32.h      Mon Jun  2 11:25:28 2003
@@ -28,20 +28,12 @@
 ** Miscellaneous:
 */

-void Parrot_sleep(unsigned int seconds);
-INTVAL Parrot_intval_time(void);
-FLOATVAL Parrot_floatval_time(void);
-void Parrot_setenv(const char *name, const char *value);


 /*
 ** Dynamic Loading:
 */

-void *Parrot_dlopen(const char *filename);
-const char *Parrot_dlerror(void);
-void *Parrot_dlsym(void *handle, const char *symbol);
-int Parrot_dlclose(void *handle);

 /*
  * Local variables:
--- parrot/include/parrot/parrot.h      Wed May 14 10:06:39 2003
+++ parrot-leo/include/parrot/parrot.h  Mon Jun  2 11:33:28 2003
@@ -190,6 +190,7 @@
 #endif

 #include "parrot/platform.h"
+#include "parrot/platform_interface.h"
 #include "parrot/global_setup.h"
 #include "parrot/interpreter.h"
 #include "parrot/encoding.h"

Reply via email to