Ignore:
Timestamp:
Apr 1, 2020, 9:32:21 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
6d43cdde
Parents:
5137f9f
Message:

add resize and more "alloc" routines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r5137f9f rcfbc703d  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  4 08:27:08 2020
    13 // Update Count     : 486
     12// Last Modified On : Tue Mar 31 13:26:46 2020
     13// Update Count     : 495
    1414//
    1515
     
    2020#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    2121#include <string.h>                                                                             // memcpy, memset
    22 #include <malloc.h>                                                                             // malloc_usable_size
    2322//#include <math.h>                                                                             // fabsf, fabs, fabsl
    2423#include <complex.h>                                                                    // _Complex_I
     
    3837        } // alloc_set
    3938
     39        T * alloc_set( T ptr[], size_t dim, T fill ) { // realloc array with fill
     40                size_t olen = malloc_usable_size( ptr );                // current allocation
     41                void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     42                size_t nlen = malloc_usable_size( nptr );               // new allocation
     43                if ( nlen > olen ) {                                                    // larger ?
     44                        for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value
     45                } // if
     46                return (T *)nptr;
     47        } // alloc_align_set
     48
    4049        T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
    4150                size_t olen = malloc_usable_size( ptr );                // current allocation
     
    4554                if ( nlen > olen ) {                                                    // larger ?
    4655                        memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     56                } // if
     57                return (T *)nptr;
     58        } // alloc_align_set
     59
     60        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill
     61                size_t olen = malloc_usable_size( ptr );                // current allocation
     62                void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
     63                // char * nptr = alloc_align( ptr, align );
     64                size_t nlen = malloc_usable_size( nptr );               // new allocation
     65                if ( nlen > olen ) {                                                    // larger ?
     66                        for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value
    4767                } // if
    4868                return (T *)nptr;
Note: See TracChangeset for help on using the changeset viewer.