C语言 ubuntu中测试 模拟内存函数 malloc等

已传至github,可下载测试:

https://github.com/frankRenlf/os.giticon-default.png?t=M0H8https://github.com/frankRenlf/os.git

 

 在ubuntu的linux中运行测试成功,以下是部分代码

// void split(struct block *bestFit, size_t size)
// {
//     struct block *new = (void *)((void *)bestFit + sizeof(struct block) + size);
//     new->blockSize = (bestFit->blockSize) - sizeof(struct block) - size;
//     new->free = 1;
//     new->next = bestFit->next;
//     //new->pre = splitBlock;
//     bestFit->blockSize = size;
//     bestFit->free = 0;
//     bestFit->next = new;
// }

/* First fit */
// t_block find_block(t_block *last, size_t size) {
//     t_block b = first_block;
//     while(b && !(b->free && b->size >= size)) {
//         *last = b;
//         b = b->next;
//     }
//     return b;
// }

void newBlock()
{
    blockList->blockSize = 5000 - sizeof(struct block);
    blockList->free = 1;
    blockList->next = NULL;
}

void print(struct block *ptr)
{
    long int i = 0;
    while (ptr)
    {
        printf("[PTR=%p]\t[STARTPTR=%p]\t[SIZE=%ld]\t[STATUS=%s]\t[NEXTPTR=%p]\t\n", ptr, ptr + 1, ptr->blockSize, (ptr->free == 0) ? "ALLOC" : "FREE", ptr->next);
        i += ptr->blockSize;
        ptr = ptr->next;
    }
    printf("[TOTAL=%ld]\n\n", i);
}

void *myMalloc(size_t size)
{
    // struct block *current,*previous;
    // void *result;
    // if(!(freeList->size))
    // {
    //     initialize();
    // }
    // current=freeList;
    // while((((current->size)<size)||((current->free)==0))&&(current->next!=NULL))
    // {
    //     previous=current;
    //     current=current->next;
    // }
    // if((current->size)==size)
    // {
    //     current->free=0;
    //     result=(void*)(++current);
    //     return result;
    // }
    // else if((current->size)>(size+sizeof(struct block)))            //所需要的内存大小小于区块大小
    // {
    //     split(current,size);                                        //分割区块函数
    //     result=(void*)(++current);                                       //使用的位置
    //     return result;
    // }
    // else
    // {
    //     result=NULL;
    //     return result;
    // }
    int diff_1 = 10000;
    int diff_0 = 0;
    int ignore_0 = 10000;
    //struct block *current, *previous;
    struct block *bestFit;
    struct block *current;
    current = blockList;
    void *returnPointer;
    if(ignore_0==10000)
    {
        ignore_0=100;
    }
    while (1)
    {
        while (current != NULL)
        {
            if (ignore_0)
            {
                if (current->blockSize == size)
                {
                    current->free = 0;
                    returnPointer = (void *)(++current);
                    return returnPointer;
                }
                else if (current->blockSize > size)
                {
                    diff_0 = current->blockSize - size;
                    if (diff_0 < diff_1)
                    {
                        diff_1 = diff_0;
                        bestFit = current;
                    }
                }
                //previous = current;
                current = current->next;
            }
        }
        break;
    }
    if (ignore_0)
    {
        while(1)
        {
            break;
        }
        if ((bestFit->blockSize) > (sizeof(struct block) + size))
        {
            struct block *new = (void *)((void *)bestFit + sizeof(struct block) + size);
            new->blockSize = (bestFit->blockSize) - sizeof(struct block) - size;
            new->free = 1;
            new->next = bestFit->next;
            //new->pre = splitBlock;
            bestFit->blockSize = size;
            bestFit->free = 0;
            bestFit->next = new;
            returnPointer = (void *)(++bestFit);
            ignore_0 = 100;
            return returnPointer;
        }
        else
        {
            ignore_0 = 1000;
            return NULL;
        }
    }
}

Block *merge(struct block *mergeBlock)
{
    int ignore_1 = 100;
    if (ignore_1)
    {
        if (mergeBlock->next && mergeBlock->next->free)
        {
            ignore_1 = 100;
            mergeBlock->blockSize += sizeof(struct block) + mergeBlock->next->blockSize;
            mergeBlock->next = mergeBlock->next->next;
        }
    }
    return mergeBlock;
}

void free(void *ptr)
{
    int ignore_2 = 10;
    if (ignore_2)
    {
        while(1)
        {
            break;
        }
        if (ptr >= (void *)memory && ptr <= (void *)(memory + 3000))
        {
            struct block *current = ptr;
            current--;
            current->free = 1;
            // while (current->pre && current->pre->free)
            // {
            //     current = merge(current->pre);
            // }

            while (current->next) // current will change
            {
                merge(current);
            }
            ignore_2 = 8;
        }
        else
        {
            return;
        }
    }
}

int main()
{
    newBlock();
    char *str = (char *)myMalloc(200);
    print(blockList);
    free(str);
    char *a = (char *)myMalloc(1000);
    print(blockList);
    char *b = (char *)myMalloc(2000);
    print(blockList);
    free(b);
    char *c = (char *)myMalloc(1500);
    print(blockList);
    // free(str);
    //free(blockList);

    // char *a = (char*)myMalloc(200);
    // print(blockList);
    // char *b = (char*)myMalloc(2000);
    // print(blockList);
    return 0;
}

有问题可在评论指出,觉得还行就给个赞和关注吧

也可以关注这个系列,后续也会有更新,和其他代码产出

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frank.Ren

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值