51Testing软件测试网(Wwy0sQ@)f2B /******************************************************* lR;l@O
@0* memory alloc & free file A2?.z'v;qrn,}9q0********************************************************/51Testing软件测试网(yD1z!PHG E}f$c*G,| z@"e@)W0#include <stdio.h>51Testing软件测试网(X$Z"fCK\8k)Z
i!^] #include <malloc.h>51Testing软件测试网Pe y2jo8E #include <string.h> :|#]`q2QF:kME$h8}051Testing软件测试网Z7L|2o }w8H v6Ba"G6fPp0/*******************************************************51Testing软件测试网N3@ JpeG t*F-y * macro definition -y6ee&`Wfm0********************************************************/ h+Z M5@ZH7H6_%O0KOg!{]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
qDd51Testing软件测试网 YgK;X
A7r /******************************************************* 4rF/|#Z _suw0* global data definition -|2X
s@0[
fUp
~0********************************************************/ 4lOs)z6K"R&W/V051Testing软件测试网]:W!m-E
Bqstatic void* pMemAddr;51Testing软件测试网A S*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*rvoid 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:t W8M2D'CK0 /* initial global data */ j{F2uj6T7E*xo W0 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++) ,^,Rfu c;s,L f0 *((char*)pMemAddr + index) = 1;51Testing软件测试网3MoOs-T1R)Al } 3~6t`y0T)T051Testing软件测试网c9SC1yKM8L4p51Testing软件测试网/?4M2\"^TDl~ /******************************************************* DWH ]+PXoJ}#]0* function: find best size for the memory51Testing软件测试网:nt6C-Q+J}\ ********************************************************/51Testing软件测试网5B f6j7Rs[s%YO 51Testing软件测试网0y:b-u*e7S?.Wint 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(F9je5iO 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软件测试网\:t c't~/v return 0;51Testing软件测试网Oy*_)RN }51Testing软件测试网 G[#su:Bu-qv!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&Dfo
m0* function: check if buffer still exists51Testing软件测试网?PK+f
_[0@/u ********************************************************/51Testing软件测试网;?YTI'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*\R1LFVU5E6z(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~6dKt&\4|O051Testing软件测试网UT"o:t\L,z return 1 ;51Testing软件测试网/M{+Hx v } 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^]
CJ#G jXd QG4Hi0char* _mem_malloc(int bufferSize) !V+n^;Jk0{ t9^C#H U1?
t0 int index;51Testing软件测试网9J?gC0H_n:m s.S[/Y|gU.Q8Uy0 for(index = 0; index < TOTAL_UNIT; index ++) 8s&m
X*WS1` tK)W0 {51Testing软件测试网Rc-V/RI if(check_if_buffer_exist(bufferSize, (char*)pMemAddr + index)) 4`Q,hf}S H0 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(WN0Of
]MK0X@0 return (char*)pMemAddr + TOTAL_UNIT + MEM_SIZE_UNIT + UNIT_SIZE * index;51Testing软件测试网4rv~k wU4`Q }51Testing软件测试网:Ma_yD
b.Wx7ag;`]C0 TRaa6Zs{0/*******************************************************51Testing软件测试网D4JB&O&Z |