C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
void free(void *firstbyte)
{
    struct mem_control_block *mcb;
    /* Backup from the given pointer to find the
    * mem_control_block
    */
    mcb = firstbyte - sizeof(struct mem_control_block);
    /* Mark the block as being available */
    mcb->is_available = ;
    /* That's It! We're done. */
    return;
}

void *malloc(long numbytes)
{
    /* Holds where we are looking in memory */  
    void *current_location;
      /* This is the same as current_location, but cast to a* memory_control_block*/  
    struct mem_control_block *current_location_mcb;
      /* This is the memory location we will return. It will* be set to 0 until we find something suitable*/  
    void *memory_location;
      /* Initialize if we haven't already done so */  
    if(!has_initialized)
    {
        malloc_init();
    }
    /* The memory we search for has to include the memory* control block,
    but the users of malloc don't need* to know this,
    so we'll just add it in for them.*/  
    numbytes = numbytes + sizeof(struct mem_control_block);

/* Set memory_location to 0 until we find a suitable* location*/  
    memory_location = ;

/* Begin searching at the start of managed memory */  
    current_location = managed_memory_start;

/* Keep going until we have searched all allocated space */  
    while(current_location != last_valid_address)      
    {
        /* current_location and current_location_mcb point* to the same address.
        However, current_location_mcb* is of the correct type, so we can use it as a struct.
        * current_location is a void pointer so we can use it* to calculate addresses.*/      
        current_location_mcb = (struct mem_control_block *)current_location;

if(current_location_mcb->is_available)          
        {
            if(current_location_mcb->size >= numbytes)              
            {
                /* Woohoo! We've found an open,* appropriately-size location.*/
                /* It is no longer available */
                current_location_mcb->is_available = ;

/* We own it */
                memory_location = current_location;

/* Leave the loop */
                break;
                  
            }          
        }      
        /* If we made it here, it's because the Current memory* block not suitable; move to the next one*/      
        current_location = current_location + current_location_mcb->size;
    }  

/* If we still don't have a valid location, we'll* have to ask the operating system for more memory*/  
    if(! memory_location)      
    {
        /* Move the program break numbytes further */
        sbrk(numbytes);
        /* The new memory will be where the last valid* address left off*/      
        memory_location = last_valid_address;
        /* We'll move the last valid address forward* numbytes*/
        last_valid_address = last_valid_address + numbytes;
        /* We need to initialize the mem_control_block */      
        current_location_mcb = memory_location;
        current_location_mcb->is_available = ;
        current_location_mcb->size = numbytes;
    }  
    /* Now, no matter what (well, except for error conditions),* memory_location has the address of the memory, including* the mem_control_block*/  
    /* Move the pointer past the mem_control_block */  
    memory_location = memory_location + sizeof(struct mem_control_block);
    
    /* Return the pointer */
    return memory_location;
}

malloc和free的实现的更多相关文章

  1. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  2. C 语言中 malloc、calloc、realloc 和free 函数的使用方法

    C标准函数库中,常见的堆上内存管理函数有malloc(), calloc(), recalloc(), free(). 之所以使用堆,是因为栈只能用来保存临时变量.局部变量和函数参数.在函数返回时,自 ...

  3. 以冒泡排序为例--malloc/free 重定向stdin stdout

    esort.c 代码如下,可关注下mallloc/free,freopen重定向的用法,排序为每轮将最小的数放在最前面: #include<stdio.h> #include<mal ...

  4. 内存动态分配之realloc(),malloc(),calloc()与new运算符

    1,malloc与free是C/C++的标准库函数,new/delete是C++的运算符,是C++面向对象的特征,它们都可用于申请动态内存和释放内存.2,对于非内部数据类型的对象而言,光用maloc/ ...

  5. 在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。

    写了个程序,在DLL中用malloc分配了一块内存,但是在exe程序中释放,结果程序crash,原因就是:其原因可能是堆被损坏,这也说明 TestMySticker.exe 中或它所加载的任何 DLL ...

  6. Linux C 堆内存管理函数malloc()、calloc()、realloc()、free()详解

    C 编程中,经常需要操作的内存可分为下面几个类别: 堆栈区(stack):由编译器自动分配与释放,存放函数的参数值,局部变量,临时变量等等,它们获取的方式都是由编译器自动执行的 堆区(heap):一般 ...

  7. malloc与new的区别

    1.new是运算符,而malloc是库函数 2.new可以重载,可以自定义内存分配策略,甚至不做内存分配,甚至分配到非内存设备上.而malloc不能. 3.new在用于定义一个新的非内部对象的时候,默 ...

  8. new 等于 malloc加构造函数

    1.new 是c++中的操作符,malloc是c 中的一个函数 2.new 不止是分配内存,而且会调用类的构造函数,同理delete会调用类的析构函数,而malloc则只分配内存,不会进行初始化类成员 ...

  9. 关于malloc函数的动态分配问题

    malloc函数动态分配了一个整型的内存空间,让abc都指向刚申请的空间,所以只有最后一个赋值语句的值保留在了空间里 #include<stdio.h> main() { int *a,* ...

  10. 转:如何实现一个malloc

    如何实现一个malloc 转载后排版效果很差,看原文!   任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉. ...

随机推荐

  1. 关于maven工程打jar的问题

    今天对maven做一些整理,更好的理了下思路: 这个篇博客介绍的还是很详细的: http://www.cnblogs.com/haippy/archive/2012/07/04/2576453.htm ...

  2. UML类图与类间六种关系表示

    UML类图与类间六种关系表示 1.类与类图 类封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性,操作,关系的对象集合的总称. 类图是使用频率最高的UML图之一. 类图用于描述系统中所包含的 ...

  3. awk 脚本同时解析多个文件

    ARGC        The number of command line arguments (does not include                   options to gawk ...

  4. shopt

    本文出自 “Mr_Computer” 博客,请务必保留此出处 Bash Shell有个extglob选项,开启之后Shell可以另外识别出5个模式匹配操作符,能使文件匹配更加方便. 开启方法很简单,使 ...

  5. 安装并使用 Wowza 发布你的 RTMP 直播流

    转载自:http://blog.csdn.net/defonds/article/details/11979095 I. 下载 Wowza         官方下载地址 http://www.wowz ...

  6. JAVA-JSP隐式对象

    JSP隐式对象 在本章中,我们将讨论和学习JSP中的隐式对象.这些对象是JSP容器为每个页面中的开发人员提供的Java对象,开发人员可以直接调用它们而不用显式地声明它们再调用. JSP隐式对象也称为预 ...

  7. [Luogu 3952] NOIP2017 时间复杂度

    [Luogu 3952] NOIP2017 时间复杂度 一年的时间说长不长,说短,也不短. 一年之内无数次觉得难得可怕的题目,原来也就模拟这么回事儿. #include <cstdio> ...

  8. Javascript实现返回上一页面并刷新

    今天写了一个小小的提示成功的页面,同时要求返回上一页面,并实现对上一页面的操作进行刷新(例如删除的,添加的),在网上搜寻了一遍,基本上90%的都是说的是用window.history.go(-1), ...

  9. 【BZOJ】3160: 万径人踪灭 FFT+回文串

    [题意]给定只含'a'和'b'字符串S,求不全连续的回文子序列数.n<=10^5. [算法]FFT+回文串 [题解]不全连续的回文子序列数=回文子序列总数-回文子串数. 回文子串数可以用回文串算 ...

  10. 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分

    [题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...