Chinaunix首页 | 论坛 | 博客
  • 博客访问: 361081
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: 嵌入式

2011-12-02 13:58:23

  1. 头文件:_mem_pool.h

  2. //define the data structures here

  3. typedef struct _list_node
  4. {
  5.     void *addr;
  6.     struct _list_node *next_node;
  7. }list_node;

  8. typedef struct _manager_node
  9. {
  10.     list_node *start_node;
  11.     list_node *last_node;
  12.     unsigned int addr_start;
  13.     unsigned int addr_end;
  14.     int node_num;
  15.     int available_num;
  16. }manager_node;

  17. manager_node manager_list[3];

  18. list_node list_8_node[400];
  19. list_node list_16_node[200];
  20. list_node list_32_node[100];

  21. #define assert_param(param)          if(param==NULL) return 0;

_mem_pool.c

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include "_mem_pool.h"

  6. list_node *available[3];
  7. list_node *rear[3];

  8. extern manager_node     manager_list[3];
  9. extern list_node     list_8_node[400];
  10. extern list_node     list_16_node[200];
  11. extern list_node     list_32_node[100];

  12. int count = 0;

  13. typedef list_node* (*init_ptr) (list_node **);

  14. list_node * eight_init(list_node **last)
  15. {
  16.     void *a = malloc(8*400);
  17.     printf("the list8 start addr is %x\n",(int)a);
  18.     int i;
  19.     assert_param(a);
  20.     for(i=0;i<400;i++)
  21.     {
  22.         list_8_node[i].addr=a+i*8;
  23.         list_8_node[i].next_node=NULL;
  24.         //printf("the list %d addr is %x\n",i,(int)(list_8_node[i].addr));
  25.         if(i==399)
  26.             break;
  27.         list_8_node[i].next_node=&list_8_node[i+1];
  28.     }
  29.     *last=&(list_8_node[i]);
  30.     printf("the list8 node %dend addr is %x\n\n",i,(int)(list_8_node[i].addr));
  31.     return (&list_8_node[0]);
  32. }

  33. list_node * sixteen_init(list_node **last)
  34. {
  35.     void *a = malloc(16*400);
  36.     printf("the list16 start addr is %x\n",(int)a);
  37.     int i;
  38.     assert_param(a);
  39.     for(i=0;i<400;i++)
  40.     {
  41.         list_16_node[i].addr=a+i*16;
  42.         list_16_node[i].next_node=NULL;
  43.         //printf("the list %d addr is %x\n",i,(int)(list_16_node[i].addr));
  44.         if(i==199)
  45.             break;
  46.         list_16_node[i].next_node=&list_16_node[i+1];
  47.     }
  48.     *last=&(list_16_node[i]);
  49.     printf("the list16 node %dend addr is %x\n\n",i,(int)(list_16_node[i].addr));
  50.     return (&list_16_node[0]);
  51. }

  52. list_node * thirtytwo_init(list_node **last)
  53. {
  54.     void *a = malloc(32*100);
  55.     printf("the list32 start addr is %x\n",(int)a);
  56.     int i;
  57.     assert_param(a);
  58.     for(i=0;i<100;i++)
  59.     {
  60.         list_32_node[i].addr=a+i*32;
  61.         list_32_node[i].next_node=NULL;
  62.         //printf("the list %d addr is %x\n",i,(int)(list_32_node[i].addr));
  63.         if(i==99)
  64.             break;
  65.         list_32_node[i].next_node=&list_32_node[i+1];
  66.     }
  67.     *last=&(list_32_node[i]);
  68. //    printf("%x++++++++++++++\n",(int)((*last)->addr));
  69.     printf("the list32 node %dend addr is %x\n\n",i,(int)(list_32_node[i].addr));
  70.     return (&list_32_node[0]);
  71. }

  72. init_ptr func_ptr[]=
  73. {
  74.     eight_init,
  75.     sixteen_init,
  76.     thirtytwo_init,
  77. };

  78. int mem_pool_init()
  79. {
  80.     int i;
  81.     for(i=0;i<3;i++)
  82.     {
  83.         available[i]=(*func_ptr[i])(&(rear[i]));
  84.     }
  85.     for(i=0;i<3;i++)
  86.     {
  87.         manager_list[i].start_node=available[i];            
  88.         manager_list[i].last_node=rear[i];
  89.         manager_list[i].addr_start=(unsigned int)(available[i]->addr);
  90.         manager_list[i].addr_end=(unsigned int)(manager_list[i].last_node->addr);
  91.         printf("the manager_list[%d] \tstart addr is%x and end addr is %x\n\n",i,(unsigned int)(unsigned int)(manager_list[i].start_node->addr),(unsigned int)(manager_list[i].last_node->addr));
  92.     }    
  93. }


  94. void * mem_alloc(int num)
  95. {
  96.     int i;
  97.     if(num>=1&&num<=8)
  98.         i=0;
  99.     else if(num>=9&&num<=16)
  100.         i=1;
  101.     else if(num>=17&&num<=32)
  102.         i=2;
  103.     else
  104.         return (malloc(num));
  105.     if(manager_list[i].start_node == NULL)
  106.     {
  107.         printf("this is no space in mempool!!");
  108.         return (malloc(num));
  109.     }
  110.     
  111.     list_node *p=manager_list[i].start_node;
  112.     manager_list[i].start_node = manager_list[i].start_node->next_node;
  113.     p->next_node=NULL;
  114.     
  115.     return (p->addr);
  116. }


  117. void mem_free(void *addr)
  118. {
  119.     if(addr==NULL)
  120.         return ;
  121.     int i;
  122.     unsigned int num=(unsigned int)(addr);
  123.     unsigned int start_addr,end_addr;
  124.     for(i=0;i<3;i++)
  125.     {
  126.         start_addr=(unsigned int)available[i]->addr;
  127.         end_addr=(unsigned int)rear[i]->addr;
  128.         if(num>=start_addr&&num<=end_addr)
  129.         break;
  130.     }
  131. //    printf("the free add is %x and the start addr is %x\n",num,start_addr);
  132.     num=(num-start_addr)/(8*(i+1));
  133.     if(num>400)
  134.         return;
  135. //    printf("the free node is %d\tand is nodelist[%d]\n",num,i);
  136.     switch(i)
  137.     {
  138.         case 0:
  139. //            printf("list_8_node[%d] the next is %x\n",num,list_8_node[num].next_node);
  140.             list_8_node[num].next_node=manager_list[i].start_node;
  141.             manager_list[i].start_node=&(list_8_node[num]);
  142.             break;
  143.         case 1:
  144. //            printf("list_16_node[%d] the next is %x\n",num,list_16_node[num].next_node);
  145.             list_16_node[num].next_node=manager_list[i].start_node;
  146.             manager_list[i].start_node=&(list_16_node[num]);
  147.             break;
  148.         case 2:
  149. //            printf("list_32_node[%d] the next is %x\n",num,list_32_node[num].next_node);
  150.             list_32_node[num].next_node=manager_list[i].start_node;
  151.             manager_list[i].start_node=&(list_32_node[num]);
  152.             break;
  153.         default:
  154.             free(addr);
  155.             break;
  156.     }
  157. }

  158. int main()
  159. {
  160.     mem_pool_init();
  161.     int i;
  162.     void *a;
  163.     void *b;
  164.     /*
  165.     for(i=0;i<1000;i++)
  166.     {
  167.         a=mem_alloc(5);
  168.         printf("+++++++++++++++the %d addr is %x\n",i,(unsigned int)a);
  169.         i++;        
  170.         b=mem_alloc(5);
  171.         printf("+++++++++++++++the %d addr is %x\n",i,(unsigned int)b);
  172.         mem_free(a);
  173.         mem_free(b);
  174.     }

  175.     for(i=0;i<1000;i++)
  176.     {
  177.         a=mem_alloc(18);
  178.         printf("+++++++++++++++the %d addr is %x\n",i,(unsigned int)a);
  179.         i++;        
  180.         b=mem_alloc(18);
  181.         printf("+++++++++++++++the %d addr is %x\n",i,(unsigned int)b);
  182.         mem_free(a);
  183.         mem_free(b);
  184.     }
  185.     */
  186.     /*
  187.     for(i=0;i<1000;i++)
  188.     {
  189.         a=mem_alloc(10);
  190.         printf("+++++++++++++++the %d addr is %x\n",i,(unsigned int)a);
  191.         i++;        
  192.         b=mem_alloc(10);
  193.         printf("+++++++++++++++the %d addr is %x\n",i,(unsigned int)b);
  194.         mem_free(a);
  195.         mem_free(b);
  196.     }
  197.     */
  198.     /*
  199.     printf("the mem alloc is %x\n",(int)a);
  200.     //mem_free(a);
  201.     a=mem_alloc(9);
  202.     //printf("the mem alloc 9 ++++++++++++++++++is %x\n",(int)a);
  203.     mem_free(a);
  204.     a=mem_alloc(18);
  205.     printf("the mem alloc is %x\n",(int)a);
  206.     a=mem_alloc(100);
  207.     free(a);
  208.     */
  209.     clock_t t1,t2,ta,tb;
  210.     t1=clock();
  211.     int t;
  212.     for(t=0;t<0x0ffffffa;t++)
  213.     {
  214.         a=mem_alloc(5);
  215.         mem_free(a);
  216.     }
  217.     t2=clock();
  218.     ta=t2-t1;
  219.     t1=clock();
  220.     for(t=0;t<0x0ffffffa;t++)
  221.     {
  222.         a=malloc(5);
  223.         free(a);
  224.     }
  225.     t2=clock();
  226.     tb=t2-t1;
  227.     printf("the time %ld\t%ld\n",ta,tb);
  228. }

makefile文件:

  1. all:     test debug
  2. cc=gcc -Wall
  3. test:
  4.     @$(cc) _mem_pool.c -o test.out
  5. debug:
  6.     @$(cc) -g _mem_pool.c -o test.gdb

测试了一下,和系统的直接分配时间比

大概是6:9
阅读(4821) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~