嵌入式操作系统内核原理和开发(固定内存分配算法)

上一篇 / 下一篇  2012-06-26 09:14:01 / 个人分类:杂谈

固定内存方式是最简单的方法,也是最容易想到的方法。所谓的固定内存,就是所有分配的内存单元都是一致的。不管你申请的内存大小是多少,它都有一个最小的内存。因此,你申请的内存都会转化为单元内存的倍数。所以,算法会根据申请的内存计算出一个合适的申请内存,51Testing软件测试网sq!x+x}

51Testing软件测试网3Q0f'G"a:AW1[A8_U

b8Y+`S6N7Z{u,@$L0int get_best_fit_size(int size)
;[5B x+Rw4OJ%~Ur"x%B0{
"L/a\!Qr3A2v0G0    int bestSize = UNIT_SIZE;

!Ps b/LM7O'Gc051Testing软件测试网?K+Nbpj1s4A{ P

    while(bestSize < size && bestSize != MEMORY_SIZE)
L ou._a(K Hi { z&W2p0    {
4?%j)j1Rw*QRNW0        bestSize <<= 1;
|\.F"~i8T XW]0    }

9b%kM y$C']W4IrZ%Us0

:xQ},~6h S,m0    if(MEMORY_SIZE == bestSize)
wxAe!}0JI0    {
j-fC!^Tg;MwB0        return 0;51Testing软件测试网qNr1V8xV
    }51Testing软件测试网;K&mg``;` Dc
   51Testing软件测试网 ~ R1[)pHUh#[
    return bestSize;
v}}:W}*Ues;][0}
51Testing软件测试网 hZlp-W ](d

'^/fG!Q@.S T0  有了合适的内存大小之后,你还需要判断是否有合适的内存。关键就是检查内存flag标记,判断是否存在连续的内存,有的话就分配给你;如果没有,那也没办法。这里为了简单,我们就用了最快分配的原则,找到一个合适的内存就立马返回。51Testing软件测试网.xL {ZtY(J~v

51Testing软件测试网@n:|+_4^}wL

)ow&v3I-V0
51Testing软件测试网^rgNf7jq

int check_if_buffer_exist(int bufferSize, char* pStart)
L:a N,^5E?,q,` th0{
7l FPx A Gf!w0    int number = bufferSize / UNIT_SIZE;
\m}8o|0    int index;

bU[JN!MG1K0

iW:|/v;n9ln0    for(index = 0; index < number; index ++)51Testing软件测试网%G^Yp J&d a
    {51Testing软件测试网Pt2x_HV
        if(&pStart[index] >= ((char*)pMemAddr + TOTAL_UNIT))51Testing软件测试网8an']7k ?$n
            return 0;
51Testing软件测试网 D@#l^ j

~d4_1q6mqr HQ0        if(0 != pStart[index])51Testing软件测试网jA Csub r7f,z\ a
            return 0;51Testing软件测试网'\V%HJ\(^7x^
    }
51Testing软件测试网VH)TmD6m

51Testing软件测试网b{KIF$W'dj

    return 1 ;51Testing软件测试网;x9Q%Sk\g:q
}

oW A3e/{1D$I C!T0
51Testing软件测试网/uT9U tUx

  当然分配到了内存之后,我们就需要进行标记,即注明当前内存已经分配了;当然在释放的时候我们还要把这些标记给清除出去。

"D.Y/a'|Xp"T051Testing软件测试网%u@-wt,{ pg

i*_7^*l,_k$cH}1y0
51Testing软件测试网"G `aV A:q H

void set_memory_used_flag(int bufferSize, char* pData, char flag)
?[ uDa:QX)Y0{
8CH){'XG4G0    int ffset = (pData - (char*)pMemAddr - TOTAL_UNIT - MEM_SIZE_UNIT) / UNIT_SIZE;51Testing软件测试网1Uxx9QkF*Q4Q
    int index;

5A&`)E [(y051Testing软件测试网*kscZ{C&g

    for(index = 0; index < (bufferSize / UNIT_SIZE); index ++)
4}0WxHx1H^0u+m0    {
"Z D:\W`4v0        ((char*) pMemAddr + offset )[index] = flag;
}(XX,~j5w[s/l a2g0    }51Testing软件测试网-iz JF:W/P0IC:qfI3e
}

