2013-06-08 21:34:03 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the COMROGUE Operating System for Raspberry Pi
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, Eric J. Bowersox / Erbosoft Enterprises
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free for commercial and non-commercial use as long as the following conditions are
|
|
|
|
* adhered to.
|
|
|
|
*
|
|
|
|
* Copyright in this file remains Eric J. Bowersox and/or Erbosoft, and as such any copyright notices
|
|
|
|
* in the code are not to be removed.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
|
|
* provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions and
|
|
|
|
* the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
|
|
|
* the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* "Raspberry Pi" is a trademark of the Raspberry Pi Foundation.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This code is based on/inspired by jemalloc-3.3.1. Please see LICENSE.jemalloc for further details.
|
|
|
|
*/
|
|
|
|
#include <comrogue/compiler_macros.h>
|
|
|
|
#include <comrogue/types.h>
|
|
|
|
#include <comrogue/objectbase.h>
|
|
|
|
#include <comrogue/scode.h>
|
|
|
|
#include <comrogue/stdobj.h>
|
|
|
|
#include <comrogue/mutex.h>
|
|
|
|
#include <comrogue/internals/mmu.h>
|
|
|
|
#include "heap_internals.h"
|
|
|
|
|
2013-06-09 19:50:14 -06:00
|
|
|
/*-----------------------------------
|
|
|
|
* Base memory allocation functions.
|
|
|
|
*-----------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new chunk of memory for use by the base allocator, and point to it with the allocator pointers.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - phd = Pointer to the HEAPDATA block.
|
|
|
|
* - szMinimum = Minimum number of bytes we need to allocate.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* - TRUE = Allocation failed.
|
|
|
|
* - FALSE = Allocation succeeded.
|
|
|
|
*/
|
2013-06-08 21:34:03 -06:00
|
|
|
static BOOL base_alloc_new_chunk(PHEAPDATA phd, SIZE_T szMinimum)
|
|
|
|
{
|
|
|
|
SIZE_T szAdjusted; /* adjusted size to multiple of chunk size */
|
|
|
|
PVOID pvNewChunk; /* pointer to new chunk */
|
2013-06-09 19:50:14 -06:00
|
|
|
PVOID pvTemp; /* temporary pointer */
|
|
|
|
BOOL fZero = FALSE; /* do we need this zeroed? (no) */
|
2013-06-08 21:34:03 -06:00
|
|
|
|
|
|
|
szAdjusted = CHUNK_CEILING(phd, szMinimum);
|
|
|
|
pvNewChunk = _HeapChunkAlloc(phd, szAdjusted, phd->szChunk, TRUE, &fZero);
|
|
|
|
if (!pvNewChunk)
|
|
|
|
return TRUE; /* allocation failed */
|
|
|
|
*((PPVOID)pvNewChunk) = phd->pvBasePages;
|
2013-06-09 19:50:14 -06:00
|
|
|
pvTemp = (PVOID)(((UINT_PTR)pvNewChunk) + sizeof(PVOID));
|
|
|
|
*((SIZE_T *)pvTemp) = szAdjusted;
|
2013-06-08 21:34:03 -06:00
|
|
|
phd->pvBasePages = pvNewChunk;
|
2013-06-09 19:50:14 -06:00
|
|
|
phd->pvBaseNext = (PVOID)(((UINT_PTR)pvNewChunk) + sizeof(PVOID) + sizeof(SIZE_T));
|
2013-06-08 21:34:03 -06:00
|
|
|
phd->pvBasePast = (PVOID)(((UINT_PTR)pvNewChunk) + szAdjusted);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:50:14 -06:00
|
|
|
/*
|
|
|
|
* Allocate a chunk of memory from the base allocator.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - phd = Pointer to the HEAPDATA block.
|
|
|
|
* - sz = Number of bytes to allocate.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* - NULL = Allocation failed.
|
|
|
|
* - Other = Pointer to the allocated block of memory.
|
|
|
|
*/
|
2013-06-08 21:34:03 -06:00
|
|
|
PVOID _HeapBaseAlloc(PHEAPDATA phd, SIZE_T sz)
|
|
|
|
{
|
|
|
|
PVOID rc = NULL; /* return from this function */
|
|
|
|
SIZE_T szAdjusted; /* adjusted size */
|
|
|
|
|
|
|
|
szAdjusted = SYS_CACHELINE_CEILING(sz); /* round up to cache line size */
|
|
|
|
|
|
|
|
IMutex_Lock(phd->pmtxBase);
|
|
|
|
if (((UINT_PTR)(phd->pvBaseNext) + szAdjusted) > (UINT_PTR)(phd->pvBasePast))
|
|
|
|
{ /* allocate new chunk if we don't have enough space for the allocation */
|
2013-06-09 19:50:14 -06:00
|
|
|
if (base_alloc_new_chunk(phd, szAdjusted + sizeof(PVOID) + sizeof(SIZE_T)))
|
|
|
|
goto error0; /* failed */
|
2013-06-08 21:34:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = phd->pvBaseNext; /* perform the allocation */
|
|
|
|
phd->pvBaseNext = (PVOID)((UINT_PTR)(phd->pvBaseNext) + szAdjusted);
|
|
|
|
|
|
|
|
error0:
|
|
|
|
IMutex_Unlock(phd->pmtxBase);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:50:14 -06:00
|
|
|
/*
|
|
|
|
* Allocate a new EXTENT_NODE from the base allocator.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - phd = Pointer to the HEAPDATA block.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* - NULL = Allocation failed.
|
|
|
|
* - Other = Pointer to the new EXTENT_NODE.
|
|
|
|
*/
|
2013-06-08 21:34:03 -06:00
|
|
|
PEXTENT_NODE _HeapBaseNodeAlloc(PHEAPDATA phd)
|
|
|
|
{
|
|
|
|
PEXTENT_NODE rc = NULL; /* return from this function */
|
|
|
|
|
|
|
|
IMutex_Lock(phd->pmtxBase);
|
|
|
|
if (phd->pexnBaseNodes)
|
|
|
|
{ /* pull a node off the free list */
|
|
|
|
rc = phd->pexnBaseNodes;
|
|
|
|
phd->pexnBaseNodes = *((PPEXTENT_NODE)rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMutex_Unlock(phd->pmtxBase);
|
|
|
|
if (!rc) /* use base allocator to get one */
|
|
|
|
rc = (PEXTENT_NODE)_HeapBaseAlloc(phd, sizeof(EXTENT_NODE));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:50:14 -06:00
|
|
|
/*
|
|
|
|
* Returns an EXTENT_NODE to the free list.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - phd = Pointer to the HEAPDATA block.
|
|
|
|
* - pexn = Pointer to the EXTENT_NODE to be freed.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Nothing.
|
|
|
|
*/
|
2013-06-08 21:34:03 -06:00
|
|
|
void _HeapBaseNodeDeAlloc(PHEAPDATA phd, PEXTENT_NODE pexn)
|
|
|
|
{
|
|
|
|
/* add it to the free list */
|
|
|
|
IMutex_Lock(phd->pmtxBase);
|
|
|
|
*((PPEXTENT_NODE)pexn) = phd->pexnBaseNodes;
|
|
|
|
phd->pexnBaseNodes = pexn;
|
|
|
|
IMutex_Unlock(phd->pmtxBase);
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:50:14 -06:00
|
|
|
/*
|
|
|
|
* Set up the base allocator.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - phd = Pointer to the HEAPDATA block.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Standard HRESULT success/failure indicator.
|
|
|
|
*/
|
2013-06-08 21:34:03 -06:00
|
|
|
HRESULT _HeapBaseSetup(PHEAPDATA phd)
|
|
|
|
{
|
|
|
|
HRESULT hr = IMutexFactory_CreateMutex(phd->pMutexFactory, &(phd->pmtxBase));
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
phd->pexnBaseNodes = NULL;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:50:14 -06:00
|
|
|
/*
|
|
|
|
* Shut down the base allocator.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - phd = Pointer to the HEAPDATA block.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Nothing.
|
|
|
|
*/
|
2013-06-08 21:34:03 -06:00
|
|
|
void _HeapBaseShutdown(PHEAPDATA phd)
|
|
|
|
{
|
2013-06-09 19:50:14 -06:00
|
|
|
PVOID pChunk; /* the chunk we're working on */
|
|
|
|
PVOID pNextChunk; /* the next chunk to be dealt with */
|
|
|
|
PVOID pvTemp; /* temporary pointer */
|
|
|
|
SIZE_T szChunk; /* size of allocated chunk */
|
|
|
|
|
|
|
|
/* All allocated "base" chunks come straight from the IChunkAllocator, so it's safe to free them this way. */
|
|
|
|
pChunk = phd->pvBasePages;
|
|
|
|
while (pChunk)
|
|
|
|
{
|
|
|
|
pNextChunk = *((PPVOID)pChunk);
|
|
|
|
pvTemp = (PVOID)(((UINT_PTR)pChunk) + sizeof(PVOID));
|
|
|
|
szChunk = *((SIZE_T *)pvTemp);
|
|
|
|
IChunkAllocator_FreeChunk(phd->pChunkAllocator, pChunk, szChunk);
|
|
|
|
pChunk = pNextChunk;
|
|
|
|
}
|
|
|
|
|
2013-06-08 21:34:03 -06:00
|
|
|
IUnknown_Release(phd->pmtxBase);
|
|
|
|
}
|