# umm_malloc - Memory Manager For Small(ish) Microprocessors
This is a memory management library specifically designed to work with the
ARM7 embedded processor, but it should work on many other 32 bit processors,
as well as 16 and 8 bit devices.
You can even use it on a bigger project where a single process might want
to manage a large number of smaller objects, and using the system heap
might get expensive.
## Acknowledgements
Joerg Wunsch and the avr-libc provided the first malloc() implementation
that I examined in detail.
http://www.nongnu.org/avr-libc
Doug Lea's paper on malloc() was another excellent reference and provides
a lot of detail on advanced memory management techniques such as binning.
http://g.oswego.edu/dl/html/malloc.html
Bill Dittman provided excellent suggestions, including macros to support
using these functions in critical sections, and for optimizing realloc()
further by checking to see if the previous block was free and could be
used for the new block size. This can help to reduce heap fragmentation
significantly.
Yaniv Ankin suggested that a way to dump the current heap condition
might be useful. I combined this with an idea from plarroy to also
allow checking a free pointer to make sure it's valid.
## Background
The memory manager assumes the following things:
1. The standard POSIX compliant malloc/realloc/free semantics are used
1. All memory used by the manager is allocated at link time, it is aligned
on a 32 bit boundary, it is contiguous, and its extent (start and end
address) is filled in by the linker.
1. All memory used by the manager is initialized to 0 as part of the
runtime startup routine. No other initialization is required.
The fastest linked list implementations use doubly linked lists so that
its possible to insert and delete blocks in constant time. This memory
manager keeps track of both free and used blocks in a doubly linked list.
Most memory managers use some kind of list structure made up of pointers
to keep track of used - and sometimes free - blocks of memory. In an
embedded system, this can get pretty expensive as each pointer can use
up to 32 bits.
In most embedded systems there is no need for managing large blocks
of memory dynamically, so a full 32 bit pointer based data structure
for the free and used block lists is wasteful. A block of memory on
the free list would use 16 bytes just for the pointers!
This memory management library sees the malloc heap as an array of blocks,
and uses block numbers to keep track of locations. The block numbers are
15 bits - which allows for up to 32767 blocks of memory. The high order
bit marks a block as being either free or in use, which will be explained
later.
The result is that a block of memory on the free list uses just 8 bytes
instead of 16.
In fact, we go even one step futher when we realize that the free block
index values are available to store data when the block is allocated.
The overhead of an allocated block is therefore just 4 bytes.
Each memory block holds 8 bytes, and there are up to 32767 blocks
available, for about 256K of heap space. If that's not enough, you
can always add more data bytes to the body of the memory block
at the expense of free block size overhead.
There are a lot of little features and optimizations in this memory
management system that makes it especially suited to small embedded, but
the best way to appreciate them is to review the data structures and
algorithms used, so let's get started.
## Detailed Description
We have a general notation for a block that we'll use to describe the
different scenarios that our memory allocation algorithm must deal with:
```
+----+----+----+----+
c |* n | p | nf | pf |
+----+----+----+----+
```
Where:
- c is the index of this block
- * is the indicator for a free block
- n is the index of the next block in the heap
- p is the index of the previous block in the heap
- nf is the index of the next block in the free list
- pf is the index of the previous block in the free list
The fact that we have forward and backward links in the block descriptors
means that malloc() and free() operations can be very fast. It's easy
to either allocate the whole free item to a new block or to allocate part
of the free item and leave the rest on the free list without traversing
the list from front to back first.
The entire block of memory used by the heap is assumed to be initialized
to 0. The very first block in the heap is special - it't the head of the
free block list. It is never assimilated with a free block (more on this
later).
Once a block has been allocated to the application, it looks like this:
```
+----+----+----+----+
c | n | p | ... |
+----+----+----+----+
```
Where:
- c is the index of this block
- n is the index of the next block in the heap
- p is the index of the previous block in the heap
Note that the free list information is gone, because it's now being used to
store actual data for the application. It would have been nice to store
the next and previous free list indexes as well, but that would be a waste
of space. If we had even 500 items in use, that would be 2,000 bytes for
free list information. We simply can't afford to waste that much.
The address of the `...` area is what is returned to the application
for data storage.
The following sections describe the scenarios encountered during the
operation of the library. There are two additional notation conventions:
`??` inside a pointer block means that the data is irrelevant. We don't care
about it because we don't read or modify it in the scenario being
described.
`...` between memory blocks indicates zero or more additional blocks are
allocated for use by the upper block.
And while we're talking about "upper" and "lower" blocks, we should make
a comment about adresses. In the diagrams, a block higher up in the
picture is at a lower address. And the blocks grow downwards their
block index increases as does their physical address.
Finally, there's one very important characteristic of the individual
blocks that make up the heap - there can never be two consecutive free
memory blocks, but there can be consecutive used memory blocks.
The reason is that we always want to have a short free list of the
largest possible block sizes. By always assimilating a newly freed block
with adjacent free blocks, we maximize the size of each free memory area.
### Operation of malloc right after system startup
As part of the system startup code, all of the heap has been cleared.
During the very first malloc operation, we start traversing the free list
starting at index 0. The index of the next free block is 0, which means
we're at the end of the list!
At this point, the malloc has a special test that checks if the current
block index is 0, which it is. This special case initializes the free
list to point at block index 1.
```
BEFORE AFTER
+----+----+----+----+ +----+----+----+----+
0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
+----+----+----+----+ +----+----+----+----+
+----+----+----+----+
1 | 0 | 0 | 0 | 0 |
+----+----+----+----+
```
The heap is now ready to complete the first malloc operation.
### Operation of malloc when we have reached the end of the free list and
there is no block large enough to accommodate the request.
This happens at the very first malloc operation, or any time the free
list is traversed and no free block large enough for the request is
found.
The current block pointer will be at the end of the free list, and we
know we're at the end of the list because the nf index is 0, like this:
```
BEFORE AFTER
+----+----+----+----+ +----+----+----+----+
pf |*?? | ?? | cf | ?? | pf |*?? | ?? | lf | ?? |
+----+----+---
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
arduino-esp8266 WiFi探针代码源码 (1140个子文件)
libstdc++.a 8.95MB
libc.a 4.84MB
libg.a 4.84MB
libbearssl.a 4.83MB
libm.a 2.16MB
libc_orig.a 1.48MB
liblwip2-1460-feat.a 1.4MB
liblwip2-536-feat.a 1.4MB
liblwip2-1460.a 1.32MB
liblwip2-536.a 1.32MB
liblwip_gcc.a 1.05MB
libaxtls.a 1MB
libgcc.a 587KB
libwpa2.a 464KB
libhal.a 341KB
libnet80211.a 328KB
libwps.a 316KB
libpp.a 238KB
libmain.a 209KB
libwpa.a 169KB
libphy.a 153KB
libcrypto.a 132KB
libsmartconfig.a 116KB
libespnow.a 70KB
libdriver.a 66KB
libairkiss.a 11KB
README.adoc 1KB
README.adoc 898B
AUTHORS 166B
esp8266-arduino-doc.bash 3KB
test.bmp 225KB
hibiscus.bmp 225KB
flower.BMP 225KB
spiffs_nucleus.c 86KB
sockets.c 67KB
dhcp.c 66KB
tcp_in.c 64KB
umm_malloc.c 59KB
tcp.c 54KB
tcp_out.c 51KB
etharp.c 51KB
espconn_tcp.c 48KB
api_msg.c 44KB
espconn.c 44KB
spiffs_check.c 44KB
spiffs_hydrogen.c 41KB
pbuf.c 40KB
dhcpserver.c 38KB
sntp.c 34KB
udp.c 33KB
ip.c 32KB
dns.c 31KB
mdns.c 30KB
ip_frag.c 28KB
igmp.c 27KB
spiffs_gc.c 25KB
gdbstub.c 24KB
mem.c 22KB
api_lib.c 22KB
netif.c 22KB
core_esp8266_si2c.c 19KB
uart.c 18KB
autoip.c 18KB
memp.c 15KB
core_esp8266_i2s.c 14KB
espconn_udp.c 14KB
timers.c 14KB
inet_chksum.c 13KB
tcpip.c 13KB
init.c 13KB
icmp.c 12KB
netdb.c 11KB
sntp-lwip2.c 11KB
raw.c 11KB
core_esp8266_waveform.c 11KB
core_esp8266_phy.c 11KB
netio.c 10KB
md5.c 10KB
spiffs_cache.c 10KB
ping.c 10KB
ip_addr.c 9KB
core_esp8266_wiring_digital.c 8KB
core_esp8266_postmortem.c 8KB
core_esp8266_wiring.c 7KB
sha1.c 7KB
netbuf.c 7KB
core_esp8266_sigma_delta.c 7KB
stats.c 6KB
netifapi.c 5KB
espconn_mdns.c 5KB
hspi_slave.c 4KB
font.c 4KB
espconn_buf.c 4KB
heap.c 4KB
eboot.c 4KB
core_esp8266_noniso.c 4KB
cdecode.c 4KB
def.c 3KB
cencode.c 3KB
core_esp8266_timer.c 3KB
共 1140 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
- 馬里奧利奧2019-03-16骗人的.zis...
- JOKY_LBY2022-05-11不是标题的内容,没有用
- bibo.bibo2019-11-22乱七八糟,根本找不到在哪里编译
来搞事
- 粉丝: 11
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
- 修复Windows 10 LTSC 2021资源占用率高
- Hash工具,小巧绿色hash校验工具,免费hash工具
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 鸭子开车记(儿童绘本)
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功