手机版
你好,游客 登录 注册
背景:
阅读新闻

一个仿照Nginx的内存池

[日期:2012-09-15] 来源:Linux社区  作者:caiyun [字体:大 中 小]

mempool.{h,c}

  1. #ifndef _MEMPOOL_H_   
  2. #define _MEMPOOL_H_   
  3.   
  4. #include <stdlib.h>   
  5.   
  6. #define POOL_ALIGNMENT 16   
  7.   
  8. typedef  unsigned char u_char;  
  9.   
  10. typedef struct pool_data_s pool_data_t;  
  11. typedef struct pool_s pool_t;  
  12.   
  13. struct pool_data_s  
  14. {  
  15.     u_char *last;  
  16.     u_char *end;  
  17.     pool_t *next;  
  18.     int failed;  
  19. } ;  
  20.   
  21.   
  22. struct pool_s  
  23. {  
  24.     pool_data_t d;  
  25.     size_t          max;  
  26.     pool_t      *current;  
  27.   
  28. } ;  
  29.   
  30.   
  31. pool_t *create_pool(size_t size);  
  32.   
  33. void *pool_alloc(size_t size);  
  34. void *pool_calloc(size_t size);  
  35.   
  36. void *palloc(pool_t *pool, size_t size);  
  37. void *pnalloc(pool_t *pool, size_t size);  
  38.   
  39. int pool_destory(pool_t *pool);  
  40. int pool_reset(pool_t *pool);  
  41.   
  42. void get_pool_status(pool_t *pool);  
  43.   
  44. #endif  
  1. #include "mempool.h"   
  2. #include "alloc.h"   
  3.   
  4. #include <stdio.h>   
  5.   
  6. static void *palloc_block(pool_t *pool, size_t size);  
  7.   
  8.   
  9. pool_t *create_pool(size_t size)  
  10. {  
  11.         pool_t *p;  
  12.         p = my_memalign(POOL_ALIGNMENT, size);  
  13.         if(p == NULL)  
  14.         {  
  15.                 return p;  
  16.         }  
  17.   
  18.         p->d.last = (u_char *) p + sizeof(pool_t);  
  19.         p->d.end = (u_char *)p + size;  
  20.         p->d.next = NULL;  
  21.         p->d.failed = 0;  
  22.   
  23.         size = p->d.end - p->d.last;  
  24.         p->max = size;  
  25.   
  26.        p->current = p;  
  27.   
  28.        return p;  
  29. }  
  30.   
  31. void *palloc(pool_t *pool, size_t size)  
  32. {  
  33.         void *m;  
  34.         pool_t *p = pool->current;  
  35.   
  36.         if(size >= p->max)  
  37.         {  
  38.                 return NULL;  
  39.         }  
  40.   
  41.         do  
  42.         {  
  43.                 if(p->d.end - p->d.last > size)  
  44.                 {  
  45.                         m = p->d.last;  
  46.                         p->d.last += size;  
  47.                         return m;  
  48.                 }  
  49.                 p = p->d.next;  
  50.   
  51.         } while(p);  
  52.   
  53.         return palloc_block(pool, size);  
  54. }  
  55.   
  56.   
  57. static void *palloc_block(pool_t *pool, size_t size)  
  58. {  
  59.         void *m;  
  60.         pool_t *new_pool, *p, *current;  
  61.         size_t pool_size;  
  62.   
  63.         pool_size = pool->d.end - (u_char *)pool;  
  64.   
  65.         new_pool = my_memalign(POOL_ALIGNMENT, pool_size);  
  66.         if(new_pool == NULL)  
  67.         {  
  68.                 return NULL;  
  69.         }  
  70.   
  71.     //init new_pool   
  72.         new_pool->d.last = (u_char *)new_pool + sizeof(pool_t);  
  73.         new_pool->d.end = (u_char *)new_pool + pool_size;  
  74.         new_pool->d.next = NULL;  
  75.         new_pool->d.failed = 0;  
  76.     new_pool->max = new_pool->d.end - new_pool->d.last;  
  77.   
  78.     //因为原来的pool_t不能分配才到这个函数,所以从current开始的pool_t块都failed加1   
  79.     //当pool->current指向的块的failed超过4时,current指向下一个块   
  80.     //其实前面的块失败次数永远  >= 后面的块,所以当一个块failed < 4时,最后的块也不会改变current了   
  81.         current = pool->current;  
  82.         for(p = current; p->d.next; p = p->d.next)  
  83.         {  
  84.         if(p->d.failed++ > 4)  
  85.         {  
  86.             current = p->d.next;  
  87.         }  
  88.         }  
  89.   
  90.     //无论如何,new_pool都是在单链表的尾部,上面的p沿current开始直到尾部   
  91.         p->d.next = new_pool;  
  92.   
  93.     //如果当前的current指向了原本链表最后一块的d.next,那么   
  94.         pool->current = current ? current : new_pool;  
  95.   
  96.   
  97.         m = new_pool->d.last;  
  98.         new_pool->d.last += size;  
  99.         return m;  
  100. }  
  101.   
  102.   
  103. void get_pool_status(pool_t *pool)  
  104. {  
  105.     int n = 0;  
  106.     pool_t *p = pool;  
  107.   
  108.     printf("**********************************************************************\n");  
  109.     for(; p; p = p->d.next, n++)  
  110.     {  
  111.         printf("pool:%d  ", n);  
  112.         printf("max:%d  left:%d\n", p->max, p->d.end - p->d.last);  
  113.     }  
  114.     printf("**********************************************************************\n");  
  115. }  
  116.   
  117. int pool_reset(pool_t *pool)  
  118. {  
  119.     pool_t *pp = pool;  
  120.     pool->current = pool;  
  121.     for(; pp ; pp = pp->d.next)  
  122.     {  
  123.         pp->d.last = (u_char *)pp + sizeof(pool_t);  
  124.         pp->d.failed = 0;  
  125.     }  
  126.   
  127.     return 0;  
  128. }  
  129.   
  130. int pool_destory(pool_t *pool)  
  131. {  
  132.     pool_t *pp,  *p;  
  133.   
  134.     pp = pool;  
  135.     p = pp->d.next;  
  136.     for(; pp; pp = p, p = pp->d.next)  
  137.     {  
  138.         pp->d.next = NULL;  
  139.         free(pp);  
  140.         if(p == NULL)  
  141.         {  
  142.             break;  
  143.         }  
  144.     }  
  145.     return 0;  
  146. }  
