亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? mem.c

?? 來源于外國的開源rtos,用于小型mcu,支持優先級搶占調度
?? C
字號:
/*
 *  Copyright (c) 2004, Jun Li, lj_sourceforge@users.sourceforge.net.
 *  All rights reserved. 
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. The name of the author may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission. 
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 *  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 *  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "types.h"
#include "mem.h"
#include "../../../inc/picoos.h"
#include <string.h>

#define ALIGN_SIZE      4

#define MEMBLOCKSIZE    ((sizeof(struct mem_block) & (ALIGN_SIZE - 1)) ? \
                        ((sizeof(struct mem_block) + ALIGN_SIZE) & ~(ALIGN_SIZE - 1)) : \
                        (sizeof(struct mem_block)))



/*  OS_MEM_Create()
        Called at system init time to set up heap for use. MUST be called
        before any calls to calloc1(). Takes a single contiguous memory space
        and sets it up to be used by calloc1() anbd mem_free().
    PARAM1: 
        char *base - address for start of heap area in memory
        long size    - size of heap area at address.
    RETURNS: 
        void
*/

void mempool_create(mempool_t *mem, char * base, u_int size)
{
    /* make sure the heap is aligned */
    if ((u_int)base & (ALIGN_SIZE-1))
    {
        base = (char*)(((u_int)base + (ALIGN_SIZE-1)) & ~(ALIGN_SIZE-1));
        size -= (ALIGN_SIZE-1);
    }

    mem->mheap_base = (struct mem_block *)base;
    mem->mh_free = (struct mem_block *)base;

    /* trim heap to multiple of ALIGN_SIZE */
    size &= ~(ALIGN_SIZE-1);

    /* start with no free space (we will add to this) */
    mem->mh_totfree = 0;

    mem->mh_free->size = (unsigned)size - MEMBLOCKSIZE;
    mem->mh_totfree += mem->mh_free->size;
    mem->mh_free->next = NULL;

    /* set starting and minimum free space from the just-initialized total */
    mem->mh_startfree = mem->mh_minfree = mem->mh_totfree;
}



/*  OS_MEM_Alloc
        Similar to standard malloc()
    PARAM1
        unsigned size
    RETURNS
        pointer to memif OK, else NULL
*/

void *mempool_alloc(mempool_t *mem, unsigned size)
{
    struct mem_block *bp;
    struct mem_block *newb;
    struct mem_block *lastb;
    unsigned    int     lostsize;           /* size of data block plus struct */

    osCriticalDeclare();

    osEnterCritical();

    /* increase requested size enough to ensure future alignment */
    if ((long)size & (ALIGN_SIZE-1))
        size = (size + ALIGN_SIZE) & ~(ALIGN_SIZE-1);

    lostsize = size + MEMBLOCKSIZE;     /* size we will take from heap */

    bp = mem->mh_free;                      /* init vars for list search */
    lastb = NULL;
    while (bp)
    {
        if (bp->size >= size)               /* take first-fit free block */
        {
            /* Decide if the block is big enough to be worth dividing */
            if (bp->size > (size + (MEMBLOCKSIZE * 2)))
            {
                /* Divide block and return front part to caller. First make a new block after the portion we will return    */
                newb = (struct mem_block *)((char*)(bp) + lostsize);
                newb->size = bp->size - lostsize;
                newb->next = bp->next;

                /* modify bp to reflect smaller size we will return */
                bp->next = newb;
                bp->size = size;
            }
            else                    /* not worth fragmenting block, return whole thing */
            {
                /* adjust lostsize */
                lostsize = bp->size + MEMBLOCKSIZE;       
            }
            
            if (lastb)   /* unlink block from queue */
                lastb->next = bp->next;
            else
                mem->mh_free = bp->next;

            /* keep statistics */
            mem->mh_totfree -= lostsize;
            if (mem->mh_totfree < mem->mh_minfree)
                mem->mh_minfree = mem->mh_totfree;
            bp->next = mem->mheap_base;     /* tag next ptr with illegal value */

            osExitCritical();
            return  (void *)((char*)(bp) + MEMBLOCKSIZE);
        }
        lastb = bp;
        bp = bp->next;
    }
    
    osExitCritical();
    mem->mh_failed++;               /* count out of memory conditions */
    
    return NULL;                /* failure return - no memory */
}