"R4d_[/Q"k0
 有了所有的这些条件,其实我们就可以有一个基本的内存分配函数了。下面我们会给出一个完整的内存分配案例,内存的空间内容是这样安排的:(1)内 存标志;(2)内存大小标记;(3)内存空间。其实(1)和(2)事实上都是管理内存的内容,但是也是必须分配的部分。打个比方来说,我们知道国家是需要 向公民收税的,但是收的税不可能全部进国库。因为收税本身是有成本的,你必须把这一步分钱提前拿出来。

#oo&hm(u~P W051Testing软件测试网| ^Ht'd

51Testing软件测试网(Wwy0sQ@)f2B

/*******************************************************
lR;l@O @0*           memory alloc & free file
A2?.z'v;qr n,}9q0********************************************************/
51Testing软件测试网(yD1z!PH G

E}f$c*G,| z@"e@)W0#include <stdio.h>51Testing软件测试网(X$Z"fCK\8k)Z i!^]
#include <malloc.h>51Testing软件测试网Pey2jo8E
#include <string.h>

:|#]`q2QF:kME$h8}051Testing软件测试网Z7L|2o}w8H


v6Ba"G6fPp0/*******************************************************51Testing软件测试网N3@ Jpe Gt*F-y
*              macro definition
-y6ee&`Wfm0********************************************************/

h+ZM5@ZH7H6_%O0

KOg!{]8Gn#Q*h,Z0#define MEMORY_SIZE    (0x1 << 24)51Testing软件测试网8S9Eq\ {+?.]
#define UNIT_SIZE      (0x1 << 12)
51Testing软件测试网g%FA.od6s.rkH/{

$tH G#nEBr0#define TOTAL_UNIT     (MEMORY_SIZE / UNIT_SIZE)
m:U {,q({#{ce0#define MEM_SIZE_UNIT  (TOTAL_UNIT << 2)

,X#\,Qk+S!PB!@051Testing软件测试网)r?i5KYw q Dd

51Testing软件测试网 YgK;X A7r
/*******************************************************
4r F/|#Z _suw0*           global data definition
-|2X s@0[ fUp ~0********************************************************/

4lOs)z6K"R&W/V051Testing软件测试网]:W!m-E Bq

static void* pMemAddr;51Testing软件测试网AS*rgG9F0Q

51Testing软件测试网3k5U,C:@(NL9u1W!K^


[ @0v _3l(Os,O0/*******************************************************51Testing软件测试网_O{geL\
* function: initial memory module function51Testing软件测试网5rgOYlh
********************************************************/

qx!r ?-U` ~051Testing软件测试网7u2R4}Ea*r

