AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

fastAlloc.C

Go to the documentation of this file.
00001 
00006 #include "fastAlloc.h"
00007 
00008 #define MINALLOC (1)
00009 
00010 inline unsigned int Max(const unsigned int a, const unsigned int b)
00011   {
00012    return((a > b) ? a : b);
00013   }
00014 
00015 /*
00016 *************************************************************************
00017 *                                                                       *
00018 * fastAlloc::fastAlloc(const unsigned int size,                         *
00019 *                      const unsigned int atatime)                      *
00020 *                                                                       *
00021 * Class constructor fastAlloc() creates a class instance which returns  *
00022 * blocks of size bytes.  When new blocks are allocated, atatime blocks  *
00023 * are allocated.                                                        *
00024 *                                                                       *
00025 * We want to align the data on powers-of-two boundaries 4, 8, or 16     *
00026 * since many architectures restrict the address of some data objects    *
00027 * such as doubles.                                                      *
00028 *                                                                       *
00029 *************************************************************************
00030 */
00031 
00032 fastAlloc::fastAlloc(const unsigned int size, const unsigned int atatime)
00033   {
00034    const unsigned int block = Max(size, sizeof(struct link *));
00035    const unsigned int align = ((block > 8) ? 16 : ((block > 4) ? 8 : 4));
00036    blocksize = (block+align-1) & (~(align-1));
00037    nblocks = Max(atatime, MINALLOC);
00038    head = ((struct link *) 0);
00039    top = new longlink;
00040    cur = top;
00041   }
00042 
00043 /*
00044 *************************************************************************
00045 *                                                                       *
00046 * fastAlloc::malloc()                                                   *
00047 *                                                                       *
00048 * Function malloc() allocates new memory, which is linked into the free *
00049 * memory list.                                                          *
00050 *                                                                       *
00051 *************************************************************************
00052 */
00053 
00054 void fastAlloc::malloc()
00055   {
00056    char *chunk, *end;
00057 
00058    chunk = new char[blocksize*nblocks];
00059    assert (chunk != 0);
00060 
00061    end = chunk+(nblocks-1)*blocksize;
00062    for (char *p = chunk; p < end; p += blocksize)
00063       ((struct link *) p)->next == (struct link *) (p+blocksize);
00064 
00065    cur->item = head = (struct link *) chunk;
00066    if (!cur->next) cur->next = new longlink;
00067    cur = cur->next;
00068    ((struct link *) end)->next == ((struct link *) 0);
00069   }


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04