/*  OS_MEM_Free
        Find block which contains buf and insert it in free 
        list. Maintains list in order, low to high memory. 
    PARAM1
        char * buf - buffer to add to free list.
    RETURNS
        void
*/
void    mempool_free(mempool_t *mem, void* buf)
{
    struct mem_block    *freep;
    struct mem_block    *tmp;
    struct mem_block    *last;
    osCriticalDeclare();
    int                 merged = 0;             /* indicates freep not merged into free list */

    /* find pointer to prepended mem_block struct */
    freep = (struct mem_block*)((unsigned int)buf - MEMBLOCKSIZE);
    osEnterCritical();
    if (freep->next != mem->mheap_base)      /* sanity check next ptr for tag */
    {
        osExitCritical();
        return;
    }

    mem->mh_totfree += ((unsigned long)freep->size + MEMBLOCKSIZE);

    last = NULL;
    for (tmp = mem->mh_free; tmp; tmp = tmp->next)
    {
        if (freep < tmp)  /* found slot to insert freep */
        {
            /* see if we can merge with next block */
            if (((char*)freep + freep->size + MEMBLOCKSIZE) == (char*)tmp)
            {
                freep->next = tmp->next;
                freep->size += (tmp->size + MEMBLOCKSIZE);
                if (last)
                    last->next = freep;
                else
                    mem->mh_free = freep;
                merged++;
            }

            /* ...and see if we can merge with previous block */
            if (last && (((char*)last + last->size + MEMBLOCKSIZE) == (char*)freep))
            {
                last->size += (freep->size + MEMBLOCKSIZE);
                if (merged) /* if already merged, preserve next ptr */
                {
                    last->next = freep->next;
                }
                merged++;
            }

            /* if didn't merge with either adjacent block, insert into list */
            if (!merged)   
            {
                if (last)
                {
                    freep->next = last->next;
                    last->next = freep;
                }
                else     /* no last, put at head of list */
                {
                    freep->next = mem->mh_free;
                    mem->mh_free = freep;
                }
                mem->mh_totfree -= MEMBLOCKSIZE;   /* we didn't get a header back */
            }

            osExitCritical();
            return;
        }
        
        last = tmp;       /* set "last" pointer for next loop */
    }
    
    /* got to end of list without finding slot for freep; add to end */
    if (last)
    {
        /* See if we can merge it with last block */
        if (((char*)last + last->size + MEMBLOCKSIZE) == (char*)freep)
        {
            last->size += (freep->size + MEMBLOCKSIZE);
        }
        else
        {
            freep->next = last->next;
            last->next = freep;
        }
    }
    else     /* there was no free list */
    {
        mem->mh_free = freep;
        freep->next = NULL;
    }

    osExitCritical();
}

void memblock_create(blockmem_t *block, void *base, int n_block, int blocksize)
{
    block->base = base;
    block->n_block = n_block;
    block->blocksize = blocksize;
    memset(block->used, 0, block->n_block+1);
}

void memblock_reset(blockmem_t *block)
{
    memset(block->used, 0, block->n_block+1);
}

void *memblock_alloc(blockmem_t *block)
{
    int index;
    osCriticalDeclare();
    
    if (!block)
        return NULL;
    osEnterCritical();
    index = strlen(block->used);
    if (index >= block->n_block)
    {
        osExitCritical();
        return NULL;
    }
    block->used[index] = '1';
    osExitCritical();
    return (void *)((u_int)block->base+index*block->blocksize);
}

