Hi,

Thanks for the fun question :)

The problem is that _POSIX_SOURCE 1 means that memalign() doesn't get defined. You're using -erroff=%all which suppressed the warning message.

Here's a short example:

#define _POSIX_SOURCE 1
#include <stdlib.h>
#include <stdio.h>

int main()
{
  char * buffer;
  printf("%i\n",sizeof(size_t));
  buffer=(char*)memalign(64,8);
  buffer[0]='\0';
}


cc -m64 -g -xs m.c
"m.c", line 9: warning: implicit function declaration: memalign

./a.out
8
Segmentation Fault (core dumped)

Because memalign is not defined in the header files it gets implicitly declared returning an int rather than a void*, so the top half of the pointer gets thrown away.

if you add the prototype

long long memalign(long long, long long);

the program works. But I wonder whether you need the _POSIX_SOURCE definition.

Regards,

Darryl.


On 09/20/12 19:26, Dennis Clarke wrote:

   I *must* be doing something epic wrong here.

However :

$
$ cat -n memalign_test.c
      1  #define _POSIX_SOURCE 1
      2
      3  #include<ctype.h>
      4  #include<errno.h>
      5  #include<locale.h>
      6  #include<stddef.h>
      7  #include<stdio.h>
      8  #include<stdlib.h>
      9  #include<string.h>
     10
     11  int main(int argc, char *argv[]) {
     12
     13       char *buffer;
     14
     15       buffer = memalign( (size_t) 64, (size_t) 8 );
     16       if ( buffer == NULL )
     17           /*****************************************
     18            *     ENOMEM                            *
     19            * In the future we will want to recover *
     20            * from this sort of epic fail. At the   *
     21            * very least, a syslog entry.           *
     22            *****************************************/
     23           exit ( EXIT_FAILURE );
     24
     25      buffer[0] = '\0'; /* something useful */
     26
     27      return ( EXIT_SUCCESS );
     28  }
     29
$
$ echo $CFLAGS
-erroff -xstrconst -xildoff -m64 -xmemalign=8s -xnolibmil -Xa -xcode=pic32 
-xregs=no%appl -xlibmieee -mc -g -xs -ftrap=%none -Qy -xbuiltin=%none 
-xdebugformat=dwarf -xunroll=1 -xtarget=ultraT2 -xcache=8/16/4:4096/64/16 
-D_TS_ERRNO -D_POSIX_PTHREAD_SEMANTICS -D_LARGEFILE64_SOURCE -D_DEBUG 
-DCGIEXTENDED
$ uname -a
SunOS node002 5.10 Generic_147440-23 sun4v sparc SUNW,T5240
$ cc -V
cc: Sun C 5.9 SunOS_sparc Patch 124867-16 2010/08/11
usage: cc [ options] files.  Use 'cc -flags' for details
$ cc -\# $CFLAGS -o memalign_test.o -c memalign_test.c
### Note: NLSPATH = 
/opt/SUNWspro/prod/bin/../lib/locale/%L/LC_MESSAGES/%N.cat:/opt/SUNWspro/prod/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat
###     command line files and options (expanded):
### -c -D_TS_ERRNO -D_POSIX_PTHREAD_SEMANTICS -D_LARGEFILE64_SOURCE -D_DEBUG 
-DCGIEXTENDED -erroff=%all -g -mc -Xa -xarch=sparcvis2 -m64 
-xcache=8/16/4:4096/64/16 -xchip=ultraT2 -xcode=pic32 -xdebugformat=dwarf 
-xlibmieee -xmemalign=8s -xregs=no%appl -xs -xstrconst -xunroll=1 
memalign_test.c -o memalign_test.o
/opt/SUNWspro/prod/bin/acomp -dg -xldscope=global -i memalign_test.c -y-fbe -y/opt/SUNWspro/prod/bin/fbe -y-xcode=pic32 -y-xarch=v9b -y-xmemalign=8s -y-g -y-o -ymemalign_test.o -y-verbose -y-xthreadvar=dynamic -y-comdat -xdbggen=no%stabs+dwarf2+usedonly -e rroff=%all -strconst -D_TS_ERRNO -D_POSIX_PTHREAD_SEMANTICS -D_LARGEFILE64_SOURCE -D_DEBUG -DCGIEXTENDED -xdbggen=incl -y-s -2K -m64 -fparam_ir -Qy -D__SunOS_5_10 -D__SUNPRO_C=0x590 -D__SVR4 -D__sun -D__SunOS -D__unix -D__sparcv9 -D__sparc -D__BUILTIN_VA_ARG_INCR -D__C99FEATURES__ -Xa -D__PRAGMA_REDEFINE_EXTNAME -Dunix -Dsun -Dsparc -D__RESTRICT -xc99=%all,no%lib -D__FLT_EVAL_METHOD__=0 -I/opt/SUNWspro/prod/include/cc "-g/opt/SUNWspro/prod/bin/cc -erroff -xstrconst -xildoff -m64 -xmemalign=8s -xnolibmil -Xa -xcode=pic32 -xregs=no%appl -xlibmieee -mc -g -xs -ftrap=%none -Qy -xbuiltin=%none -xdebugformat=dwarf -xunroll=1 -xtarget=ultraT2 -xcache=8/16/4:4096/64/16 -D_TS_ERRNO -D_POSIX_PTHREAD_SEMANTICS -D_LARGEFILE64_SOURCE
-
D_DEBUG -DCGIEXTENDED -o memalign_test.o -c " -fsimple=0 -D__SUN_PREFETCH 
-destination_ir=yabe
/usr/ccs/bin/mcs -c memalign_test.o
$ cc -\# $CFLAGS -o memalign_test memalign_test.o
### Note: NLSPATH = 
/opt/SUNWspro/prod/bin/../lib/locale/%L/LC_MESSAGES/%N.cat:/opt/SUNWspro/prod/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat
###     command line files and options (expanded):
### -D_TS_ERRNO -D_POSIX_PTHREAD_SEMANTICS -D_LARGEFILE64_SOURCE -D_DEBUG 
-DCGIEXTENDED -erroff=%all -g -mc -Xa -xarch=sparcvis2 -m64 
-xcache=8/16/4:4096/64/16 -xchip=ultraT2 -xcode=pic32 -xdebugformat=dwarf 
-xlibmieee -xmemalign=8s -xregs=no%appl -xs -xstrconst -xunroll=1 
memalign_test.o -o memalign_test
### Note: LD_LIBRARY_PATH =<null>
### Note: LD_LIBRARY_PATH_64 =<null>
### Note: LD_RUN_PATH =<null>
/usr/ccs/bin/ld /opt/SUNWspro/prod/lib/v9/crti.o /opt/SUNWspro/prod/lib/v9/crt1.o 
/opt/SUNWspro/prod/lib/v9/values-xi.o -o memalign_test memalign_test.o -Y 
"P,/opt/SUNWspro/prod/lib/sparcvis2/64:/opt/SUNWspro/prod/lib/v9:/usr/ccs/lib/sparcv9:/lib/sparcv9:/usr/lib/sparcv9"
 -Qy -lc /opt/SUNWspro/prod/lib/v9/crtn.o