alloc.{h, c}
  1. #ifndef _ALLOC_H_   
  2. #define _ALLOC_H_   
  3.   
  4. #include <stdlib.h>   
  5.   
  6. void *my_memalign(size_t alignment, size_t size);  
  7.   
  8. #endif  
  1. #include "alloc.h"   
  2. #include <stdlib.h>   
  3.   
  4. void *my_memalign(size_t alignment, size_t size)  
  5. {  
  6.         void *p;  
  7.         int err;  
  8.   
  9.         err = posix_memalign(&p, alignment, size);  
  10.   
  11.         return p;  
  12. }  
mempool_test.c
  1. #include "mempool.h"   
  2. #include <stdio.h>   
  3.   
  4. #define ITEM 20   
  5.   
  6. typedef struct  
  7. {  
  8.         int a[10];  
  9. } data_t;  
  10.   
  11.   
  12.   
  13. int main()  
  14. {  
  15.         pool_t *pool;  
  16.         data_t *p[ITEM];  
  17.         size_t size;  
  18.         int i;  
  19.   
  20.     size = 128;  
  21.         pool = create_pool(size);  
  22.         get_pool_status(pool);  
  23.   
  24.     for(i = 0; i < ITEM ;i++)  
  25.     {  
  26.         p[i] = palloc(pool, sizeof(data_t));  
  27.         p[i]->a[0] = i;  
  28.     }  
  29.   
  30.     get_pool_status(pool);  
  31.     pool_reset(pool);  
  32.     get_pool_status(pool);  
  33.     pool_destory(pool);  
  34.     pool = NULL;  
  35.     get_pool_status(pool);  
  36.   
  37.         return 0;  
  38. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款