int memblock_free(blockmem_t *block, void *ptr)
{
    int index;
    if ((u_int)ptr < (u_int)block->base)
        return 0;
    index = ((u_int)ptr - (u_int)block->base)/block->blocksize;
    if (index >= block->n_block)
        return 0;
    block->used[index] = 0;
    return 1;
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲另类在线视频| 欧美国产日本韩| 欧美日韩一本到| 色综合天天狠狠| 91在线视频播放| 在线亚洲精品福利网址导航| 欧美曰成人黄网| 色吊一区二区三区| 欧美日韩美少妇| 欧美日韩视频在线观看一区二区三区 | 久久av资源站| 国产一区二区在线看| 国产91色综合久久免费分享| 夫妻av一区二区| 91国产福利在线| 7777精品伊人久久久大香线蕉完整版 | 免费在线欧美视频| 欧美96一区二区免费视频| 久久精品噜噜噜成人88aⅴ| 国产麻豆精品一区二区| 成人av在线影院| 欧美日韩日日摸| 久久奇米777| 亚洲激情图片qvod| 日韩av中文字幕一区二区| 国产乱子伦视频一区二区三区 | 一区精品在线播放| 亚洲不卡av一区二区三区| 国产一区二区免费在线| av不卡在线播放| 3d动漫精品啪啪| 国产午夜一区二区三区| 亚洲精品中文字幕在线观看| 男男视频亚洲欧美| 国产乱码一区二区三区| 欧美在线免费视屏| 精品美女被调教视频大全网站| 中文字幕高清一区| 日本一不卡视频| 成人福利电影精品一区二区在线观看 | 麻豆91精品视频| 91视视频在线观看入口直接观看www | 亚洲激情一二三区| 久草热8精品视频在线观看| 99国产精品99久久久久久| 日韩欧美一区中文| 亚洲男人的天堂在线观看| 久久99精品久久只有精品| 色丁香久综合在线久综合在线观看| 精品国产成人系列| 亚洲线精品一区二区三区| 精品一区二区免费| 欧美午夜宅男影院| 日韩美女视频19| 国产一区二区伦理片| 欧美精品一卡二卡| 亚洲美女一区二区三区| 国产成人av电影在线| 日韩欧美一级在线播放| 一区二区三区资源| 不卡av在线免费观看| 亚洲精品在线免费播放| 美女一区二区三区在线观看| 欧美日韩小视频| 亚洲综合一区在线| 成人免费黄色大片| 久久精品免费在线观看| 精品午夜一区二区三区在线观看| 6080亚洲精品一区二区| 亚洲一区二区三区四区在线| 色婷婷综合在线| 亚洲另类一区二区| 在线观看亚洲精品| 亚洲一区免费观看| 欧美视频完全免费看| 亚洲夂夂婷婷色拍ww47| 一本高清dvd不卡在线观看| 中文字幕一区二区三区不卡在线 | 在线视频观看一区| 亚洲最新视频在线观看| 欧美性受xxxx黑人xyx| 一区二区三区日韩在线观看| 欧美日韩在线一区二区| 亚洲国产成人av网| 日韩午夜在线播放| 另类小说欧美激情| 欧美mv和日韩mv国产网站| 国产老肥熟一区二区三区| 久久精品一区二区三区av| 国产精品香蕉一区二区三区| 欧美国产精品劲爆| 色综合天天综合| 亚洲国产一区二区在线播放| 欧美精品黑人性xxxx| 久久国内精品自在自线400部| 久久色中文字幕| 91在线播放网址| 午夜精品一区二区三区免费视频| 日韩一区二区高清| 大陆成人av片| 亚洲午夜免费电影| 日韩欧美123| www.性欧美| 日韩精品一卡二卡三卡四卡无卡| 亚洲精品一区二区三区在线观看| 粉嫩一区二区三区在线看| 一二三区精品视频| 精品国产在天天线2019| 成人精品在线视频观看| 香港成人在线视频| 中文字幕av资源一区| 欧美日韩国产欧美日美国产精品| 精品一区二区在线观看| 国产精品国产精品国产专区不蜜 | 国产在线精品免费| 亚洲欧美日韩在线| 久久综合国产精品| 欧美日韩视频不卡| 99这里都是精品| 激情五月婷婷综合| 亚洲444eee在线观看| 中文字幕高清一区| 日韩免费看的电影| 91久久一区二区| 国产不卡视频在线播放| 人人爽香蕉精品| 夜夜亚洲天天久久| 国产精品第五页| 日韩欧美国产高清| 欧美狂野另类xxxxoooo| 色综合天天综合色综合av | 蜜桃精品视频在线| 亚洲一区日韩精品中文字幕| 国产精品青草久久| 国产亚洲欧美色| 精品精品国产高清a毛片牛牛| 欧美日韩综合色| 91香蕉视频mp4| 成人午夜激情视频| 国产麻豆成人精品| 国产综合色产在线精品| 男人的天堂久久精品| 亚洲18色成人| 天天爽夜夜爽夜夜爽精品视频| 一色桃子久久精品亚洲| 欧美—级在线免费片| 久久精品一区二区三区不卡| 精品国产乱子伦一区| 欧美成人精品3d动漫h| 欧美大片国产精品| 日韩欧美一区二区免费| 日韩一二在线观看| 精品少妇一区二区三区免费观看 | 亚洲欧美自拍偷拍| 国产精品久久久久久久久免费丝袜| 久久色.com| 日本一区二区三区四区在线视频| 久久女同精品一区二区| 精品福利一区二区三区免费视频| 欧美成人精品二区三区99精品| 亚洲精品一区二区三区99| 精品电影一区二区| 国产欧美一区视频| 国产精品久久久久久亚洲毛片| 亚洲欧美日韩国产手机在线| 一区二区在线电影| 亚洲成人免费视| 久久精品国产澳门| 丰满白嫩尤物一区二区| 99re成人在线| 欧美三级一区二区| 日韩欧美激情四射| 欧美国产成人精品| 亚洲女人小视频在线观看| 亚洲va在线va天堂| 美国毛片一区二区三区| 国产福利不卡视频| 972aa.com艺术欧美| 欧美精品高清视频| 久久这里只有精品视频网| 国产精品国产三级国产aⅴ无密码| 亚洲精品自拍动漫在线| 美腿丝袜在线亚洲一区| 成人国产电影网| 91麻豆精品国产91久久久| 国产欧美日韩在线观看| 亚洲激情图片小说视频| 美女被吸乳得到大胸91| 99精品久久免费看蜜臀剧情介绍| 欧美另类变人与禽xxxxx| 国产亚洲欧美日韩日本| 亚洲成人免费av| 成人小视频在线| 欧美日韩国产美| 国产精品卡一卡二卡三| 肉色丝袜一区二区| 99久久精品国产精品久久| 欧美成人bangbros| 亚洲一区二区在线视频| 成人免费高清视频|