首页
技术博客
PHP教程
数据库技术
前端开发
HTML5
Nginx
php论坛
新用户注册
|
会员登录
PHP教程
技术博客
编程问答
PNG素材
编程语言
前端技术
Android
PHP教程
HTML5教程
数据库
Linux技术
Nginx技术
PHP安全
WebSerer
职场攻略
JavaScript
开放平台
业界资讯
大话程序猿
登录
极速注册
取消
热门标签 | HotTags
email
case
format
sum
split
java
chat
usb
scala
import
dll
hashcode
md5
web3
bash
ip
solr
dockerfile
httpclient
audio
hash
tree
foreach
flutter
byte
header
loops
utf-8
heatmap
future
text
php8
数组
httprequest
expression
string
fetch
perl
uri
vbscript
substring
runtime
datetime
post
main
schema
c语言
window
bytecode
iostream
node.js
less
golang
testing
timestamp
default
uml
int
yaml
process
jar
range
controller
keyword
regex
cpython
stream
spring
javascript
plugins
bit
hashset
blob
php
frameworks
js
tags
eval
settings
当前位置:
开发笔记
>
编程语言
> 正文
单片机(不基于os)下如何实现简单的内存管理(malloc,realloc和free函数的重新实现)
作者:牧高风_457 | 来源:互联网 | 2024-10-14 17:37
实现的原理是将内存分成小的片段进行管理,代码如下:[cpp]viewplaincopy#defineMEM_BLOCK_SIZE4096#defineMEM_LARGE_BLOCK_TH
实现的原理是将内存分成小的片段进行管理,代码如下:
[cpp]
view plaincopy
#define MEM_BLOCK_SIZE 4096
#define MEM_LARGE_BLOCK_THRESHOLD 40960 //>MEM_LARGE_BLOCK_THRESHOLD :requested size is large block
#define MEM_BASE_ADDRESS (0x90000000)
#define MEM_ALLOC_TABLE_FIRST_ENTRY 0
#define MAX_MEM_SIZE 7*1024*1024
#define MEM_ALLOC_TABLE_SIZE (MAX_MEM_SIZE/MEM_BLOCK_SIZE)
static
INT16 memory_map[MEM_ALLOC_TABLE_SIZE];
static
char
isMemoryManagementReady=0;
void
*memset(
void
*s,
int
c,
size_t
count)
{
char
*xs = s;
while
(count--)
*xs++ = c;
return
s;
}
INT32
ssd_mem_init(
void
)
{
memset(memory_map, 0,
sizeof
(memory_map));
isMemoryManagementReady=1;
}
INT16 ssd_mem_percentage_used()
{
int
used=0;
int
i;
for
(i=0;i
{
if
(memory_map[i])
{
used++;
}
}
return
used*100/MEM_ALLOC_TABLE_SIZE;
}
//return -1:FAIL
//>=0: return allocated address offset
INT32
ssd_mem_malloc(
UINT32
size)
{
int
offset=0;
int
startEntry=MEM_ALLOC_TABLE_FIRST_ENTRY;
int
nmemb;
int
i;
if
(!isMemoryManagementReady)
{
ssd_mem_init();
}
if
(size==0)
{
return
-1;
}
nmemb=size/MEM_BLOCK_SIZE;
if
(size%MEM_BLOCK_SIZE)
{
nmemb++;
}
if
(size > MEM_LARGE_BLOCK_THRESHOLD)
{
for
(offset=startEntry;offset
{
if
(!memory_map[offset])
{
int
vacantSize=0;
for
(vacantSize=0;vacantSize
if
(vacantSize==nmemb)
{
for
(i=0;i
{
memory_map[offset+i]=nmemb;
}
return
(offset*MEM_BLOCK_SIZE);
}
}
}
}
else
{
for
(offset=MEM_ALLOC_TABLE_SIZE-1;offset>=0;offset--)
{
if
(!memory_map[offset] && ((offset+nmemb)<=MEM_ALLOC_TABLE_SIZE))
//search start of vacant block
{
int
vacantSize=0;
for
(vacantSize=0;vacantSize
if
(vacantSize==nmemb)
{
for
(i=0;i
{
memory_map[offset+i]=nmemb;
}
return
(offset*MEM_BLOCK_SIZE);
}
}
}
}
puts(
"malloc size erorr="
);
putInt32(size);
return
-1;
}
//return 0:OK
//return 1:Out of bound
INT32
ssd_mem_free(
INT32
offset)
{
int
i;
if
(!isMemoryManagementReady)
{
ssd_mem_init();
return
1;
}
if
(offset
{
int
index=offset/MEM_BLOCK_SIZE;
int
nmemb=memory_map[index];
for
(i=0;i
{
memory_map[index+i]=0;
}
return
0;
}
else
{
return
1;
//out of bound
}
}
void
free(
void
*ptr)
{
if
(ptr==NULL)
return
;
INT32
offset;
offset=ptr-MEM_BASE_ADDRESS;
ssd_mem_free(offset);
}
void
* malloc(
UINT32
size)
{
INT32
offset;
offset=ssd_mem_malloc(size);
if
(offset==-1)
{
return
NULL;
}
else
return
MEM_BASE_ADDRESS+offset;
}
void
*realloc(
void
*ptr,
UINT32
size)
{
INT32
offset;
offset=ssd_mem_malloc(size);
if
(offset==-1)
{
puts(
"realloc error/n"
);
return
NULL;
}
else
{
memcpy((
void
*)(MEM_BASE_ADDRESS+offset),ptr,size);
free(ptr);
return
MEM_BASE_ADDRESS+offset;
}
}
view
copy
request
static
int
char
search
写下你的评论吧 !
吐个槽吧,看都看了
会员登录
|
用户注册
推荐阅读
search
OSG嵌入Qt窗口中+实时渲染数据输出
实现方式1、首先设置一个Qt下的一个窗口基类;2、窗口基类继承自一个重写的QGLWidget类和一个osgViewer::Viewer类3、重新QGLWidget类& ...
[详细]
蜡笔小新 2024-10-16 16:16:19
byte
C#中的volatile关键字
volatile关键字指示一个字段可以由多个同时执行的线程修改。声明为volatile的字段不受编译器优化(假定由单个线程访问)的限制。这样可以确保该字 ...
[详细]
蜡笔小新 2024-10-15 15:43:15
utf-8
springboot 配置返回多个视图
自己测试成功!不喜忽喷。直接上代码。第一步:packagecom.example.demo.base.web;importjava.io.File;importjava.util. ...
[详细]
蜡笔小新 2024-10-15 12:55:24
java
java阅读firebase DB后如何在ListView上更改颜色和字体
如何处理为ListView显示空视图的问题,因为当我从Firebase数据库读取数据并将其显示在ListView中时,我不知道要放入哪些数据.我正在使用CustomListAdapter更改字体 ...
[详细]
蜡笔小新 2024-10-16 18:06:29
java
较真,ArrayList和LinkList增加,插入速度比较,代码详解
网上有很多ArrayList和LinkList的各种对比,以前学习这两个集合的时候老师讲的是:查询ArrayList快,新增和删除Link ...
[详细]
蜡笔小新 2024-10-16 14:37:15
java
洛咕P4542 [ZJOI2011]营救皮卡丘
套路题?感觉讲不清,先写建图把每个点拆成两个,A和B,S-Ai流量1费用0,Bi-T流量1费用0ÿ ...
[详细]
蜡笔小新 2024-10-16 13:48:52
java
[译] OS X 和 iOS 的测绘框架Core Plot 入门教程
[译] OS X 和 iOS 的测绘框架Core Plot 入门教程 ...
[详细]
蜡笔小新 2024-10-16 09:58:13
java
c++ string 拼接_羞,Java 字符串拼接竟然有这么多姿势
二哥,我今年大二,看你分享的《阿里巴巴Java开发手册》上有一段内容说:“循环体内,拼接字符串最好使用StringBuild ...
[详细]
蜡笔小新 2024-10-15 23:10:10
java
c++ 静态函数
对象与对象之间的成员变量是相互独立的.要想共用数据,则需要使用静态成员或静态方法#只要在类中声明静态成员变量,即使不定义对象,也可以为静态成员变量分配空间,进而可以使用静态成员变 ...
[详细]
蜡笔小新 2024-10-15 19:20:03
java
开发笔记:高效算法求解数独
篇首语:本文由编程笔记#小编为大家整理,主要介绍了高效算法求解数独相关的知识,希望对你有一定的参考价值。title:高效算法求解数独 ...
[详细]
蜡笔小新 2024-10-15 15:01:47
utf-8
python爬取各类文档方法归类小结
HTML文档是互联网上的主要文档类型,但还存在如TXT、WORD、Excel、PDF、csv等多种类型的文档。网络爬虫不仅需要能够抓取HTML中的敏感信息,也需要有抓取其他类型文档的能 ...
[详细]
蜡笔小新 2024-10-15 14:13:52
java
java实现幻灯片页面切换动画_java – Android ViewPager – 添加带淡入/淡出动画的幻灯片,以在视图之间切换...
这就是我做到的.首先,我使用了很多AndroidDisplayingBitmapsinYourUItutorial的代码.然后,我添加了以下调整:在包含ViewPag ...
[详细]
蜡笔小新 2024-10-15 14:11:23
java
java – RecyclerView不调用onCreateViewHolder或onBindView
没有得到任何错误,所有数据似乎都有效.出于某种原因,正在调用与视图相关的方法.我确定了以下内容:getItemCount()是唯一被调用的适配器方法,并返回一个正整数值,(我知道 ...
[详细]
蜡笔小新 2024-10-15 11:46:08
sum
AndroidActivity——activity详细说明书
Activity为Android应用程序的一个关键组成部分,它通常提供一个用户界面用来和用户交互以完成某个功能,比如拨号,拍照,发送电子邮件或者是浏览地图,在移动设备上,Activ ...
[详细]
蜡笔小新 2024-10-15 08:47:30
tree
Mybatis一级缓存的锅
问题背景项目开发中有一个树形数据结构,不像经典组织结构树、菜单级别树,我们这个树形结构是用户后期手动建立起来的关系。因此数据库表结构为两张表:数据记录表、记录关系表,通过业务规则限 ...
[详细]
蜡笔小新 2024-10-14 08:57:17
牧高风_457
这个家伙很懒,什么也没留下!
Tags | 热门标签
email
case
format
sum
split
java
chat
usb
scala
import
dll
hashcode
md5
web3
bash
ip
solr
dockerfile
httpclient
audio
hash
tree
foreach
flutter
byte
header
loops
utf-8
heatmap
future
RankList | 热门文章
1
【备战十四届蓝桥杯 | 开篇】如何高效备战蓝桥杯
2
术士上DOT要多长时间?要上些什么?怎么上最快?
3
开发笔记:100道Java基础面试题
4
单片机(不基于os)下如何实现简单的内存管理(malloc,realloc和free函数的重新实现)
5
搭建weblogic12.1.3
6
Springboot整合dubbo搭建基本的消费、提供和负载均衡
7
mysql数据库的主外键及几种查询
8
把全角数字转为半角数字
9
大哥、大姐们好,求教!!!今天老总发难题了。(看看你碰到过没有:)!!!)
10
C#知识点StopWatch计时
11
输入框点击后发光颜色的设置
12
实现Servlet程序的三种方法(小结)_java
13
左上角|开局_#冲刺创作新星# O了个H(OpenHarmony羊了个羊复刻学习)
14
java xml 读取孙元素_Java操作XML(7)XPath使用
15
2019年社交媒体趋势报告
PHP1.CN | 中国最专业的PHP中文社区 |
DevBox开发工具箱
|
json解析格式化
|
PHP资讯
|
PHP教程
|
数据库技术
|
服务器技术
|
前端开发技术
|
PHP框架
|
开发工具
|
在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved |
京公网安备 11010802041100号
|
京ICP备19059560号-4
| PHP1.CN 第一PHP社区 版权所有