Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/alloc.c

    r2afec66 r91c389a  
    1 //
     1// 
    22// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
    33//
     
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // alloc.c --
    8 //
     7// alloc.c -- 
     8// 
    99// Author           : Peter A. Buhr
    1010// Created On       : Wed Feb  3 07:56:22 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun  2 15:13:03 2017
    13 // Update Count     : 316
    14 //
    15 
    16 #include <assert>
    17 extern "C" {
    18 #include <malloc.h>                                     // malloc_usable_size
    19 #include <stdint.h>                                     // uintptr_t
    20 #include <stdlib.h>                                     // posix_memalign
    21 } // extern
     12// Last Modified On : Thu Jul 20 16:01:10 2017
     13// Update Count     : 318
     14//
     15
     16#include <assert.h>
     17#include <malloc.h>                                                                             // malloc_usable_size
     18#include <stdint.h>                                                                             // uintptr_t
     19#include <stdlib.h>                                                                             // posix_memalign
    2220#include <fstream>
    23 #include <stdlib>                                       // access C malloc, realloc
     21#include <stdlib>                                                                               // access C malloc, realloc
    2422
    2523int * foo( int * p, int c ) { return p; }
     
    2826
    2927int main( void ) {
    30         size_t dim = 10;
    31         int * p;
     28    size_t dim = 10;
     29    int * p;
    3230        char fill = '\1';
    3331
    3432        // allocation, non-array types
    3533
    36         p = (void *)malloc( sizeof(*p) );                   // C malloc, type unsafe
     34    p = (void *)malloc( sizeof(*p) );                                   // C malloc, type unsafe
    3735        *p = 0xdeadbeef;
    3836        printf( "C   malloc %#x\n", *p );
    39         free( p );
    40 
    41         p = malloc();                                       // CFA malloc, type safe
     37    free( p );
     38
     39    p = malloc();                                                                               // CFA malloc, type safe
    4240        *p = 0xdeadbeef;
    4341        printf( "CFA malloc %#x\n", *p );
    44         free( p );
    45 
    46         p = alloc();                                        // CFA alloc, type safe
     42    free( p );
     43
     44    p = alloc();                                                                                // CFA alloc, type safe
    4745        *p = 0xdeadbeef;
    4846        printf( "CFA alloc %#x\n", *p );
    49         free( p );
    50 
    51         p = alloc( fill );                                  // CFA alloc, fill
     47    free( p );
     48
     49    p = alloc( fill );                                                                  // CFA alloc, fill
    5250        printf( "CFA alloc, fill %08x\n", *p );
    5351
     
    5654        printf( "\n" );
    5755
    58         p = calloc( dim, sizeof( *p ) );                    // C array calloc, type unsafe
     56    p = calloc( dim, sizeof( *p ) );                                    // C array calloc, type unsafe
    5957        printf( "C   array calloc, fill 0\n" );
    6058        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
    6159        printf( "\n" );
    62         free( p );
    63 
    64         p = calloc( dim );                                  // CFA array calloc, type safe
     60    free( p );
     61
     62    p = calloc( dim );                                                                  // CFA array calloc, type safe
    6563        printf( "CFA array calloc, fill 0\n" );
    6664        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
    6765        printf( "\n" );
    68         free( p );
    69 
    70         p = alloc( dim );                                   // CFA array alloc, type safe
     66    free( p );
     67
     68    p = alloc( dim );                                                                   // CFA array alloc, type safe
    7169        for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; }
    7270        printf( "CFA array alloc, no fill\n" );
    7371        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
    7472        printf( "\n" );
    75         free( p );
    76 
    77         p = alloc( 2 * dim, fill );                         // CFA array alloc, fill
     73    free( p );
     74
     75    p = alloc( 2 * dim, fill );                                                 // CFA array alloc, fill
    7876        printf( "CFA array alloc, fill %#x\n", fill );
    7977        for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); }
     
    8583        printf( "\n" );
    8684
    87         p = (void *)realloc( p, dim * sizeof(*p) );         // C realloc
     85    p = (void *)realloc( p, dim * sizeof(*p) );                 // C realloc
    8886        for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; }
    8987        printf( "C   realloc\n" );
     
    9189        printf( "\n" );
    9290
    93         p = realloc( p, 2 * dim * sizeof(*p) );             // CFA realloc
     91    p = realloc( p, 2 * dim * sizeof(*p) );                             // CFA realloc
    9492        for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; }
    9593        printf( "CFA realloc\n" );
     
    102100        printf( "\n" );
    103101
    104         p = alloc( p, dim );                                // CFA resize array alloc
     102    p = alloc( p, dim );                                                                // CFA resize array alloc
    105103        for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; }
    106104        printf( "CFA resize alloc\n" );
     
    108106        printf( "\n" );
    109107
    110         p = alloc( p, 2 * dim );                            // CFA resize array alloc
     108    p = alloc( p, 2 * dim );                                                    // CFA resize array alloc
    111109        for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; }
    112110        printf( "CFA resize array alloc\n" );
     
    114112        printf( "\n" );
    115113
    116         p = alloc( p, dim );                                // CFA array alloc
     114    p = alloc( p, dim );                                                                // CFA array alloc
    117115        printf( "CFA resize array alloc\n" );
    118116        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
     
    122120        p = 0;
    123121
    124         p = alloc( p, dim, fill );                          // CFA array alloc, fill
     122    p = alloc( p, dim, fill );                                                  // CFA array alloc, fill
    125123        printf( "CFA resize array alloc, fill\n" );
    126124        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
    127125        printf( "\n" );
    128126
    129         p = alloc( p, 2 * dim, fill );                      // CFA array alloc, fill
     127    p = alloc( p, 2 * dim, fill );                                              // CFA array alloc, fill
    130128        printf( "CFA resize array alloc, fill\n" );
    131129        for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); }
    132130        printf( "\n" );
    133131
    134         p = alloc( p, dim, fill );                          // CFA array alloc, fill
     132    p = alloc( p, dim, fill );                                                  // CFA array alloc, fill
    135133        printf( "CFA resize array alloc, fill\n" );
    136134        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] );; }
     
    139137
    140138
    141         struct Struct { int x; double y; };
    142         Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
     139    struct Struct { int x; double y; };
     140    Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
    143141
    144142        // alignment, non-array types
     
    146144        enum { Alignment = 128 };
    147145
    148         stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
     146    stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
    149147        assert( (uintptr_t)stp % Alignment == 0 );
    150148        printf( "C   memalign %d %g\n", stp->x, stp->y );
    151         free( stp );
    152 
    153         stp = (memalign( Alignment )){ 42, 42.5 };          // CFA memalign
     149    free( stp );
     150
     151    stp = (memalign( Alignment )){ 42, 42.5 };                  // CFA memalign
    154152        assert( (uintptr_t)stp % Alignment == 0 );
    155153        printf( "CFA memalign %d %g\n", stp->x, stp->y );
    156         free( stp );
    157 
    158         posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
     154    free( stp );
     155
     156    posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
    159157        *stp = (Struct){ 42, 42.5 };
    160158        assert( (uintptr_t)stp % Alignment == 0 );
    161159        printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
    162         free( stp );
    163 
    164         posix_memalign( &stp, Alignment );                  // CFA posix_memalign
     160    free( stp );
     161
     162    posix_memalign( &stp, Alignment );                                  // CFA posix_memalign
    165163        *stp = (Struct){ 42, 42.5 };
    166164        assert( (uintptr_t)stp % Alignment == 0 );
    167165        printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
    168         free( stp );
    169 
    170         stp = (aligned_alloc( Alignment )){ 42, 42.5 };     // CFA aligned_alloc
     166    free( stp );
     167
     168    stp = (aligned_alloc( Alignment )){ 42, 42.5 };             // CFA aligned_alloc
    171169        assert( (uintptr_t)stp % Alignment == 0 );
    172170        printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y );
    173         free( stp );
    174 
    175         stp = (align_alloc( Alignment )){ 42, 42.5 };       // CFA align_alloc
     171    free( stp );
     172
     173    stp = (align_alloc( Alignment )){ 42, 42.5 };               // CFA align_alloc
    176174        assert( (uintptr_t)stp % Alignment == 0 );
    177175        printf( "CFA align_alloc %d %g\n", stp->x, stp->y );
    178         free( stp );
    179 
    180         stp = align_alloc( Alignment, fill );               // CFA memalign, fill
     176    free( stp );
     177
     178    stp = align_alloc( Alignment, fill );                               // CFA memalign, fill
    181179        assert( (uintptr_t)stp % Alignment == 0 );
    182180        printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y );
    183         free( stp );
     181    free( stp );
    184182
    185183
     
    187185        printf( "\n" );
    188186
    189         stp = align_alloc( Alignment, dim );                // CFA array memalign
     187    stp = align_alloc( Alignment, dim );                                // CFA array memalign
    190188        assert( (uintptr_t)stp % Alignment == 0 );
    191189        for ( int i = 0; i < dim; i += 1 ) { stp[i] = (Struct){ 42, 42.5 }; }
     
    193191        for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
    194192        printf( "\n" );
    195         free( stp );
    196 
    197         stp = align_alloc( Alignment, dim, fill );          // CFA array memalign, fill
     193    free( stp );
     194
     195    stp = align_alloc( Alignment, dim, fill );                  // CFA array memalign, fill
    198196        assert( (uintptr_t)stp % Alignment == 0 );
    199197        printf( "CFA array align_alloc, fill\n" );
    200198        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
    201199        printf( "\n" );
    202         free( stp );
     200    free( stp );
    203201
    204202
     
    206204        printf( "\n" );
    207205
    208         memset( &st, fill );                                // CFA memset, type safe
     206    memset( &st, fill );                                                                // CFA memset, type safe
    209207        printf( "CFA memset %#x %a\n", st.x, st.y );
    210         memcpy( &st1, &st );                                // CFA memcpy, type safe
     208    memcpy( &st1, &st );                                                                // CFA memcpy, type safe
    211209        printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
    212210
     
    215213        printf( "\n" );
    216214
    217         memset( sta, dim, fill );                           // CFA array memset, type safe
     215    memset( sta, dim, fill );                                                   // CFA array memset, type safe
    218216        printf( "CFA array memset\n" );
    219217        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
    220218        printf( "\n" );
    221219
    222         memcpy( sta1, sta, dim );                           // CFA array memcpy, type safe
     220    memcpy( sta1, sta, dim );                                                   // CFA array memcpy, type safe
    223221        printf( "CFA memcpy\n" );
    224222        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
     
    247245        printf( "\n" );
    248246
    249         float * fp = malloc() + 1;
    250         printf( "pointer arithmetic %d\n", fp == fp - 1 );
    251         free( fp - 1 );
    252 
    253         p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
     247    float * fp = malloc() + 1;
     248    printf( "pointer arithmetic %d\n", fp == fp - 1 );
     249    free( fp - 1 );
     250
     251    p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
    254252        *p = 0xdeadbeef;
    255253        printf( "CFA deep malloc %#x\n", *p );
    256         free( p );
     254    free( p );
    257255
    258256        stp = malloc();
    259257        printf( "\nSHOULD FAIL\n" );
    260         p = alloc( stp, dim * sizeof(*stp) );
    261         p = memset( stp, 10 );
    262         p = memcpy( &st1, &st );
     258    p = alloc( stp, dim * sizeof(*stp) );
     259    p = memset( stp, 10 );
     260    p = memcpy( &st1, &st );
    263261} // main
    264262
Note: See TracChangeset for help on using the changeset viewer.