xvid算法看到的,不太明白为什么要这样做?

void * xvid_malloc(int size,unsigned char alignment)
{
 unsigned char *mem_ptr;

 if (!alignment)
 {

  /* We have not to satisfy any alignment */
  if ((mem_ptr = (unsigned char *) malloc(size + 1)) != NULL)
  {

   /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-1) */
   *mem_ptr = 1;

   /* Return the mem_ptr pointer */
   return (void *)(mem_ptr+1);

  }

 }
 else
 {
  unsigned char *tmp;

  /*
   * Allocate the required size memory + alignment so we
   * can realign the data if necessary
   */

  if ((tmp = (unsigned char *) malloc(size + alignment)) != NULL)
  {

   /* Align the tmp pointer */
   mem_ptr =(unsigned char *) ((unsigned int) (tmp + alignment - 1) &
      (~(unsigned int) (alignment - 1)));

   /*
    * Special case where malloc have already satisfied the alignment
    * We must add alignment to mem_ptr because we must store
    * (mem_ptr - tmp) in *(mem_ptr-1)
    * If we do not add alignment to mem_ptr then *(mem_ptr-1) points
    * to a forbidden memory space
    */
   if (mem_ptr == tmp)
    mem_ptr += alignment;

   /*
    * (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve
    * the real malloc block allocated and free it in xvid_free
    */
   *(mem_ptr - 1) = (unsigned char) (mem_ptr - tmp);

   /* Return the aligned pointer */
   return (void *) mem_ptr;

  }
 }

 return NULL;

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值