From 2a843e9a6bb5cb70f69794419ab4a7d16ee3c6cb Mon Sep 17 00:00:00 2001
From: brad kittenbrink <brad@lindenlab.com>
Date: Thu, 12 May 2011 16:09:42 -0700
Subject: [PATCH] Fix up alignment problems for debug build. reviewed by davep.

---
 indra/llcommon/llmemory.h            | 12 ++++-
 indra/llmath/llvolume.cpp            | 78 ++++++++++++++--------------
 indra/llrender/llvertexbuffer.cpp    | 24 ++++-----
 indra/newview/llpolymesh.cpp         |  4 +-
 indra/newview/llspatialpartition.cpp |  4 +-
 5 files changed, 65 insertions(+), 57 deletions(-)

diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h
index 0adb78236ea..3bd14035768 100644
--- a/indra/llcommon/llmemory.h
+++ b/indra/llcommon/llmemory.h
@@ -28,7 +28,7 @@
 
 #include "llmemtype.h"
 
-#if 0  //DON'T use ll_aligned_foo now that we use tcmalloc everywhere (tcmalloc aligns automatically at appropriate intervals)
+#if LL_DEBUG
 inline void* ll_aligned_malloc( size_t size, int align )
 {
 	void* mem = malloc( size + (align - 1) + sizeof(void*) );
@@ -95,7 +95,15 @@ inline void ll_aligned_free_32(void *p)
 	free(p); // posix_memalign() is compatible with heap deallocator
 #endif
 }
-#endif
+#else // LL_DEBUG
+// ll_aligned_foo are noops now that we use tcmalloc everywhere (tcmalloc aligns automatically at appropriate intervals)
+#define ll_aligned_malloc( size, align ) malloc(size)
+#define ll_aligned_free( ptr ) free(ptr)
+#define ll_aligned_malloc_16 malloc
+#define ll_aligned_free_16 free
+#define ll_aligned_malloc_32 malloc
+#define ll_aligned_free_32 free
+#endif // LL_DEBUG
 
 class LL_COMMON_API LLMemory
 {
diff --git a/indra/llmath/llvolume.cpp b/indra/llmath/llvolume.cpp
index 70e1e1f312e..b3446b72b89 100644
--- a/indra/llmath/llvolume.cpp
+++ b/indra/llmath/llvolume.cpp
@@ -1883,9 +1883,9 @@ LLVolume::~LLVolume()
 	mProfilep = NULL;
 	mVolumeFaces.clear();
 
-	free(mHullPoints);
+	ll_aligned_free_16(mHullPoints);
 	mHullPoints = NULL;
-	free(mHullIndices);
+	ll_aligned_free_16(mHullIndices);
 	mHullIndices = NULL;
 }
 
@@ -2007,7 +2007,7 @@ void LLVolumeFace::VertexData::init()
 {
 	if (!mData)
 	{
-		mData = (LLVector4a*) malloc(sizeof(LLVector4a)*2);
+		mData = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*2);
 	}
 }
 
@@ -2036,7 +2036,7 @@ const LLVolumeFace::VertexData& LLVolumeFace::VertexData::operator=(const LLVolu
 
 LLVolumeFace::VertexData::~VertexData()
 {
-	free(mData);
+	ll_aligned_free_16(mData);
 	mData = NULL;
 }
 
@@ -5192,7 +5192,7 @@ LLVolumeFace::LLVolumeFace() :
 	mWeights(NULL),
 	mOctree(NULL)
 {
-	mExtents = (LLVector4a*) malloc(sizeof(LLVector4a)*3);
+	mExtents = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*3);
 	mCenter = mExtents+2;
 }
 
@@ -5213,7 +5213,7 @@ LLVolumeFace::LLVolumeFace(const LLVolumeFace& src)
 	mWeights(NULL),
 	mOctree(NULL)
 { 
-	mExtents = (LLVector4a*) malloc(sizeof(LLVector4a)*3);
+	mExtents = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*3);
 	mCenter = mExtents+2;
 	*this = src;
 }
