00001 //# AlignMemory.h: Class to specify and calculate memory alignment 00002 //# Copyright (C) 2014 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: CountedPtr.h 21469 2014-08-12 11:25:55Z gervandiepen $ 00027 00028 #ifndef CASA_ALIGNMEMORY_H 00029 #define CASA_ALIGNMEMORY_H 00030 00031 #include <casacore/casa/aips.h> 00032 #include <stddef.h> 00033 00034 00035 namespace casacore { //#Begin casa namespace 00036 00037 // <summary>Referenced counted pointer for constant data</summary> 00038 // <use visibility=export> 00039 // <reviewed reviewer="Friso Olnon" date="1995/03/15" tests="tCountedPtr" demos=""> 00040 00041 // <etymology> 00042 // This class is <em>Counted</em> because it is reference counted. 00043 // </etymology> 00044 00045 // <synopsis> 00046 // This class implements a reference counting mechanism. It 00047 // allows <src>CountedPtr</src>s to be passed around freely, 00048 // incrementing or decrementing the reference count as needed when one 00049 // <src>CountedPtr</src> is assigned to another. When the 00050 // reference count reaches zero the internal storage is deleted by 00051 // default, but this behavior can be overridden. 00052 // 00053 // Internally the class uses std::shared_ptr to be thread-safe. Note that 00054 // tr1 is used if the compiler does not support C++11 yet. 00055 // </synopsis> 00056 00057 // <motivation> 00058 // Reference counting 00059 // </motivation> 00060 00061 class AlignMemory 00062 { 00063 00064 public: 00065 // Default alignment is none. 00066 explicit AlignMemory (uInt alignment=0) 00067 : itsAlign(alignment) 00068 {} 00069 00070 // Get the alignment. 00071 uInt alignment() const 00072 { return itsAlign; } 00073 00074 // Allocate the given amount of memory with the correct alignment. 00075 // If alignment < sizeof(void*), malloc will be used, otherwise posix_memalign. 00076 // The alignment must be a power of 2 for posix_memalign to succeed. 00077 // It can be freed with the normal free. 00078 void* alloc (size_t size) const; 00079 00080 private: 00081 uInt itsAlign; 00082 }; 00083 00084 } //#End casa namespace 00085 00086 #endif