/usr/ccs/bin/mcs -c memalign_test
$
$ dbx memalign_test
Reading memalign_test
Reading ld.so.1
Reading libc.so.1
(dbx) stop at 1
(2) stop at "memalign_test.c":15
(dbx) run
Running: memalign_test
(process id 19599)
Reading libc_psr.so.1
stopped in main at line 15 in file "memalign_test.c"
    15        buffer = memalign( (size_t) 64, (size_t) 8 );
(dbx) print buffer
buffer = (nil)
(dbx) step
stopped in main at line 16 in file "memalign_test.c"
    16        if ( buffer == NULL )
(dbx) print buffer
buffer = 0x101080 "<bad address 0x101080>"
(dbx) step
stopped in main at line 25 in file "memalign_test.c"
    25       buffer[0] = '\0'; /* something useful */
(dbx) step
signal SEGV (no mapping at the fault address) in main at line 25 in file 
"memalign_test.c"
    25       buffer[0] = '\0'; /* something useful */
(dbx) quit
$
$


Good ol malloc :

$ cat -n foo.c
      1  #define _POSIX_SOURCE 1
      2
      3  #include<ctype.h>
      4  #include<errno.h>
      5  #include<locale.h>
      6  #include<stddef.h>
      7  #include<stdio.h>
      8  #include<stdlib.h>
      9  #include<string.h>
     10
     11  int main(int argc, char *argv[]) {
     12
     13       char *buffer;
     14
     15       buffer = malloc( (size_t) 8 );
     16       if ( buffer == NULL )
     17           /*****************************************
     18            *     ENOMEM                            *
     19            * In the future we will want to recover *
     20            * from this sort of epic fail. At the   *
     21            * very least, a syslog entry.           *
     22            *****************************************/
     23           exit ( EXIT_FAILURE );
     24
     25      buffer[0] = '\0'; /* something useful */
     26
     27      return ( EXIT_SUCCESS );
     28  }
     29
$
$ cc $CFLAGS -o foo foo.c

$ dbx foo
Reading foo
Reading ld.so.1
Reading libc.so.1
(dbx) stop at 1
(2) stop at "foo.c":15
(dbx) run
Running: foo
(process id 19633)
Reading libc_psr.so.1
stopped in main at line 15 in file "foo.c"
    15        buffer = malloc( (size_t) 8 );
(dbx) print buffer
buffer = (nil)
(dbx) step
stopped in main at line 16 in file "foo.c"
    16        if ( buffer == NULL )
(dbx) print buffer
buffer = 0x100100fc0 ""
(dbx) step
stopped in main at line 25 in file "foo.c"
    25       buffer[0] = '\0'; /* something useful */
(dbx) step
stopped in main at line 27 in file "foo.c"
    27       return ( EXIT_SUCCESS );
(dbx) print buffer
buffer = 0x100100fc0 ""
(dbx) step
stopped in main at line 28 in file "foo.c"
    28   }
(dbx) step

execution completed, exit code is 0
(dbx) quit
$

So I went and looked at 
http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libbc/libc/gen/common/memalign.c
 to see, what bloody obvious thing I am doing wrong[1]. Perhaps  alignment 
means bytes and I should have 8 there.  Not sure.

Dennis

[1] other than leaving out the free(buffer)


_______________________________________________
opensolaris-code mailing list
opensolaris-code@opensolaris.org
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

--
Darryl Gove
Phone: +408 276 7421
Blog : http://blogs.oracle.com/d/
Books: http://my.safaribooksonline.com/9780321711441
       http://my.safaribooksonline.com/9780768681390
       http://my.safaribooksonline.com/0595352510
_______________________________________________
opensolaris-code mailing list
opensolaris-code@opensolaris.org
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to