@@ -5263,7 +5263,7 @@ LLVolumeFace& LLVolumeFace::operator=(const LLVolumeFace& src)
 		}
 		else
 		{
-			free(mBinormals);
+			ll_aligned_free_16(mBinormals);
 			mBinormals = NULL;
 		}
 
@@ -5274,7 +5274,7 @@ LLVolumeFace& LLVolumeFace::operator=(const LLVolumeFace& src)
 		}
 		else
 		{
-			free(mWeights);
+			ll_aligned_free_16(mWeights);
 			mWeights = NULL;
 		}
 	}
@@ -5292,7 +5292,7 @@ LLVolumeFace& LLVolumeFace::operator=(const LLVolumeFace& src)
 
 LLVolumeFace::~LLVolumeFace()
 {
-	free(mExtents);
+	ll_aligned_free_16(mExtents);
 	mExtents = NULL;
 
 	freeData();
@@ -5300,17 +5300,17 @@ LLVolumeFace::~LLVolumeFace()
 
 void LLVolumeFace::freeData()
 {
-	free(mPositions);
+	ll_aligned_free_16(mPositions);
 	mPositions = NULL;
-	free( mNormals);
+	ll_aligned_free_16( mNormals);
 	mNormals = NULL;
-	free(mTexCoords);
+	ll_aligned_free_16(mTexCoords);
 	mTexCoords = NULL;
-	free(mIndices);
+	ll_aligned_free_16(mIndices);
 	mIndices = NULL;
-	free(mBinormals);
+	ll_aligned_free_16(mBinormals);
 	mBinormals = NULL;
-	free(mWeights);
+	ll_aligned_free_16(mWeights);
 	mWeights = NULL;
 
 	delete mOctree;
@@ -5827,21 +5827,21 @@ void LLVolumeFace::cacheOptimize()
 	
 	//allocate space for new buffer
 	S32 num_verts = mNumVertices;
-	LLVector4a* pos = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
-	LLVector4a* norm = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+	LLVector4a* pos = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
+	LLVector4a* norm = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 	S32 size = ((num_verts*sizeof(LLVector2)) + 0xF) & ~0xF;
-	LLVector2* tc = (LLVector2*) malloc(size);
+	LLVector2* tc = (LLVector2*) ll_aligned_malloc_16(size);
 
 	LLVector4a* wght = NULL;
 	if (mWeights)
 	{
-		wght = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+		wght = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 	}
 
 	LLVector4a* binorm = NULL;
 	if (mBinormals)
 	{
-		binorm = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+		binorm = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 	}
 
 	//allocate mapping of old indices to new indices
@@ -5878,11 +5878,11 @@ void LLVolumeFace::cacheOptimize()
 		mIndices[i] = new_idx[mIndices[i]];
 	}
 	
-	free(mPositions);
-	free(mNormals);
-	free(mTexCoords);
-	free(mWeights);
-	free(mBinormals);
+	ll_aligned_free_16(mPositions);
+	ll_aligned_free_16(mNormals);
+	ll_aligned_free_16(mTexCoords);
+	ll_aligned_free_16(mWeights);
+	ll_aligned_free_16(mBinormals);
 
 	mPositions = pos;
 	mNormals = norm;
@@ -6603,23 +6603,23 @@ void LLVolumeFace::createBinormals()
 
 void LLVolumeFace::resizeVertices(S32 num_verts)
 {
-	free(mPositions);
-	free(mNormals);
-	free(mBinormals);
-	free(mTexCoords);
+	ll_aligned_free_16(mPositions);
+	ll_aligned_free_16(mNormals);
+	ll_aligned_free_16(mBinormals);
+	ll_aligned_free_16(mTexCoords);
 
 	mBinormals = NULL;
 
 	if (num_verts)
 	{
-		mPositions = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+		mPositions = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 		assert_aligned(mPositions, 16);
-		mNormals = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+		mNormals = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 		assert_aligned(mNormals, 16);
 
 		//pad texture coordinate block end to allow for QWORD reads
 		S32 size = ((num_verts*sizeof(LLVector2)) + 0xF) & ~0xF;
-		mTexCoords = (LLVector2*) malloc(size);
+		mTexCoords = (LLVector2*) ll_aligned_malloc_16(size);
 		assert_aligned(mTexCoords, 16);
 	}
 	else
@@ -6655,7 +6655,7 @@ void LLVolumeFace::pushVertex(const LLVector4a& pos, const LLVector4a& norm, con
 	
 
 	//just clear binormals
-	free(mBinormals);
+	ll_aligned_free_16(mBinormals);
 	mBinormals = NULL;
 
 	mPositions[mNumVertices] = pos;
@@ -6667,26 +6667,26 @@ void LLVolumeFace::pushVertex(const LLVector4a& pos, const LLVector4a& norm, con
 
 void LLVolumeFace::allocateBinormals(S32 num_verts)
 {
-	free(mBinormals);
-	mBinormals = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+	ll_aligned_free_16(mBinormals);
+	mBinormals = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 }
 
 void LLVolumeFace::allocateWeights(S32 num_verts)
 {
-	free(mWeights);
-	mWeights = (LLVector4a*) malloc(sizeof(LLVector4a)*num_verts);
+	ll_aligned_free_16(mWeights);
+	mWeights = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*num_verts);
 }
 
 void LLVolumeFace::resizeIndices(S32 num_indices)
 {
-	free(mIndices);
+	ll_aligned_free_16(mIndices);
 	
 	if (num_indices)
 	{
 		//pad index block end to allow for QWORD reads
 		S32 size = ((num_indices*sizeof(U16)) + 0xF) & ~0xF;
 		
-		mIndices = (U16*) malloc(size);
+		mIndices = (U16*) ll_aligned_malloc_16(size);
 	}
 	else
 	{
diff --git a/indra/llrender/llvertexbuffer.cpp b/indra/llrender/llvertexbuffer.cpp
index 73efbfc9998..94a04ba6ca1 100644
--- a/indra/llrender/llvertexbuffer.cpp
+++ b/indra/llrender/llvertexbuffer.cpp
@@ -633,7 +633,7 @@ void LLVertexBuffer::createGLBuffer()
 	{
 		static int gl_buffer_idx = 0;
 		mGLBuffer = ++gl_buffer_idx;
-		mMappedData = (U8*) malloc(size);
+		mMappedData = (U8*) ll_aligned_malloc_16(size);
 	}
 }
 
@@ -667,7 +667,7 @@ void LLVertexBuffer::createGLIndices()
 	}
 	else
 	{
-		mMappedIndexData = (U8*) malloc(size);
+		mMappedIndexData = (U8*) ll_aligned_malloc_16(size);
 		static int gl_buffer_idx = 0;
 		mGLIndices = ++gl_buffer_idx;
 	}
@@ -690,7 +690,7 @@ void LLVertexBuffer::destroyGLBuffer()
 		}
 		else
 		{
-			free(mMappedData);
+			ll_aligned_free_16(mMappedData);
 			mMappedData = NULL;
 			mEmpty = TRUE;
 		}
@@ -719,7 +719,7 @@ void LLVertexBuffer::destroyGLIndices()
 		}
 		else
 		{
-			free(mMappedIndexData);
+			ll_aligned_free_16(mMappedIndexData);
 			mMappedIndexData = NULL;
 			mEmpty = TRUE;
 		}
@@ -852,8 +852,8 @@ void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
 			{
 				if (!useVBOs())
 				{
-					free(mMappedData);
-					mMappedData = (U8*) malloc(newsize);
+					ll_aligned_free_16(mMappedData);
+					mMappedData = (U8*) ll_aligned_malloc_16(newsize);
 				}
 				mResized = TRUE;
 			}
@@ -873,8 +873,8 @@ void LLVertexBuffer::resizeBuffer(S32 newnverts, S32 newnindices)
 			{
 				if (!useVBOs())
 				{
-					free(mMappedIndexData);
-					mMappedIndexData = (U8*) malloc(new_index_size);
+					ll_aligned_free_16(mMappedIndexData);
+					mMappedIndexData = (U8*) ll_aligned_malloc_16(new_index_size);
 				}
 				mResized = TRUE;
 			}
@@ -915,8 +915,8 @@ void LLVertexBuffer::freeClientBuffer()
 {
 	if(useVBOs() && sDisableVBOMapping && (mMappedData || mMappedIndexData))
 	{
-		free(mMappedData) ;
-		free(mMappedIndexData) ;
+		ll_aligned_free_16(mMappedData) ;
+		ll_aligned_free_16(mMappedIndexData) ;
 		mMappedData = NULL ;
 		mMappedIndexData = NULL ;
 	}
@@ -926,7 +926,7 @@ void LLVertexBuffer::allocateClientVertexBuffer()
 {
 	if(!mMappedData)
 	{
-		mMappedData = (U8*)malloc(getSize());
+		mMappedData = (U8*)ll_aligned_malloc_16(getSize());
 	}
 }
 
@@ -934,7 +934,7 @@ void LLVertexBuffer::allocateClientIndexBuffer()
 {
 	if(!mMappedIndexData)
 	{
-		mMappedIndexData = (U8*)malloc(getIndicesSize());
+		mMappedIndexData = (U8*)ll_aligned_malloc_16(getIndicesSize());
 	}
 }
 
diff --git a/indra/newview/llpolymesh.cpp b/indra/newview/llpolymesh.cpp
index 4b2c569cc3a..450f9b2be72 100644
--- a/indra/newview/llpolymesh.cpp
+++ b/indra/newview/llpolymesh.cpp
@@ -770,7 +770,7 @@ LLPolyMesh::LLPolyMesh(LLPolyMeshSharedData *shared_data, LLPolyMesh *reference_
 		int nverts = mSharedData->mNumVertices;
 		int nfloats = nverts * (2*4 + 3*3 + 2 + 4);
 		//use 16 byte aligned vertex data to make LLPolyMesh SSE friendly
-		mVertexData = (F32*) malloc(nfloats*4);
+		mVertexData = (F32*) ll_aligned_malloc_16(nfloats*4);
 		int offset = 0;
 		mCoords				= 	(LLVector4*)(mVertexData + offset); offset += 4*nverts;
 		mNormals			=	(LLVector4*)(mVertexData + offset); offset += 4*nverts;
@@ -799,7 +799,7 @@ LLPolyMesh::~LLPolyMesh()
                 mJointRenderData[i] = NULL;
         }
 
-		free(mVertexData);
+		ll_aligned_free_16(mVertexData);
 
 }
 
diff --git a/indra/newview/llspatialpartition.cpp b/indra/newview/llspatialpartition.cpp
index 65f7d299bc9..34a42ff28b5 100644
--- a/indra/newview/llspatialpartition.cpp
+++ b/indra/newview/llspatialpartition.cpp
@@ -3169,11 +3169,11 @@ void renderPhysicsShape(LLDrawable* drawable, LLVOVolume* volume)
 					LLConvexDecomposition::getInstance()->generateSingleHullMeshFromMesh( &mesh, &res );
 
 					//copy res into phys_volume
-					phys_volume->mHullPoints = (LLVector4a*) malloc(sizeof(LLVector4a)*res.mNumVertices);
+					phys_volume->mHullPoints = (LLVector4a*) ll_aligned_malloc_16(sizeof(LLVector4a)*res.mNumVertices);
 					phys_volume->mNumHullPoints = res.mNumVertices;
 
 					S32 idx_size = (res.mNumTriangles*3*2+0xF) & ~0xF;
-					phys_volume->mHullIndices = (U16*) malloc(idx_size);
+					phys_volume->mHullIndices = (U16*) ll_aligned_malloc_16(idx_size);
 					phys_volume->mNumHullIndices = res.mNumTriangles*3;
 
 					const F32* v = res.mVertexBase;
-- 
GitLab