Chinaunix首页 | 论坛 | 博客
  • 博客访问: 946434
  • 博文数量: 116
  • 博客积分: 3923
  • 博客等级: 中校
  • 技术积分: 1337
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-23 01:22
文章分类

全部博文(116)

文章存档

2013年(1)

2012年(17)

2011年(69)

2009年(29)

分类: C/C++

2013-06-09 22:31:10

今天中午快速的实现了一个malloc和free的另一个版本代码,用位图的形式实现,速度暂时没优化,记录一下:




点击(此处)折叠或打开

  1. /*
  2.  * file: main.c
  3.  * author: vincent.cws2008@gmail.com
  4.  * date: 2013-06-09
  5.  * brief: main
  6.  */

  7. #include "stdafx.h"

  8. #include "string.h"
  9. #include "stdlib.h"
  10. #include "stdio.h"
  11. #include <time.h>

  12. void xmfree(void *hd, void *pmblk);
  13. void *xmalloc(void *hd, size_t size);
  14. void dump_map(void *hd);
  15. void* xmcreate(char *buf, size_t size);

  16. void ConPrint(char *CharBuffer, int len);
  17. void ConPrintAt(int x, int y, char *CharBuffer, int len);
  18. void gotoXY(int x, int y);
  19. void ClearConsole(void);
  20. void ClearConsoleToColors(int ForgC, int BackC);
  21. void SetColorAndBackground(int ForgC, int BackC);
  22. void SetColor(int ForgC);
  23. void HideTheCursor(void);
  24. void ShowTheCursor(void);

  25. struct stat_info{
  26.     int success;
  27.     int failed;
  28.     int total;
  29.     int time_val;
  30. };

  31. int main(int argc, char* argv[])
  32. {
  33.     char *arr_pnew[10];
  34.     char buf[10000];
  35.     void *handle=0;
  36.     int rd_idx=0;

  37.     unsigned int success=0;
  38.     unsigned int failed=0;
  39.     unsigned int total=0;
  40.     unsigned int time_val=0;

  41.     clock_t start, finish;
  42.     
  43.     srand(time(0));
  44.     memset(arr_pnew, 0, sizeof(arr_pnew));
  45.     handle = xmcreate(buf, sizeof(buf));

  46.     start = clock();

  47.     while (1) {

  48.         rd_idx = rand()%(sizeof(arr_pnew)/sizeof(arr_pnew[0]));
  49.         if(arr_pnew[rd_idx]==0){
  50.             arr_pnew[rd_idx] = (char*)xmalloc(handle, rand()%sizeof(buf));
  51.             if (arr_pnew[rd_idx])
  52.                 success++;
  53.             else
  54.                 failed++;

  55.             total++;
  56.             if(total) {
  57.                 finish = clock();
  58.                 gotoXY(0, 0);
  59.                 SetColor(15);
  60.                 printf("total:%-8d success=%-8d failed=%-8drntime(ms)=%-8d precent=%d%% ",
  61.                     total, success, failed, finish-start, (success*100)/total);
  62.             }
  63.         }else {
  64.             xmfree(handle, arr_pnew[rd_idx]);
  65.             arr_pnew[rd_idx]=0;
  66.         }
  67.     }

  68.     //ClearConsole();
  69.     return 0;
  70. }



