malloc和mfree原始碼

來源:互聯網
上載者:User

兩個函數取自UNIX 版本6 malloc.c檔案,一個為malloc函數,一個為mfree函數

2515:/*map數組是一個空閑資源清單,其中每個儲存區由其長度和相對位址定義*/
      struct map 
2516: {
2517:         char *m_size;
2518:         char *m_addr;
2519: };
2520: /* ---------------------------       */
2521:
2522: /*
2523:  * Allocate size units from the given
2524:  * map. Return the base of the allocated
2525:  * space.
2526:  * Algorithm is first fit.
2527:  */
2528: malloc(mp, size)
2529: struct map *mp;
2530: {
2531:         register int a;
2532:         register struct map *bp;
2533:
2534:         for (bp = mp; bp->m_size; bp++) { /*搜尋map數組,直到到達map列表尾部,或者搜尋到一個長度滿足請求的空閑空間*/
2535:                 if (bp->m_size >= size) { /*該空閑塊滿足請求*/
2536:                         a = bp->m_addr;   /*記錄該區首地址*/
2537:                         bp->m_addr =+ size; /*空閑塊地址首端大小為size塊被分配,因此首地址前移size*/
2538:                         if ((bp->m_size =- size) == 0) /*分配的空閑塊大小剛好等於請求空間的大小*/
2539:                                 do {    /*將該空間塊在map中的後繼記錄前移*/
2540:                                         bp++;
2541:                                         (bp-1)->m_addr = bp->m_addr;
2542:                                 } while ((bp-1)->m_size = bp->m_size);
2543:                         return(a);
2544:                 }
2545:         }
2546:         return(0);
2547: }
2548: /* ---------------------------       */
2549:
2550: /*
2551:  * Free the previously allocated space aa
2552:  * of size units into the specified map.
2553:  * Sort aa into map and combine on
2554:  * one or both ends if possible.
2555:  */
  /*將一個大小為size,起始地址為aa的儲存區還給map*/
2556: mfree(mp, size, aa)
2557: struct map *mp;
2558: {
2559:         register struct map *bp;
2560:         register int t;
2561:         register int a;
2562:
2563:         a = aa;
2564:         for (bp = mp; bp->m_addr<=a && bp->m_size!=0; bp++); /*搜尋map,直到找到map中起始地址小於釋放空間地址的記錄*/
2565:         if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) { /*釋放空間和前一空間連續*/
2566:                 (bp-1)->m_size =+ size; /*map中前一元素的大小增加被釋放空間的大小*/
2567:                 if (a+size == bp->m_addr) { /*釋放的儲存區是否與map中下一個區塊相鄰*/
2568:                         (bp-1)->m_size =+ bp->m_size; /*合并被釋放塊和後一Block Storage區*/
2569:                         while (bp->m_size) { /*前移餘下的儲存區*/
2570:                                 bp++;
2571:                                 (bp-1)->m_addr = bp->m_addr;
2572:                                 (bp-1)->m_size = bp->m_size;
2573:                         }
2574:                 }
2575:         } else { /*不與前一空間連續*/
2576:                 if (a+size == bp->m_addr && bp->m_size) { /*是否與後一空間連續*/
2577:                         bp->m_addr =- size;
2578:                         bp->m_size =+ size;
2579:                 } else if (size) do { /*釋放空間大小不為0,在map中加入新記錄,後移map中餘下的空間記錄*/
2580:                         t = bp->m_addr;
2581:                         bp->m_addr = a;
2582:                         a = t;
2583:                         t = bp->m_size;
2584:                         bp->m_size = size;
2585:                         bp++;
2586:                 } while (size = t);
2587:         }
2588: }
2589: /* ---------------------------       */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.