日期:2014-05-16  浏览次数:20709 次

qmail中两个函数的问题
qmail源码中两个函数看不太明白
struct cdbmake_hp 
{
  uint32 h; uint32 p; 
} ;

struct cdbmake_hplist {
  struct cdbmake_hp hp[CDBMAKE_HPLIST];
  struct cdbmake_hplist *next;
  int num;
} ;

struct cdbmake {
  char final[2048];
  uint32 count[256];
  uint32 start[256];
  struct cdbmake_hplist *head;
  struct cdbmake_hp *split;
  struct cdbmake_hp *hash;
  uint32 numentries;
} ; //结构体

//函数正体

int cdbmake_split(cdbm,alloc)
struct cdbmake *cdbm;
char *(*alloc)();
{
  int i;
  uint32 u;
  uint32 memsize;
  struct cdbmake_hplist *x;

  for (i = 0;i < 256;++i)
  cdbm->count[i] = 0;

  for (x = cdbm->head;x;x = x->next) {
  i = x->num;
  while (i--)
  ++cdbm->count[255 & x->hp[i].h];
  }

  memsize = 1;
  for (i = 0;i < 256;++i) {
  u = cdbm->count[i] * 2;
  if (u > memsize)
  memsize = u;
  }

  memsize += cdbm->numentries; /* no overflow possible up to now */
  u = (uint32) 0 - (uint32) 1;
  u /= sizeof(struct cdbmake_hp);
  if (memsize > u) return 0;

  cdbm->split = (struct cdbmake_hp *) alloc(memsize * sizeof(struct cdbmake_hp));
  if (!cdbm->split) return 0;

  cdbm->hash = cdbm->split + cdbm->numentries;

  u = 0;
  for (i = 0;i < 256;++i) {
  u += cdbm->count[i]; /* bounded by numentries, so no overflow */
  cdbm->start[i] = u;
  }

  for (x = cdbm->head;x;x = x->next) {
  i = x->num;
  while (i--)
  cdbm->split[--cdbm->start[255 & x->hp[i].h]] = x->hp[i];
  }

  return 1;
}



uint32 cdbmake_throw(cdbm,pos,b)
struct cdbmake *cdbm;
uint32 pos;
int b;
{
  uint32 len;
  uint32 j;
  uint32 count;
  struct cdbmake_hp *hp;
  uint32 where;

  count = cdbm->count[b];

  len = count + count; /* no overflow possible */
  cdbmake_pack(cdbm->final + 8 * b,pos);
  cdbmake_pack(cdbm->final + 8 * b + 4,len);

  if (len) {
  for (j = 0;j < len;++j)
  cdbm->hash[j].h = cdbm->hash[j].p = 0;

  hp = cdbm->split + cdbm->start[b];
  for (j = 0;j < count;++j) {
  where = (hp->h >> 8) % len;
  while (cdbm->hash[where].p)
if (++where == len)
where = 0;
  cdbm->hash[where] = *hp++;
  }
  }

  return len;
}


看不明白这两个函数到底是要做什么


------解决方案--------------------
int cdbmake_split(cdbm,alloc) 
struct cdbmake *cdbm; 
char *(*alloc)(); 
晚上心静些,大概看了下
该函数的意思就是给cdbmake *cdbm结构指针,制定用alloc指针函数指的函数体
根据其对象cdbmake_hplist链表分配其对象cdbmake_hp *split的内存空间,具体细节跟存储算法有关