Malloc source code

Source: Internet
Author: User
The two functions are taken from the unix version 6 malloc. in the c file, one is the malloc function and the other is the mfree function 2515:/* the map array is a list of idle resources, each storage area is defined by its length and relative address */struct map 2516: {2517: char * m_size; 2518: char * m_addr; 2519 :}; 2520: /* ------------------------- */2521: 2522:/* 2523: * allocate size units from the given2524: * map. return the base of the allocated2525: * space.2526: * algorithm is first fit.2527: */2528: malloc (MP, size) 2529: struct map * MP; 2530: {253 1: Register int A; 2532: Register struct map * bp; 2533: 2534: For (BP = mp; BP-> m_size; BP ++) {/* search for a map array, until the end of the map list is reached, or a free space meeting the request length */2535: If (BP-> m_size> = size) is found) {/* the idle block meets the request */2536: A = BP-> m_addr;/* record the first address of the zone */2537: BP-> m_addr = + size; /* the size of the first part of the idle block address is the size block allocated. Therefore, the size of the first part is forward */2538: If (BP-> m_size =-size) = 0) /* The allocated idle block size is exactly equal to the size of the requested space */2539: do {/* move the subsequent records of the space block in the map forward */2540: BP ++; 2541: (bp-1)-> m_addr = B P-> 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 previusly allocated space aa2552: * of size units into the specified map.2553: * sort AA into map and combine on2554: * one or both ends if possible.2555: * // * return a storage area with the size and starting address AA to map */2556: mfree (MP, size, AA) 2557: St Ruct 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 ++);/* search for map until you find the record where the starting address in map is less than the address of the released space */2565: If (BP> MP & (bp-1) -> m_addr + (bp-1)-> m_size = A) {/* release space and previous space consecutively */2566: (bp-1)-> m_size = + size; /* the size of the first element in the map increases the size of the space to be released */2567: if (a + size = BP-> m_addr) {/* Whether the released bucket is adjacent to the next block in map */2568: (bp-1)-> m_size = + bp-> m_size; /* merge the released block and the last block of storage */2569: While (BP-> m_size) {/* Forward the remaining block */2570: BP ++; 2571: (bp-1)-> m_addr = BP-> m_addr; 2572: (bp-1)-> m_size = BP-> m_size; 2573:} 2574:} 2575 :} else {/* not consecutive with the previous space */2576: if (a + size = BP-> m_addr & BP-> m_size) {/* Whether it is consecutive with the next space */2577: BP-> m_addr =-size; 2578: BP-> m_size = + size; 2579:} else if (size) do {/* release space is not 0, add a new record to map, and move the space record in the remainder of the 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 :/*---------------------------*/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.