Skip to content

Commit 6734b47

Browse files
committed
优化内存管理
1 parent 9db9ad7 commit 6734b47

File tree

9 files changed

+22
-6
lines changed

9 files changed

+22
-6
lines changed

apps/apps_api.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void close_window(SHEET *sht){
4949

5050
void api_loopforever(LambdaContainer func,int interval){
5151
auto task=task_now();
52+
task->callback=&func;
5253
auto fifo=(FIFO*)malloc(sizeof(FIFO));
5354
fifo->init(128,task);
5455
task->fifo=fifo;
@@ -59,8 +60,8 @@ void api_loopforever(LambdaContainer func,int interval){
5960
sleepTask();
6061
}else{
6162
fifo->get();
62-
func();
6363
timer->set(interval);
64+
func();
6465
}
6566
}
6667
}

fifo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void FIFO::init(int size,Task *task){
2626

2727
void FIFO::remove(){
2828
mfree((uintptr_t)buf,size*4);
29+
destroyed=true;
2930
return;
3031
}
3132

heap.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ int MEMMAN::free_4k(unsigned int addr,unsigned int size){
147147
}
148148

149149
uintptr_t malloc(uint32_t size){
150-
return memman->alloc_4k(size);
150+
auto t=memman->alloc_4k(size);
151+
hanastd::memset((uintptr_t)t,0,size);
152+
return t;
151153
}
152154

153155
int mfree(uintptr_t addr,uint32_t size){

include/fifo.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#define FLAGS_OVERRUN 0x0001
44
class FIFO {
55
private:
6+
int *buf;
67
int r,w,size,free,flags;
78
public:
8-
int *buf;
9+
bool destroyed=false;
910
struct Task *task;
1011
void init(int size);
1112
void init(int size,Task *task);

include/lambda_container.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class LambdaContainer {
5757
}
5858

5959
~LambdaContainer() {
60+
//mfree((uintptr_t)buf,size);
61+
}
62+
63+
void destroy() {
6064
mfree((uintptr_t)buf,size);
6165
}
6266
};

include/task.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdint.h>
44
#include "timer.hpp"
5+
#include "lambda_container.hpp"
56

67
#define TASK_STACK_SIZE (64*1024)
78

@@ -17,6 +18,7 @@ typedef struct Task {
1718
int level,priority;
1819
uint32_t stackbottom;
1920
FIFO *fifo;
21+
LambdaContainer *callback=NULL;
2022
} __attribute__((packed)) Task;
2123

2224
#define MAX_TASKS 1000

sheet.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ SHEET *SHEETCTRL::allocsheet(int xsize,int ysize){
3434
SHEET *sht=&sheets0[i];
3535
sht->graphics=(GRAPHICS *)malloc(sizeof(GRAPHICS));
3636
uint32_t *buf=(uint32_t *)malloc(xsize*ysize*4);
37-
memset(buf,0,xsize*ysize*4);
3837
if(buf==0){
3938
return 0;
4039
}

task.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ void task_switchsub(){
6868

6969
Task *initTasking(TIMER *timer){
7070
taskctl=(TASKCTRL*)malloc(sizeof(TASKCTRL));
71-
memset(taskctl,0,sizeof(TASKCTRL));
7271
for(int i=0;i<MAX_TASKS;i++)
7372
taskctl->tasks0[i].stat=EMPTY;
7473
auto task=taskctl->alloc();
@@ -177,4 +176,10 @@ void killTask(Task *task){
177176
mfree(task->stackbottom,TASK_STACK_SIZE);
178177
task->stat=EMPTY;
179178
}
179+
if(task->fifo!=NULL){
180+
task->fifo->remove();
181+
mfree((uintptr_t)task->fifo,sizeof(FIFO));
182+
}
183+
if(task->callback!=NULL)
184+
task->callback->destroy();
180185
}

timer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ void TIMERCTRL::interrupt(){
4444
for(;;){
4545
if(timer->timeout>count)break;
4646
if(timer!=mt_timer){
47-
timer->push();
47+
if(!timer->fifo->destroyed)
48+
timer->push();
4849
}else{
4950
timer->flag=ALLOC;
5051
ts=1;

0 commit comments

Comments
 (0)