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