LCC Compiler source program analysis (68) Memory allocation list

Source: Internet
Author: User
Tags assert
LCC uses a large chunk of memory, it is also very special allocation of memory, its source program is as follows: #001  //large memory structure. #002  struct block #003  {#004      struct block *next;//successor block pointer. #005      Char *limit; Tail position #006      char *avail; The available start position. #007  }; #008   #009  //share the largest type. #010  union align #011  {#012      long l; #013      char *p; #014       Double D; #015      Int (*f) (void); #016  }; #017   #018  //#019  union header #020  {#021      struct block B; #022       Union align A; #023  }; #024   #025   #ifdef PURIFY #026  union header *arena[3]; #027   #028  void *allocate (unsigned long n, unsigned a) {#029      union header *new = malloc (sizeof *new + N); #030   #031      assert (A < Nelems (arena)); #032      if (new = = NULL) {#033           error ("Insufficient memory/n"); #034           exit (1); #035     } #036      new->b.next = (void *) arena[a]; #037      Arena[a] = new; #038      return new + 1; #039  } #040   #041  void deallocate (unsigned a) {#042      Union header *p, *q; #043 &nbs P #044      assert (A < Nelems (arena)); #045      for (p = arena[a]; p = q) {#046           q = (void *) p->b.next; #047           Free (p); #048     } #049      arena[a] = NULL; #050  } #051   #052  void *newarray (unsigned long m, unsigned long n, unsigned a) {#053    &nbs P Return allocate (m*n, a); #054 &nbsp, #055   #else #056   #057 //three chunks of memory start head. #058  static struct block first[] = {#059      {null}, {null}, {null} #060  }; #061   #062  //The tail pointer of three large chunks of memory. #063  static struct block *arena[] = {&first[0], &first[1], &first[2]}; #064   #065 The memory head pointer of the  //free block. #066  static struct block *freeblocks; #067   #068  //allocates n bytes in the A zone. #069  void *allocate (unsigned long n, unsigned a) #070  {#071      struct block *ap; #072 &nbs P #073      assert (A < Nelems (arena)); #074      assert (n > 0); #075   #076     //Get the tail block pointer. #077      ap = Arena[a]; #078   #079     //Allocate memory and memory headers that need to be used. #080      n = Roundup (n, sizeof (union align)); #081   #082     //free memory is greater than the need to allocate. #083      while (n > (unsigned long) (ap->limit-ap->avail)) #084      {#085          //If there are free blocks in the list. #086           if ((Ap->next = freeblocks)! = NULL) #087            {#088                 //Get large chunks of free memory already allocated. #089                 freeblocks = freeblocks->next; #090                 ap = ap->next; #091          } #092            Else #093           {#094                 //No large chunk of memory to begin allocating large chunks of memory. #095                 Unsigned m = sizeof (Union header) + N + roundup (10*1024, sizeof (Union align)); #096                 #097                 //Memory real start address. #098                 Ap->next = ( block*) malloc (m); #099   #100                // Point to the tail pointer. #101                 ap = ap->next; #102                 #103                 //allocating memory for errors. #104                 if (AP = = NULL) #105                 {#106                       error ("Insufficient memory/n"); #107                       exit (1); #108                } #109   #110                //memory block footer address. #111                 Ap->limit = ( char *) AP + M; #112          } #113   #114           //can actually use the memory start location. #115           ap->avail = (char *) ((Union header *) AP + 1); #116   #117          //DownA piece of memory is empty. #118           ap->next = NULL; #119           Arena[a] = AP; #120     } #121   #122     //Move the idle start position of large chunks of memory, N is the memory that needs to be allocated. #123      Ap->avail + = n; #124   #125     //returns the assigned memory address start location. #126      return ap->avail-n; #127  }    

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.