Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib.c

    rf3fc631f r8dc51c8  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 30 09:07:56 2017
    13 // Update Count     : 237
     12// Last Modified On : Wed May 24 18:13:15 2017
     13// Update Count     : 198
    1414//
    1515
     
    2121#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    2222#include <stdlib.h>                                                                             // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
    23 #include <string.h>                                                                             // memcpy, memset
     23#include <string.h>                                                                             // memset
    2424#include <malloc.h>                                                                             // malloc_usable_size
    2525#include <math.h>                                                                               // fabsf, fabs, fabsl
     
    2727} // extern "C"
    2828
    29 // resize, non-array types
    30 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value
    31         //printf( "X6\n" );
    32         size_t olen = malloc_usable_size( ptr );                        // current allocation
    33     char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc
    34         size_t nlen = malloc_usable_size( nptr );                       // new allocation
    35         if ( nlen > olen ) {                                                            // larger ?
    36                 memset( nptr + olen, (int)fill, nlen - olen );  // initialize added storage
    37         } //
    38     return (T *)nptr;
     29forall( dtype T | sized(T) ) T * malloc( void ) {               // type-safe
     30    return (T *)(void *)malloc( (size_t)sizeof(T) );
     31} // malloc
     32
     33forall( dtype T | sized(T) ) T * malloc( char fill ) {  // initial with fill value (like calloc)
     34        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );
     35    return memset( ptr, (int)fill, sizeof(T) );
     36} // malloc
     37
     38forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc
     39    return (T *)realloc( ptr, size );
     40} // malloc
     41
     42forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
     43    return (T *)realloc( ptr, size, fill );
     44} // malloc
     45
     46
     47forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0
     48    return (T *)calloc( nmemb, sizeof(T) );
     49} // calloc
     50
     51
     52forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe
     53    return (T *)(void *)realloc( (void *)ptr, size );
    3954} // realloc
    4055
    41 // allocation/deallocation and constructor/destructor
    42 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
     56forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
     57    char * nptr = (T *)(void *)realloc( (void *)ptr, size );
     58    size_t unused = malloc_usable_size( nptr );
     59    memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage
     60    return nptr;
     61} // realloc
     62
     63
     64forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation
     65    return (T *)memalign( alignment, sizeof(T) );
     66} // aligned_alloc
     67
     68forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
     69    return (T *)memalign( alignment, sizeof(T) );
     70} // memalign
     71
     72forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
     73    return posix_memalign( (void **)ptr, alignment, sizeof(T) );
     74} // posix_memalign
     75
     76
     77forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) //  new
    4378T * new( Params p ) {
    4479        return ((T *)malloc()){ p };
    4580} // new
    4681
    47 forall( dtype T | { void ^?{}( T * ); } )
     82forall( dtype T | { void ^?{}(T *); } )                                 // delete
    4883void delete( T * ptr ) {
    4984        if ( ptr ) {
    50                 ^ptr{};                                                                                 // run destructor
     85                ^ptr{};
    5186                free( ptr );
    52         } // if
     87        }
    5388} // delete
    5489
    55 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
     90forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } )
    5691void delete( T * ptr, Params rest ) {
    5792        if ( ptr ) {
    58                 ^ptr{};                                                                                 // run destructor
     93                ^ptr{};
    5994                free( ptr );
    60         } // if
     95        }
    6196        delete( rest );
    6297} // delete
     
    207242
    208243forall( otype T | { int ?<?( T, T ); } )
    209 T * bsearch( T key, const T * arr, size_t dim ) {
     244T * bsearch( T key, const T * arr, size_t dimension ) {
    210245        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    211         return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
     246        return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
    212247} // bsearch
    213248
    214249forall( otype T | { int ?<?( T, T ); } )
    215 unsigned int bsearch( T key, const T * arr, size_t dim ) {
    216         T *result = bsearch( key, arr, dim );
    217         return result ? result - arr : dim;                                     // pointer subtraction includes sizeof(T)
     250unsigned int bsearch( T key, const T * arr, size_t dimension ) {
     251        T *result = bsearch( key, arr, dimension );
     252        return result ? result - arr : dimension;                       // pointer subtraction includes sizeof(T)
    218253} // bsearch
    219254
    220255forall( otype T | { int ?<?( T, T ); } )
    221 void qsort( const T * arr, size_t dim ) {
     256void qsort( const T * arr, size_t dimension ) {
    222257        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    223         qsort( arr, dim, sizeof(T), comp );
     258        qsort( arr, dimension, sizeof(T), comp );
    224259} // qsort
    225260
Note: See TracChangeset for help on using the changeset viewer.