zoukankan      html  css  js  c++  java
  • malloc free

    /*
     * File:        alloc.c
     * Purpose:     generic malloc() and free() engine
     *
     * Notes:       99% of this code stolen/borrowed from the K&R C
     *              examples.
     *
     */
    
    #include "common.h"
    #include "stdlib.h"
    
    #pragma section = "HEAP"
    
    /********************************************************************/
    
    /*
     * This struct forms the minimum block size which is allocated, and
     * also forms the linked list for the memory space used with alloc()
     * and free().  It is padded so that on a 32-bit machine, all malloc'ed
     * pointers are 16-byte aligned.
     */
    typedef struct ALLOC_HDR
    {
      struct
      {
        struct ALLOC_HDR *ptr;
        unsigned int size;
      } s;
      unsigned int align;
      unsigned int pad;
    } ALLOC_HDR;
    
    static ALLOC_HDR base;
    static ALLOC_HDR *freep = NULL;
    
    /********************************************************************/
    void free( void *ap )
    {
      ALLOC_HDR *bp, *p;
    
      bp = (ALLOC_HDR *) ap - 1; /* point to block header */
      for ( p = freep; !( ( bp > p ) && ( bp < p->s.ptr ) ); p = p->s.ptr )
      {
        if ( ( p >= p->s.ptr ) && ( ( bp > p ) || ( bp < p->s.ptr ) ) )
        {
          break; /* freed block at start or end of arena */
        }
      }
    
      if ( ( bp + bp->s.size ) == p->s.ptr )
      {
        bp->s.size += p->s.ptr->s.size;
        bp->s.ptr = p->s.ptr->s.ptr;
      }
      else
      {
        bp->s.ptr = p->s.ptr;
      }
    
      if ( ( p + p->s.size ) == bp )
      {
        p->s.size += bp->s.size;
        p->s.ptr = bp->s.ptr;
      }
      else
      {
        p->s.ptr = bp;
      }
    
      freep = p;
    }
    
    /********************************************************************/
    void * malloc( unsigned nbytes )
    {
      /* Get addresses for the HEAP start and end */
      char* __HEAP_START = __section_begin("HEAP");
      char* __HEAP_END = __section_end("HEAP");
    
      ALLOC_HDR *p, *prevp;
      unsigned nunits;
    
      nunits = ( ( nbytes + sizeof(ALLOC_HDR) - 1 ) / sizeof(ALLOC_HDR) ) + 1;
    
      if ( ( prevp = freep ) == NULL )
      {
        p = (ALLOC_HDR *) __HEAP_START;
        p->s.size = ( ( (uint32) __HEAP_END - (uint32) __HEAP_START )
          / sizeof(ALLOC_HDR) );
        p->s.ptr = &base;
        base.s.ptr = p;
        base.s.size = 0;
        prevp = freep = &base;
      }
    
      for ( p = prevp->s.ptr;; prevp = p, p = p->s.ptr )
      {
        if ( p->s.size >= nunits )
        {
          if ( p->s.size == nunits )
          {
            prevp->s.ptr = p->s.ptr;
          }
          else
          {
            p->s.size -= nunits;
            p += p->s.size;
            p->s.size = nunits;
          }
          freep = prevp;
          return (void *) ( p + 1 );
        }
    
        if ( p == freep )
          return NULL;
      }
    }
    
    /********************************************************************/

  • 相关阅读:
    KingPaper初探Java之初学者编码遇到的问题
    KingPaper初探redis之redis数据类型解析(String类型)
    KingPaper初探Java之面向对象对象的声名和实例化(一)
    KingPaper初探百度应用之百度地图API
    MYSQL之用户授权
    nginx入门到精通目录
    nginx入门篇负载均衡策略
    nginx入门篇功能特性
    开博啦
    ubuntu14.04下pycharm的安装及破解
  • 原文地址:https://www.cnblogs.com/shangdawei/p/3036831.html
Copyright © 2011-2022 走看看