| [658] | 1 | /*
 | 
|---|
 | 2 |  * $Id: memblock.cc,v 1.1.1.1 1999-11-26 16:37:04 ansari Exp $
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  * Copyright (C) 1997 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
 | 5 |  * All rights reserved.  Please see <blitz/blitz.h> for terms and
 | 
|---|
 | 6 |  * conditions of use.
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  * $Log: not supported by cvs2svn $
 | 
|---|
 | 9 | // Revision 1.1.1.1  1999/04/09  17:58:58  ansari
 | 
|---|
 | 10 | // Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
 | 11 | //
 | 
|---|
 | 12 |  * Revision 1.4  1998/03/14 00:04:47  tveldhui
 | 
|---|
 | 13 |  * 0.2-alpha-05
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  * Revision 1.3  1997/07/16 14:51:20  tveldhui
 | 
|---|
 | 16 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
 | 17 |  *
 | 
|---|
 | 18 |  * Revision 1.2  1997/01/24 14:42:00  tveldhui
 | 
|---|
 | 19 |  * Periodic RCS update
 | 
|---|
 | 20 |  *
 | 
|---|
 | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | #ifndef BZ_MEMBLOCK_CC
 | 
|---|
 | 24 | #define BZ_MEMBLOCK_CC
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #ifndef BZ_MEMBLOCK_H
 | 
|---|
 | 27 |  #include <blitz/memblock.h>
 | 
|---|
 | 28 | #endif
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | BZ_NAMESPACE(blitz)
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | // Null memory block for each (template) instantiation of MemoryBlockReference
 | 
|---|
 | 33 | template<class P_type> 
 | 
|---|
 | 34 | NullMemoryBlock<P_type> MemoryBlockReference<P_type>::nullBlock_;
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | template<class P_type>
 | 
|---|
 | 37 | inline void MemoryBlock<P_type>::allocate(int length)
 | 
|---|
 | 38 | {
 | 
|---|
 | 39 |     TAU_TYPE_STRING(p1, "MemoryBlock<T>::allocate() [T="
 | 
|---|
 | 40 |         + CT(P_type) + "]");
 | 
|---|
 | 41 |     TAU_PROFILE(p1, "void ()", TAU_BLITZ);
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | #ifndef BZ_ALIGN_BLOCKS_ON_CACHELINE_BOUNDARY
 | 
|---|
 | 44 |     data_ =  new T_type[length];
 | 
|---|
 | 45 |     dataBlockAddress_ = data_;
 | 
|---|
 | 46 | #else
 | 
|---|
 | 47 |     int numBytes = length * sizeof(T_type);
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |     if (numBytes < 1024)
 | 
|---|
 | 50 |     {
 | 
|---|
 | 51 |         data_ =  new T_type[length];
 | 
|---|
 | 52 |         dataBlockAddress_ = data_;
 | 
|---|
 | 53 |     }
 | 
|---|
 | 54 |     else
 | 
|---|
 | 55 |     {
 | 
|---|
 | 56 |         // We're allocating a large array.  For performance reasons,
 | 
|---|
 | 57 |         // it's advantageous to force the array to start on a
 | 
|---|
 | 58 |         // cache line boundary.  We do this by allocating a little
 | 
|---|
 | 59 |         // more memory than necessary, then shifting the pointer
 | 
|---|
 | 60 |         // to the next cache line boundary.
 | 
|---|
 | 61 |         // NB: This code assumes that T_type has a trivial
 | 
|---|
 | 62 |         // constructor.
 | 
|---|
 | 63 |         const int cacheBlockSize = 128;    // Will work for 32, 16 also
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |         dataBlockAddress_ = new T_type[length
 | 
|---|
 | 66 |             + (cacheBlockSize + sizeof(T_type) - 1) / sizeof(T_type)];
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |         // Shift to the next cache line boundary
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |         ptrdiff_t offset = ptrdiff_t(dataBlockAddress_) % cacheBlockSize;
 | 
|---|
 | 71 |         int shift = (offset == 0) ? 0 : (cacheBlockSize - offset);
 | 
|---|
 | 72 |         data_ = (T_type*)(((char *)dataBlockAddress_) + shift);
 | 
|---|
 | 73 |     }
 | 
|---|
 | 74 | #endif
 | 
|---|
 | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | BZ_NAMESPACE_END
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | #endif // BZ_MEMBLOCK_CC
 | 
|---|