FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/memory.c
/* Used to calculate memory size for optimum use with buckets.
   returns the suggested size rounded down to the nearest bucket. */
Used to calculate memory size for optimum use with buckets.
returns the suggested size rounded down to the nearest bucket.
Used to calculate memory size for optimum use with buckets . returns the suggested size rounded down to the nearest bucket .
int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag)
{
    word32 bucketSz[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_BUCKETS};
    word32 distList[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_DIST};
    word32 ava = sz;
    byte*  pt  = buffer;
    word32 memSz = (word32)sizeof(wc_Memory);
    word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
    WOLFSSL_ENTER("wolfSSL_static_size");
    if (buffer == NULL) {
        return BAD_FUNC_ARG;
    }
    while ((wolfssl_word)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
        pt++;
        ava--;
    }
    if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
        if (ava < (memSz + padSz + WOLFMEM_IO_SZ)) {
            return 0; 
        }
        ava = ava % (memSz + padSz + WOLFMEM_IO_SZ);
    }
    else {
        int i, k;
        if (ava < (bucketSz[0] + padSz + memSz)) {
            return 0; 
        }
        while ((ava >= (bucketSz[0] + padSz + memSz)) && (ava > 0)) {
            for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
                for (k = distList[i]; k > 0; k--) {
                    if ((bucketSz[i] + padSz + memSz) <= ava) {
                        ava -= bucketSz[i] + padSz + memSz;
                    }
                }
            }
        }
    }
    return sz - ava; 
}
int wolfSSL_StaticBufferSz ( byte * buffer , word32 sz , int flag ) { word32 bucketSz [ WOLFMEM_MAX_BUCKETS ] = { WOLFMEM_BUCKETS } ; word32 distList [ WOLFMEM_MAX_BUCKETS ] = { WOLFMEM_DIST } ; word32 ava = sz ; byte * pt = buffer ; word32 memSz = ( word32 ) sizeof ( wc_Memory ) ; word32 padSz = - ( int ) memSz & ( WOLFSSL_STATIC_ALIGN - 1 ) ; WOLFSSL_ENTER ( " " ) ; if ( buffer == NULL ) { return BAD_FUNC_ARG ; } while ( ( wolfssl_word ) pt % WOLFSSL_STATIC_ALIGN && pt < ( buffer + sz ) ) { pt ++ ; ava -- ; } if ( flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED ) { if ( ava < ( memSz + padSz + WOLFMEM_IO_SZ ) ) { return 0 ; } ava = ava % ( memSz + padSz + WOLFMEM_IO_SZ ) ; } else { int i , k ; if ( ava < ( bucketSz [ 0 ] + padSz + memSz ) ) { return 0 ; } while ( ( ava >= ( bucketSz [ 0 ] + padSz + memSz ) ) && ( ava > 0 ) ) { for ( i = ( WOLFMEM_MAX_BUCKETS - 1 ) ; i >= 0 ; i -- ) { for ( k = distList [ i ] ; k > 0 ; k -- ) { if ( ( bucketSz [ i ] + padSz + memSz ) <= ava ) { ava -= bucketSz [ i ] + padSz + memSz ; } } } } } return sz - ava ; }
Used to calculate memory size for optimum use with buckets.
Used to calculate memory size for optimum use with buckets .
/* align pt */ /* creating only IO buffers from memory passed in, max TLS is 16k */ /* not enough room for even one bucket */ /* not enough room for even one bucket */ /* start at largest and move to smaller buckets */ /* round down */