点击(此处)折叠或打开

  1. /*
  2.  * file: xmalloc.c
  3.  * author: vincent.cws2008@gmail.com
  4.  * date: 2013-06-09
  5.  * brief: xmalloc and xmfree
  6.  */

  7. #include "string.h"
  8. #include "stdio.h"

  9. void ConPrint(char *CharBuffer, int len);
  10. void ConPrintAt(int x, int y, char *CharBuffer, int len);
  11. void gotoXY(int x, int y);
  12. void ClearConsole(void);
  13. void ClearConsoleToColors(int ForgC, int BackC);
  14. void SetColorAndBackground(int ForgC, int BackC);
  15. void SetColor(int ForgC);
  16. void HideTheCursor(void);
  17. void ShowTheCursor(void);

  18. void xmfree(void *hd, void *pmblk);
  19. void *xmalloc(void *hd, size_t size);
  20. void dump_map(void *hd);
  21. void* xmcreate(char *buf, size_t size);


  22. #define DIV_SYS        1
  23. #define ALIGN_SIZE    8 /* 8 Byte */
  24. #define XM_MAGIC    0x12345678
  25. #define XM_BLOCK    S_ALIGN(32)    /* memory block size 32 Byte */    

  26. #if DIV_SYS
  27.     #define INTDIV(dend,div) ((dend)/(div))
  28. #else
  29.     #define INTDIV(dend,div) int_div(dend,div)
  30. #endif

  31. #define S_ALIGN(sz)            (((unsigned int)(sz)+(ALIGN_SIZE-1))&~(ALIGN_SIZE-1))
  32. #define F_ALIGN(addr)        ((unsigned int)((unsigned int)(addr)+(ALIGN_SIZE-1))&~(ALIGN_SIZE-1))
  33. #define B_ALIGN(addr)        ((unsigned int)(addr)&~(ALIGN_SIZE-1))
  34. #define MEM_ADDR_END(p,s)    (B_ALIGN((unsigned int)(p)+(s)))
  35. #define MAP_ADDR(p)            (F_ALIGN(F_ALIGN(p)+sizeof(struct xmem_head)))
  36. #define MAP_SIZE(p,s)        (INTDIV(((INTDIV((MEM_ADDR_END(p,s)-MAP_ADDR(p)+(XM_BLOCK-1)),XM_BLOCK))+(8-1)),8))
  37. #define BLK_ADDR(p,s)        (F_ALIGN((unsigned int)MAP_ADDR(p)+MAP_SIZE(p,s)))            
  38. #define BLK_NUM(p,s)        (INTDIV((MEM_ADDR_END(p,s)-BLK_ADDR(p,s)),XM_BLOCK))

  39. #define RBIT(v,n)        ((v)&(1<<(n)))
  40. #define WBIT(v,n)        ((v)^=(1<<(n)))
  41. #define CBIT(v,n)        ((v)&=~(1<<(n)))
  42. #define MAP_RBIT(p,c)    RBIT(*((char*)p+(c)/8),(c)%8)
  43. #define MAP_WBIT(p,c)    WBIT(*((char*)p+(c)/8),(c)%8)
  44. #define MAP_CBIT(p,c)    CBIT(*((char*)p+(c)/8),(c)%8)

  45. #define dump printf

  46. #ifndef size_t
  47. #define size_t unsigned int
  48. #endif


  49. #define assert(x) do {
  50.     if (!(x)) {
  51.         printf("ERROR: ASSERT ON LINE(%d)!!!rn", __LINE__);
  52.         while(1);
  53.     }
  54. }while(0);

  55. struct xmem_head {
  56.     unsigned int magic;
  57.     char *map_addr;
  58.     char *blk_addr;
  59.     unsigned int blk_nums;
  60. };

  61. static int int_div(unsigned int dividend, unsigned int divisor)
  62. {
  63.     unsigned int k, c, res=0;

  64.     if(divisor == 0) {
  65.         assert(divisor!=0);
  66.         while(1);
  67.     }
  68.     if (dividend < divisor) return 0;
  69.     if (dividend == divisor) return 1;
  70.     while (dividend > divisor) {
  71.         for (k = 0,c = divisor; dividend >= c; c <<= 1, k++) {
  72.             if (dividend - c < divisor){
  73.                 res += 1<<k;
  74.                 break;
  75.             }
  76.         }
  77.         if (dividend-c < divisor) break;
  78.         res += 1<<(k - 1);
  79.         dividend -= c>>1;
  80.     }
  81.     return res;
  82. }

  83. void* xmcreate(char *buf, size_t size)
  84. {
  85.     struct xmem_head *pxmh=0;
  86.     if (!buf || size<sizeof(struct xmem_head)+ALIGN_SIZE+XM_BLOCK*2)
  87.         return (void*)0;

  88.     pxmh = (struct xmem_head*)(F_ALIGN(buf));
  89.     pxmh->magic = XM_MAGIC;
  90.     pxmh->map_addr=(char*)MAP_ADDR(buf);
  91.     pxmh->blk_addr=(char*)BLK_ADDR(buf,size);
  92.     pxmh->blk_nums=BLK_NUM(buf,size);

  93.     memset(pxmh->map_addr, 0, MAP_SIZE(buf,size));

  94.     if (pxmh->blk_addr<=pxmh->map_addr || pxmh->blk_nums<2) {
  95.         pxmh->magic = 0;
  96.         return (void*)0;
  97.     }
  98.     return pxmh;
  99. }

  100. void xmdestroy(void *hd)
  101. {
  102.     if (!hd || ((struct xmem_head*)hd)->magic!=XM_MAGIC)
  103.         return ;
  104.     ((struct xmem_head*)hd)->magic=0x11223344;
  105. }

  106. void dump_map(void *hd)
  107. {
  108.     int i=0, of, cnt;
  109.     char* pmap;
  110.     struct xmem_head *pxmh=(struct xmem_head *)hd;
  111.     if (!hd || ((struct xmem_head*)hd)->magic!=XM_MAGIC)
  112.         return;
  113.     pmap = pxmh->map_addr;
  114.     of=i=0;
  115.     gotoXY(0,1);
  116.     cnt=0;
  117.     SetColor(10);
  118.     printf("rn---------------------------------------------------------------rn");
  119.     do {
  120.         for(i=0; i<8; i++){
  121.             if (of*8+i >= pxmh->blk_nums) {
  122.                 SetColor(10);
  123.                 printf("rn---------------------------------------------------------------rn");
  124.                 assert(cnt%2==0);
  125.                 return;
  126.             }
  127.             if (!!MAP_RBIT(pmap,of*8+i)) {
  128.                 SetColor(14);
  129.                 cnt++;
  130.                 printf("%d ", !!MAP_RBIT(pmap,of*8+i));
  131.             }
  132.             else
  133.             {
  134.                 if ((cnt%2)==0)
  135.                     SetColor(4);
  136.                 else
  137.                     SetColor(14);
  138.                 printf("%d ", !!MAP_RBIT(pmap,of*8+i));
  139.             }
  140.         }
  141.         if ((of+1)%4==0) printf("rn");
  142.         of++;
  143.     } while(1);
  144. }


  145. void *xmalloc(void *hd, size_t size)
  146. {
  147.     char *pmap=0;
  148.     struct xmem_head *pxmh=0;
  149.     int of, i, f, c;
  150.     if (!hd || ((struct xmem_head*)hd)->magic!=XM_MAGIC)
  151.         return (void*)0;
  152.     //search of the free part for memory size
  153.     pxmh = (struct xmem_head*)hd;
  154.     pmap = pxmh->map_addr;
  155.     f=c=i=0;
  156.     of=0;
  157.     //printf("INFO: xmalloc size=%drn", size);
  158.     do {
  159.         for (i=0; i<8; i++){
  160.             if (of*8+i >= pxmh->blk_nums)
  161.                 goto MATCH_FAIL;

  162.             if(MAP_RBIT(pmap, of*8+i)){
  163.                 f++;
  164.                 if (!(f&0x01)) {
  165.                     c=0;
  166.                     continue;
  167.                 }
  168.             }
  169.             c++;
  170.             if(!(f&0x01) && c*XM_BLOCK>=size){
  171.                 if (c==1) continue; //must bigger than 2 blocks every malloc
  172.                 //printf("INFO: xmalloc index:%d-%drn", of*8+i-c+1, of*8+i);
  173.                 assert(MAP_RBIT(pmap,of*8+i-c+1)==0);
  174.                 MAP_WBIT(pmap,of*8+i-c+1); //set begin offset of free block
  175.                 assert(MAP_RBIT(pmap,of*8+i)==0);
  176.                 MAP_WBIT(pmap,of*8+i); //set end offset of free block
  177.                 dump_map(hd);
  178.                 return pxmh->blk_addr+(of*8+i-c+1)*XM_BLOCK;
  179.             }
  180.         }
  181.         of++;
  182.     } while (1);
  183.     
  184. MATCH_FAIL:
  185.     //printf("ERROR: NO FREE SPACE FOR XMALLOC!!!rn");
  186.     return (void*)0;
  187. }

  188. void xmfree(void *hd, void *pmblk)
  189. {
  190.     struct xmem_head *pxmh=0;
  191.     char *pmb=(char*)pmblk;
  192.     char *pmap, *pblk;
  193.     int of=0, i=0;
  194.     if (!hd || ((struct xmem_head*)hd)->magic!=XM_MAGIC)
  195.         return ;
  196.     pxmh = (struct xmem_head*)hd;
  197.     pblk = pxmh->blk_addr;
  198.     pmap = pxmh->map_addr;
  199.     assert(pblk&&pmap);
  200.     if (pmb>=pblk && pmb<pblk+pxmh->blk_nums*XM_BLOCK) {
  201.         assert((pmb-pblk)%XM_BLOCK==0);
  202.         of = (pmb-pblk)/XM_BLOCK;
  203.         assert(of<pxmh->blk_nums);
  204.         MAP_CBIT(pmap,of);
  205.         //printf("INFO: xmfree index:%d-", of);
  206.         while (1) {
  207.             of++;
  208.             if(MAP_RBIT(pmap,of)) {
  209.                 MAP_CBIT(pmap,of);
  210.                 //printf("%drn", of);
  211.                 dump_map(hd);
  212.                 break;
  213.             }
  214.         }
  215.     }
  216. }