void memory_init()
,BR)L|M0{51Testing软件测试网|_b-V1\O-PE E
    int index;

D%N m(~O1u051Testing软件测试网$C San&T-CS

    /* alloca memory */51Testing软件测试网$[N9rS)d@HC
    pMemAddr = (void*)malloc(MEMORY_SIZE);51Testing软件测试网#w5@*ydp,b`f,z4yo
    if(NULL == pMemAddr)
4dm ?N xD;_0        return;
51Testing软件测试网^i4F%B0a^

"f(V6b:tW8M2D'CK0    /* initial global data */
j{F2uj6T7E*xoW0    memset(pMemAddr, 0, MEMORY_SIZE);
51Testing软件测试网#LG Rf%H/O)V:Wd

&U+A ^C"z0    /* set mng memory flag */51Testing软件测试网BttI3t{K
    for(index = 0; index < TOTAL_UNIT ; index++)
,^,Rfuc;s,Lf0        *((char*)pMemAddr + index) = 1;51Testing软件测试网3MoOs-T1R)Al
}

3~6t`y0T)T051Testing软件测试网 c9SC1yKM8L4p

51Testing软件测试网/?4M2\"^TD l~
/*******************************************************
DWH]+PXoJ}#]0* function: find best size for the memory51Testing软件测试网:nt6C-Q+J}\
********************************************************/
51Testing软件测试网5Bf6j7Rs[s%YO

51Testing软件测试网0y:b-u*e7S?.W

int get_best_fit_size(int size)51Testing软件测试网*F4{/N@"O$`ZYQ
{51Testing软件测试网!`a^I Y-z
    int bestSize = UNIT_SIZE;
51Testing软件测试网#t N/r![t_[t Z[4G

51Testing软件测试网mC,x(F9je5i O

    while(bestSize < size && bestSize != MEMORY_SIZE)51Testing软件测试网3Z)]qt[H?g jp)x
    {
5y R,}G8N/Wc0        bestSize <<= 1;
V'J%wNEr} KXFB0    }
51Testing软件测试网k(Z2JZ ?v&ew

51Testing软件测试网nj&lmW7V;f|

    if(MEMORY_SIZE == bestSize)51Testing软件测试网%v0]2^6QE$tMb
    {51Testing软件测试网\:tc't~/v
        return 0;51Testing软件测试网Oy*_)RN
    }51Testing软件测试网G[#su:Bu-q v!K
   51Testing软件测试网 x%F&R-Sb$|y
    return bestSize;51Testing软件测试网5o:s)W,jl'[
}
51Testing软件测试网*O9DC:S;[ ]

w&[yOy/a0}0
~[gT%Vx0/*******************************************************
x @K&Df o m0* function: check if buffer still exists51Testing软件测试网 ?PK+f _[0@/u
********************************************************/
51Testing软件测试网;?YT I'pF*VJ

S{ M#o,[@0int check_if_buffer_exist(int bufferSize, char* pStart)51Testing软件测试网7H3XH4e$n;_,yIq
{51Testing软件测试网x1n9Ijb`Y
    int number = bufferSize / UNIT_SIZE;
1B8C'\/`RLq0    int index;
51Testing软件测试网(X]$gq]rbD(y

51Testing软件测试网 Zh9W m HGD:D}

    for(index = 0; index < number; index ++)
"V6`F5rc+TxV0    {
JBk*\R1LF VU5E6z(W0        if(&pStart[index] >= ((char*)pMemAddr + TOTAL_UNIT))
.gT5j3[R#Qr0            return 0;

4{8S#@Fr051Testing软件测试网Ny1B/WW1a P8sU

        if(0 != pStart[index])
k'F0r)p+C#Z@0            return 0;
Yj1i[-vK}yO;[0    }

vR~6dK t&\4|O051Testing软件测试网UT"o:t\L,z

    return 1 ;51Testing软件测试网/M{+Hxv
}

c3RCZ4Nq#kO051Testing软件测试网#_ed;[;_


4ZQO x3K!d,v-{ZOn0/*******************************************************51Testing软件测试网-a8vFq.E1awZk&L kc-S
* function: process memory allocation51Testing软件测试网0O#B.M5e"vG?4p
********************************************************/
51Testing软件测试网K9T5^q6^]

C J#G jXdQG4Hi0char* _mem_malloc(int bufferSize)
!V+n^;Jk0{
t9^C#HU1? t0    int index;51Testing软件测试网9J?gC0H_n:m
  
s.S[/Y|gU.Q8Uy0    for(index = 0; index < TOTAL_UNIT; index ++)
8s&m X*WS1` t K)W0    {51Testing软件测试网Rc-V/R I
        if(check_if_buffer_exist(bufferSize, (char*)pMemAddr + index))
4`Q,hf}SH0            break;51Testing软件测试网i2l4S0DN
    }

B*pPni&V051Testing软件测试网!d r,PE4u:G

    if(TOTAL_UNIT == index)51Testing软件测试网wp@7V e(V9~$w
        return NULL;

$TO0F,cH$S%j:l1\x(WN0

Of ]M K0X@0    return (char*)pMemAddr + TOTAL_UNIT + MEM_SIZE_UNIT + UNIT_SIZE * index;51Testing软件测试网4rv~k wU4`Q
}
51Testing软件测试网:Ma_yD

b.Wx7ag;`]C0
TRaa6Z s {0/*******************************************************51Testing软件测试网D4J B&O&ZIK~
* function: set the applied memory size51Testing软件测试网$DM9Z A O mH? n,k
********************************************************/
51Testing软件测试网8`4M$a#H!g

51Testing软件测试网 }M8VrX+Sn2j$J

void set_memory_size(int bufferSize, char* pData)
'L s Ibt5C,l$j2]|0{51Testing软件测试网2oN.qZ.Z-xC
    int ffset = (pData - (char*)pMemAddr - TOTAL_UNIT - MEM_SIZE_UNIT) / UNIT_SIZE;51Testing软件测试网F `b6}(U3QX'QC
    int number = bufferSize / UNIT_SIZE;

1TR3P}TR3])t,_*o051Testing软件测试网[ [6Y@2mR1@u

    ((int*)((char*) pMemAddr + TOTAL_UNIT ))[offset] = number;
X(?1ES RU0}
51Testing软件测试网0L/hI*e7g@2^

51Testing软件测试网R'ZeC o@{+U

51Testing软件测试网%^tjcikiT9X5J
/*******************************************************
E6Q??'dv]m1]*?0* function: get the applied memory size from address
M{#WF/C3e0********************************************************/
51Testing软件测试网&yy)]^ I4b*c@{

Eh4v/])X(KV N0int get_memory_size(char* pData)
DZ5z&V1TmlU%r0{
&V6a U?z3l?5V0    int ffset = (pData - (char*)pMemAddr - TOTAL_UNIT - MEM_SIZE_UNIT) / UNIT_SIZE;
8MPqcO g oe0  51Testing软件测试网 ?%r`8^ A*XPR
    return  ((int*)((char*) pMemAddr + TOTAL_UNIT))[offset];51Testing软件测试网l2` jG@
}

x(y'L8u9z!Z0

[BOc'WQ/A|5s o%o0
Gp lQ?)O'LG@0/*******************************************************
M/[JM!x0* function: set memory used flag51Testing软件测试网o8r@6w8F.`$} q
********************************************************/
51Testing软件测试网g8kfd*K&a1ie+l)v

51Testing软件测试网XZum2c?5@-SN

void set_memory_used_flag(int bufferSize, char* pData, char flag)51Testing软件测试网^3Yu1F z_
{51Testing软件测试网\^s O(@+bU)E]
    int ffset = (pData - (char*)pMemAddr - TOTAL_UNIT - MEM_SIZE_UNIT) / UNIT_SIZE;51Testing软件测试网cf"?$iBb-x%M H
    int index;

rA%F|*J"vPt J051Testing软件测试网bh?7z@-V%A

    for(index = 0; index < (bufferSize / UNIT_SIZE); index ++)
W*KJYM3Q,n3y0    {51Testing软件测试网3A}.E0Sfx
        ((char*) pMemAddr + offset )[index] = flag;
z:V t5d/\UXcr0    }51Testing软件测试网F5Lxtx\*w/g
}
51Testing软件测试网7hMP:T aR5g8]4i&[

51Testing软件测试网$Wg#I!}3{]2RW!L e


`/`"V-G)t+h4~5p(El0/*******************************************************51Testing软件测试网N ?3Q oI$q
* function: allocate memory from buffer51Testing软件测试网3[:y:|T_$t-H
********************************************************/

a+_ao7KC#f0

N4Ke(B:w+c7W%y,ET0void* mem_malloc(int size)51Testing软件测试网]q OP!R7x2w(g9n
{51Testing软件测试网O3t X'mC@ c
    int bufferSize;51Testing软件测试网|@;h7uO cZ
    char* pData;

}3O%U0st3U0

`n8LW/`0    if(0 == size)
7v pVy5x"X ^0        return NULL;
51Testing软件测试网E#nb Qr[#R7cI

[.sz+Y O@9x0    bufferSize = get_best_fit_size(size);
/eRsqv s!ia F0    if(0 == bufferSize)
X%]@`1Z f'M2JU0        return NULL;
51Testing软件测试网\"iV!W%j$Q

51Testing软件测试网#{CTSY A [

    if(NULL == (pData = _mem_malloc(bufferSize)))51Testing软件测试网t3@M ^,Y
        return NULL;
51Testing软件测试网!Oi*l.M+y{

51Testing软件测试网;J[/F b!L9s7j`1_5j

    set_memory_used_flag(bufferSize / UNIT_SIZE, pData, 1); 51Testing软件测试网-sL7ne`U0X| h
    set_memory_size(bufferSize, pData);51Testing软件测试网+\;bO}~
    return (void*)pData;
)vfN xj#[F"O0}

2^S Cof!yP or051Testing软件测试网Z(v\1UV9p T C4jkJ

/*******************************************************51Testing软件测试网_,~[Xi-x
* function: free memory to buffer
ph/X;XLl:q8^m0********************************************************/

!y(^ v%I qir0

e(oixH@S3T0void mem_free(char* pData)
W4X-N'K#On7k0{
%w,Z)_.KU1_6Q1p)Y5|P l#~0    int number;
51Testing软件测试网8^ m7AZA,St)B9q

:N2V'gWZT0    if(NULL == pData)
yzOZV0Sd~X0        return;

@or@ r;Z~4?051Testing软件测试网6}f-ME6zA8w.@MJ0Y

    if(pData >= (char*) pMemAddr && pData < ((char*) pMemAddr + TOTAL_UNIT + MEM_SIZE_UNIT))
!b X-JEz(Q Ibgv0        return;

R~uL{o V&Ln0

T'OeL0\@iQe0    number = get_memory_size(pData);
pCY gx kD'|0    set_memory_size(0, pData);51Testing软件测试网Fcl#Y`o+C
    set_memory_used_flag(number, pData, 0);51Testing软件测试网 N `!f f+d.y_$y
}

)q@6~.aRG#M,^051Testing软件测试网 t5r(\(p_"_oG


Fc[q,c0F,`#x^3r0/*******************************************************
#D9T5a*_!AE3A0* function: free all memory
x#TYWZSKu&X0********************************************************/

j _aT1l6U P0h A0

U!u&xb[w%b0void memory_exit()51Testing软件测试网!n9l[bR
{51Testing软件测试网 g/rd4a1m(s/z5\t
    free(pMemAddr);51Testing软件测试网e(Nzc)p2BE3I|
}
51Testing软件测试网t9V1TdOwpuu%x$m

51Testing软件测试网JUM5JPA


4`!PG*Us7_}!E0/*******************************************************51Testing软件测试网C p;q;y b
* function: file entry starts here51Testing软件测试网!M M2`,K6L pyl
********************************************************/
51Testing软件测试网m%v'{ z%F-O^V+H

EH8`*rt:?0int main(int argc, char* argv[])
U,HUm+c-Z;[0{
8D'U?9e ]|O ]PK0    memory_init();51Testing软件测试网 j3J5X1]BVV,W
    memory_exit();51Testing软件测试网\:lgI {{v
    return 1;
s s8XEZe$XP0}
51Testing软件测试网6[ M5RE*F'O+vg+Z

相关链接:51Testing软件测试网/G[6Z/M Y;h-^k

嵌入式操作系统内核原理和开发(开篇)51Testing软件测试网"V+eDa(GY

嵌入式操作系统内核原理和开发(cpu的那些事)

@Y#O;@cO0

嵌入式操作系统内核原理和开发(中断)51Testing软件测试网9a;jv;Iw|S5S

嵌入式操作系统内核原理和开发(地址空间)

Q;Ih.LG\-Y0

嵌入式操作系统内核原理和开发(系统中断仿真)51Testing软件测试网/k\~*\1`,vB

嵌入式操作系统内核原理和开发(线程切换)51Testing软件测试网0e8[j MXI

嵌入式操作系统内核原理和开发(任务创建和堆栈溢出检查)51Testing软件测试网#J)Q)dB(TEU1o

嵌入式操作系统内核原理和开发(多线程轮转)

jAu/Lb'b(k_0Y0

嵌入式操作系统内核原理和开发(通用优先级调度)51Testing软件测试网.Nd;ZBSM"KA

嵌入式操作系统内核原理和开发(抢占式优先级调度)51Testing软件测试网.o @1{6c.U3^$l

嵌入式操作系统内核原理和开发(头文件调整)

SI?!\|S0

嵌入式操作系统内核原理和开发(内存分配算法)51Testing软件测试网:QCq&M.j


TAG:

 

评分:0

我来说两句

Open Toolbar