⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yc_memalgo.c

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 C
字号:
/*
 * The young Library
 * Copyright (c) 2005 by Yang Huan(杨桓)

 * Permission to use, copy, modify, distribute and sell this software for any
 * purpose is hereby granted without fee, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and this
 * permission notice appear in supporting documentation.
 * The author make no representations about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 */

/******************************************************************************/
/******************************************************************************/
#include "yc_memalgo.h"

#ifdef __cplusplus
    namespace youngc {  extern "C" {
#endif
/******************************************************************************/
/******************************************************************************/

enum YOUNG_LIBRARY_MEMORY_FUNCTION_CONSTANT
{
    /* 循环展开的次数 */
    OUTSPREAD = 16,

    /* 最大余数,先将其按位取反,再与整数 N 按位与,
     * 可得最接近 N 的 OUTSPREAD 的倍数 */
    RESIDUE = OUTSPREAD - 1
};

#define  CYCLE_OUTSPREAD( expression ) \
         expression;  expression;  expression;  expression; \
         expression;  expression;  expression;  expression; \
         expression;  expression;  expression;  expression; \
         expression;  expression;  expression;  expression;

/******************************************************************************/
/******************************************************************************/

#ifndef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_STANDARD_MEMORY_FUNCTION__
    void* ylib_memcopy( void* dst, const void* src, size_t count )
    {
        ylib_byte_t *d, *s;
        ylib_word_t* dstword = (ylib_word_t*)dst;
        const ylib_word_t* srcword = (const ylib_word_t*)src;
        size_t tmp, word_count = count / sizeof(ylib_word_t);
        size_t word_outspread = word_count & ~((size_t)RESIDUE);

        /* 复制可被循环展开的机器字 */
        for( tmp = 0; tmp < word_outspread; tmp += OUTSPREAD )
        {
            CYCLE_OUTSPREAD( *dstword++ = *srcword++ )
        }

        /* 复制剩下的机器字 */
        for( ; tmp < word_count; ++tmp )
            *dstword++ = *srcword++;

        /* 复制剩下的字节 */
        d = (ylib_byte_t*)dstword;
        s = (ylib_byte_t*)srcword;
        for( tmp *= sizeof(ylib_word_t); tmp < count; ++tmp )
            *d++ = *s++;

        return dst;
    }

/******************************************************************************/

    void* ylib_memmove( void* dst, const void* src, size_t count )
    {
        ylib_byte_t *d_last, *s_last;
        ylib_word_t* dstword = (ylib_word_t*)((ylib_byte_t*)dst + count);
        const ylib_word_t* srcword = (ylib_word_t*)((ylib_byte_t*)src + count);
        size_t tmp, word_count = count / sizeof(ylib_word_t);
        size_t word_outspread = word_count & ~((size_t)RESIDUE);

        for( tmp = 0; tmp < word_outspread; tmp += OUTSPREAD )
        {
            CYCLE_OUTSPREAD( *--dstword = *--srcword )
        }

        for( ; tmp < word_count; ++tmp )
            *--dstword = *--srcword;

        d_last = (ylib_byte_t*)dstword;
        s_last = (ylib_byte_t*)srcword;
        for( tmp *= sizeof(ylib_word_t); tmp < count; ++tmp )
            *--d_last = *--s_last;

        return dst;
    }

/******************************************************************************/

    void* ylib_memset( void* dst, int c, size_t count )
    {
        ylib_byte_t* d = (ylib_byte_t*)dst;
        size_t tmp, n = count & ~((size_t)RESIDUE);

        for( tmp = 0; tmp < n; tmp += OUTSPREAD )
        {
            CYCLE_OUTSPREAD( *d++ = (ylib_byte_t)c )
        }

        for( ; n < count; ++n )
            *d++ = (ylib_byte_t)c;

        return dst;
    }
#endif  /* SUPPORT_STANDARD_MEMORY_FUNCTION */

/******************************************************************************/

wchar_t* ylib_wmemset( wchar_t* dst, wchar_t value, size_t count )
{
    size_t tmp, n = count & ~((size_t)RESIDUE);

    for( tmp = 0; tmp < n; tmp += OUTSPREAD )
    {
        CYCLE_OUTSPREAD( *dst++ = value )
    }

    for( ; n < count; ++n )
        *dst++ = value;

    return dst - count;
}

/******************************************************************************/

void* memfill( void* dst, const void* value, size_t count, size_t element_size )
{
    ylib_byte_t* d = (ylib_byte_t*)dst;

    if( sizeof(ylib_byte_t) == element_size || sizeof(wchar_t) == element_size )
    {
        size_t count_size = count * element_size;

        return sizeof(ylib_byte_t) == element_size
               ? ylib_memset( dst, *( (ylib_byte_t*)value ), count_size )
               : ylib_wmemset( (wchar_t*)dst, *( (wchar_t*)value ), count );
    }
    else
    {
        ylib_byte_t* v;
        ylib_word_t *valword, *dstword;
        size_t tmp, word = element_size / sizeof(ylib_word_t);

        for( ; count > 0; --count )
        {
            dstword = (ylib_word_t*)d;
            valword = (ylib_word_t*)value;
            for( tmp = 0; tmp < word; ++tmp )
                *dstword++ = *valword++;
            d = (ylib_byte_t*)dstword;
            v = (ylib_byte_t*)valword;
            for( tmp *= sizeof(ylib_word_t); tmp < element_size; ++tmp )
                *d++ = *v++;
        }
    }

    return d;
}

/******************************************************************************/

void memswap( void* left, void* right, size_t size )
{
    ylib_byte_t tmp;
    ylib_byte_t* a = (ylib_byte_t*)left;
    ylib_byte_t* b = (ylib_byte_t*)right;

    for( ; size > 0; --size,++a,++b )
    {
        tmp = *a;
        *a = *b;
        *b = tmp;
    }
}

/******************************************************************************/

void memreverse( void* ptr, size_t count, size_t element_size )
{
    size_t i, swap_count;
    ylib_byte_t tmp, *f, *b;
    ylib_byte_t* front = (ylib_byte_t*)ptr;
    ylib_byte_t* back = (ylib_byte_t*)ptr + (count - 1) * element_size;

    for( swap_count = count / 2; swap_count > 0; --swap_count )
    {
        f = front;
        b = back;
        for( i = element_size; i > 0; --i,++f,++b )
        {
            tmp = *f;
            *f = *b;
            *b = tmp;
        }
        front += element_size;
        back -= element_size;
    }
}

/******************************************************************************/
/******************************************************************************/
#ifdef __cplusplus
    }  }
#endif
/******************************************************************************/
/******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -