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. C/C++ 多继承{虚基类,虚继承,构造顺序,析构顺序}

    C/C++:一个基类继承和多个基类继承的区别 1.对多个基类继承会出现类之间嵌套时出现的同名问题,如果同名变量或者函数出现不在同一层次,则底层派生隐藏外层比如继承基类的同名变量和函数,不会出现二义性, ...

  2. python抓取

    我要抓取奥巴马每周的演讲内容http://www.putclub.com/html/radio/VOA/presidentspeech/index.html 如果手动提取,就需要一个个点进去,再复制保 ...

  3. [python]python安装包错误

    “UnicodeDecodeError: ‘ascii’ codec can’t decode : ordinal not )” 在windows XP上 解决方法: Solution: ====== ...

  4. Ansible1: 简介与基本安装

    目录 Ansible特性 Ansible的基本组件 Ansible工作机制 Ansible的安装 Ansible是一个综合的强大的管理工具,他可以对多台主机安装操作系统,并为这些主机安装不同的应用程序 ...

  5. OpenStack 认证服务 KeyStone连接和用户管理(四)

    连接keystone两种方式: 一种使用命令 一种使用环境变量 1.通过环境变量方式连接keystone(适合在初始化场景使用) 配置认证令牌环境变量 export OS_TOKEN=07081849 ...

  6. Java基础-字符串连接运算符String link operator

    Java基础-字符串连接运算符String link operator 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 字符串链接运算符是通过“+”进行拼接的. 一.使用“+”进行字 ...

  7. include动作和include指令的区别

    1. include指令在被导入页面时,会与原有的jsp代码完全融合,共同生成同一个Servlet:而include动作则会在原有的jsp代码使用include方法而被导入页面,所以includ指令在 ...

  8. IOS的__bridge

    使用 __bridge 关键字来实现id类型与void*类型的相互转换.看下面的例子. id obj = [[NSObject alloc] init]; void *p = (__bridge vo ...

  9. hdu1286 找新朋友

    找新朋友 http://acm.hdu.edu.cn/showproblem.php?pid=1286 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  10. 【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一. 目标 使用Redis做分布式缓存:使用lua API来访问redis缓存:使用nginx向客户端提 ...