点击(此处)折叠或打开

  1. /*
  2.  * file: screen.c
  3.  * brief: drawing
  4.  */

  5. #include <windows.h>
  6. #include <stdio.h>
  7.  
  8. void ConPrint(char *CharBuffer, int len);
  9. void ConPrintAt(int x, int y, char *CharBuffer, int len);
  10. void gotoXY(int x, int y);
  11. void ClearConsole(void);
  12. void ClearConsoleToColors(int ForgC, int BackC);
  13. void SetColorAndBackground(int ForgC, int BackC);
  14. void SetColor(int ForgC);
  15. void HideTheCursor(void);
  16. void ShowTheCursor(void);
  17.  
  18. int test_main(int argc, char* argv[])
  19. {
  20.    HideTheCursor();
  21.    ClearConsoleToColors(15, 1);
  22.    ClearConsole();
  23.    gotoXY(1, 1);
  24.    SetColor(14);
  25.    printf("This is a test...n");
  26.    Sleep(5000);
  27.    ShowTheCursor();
  28.    SetColorAndBackground(15, 12);
  29.    ConPrint("This is also a test...n", 23);
  30.    SetColorAndBackground(1, 7);
  31.    ConPrintAt(22, 15, "This is also a test...n", 23);
  32.    gotoXY(0, 24);
  33.    SetColorAndBackground(7, 1);
  34.    return 0;
  35. }
  36.  
  37. //This will clear the console while setting the forground and
  38. //background colors.
  39. void ClearConsoleToColors(int ForgC, int BackC)
  40. {
  41.    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  42.    //Get the handle to the current output buffer...
  43.    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  44.    //This is used to reset the carat/cursor to the top left.
  45.    COORD coord = {0, 0};
  46.    //A return value... indicating how many chars were written
  47.    //not used but we need to capture this since it will be
  48.    //written anyway (passing NULL causes an access violation).
  49.    DWORD count;
  50.  
  51.    //This is a structure containing all of the console info
  52.    // it is used here to find the size of the console.
  53.    CONSOLE_SCREEN_BUFFER_INFO csbi;
  54.    //Here we will set the current color
  55.    SetConsoleTextAttribute(hStdOut, wColor);
  56.    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  57.    {
  58.       //This fills the buffer with a given character (in this case 32=space).
  59.       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  60.  
  61.       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  62.       //This will set our cursor position for the next print statement.
  63.       SetConsoleCursorPosition(hStdOut, coord);
  64.    }
  65. }
  66.  
  67. //This will clear the console.
  68. void ClearConsole()
  69. {
  70.    //Get the handle to the current output buffer...
  71.    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  72.    //This is used to reset the carat/cursor to the top left.
  73.    COORD coord = {0, 0};
  74.    //A return value... indicating how many chars were written
  75.    // not used but we need to capture this since it will be
  76.    // written anyway (passing NULL causes an access violation).
  77.    DWORD count;
  78.    //This is a structure containing all of the console info
  79.    // it is used here to find the size of the console.
  80.    CONSOLE_SCREEN_BUFFER_INFO csbi;
  81.    //Here we will set the current color
  82.    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  83.    {
  84.       //This fills the buffer with a given character (in this case 32=space).
  85.       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  86.       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  87.       //This will set our cursor position for the next print statement.
  88.       SetConsoleCursorPosition(hStdOut, coord);
  89.    }
  90. }
  91.  
  92. //This will set the position of the cursor
  93. void gotoXY(int x, int y)
  94. {
  95.    //Initialize the coordinates
  96.    COORD coord = {x, y};
  97.    //Set the position
  98.    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  99. }
  100.  
  101. //This will set the forground color for printing in a console window.
  102. void SetColor(int ForgC)
  103. {
  104.    WORD wColor;
  105.    //We will need this handle to get the current background attribute
  106.    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  107.    CONSOLE_SCREEN_BUFFER_INFO csbi;
  108.  
  109.    //We use csbi for the wAttributes word.
  110.    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  111.    {
  112.       //Mask out all but the background attribute, and add in the forgournd color
  113.       wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  114.       SetConsoleTextAttribute(hStdOut, wColor);
  115.    }
  116. }
  117.  
  118. //This will set the forground and background color for printing in a console window.
  119. void SetColorAndBackground(int ForgC, int BackC)
  120. {
  121.    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
  122.    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
  123. }
  124.  
  125. //Direct console output
  126. void ConPrint(char *CharBuffer, int len)
  127. {
  128.    DWORD count;
  129.    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
  130. }
  131.  
  132. //Direct Console output at a particular coordinate.
  133. void ConPrintAt(int x, int y, char *CharBuffer, int len)
  134. {
  135.    DWORD count;
  136.    COORD coord = {x, y};
  137.    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  138.    SetConsoleCursorPosition(hStdOut, coord);
  139.    WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
  140. }
  141.  
  142. //Hides the console cursor
  143. void HideTheCursor()
  144. {
  145.    CONSOLE_CURSOR_INFO cciCursor;
  146.    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  147.  
  148.    if(GetConsoleCursorInfo(hStdOut, &cciCursor))
  149.    {
  150.       cciCursor.bVisible = FALSE;
  151.       SetConsoleCursorInfo(hStdOut, &cciCursor);
  152.    }
  153. }
  154.  
  155. //Shows the console cursor
  156. void ShowTheCursor()
  157. {
  158.    CONSOLE_CURSOR_INFO cciCursor;
  159.    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  160.  
  161.    if(GetConsoleCursorInfo(hStdOut, &cciCursor))
  162.    {
  163.       cciCursor.bVisible = TRUE;
  164.       SetConsoleCursorInfo(hStdOut, &cciCursor);
  165.    }
  166. }


附件:

xmalloc.rar




阅读(9175) | 评论(0) | 转发(0) |
0

上一篇:重写无线模块上电时序

下一篇:没有了

给主人留下些什么吧!~~