的malloc在嵌入式系统中没有一个操作系统 [英] malloc in an embedded system without an operating system

查看:513
本文介绍了的malloc在嵌入式系统中没有一个操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询使用关于内存分配的malloc

This query is regarding allocation of memory using malloc.

通常我们说的是的malloc 从堆中分配内存。

Generally what we say is malloc allocates memory from heap.

现在说我有一个普通的嵌入式系统(无操作系统),我有正常的程序加载在那里我做的malloc 在我的计划。

Now say I have a plain embedded system(No operating system), I have normal program loaded where I do malloc in my program.

在此情况下是从内存?分配

In this case where is the memory allocated from ?

推荐答案

的malloc()是通常由运行时库实现的功能。你是对的,如果你是在一个操作系统上运行,那么的malloc有时(但不是每次)触发一个系统调用,使操作系统映射一些内存到你的程序的地址空间。

malloc() is a function that is usually implemented by the runtime-library. You are right, if you are running on top of an operating system, then malloc will sometimes (but not every time) trigger a system-call that makes the OS map some memory into your program's address space.

如果你的程序没有操作系统上运行,那么你可以把你的程序为的操作系统。您可以访问所有地址,这意味着你可以直接将地址分配给一个指针,然后去引用该指针读/写。

If your program runs without an operating system, then you can think of your program as being the operating system. You have access to all addresses, meaning you can just assign an address to a pointer, then de-reference that pointer to read/write.

当然,你必须确保你的程序不是其他部分只是使用相同的内存,让你写你自己的内存管理器:

Of course you have to make sure that not other parts of your program just use the same memory, so you write your own memory-manager:

要简单地说,你可以设置预留的地址范围,你的内存管理器用来存储这些地址范围是已在使用(储存在那里可以为数据结构容易链表或许多复杂得多)。然后,你会写一个函数,例如调用它的malloc()形成你的内存管理器的功能部分。它看起来进入提到数据结构来寻找范围的地址是只要参数指定,并返回一个指向它。

To put it simply you can set-aside a range of addresses which your "memory-manager" uses to store which address-ranges are already in use (the datastructures stored in there can be as easy as a linked list or much much more complex). Then you will write a function and call it e.g. malloc() which forms the functional part of your memory-manager. It looks into the mentioned datastructure to find an address of ranges that is as long as the argument specifies and return a pointer to it.

现在,如果程序中的每个函数调用你的malloc(),而不是随机写入自定义地址你所做的第一步。你可以写一个免费() - 函数,将寻找它在提到数据结构中给出的指针,并调整数据结构(在天真的链表,将合并两个链接)。

Now, if every function in your program calls your malloc() instead of randomly writing into custom addresses you've done the first step. You can write a free()-function which will look for the pointer it is given in the mentioned datastructure, and adapts the datastructure (in the naive linked-list it would merge two links).

这篇关于的malloc在嵌入式系统中没有一个操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