home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,969 of 33,346   
   Dave Harris to David Lowndes   
   Re: Sequence container capacity after ca   
   02 Apr 13 14:05:22   
   
   From: brangdonj@googlemail.com   
      
   In article ,   
   DavidL@example.invalid (David Lowndes) wrote:   
   > >Allocations and deallocations are way cheaper than they were old   
   > >times.   
   >   
   > Why, what's magically changed?   
      
   Better algorithms. In the olden days, there would be a single heap   
   used for blocks of all sizes. Allocation code would have to search   
   for a free block of a suitable size, and potentially split it in   
   two, and deallocation code would potentially have to coalesce   
   adjacent free blocks.   
      
   I believe modern allocators keep, in effect, a free list for each   
   size of block. Allocation code is like:   
      
        void *alloc( size_t sz ) {   
            size_t index = sz / 8;   
            if (index < 256)   
                if (Block *pBlock = freeList[index]) {   
                    freeList[index]->next = pBlock->next;   
                    pBlock->next = (Block *)index;   
                    return pBlock->memory;   
                }   
            // ...   
        }   
      
        void free( void *memory ) {   
            if (!memory)   
                return;   
            Block *pBlock = (Block *)((char *)memory - sizeof(Block *));   
            size_t index = (size_t) pBlock->next;   
            if (index < 256) {   
                pBlock->next = freeList[index];   
                freeList[index] = pBlock;   
                return;   
            }   
            // ...   
        }   
      
   So in the common case, there is no looping at all, and not many   
   if-statements.   
      
   -- Dave Harris, Nottingham, UK.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca