美文网首页
sqlite mem2.c debug模式下的统计直方图

sqlite mem2.c debug模式下的统计直方图

作者: zhouyao | 来源:发表于2017-02-24 10:58 被阅读30次

    void **

    void** array;
    int arrayLen = 10;
    array = (void**)malloc(arrayLen * sizeof(void*));
    
    some_type_t* some_object_ptr;    
    // The following two assignment are equivalent since in C,
    // array[index] <=> *(array + index)
    array[index] = (void*)some_object_ptr;
    *(array + index) = (void*)some_object_ptr;
    

    void *可以指向任何数据结构的指针
    stackoverflow void **

    void * 中文博客

    mem 统计直方图 直方图的实现

    mem 分配中,统计直方图的实现mem2.c

    static void adjustStats(int iSize, int increment){# icrement的值为正或者负
      int i = ROUND8(iSize)/8;//以8为间隔
      if( i>NCSIZE-1 ){//大于800byte的统一归在一个图中
        i = NCSIZE - 1;
      }
      if( increment>0 ){
        mem.nAlloc[i]++;
        mem.nCurrent[i]++;
        if( mem.nCurrent[i]>mem.mxCurrent[i] ){
          mem.mxCurrent[i] = mem.nCurrent[i];
        }
    }else{//释放就减小
        mem.nCurrent[i]--;
        assert( mem.nCurrent[i]>=0 );
      }
    }
    

    mem结果中有一些结构如下

    ...
    int nAlloc[NCSIZE];      /* Total number of allocations */
    int nCurrent[NCSIZE];    /* Current number of allocations */
    int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
    ...
    

    对8向上取倍数#define ROUND8(x) (((x)+7)&~7) 向下取倍数#define ROUNDDOWN8(x) ((x)&~7)

    为了让代码更有语意。需要对代码进行包装例如下面的代码,也是解耦的一种做法

    /*
    ** Round up a request size to the next valid allocation size.
    */
    static int sqlite3MemRoundup(int n){
      return ROUND8(n);
    }
    

    Scratch memory用来进行例如btree整理的时候,用的临时内存

    look aside allocator slite 常常会分配一些很小的内存来存放例如the names of tables and columns, parse tree nodes, individual query results values, and B-Tree cursor objects.等等,利用系统的malloc就会很浪费cpu时间,所以开发出了一个相对高效的 look aside allocator

    mem2.c 内存管理

    sqlite 内存分配管理

    1. 分配失败之后的管理。如果分配内存失败,会释放掉,cache page。在 pcache1AllocPage函数中实现
    2. 没有内存泄露,应用程序负责销毁
    3. 可以设置内存使用限制。可以重复利用分配的cache
    4. zero-malloc option sqlite会先分配一个大的bulk memory。然后在里面自己组织内存分配
    5. 程序自定义,内存分配程序
    6. 提供内存统计功能
    7. sqltie最小化使用系统的malloc free

    当开启 debug 模式之后,会使用 instrumented memory allocator
    有特殊的函数,来模拟out of memery

    sqlite3_free 必须是之前sqlite3Malloc或者sqlite3_realloc函数返回的指针

    sqlite3_realloc(X,N)重新组织之前分配的内存,X是分配内存的位置指针,如果x为空指针,结果跟 allocatte下效果一样.如果N为0就跟 free的效果一样

    sqlite3Malloc模式

    当使用bebug模式,会在系统提供的函数中,提供wapper 会额外分配100byte 来防止防止超出边界的哨兵。这100个byte 还会记录测试脚本的名称,方便测试

    memsys5选项,使用zero-malloc option 会事先分配一个大内存自己管理,通常在嵌入式系统中会使用
    memsys3类似zero-malloc 只是不提供严格的,memory fragmentation and breakdown mem3是 mem5的前身 . 建议所有希望zero-config的都应该使用mem5而不是使用mem3 mem3很可能在未来从代码库中去掉 mem4mem6是实验性的代码都被删除了

    默认是memsys1 debug模式下是memsys2

    相关文章

      网友评论

          本文标题:sqlite mem2.c debug模式下的统计直方图

          本文链接:https://www.haomeiwen.com/subject/zvtwwttx.html