@@ -23,15 +23,19 @@ void CHeap::NewChunk()
2323}
2424
2525// ****************
26- void *CHeap::AllocateFromChunk (unsigned int Size)
26+ void *CHeap::AllocateFromChunk (unsigned int Size, unsigned int Alignment )
2727{
28+ unsigned int Offset = ((unsigned long ) m_pCurrent->m_pCurrent ) % Alignment;
29+ if (Offset)
30+ Offset = Alignment - Offset;
31+
2832 // check if we need can fit the allocation
29- if (m_pCurrent->m_pCurrent + Size > m_pCurrent->m_pEnd )
33+ if (m_pCurrent->m_pCurrent + Size + Offset > m_pCurrent->m_pEnd )
3034 return (void *) 0x0 ;
3135
3236 // get memory and move the pointer forward
33- char *pMem = m_pCurrent->m_pCurrent ;
34- m_pCurrent->m_pCurrent += Size;
37+ char *pMem = m_pCurrent->m_pCurrent + Offset ;
38+ m_pCurrent->m_pCurrent += Size + Offset ;
3539 return pMem;
3640}
3741
@@ -69,17 +73,17 @@ void CHeap::Clear()
6973}
7074
7175//
72- void *CHeap::Allocate (unsigned int Size)
76+ void *CHeap::Allocate (unsigned int Size, unsigned int Alignment )
7377{
7478 // try to allocate from current chunk
75- char *pMem = (char *) AllocateFromChunk (Size);
79+ char *pMem = (char *) AllocateFromChunk (Size, Alignment );
7680 if (!pMem)
7781 {
7882 // allocate new chunk and add it to the heap
7983 NewChunk ();
8084
8185 // try to allocate again
82- pMem = (char *) AllocateFromChunk (Size);
86+ pMem = (char *) AllocateFromChunk (Size, Alignment );
8387 }
8488
8589 return pMem;
@@ -88,7 +92,7 @@ void *CHeap::Allocate(unsigned int Size)
8892const char *CHeap::StoreString (const char *pSrc)
8993{
9094 const int Size = str_length (pSrc) + 1 ;
91- char *pMem = static_cast <char *>(Allocate (Size));
95+ char *pMem = static_cast <char *>(Allocate (Size, alignof ( char ) ));
9296 str_copy (pMem, pSrc, Size);
9397 return pMem;
9498}
0 commit comments