beginning to fill in the lower levels of the heap allocation code

This commit is contained in:
Eric J. Bowersox
2013-06-08 21:34:03 -06:00
parent 3f4e807025
commit 0f309bbbc0
11 changed files with 605 additions and 9 deletions

View File

@@ -32,8 +32,9 @@ MAKEFLAGS += -rR
CRBASEDIR := $(abspath ../..)
include $(CRBASEDIR)/armcompile.mk
LIB_OBJS = divide.o qdivrem.o heap_toplevel.o intlib.o objhelp.o objhelp_enumconn.o objhelp_enumgeneric.o \
objhelp_fixedcp.o rbtree.o str.o strcopymem.o strcomparemem.o strsetmem.o lib_guids.o
LIB_OBJS = divide.o qdivrem.o heap_toplevel.o heap_base.o heap_chunks.o intlib.o objhelp.o objhelp_enumconn.o \
objhelp_enumgeneric.o objhelp_fixedcp.o rbtree.o str.o strcopymem.o strcomparemem.o \
strsetmem.o lib_guids.o
all: kernel-lib.o

122
kernel/lib/heap_base.c Normal file
View File

@@ -0,0 +1,122 @@
/*
* 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"
static BOOL base_alloc_new_chunk(PHEAPDATA phd, SIZE_T szMinimum)
{
BOOL fZero = FALSE; /* do we need this zeroed? (no) */
SIZE_T szAdjusted; /* adjusted size to multiple of chunk size */
PVOID pvNewChunk; /* pointer to new chunk */
szAdjusted = CHUNK_CEILING(phd, szMinimum);
pvNewChunk = _HeapChunkAlloc(phd, szAdjusted, phd->szChunk, TRUE, &fZero);
if (!pvNewChunk)
return TRUE; /* allocation failed */
*((PPVOID)pvNewChunk) = phd->pvBasePages;
phd->pvBasePages = pvNewChunk;
phd->pvBaseNext = (PVOID)(((UINT_PTR)pvNewChunk) + sizeof(PVOID));
phd->pvBasePast = (PVOID)(((UINT_PTR)pvNewChunk) + szAdjusted);
return FALSE;
}
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 */
if (base_alloc_new_chunk(phd, szAdjusted + sizeof(PVOID)))
goto error0;
}
rc = phd->pvBaseNext; /* perform the allocation */
phd->pvBaseNext = (PVOID)((UINT_PTR)(phd->pvBaseNext) + szAdjusted);
error0:
IMutex_Unlock(phd->pmtxBase);
return rc;
}
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;
}
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);
}
HRESULT _HeapBaseSetup(PHEAPDATA phd)
{
HRESULT hr = IMutexFactory_CreateMutex(phd->pMutexFactory, &(phd->pmtxBase));
if (FAILED(hr))
return hr;
phd->pexnBaseNodes = NULL;
return S_OK;
}
void _HeapBaseShutdown(PHEAPDATA phd)
{
/* TODO */
IUnknown_Release(phd->pmtxBase);
}

122
kernel/lib/heap_chunks.c Normal file
View File

@@ -0,0 +1,122 @@
/*
* 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/internals/mmu.h>
#include "heap_internals.h"
/*------------------------------------------------------------------------
* Functions driving the red-black trees that contain EXTENT_NODE objects
*------------------------------------------------------------------------
*/
static INT32 compare_sizeaddr(PEXTENT_NODE pexnA, PEXTENT_NODE pexnB)
{
SIZE_T szA = pexnA->sz;
SIZE_T szB = pexnB->sz;
INT32 rc = (szA > szB) - (szA < szB);
if (rc == 0)
{
UINT_PTR addrA = (UINT_PTR)(pexnA->pv);
UINT_PTR addrB = (UINT_PTR)(pexnB->pv);
rc = (addrA > addrB) - (addrA < addrB);
}
return rc;
}
static INT32 compare_addr(PEXTENT_NODE pexnA, PEXTENT_NODE pexnB)
{
UINT_PTR addrA = (UINT_PTR)(pexnA->pv);
UINT_PTR addrB = (UINT_PTR)(pexnB->pv);
return (addrA > addrB) - (addrA < addrB);
}
static PRBTREENODE get_sizeaddr_node(PEXTENT_NODE pexn)
{
return &(pexn->rbtnSizeAddress);
}
static PRBTREENODE get_addr_node(PEXTENT_NODE pexn)
{
return &(pexn->rbtnAddress);
}
static PEXTENT_NODE get_from_sizeaddr_node(PRBTREENODE prbtn)
{
return (PEXTENT_NODE)(((UINT_PTR)prbtn) - OFFSETOF(EXTENT_NODE, rbtnSizeAddress));
}
static PEXTENT_NODE get_from_addr_node(PRBTREENODE prbtn)
{
return (PEXTENT_NODE)(((UINT_PTR)prbtn) - OFFSETOF(EXTENT_NODE, rbtnAddress));
}
/*----------------------
* Heap chunk functions
*----------------------
*/
PVOID _HeapChunkAlloc(PHEAPDATA phd, SIZE_T sz, SIZE_T szAlignment, BOOL fBase, BOOL *pfZeroed)
{
return NULL; /* TODO */
}
void _HeapChunkUnmap(PHEAPDATA phd, PVOID pvChunk, SIZE_T sz)
{
/* TODO */
}
void _HeapChunkDeAlloc(PHEAPDATA phd, PVOID pvChunk, SIZE_T sz, BOOL fUnmap)
{
/* TODO */
}
HRESULT _HeapChunkSetup(PHEAPDATA phd)
{
rbtInitTree(&(phd->rbtExtSizeAddr), (PFNTREECOMPARE)compare_sizeaddr, (PFNGETTREEKEY)get_from_sizeaddr_node,
(PFNGETTREENODEPTR)get_sizeaddr_node, (PFNGETFROMTREENODEPTR)get_from_sizeaddr_node);
rbtInitTree(&(phd->rbtExtAddr), (PFNTREECOMPARE)compare_addr, (PFNGETTREEKEY)get_from_addr_node,
(PFNGETTREENODEPTR)get_addr_node, (PFNGETFROMTREENODEPTR)get_from_addr_node);
return S_OK;
}
extern void _HeapChunkShutdown(PHEAPDATA phd)
{
/* TODO */
}

View File

@@ -39,11 +39,32 @@
#ifndef __ASM__
#include <comrogue/compiler_macros.h>
#include <comrogue/types.h>
#include <comrogue/objectbase.h>
#include <comrogue/connpoint.h>
#include <comrogue/allocator.h>
#include <comrogue/mutex.h>
#include <comrogue/heap.h>
#include <comrogue/objhelp.h>
#include <comrogue/internals/rbtree.h>
/*-------------------
* Extent management
*-------------------
*/
/* Tree of extents managed by the heap management code. */
typedef struct tagEXTENT_NODE
{
RBTREENODE rbtnSizeAddress; /* tree node for size and address ordering */
RBTREENODE rbtnAddress; /* tree node for address ordering */
/* TODO prof_ctx? */
PVOID pv; /* base pointer to region */
SIZE_T sz; /* size of region */
BOOL fZeroed; /* is this extent zeroed? */
} EXTENT_NODE, *PEXTENT_NODE;
typedef PEXTENT_NODE *PPEXTENT_NODE;
/*----------------------------------
* The actual heap data declaration
@@ -57,12 +78,59 @@ typedef struct tagHEAPDATA {
UINT32 uiFlags; /* flags word */
PFNRAWHEAPDATAFREE pfnFreeRawHeapData; /* pointer to function that frees the raw heap data, if any */
IChunkAllocator *pChunkAllocator; /* chunk allocator pointer */
IMutexFactory *pMutexFactory; /* mutex factory pointer */
FIXEDCPDATA fcpMallocSpy; /* connection point for IMallocSpy */
FIXEDCPDATA fcpSequentialStream; /* connection point for ISequentialStream for debugging */
IMallocSpy *pMallocSpy; /* IMallocSpy interface for the allocator */
ISequentialStream *pDebugStream; /* debugging output stream */
UINT32 nChunkBits; /* number of bits in a chunk */
UINT32 szChunk; /* size of a chunk */
UINT32 uiChunkSizeMask; /* bitmask for a chunk */
UINT32 cpgChunk; /* number of pages in a chunk */
RBTREE rbtExtSizeAddr; /* tree ordering extents by size and address */
RBTREE rbtExtAddr; /* tree ordering extents by address */
IMutex *pmtxBase; /* base mutex */
PVOID pvBasePages; /* pages being used for internal memory allocation */
PVOID pvBaseNext; /* next allocation location */
PVOID pvBasePast; /* address immediately past pvBasePages */
PEXTENT_NODE pexnBaseNodes; /* pointer to base nodes */
} HEAPDATA, *PHEAPDATA;
/*-------------------------------------
* Internal chunk management functions
*-------------------------------------
*/
/* Get chunk address for allocated address a. */
#define CHUNK_ADDR2BASE(phd, a) ((PVOID)(((UINT_PTR)(a)) & ~((phd)->uiChunkSizeMask)))
/* Get chunk offset of allocated address a. */
#define CHUNK_ADDR2OFFSET(phd, a) ((SIZE_T)(((UINT_PTR)(a)) & (phd)->uiChunkSizeMask))
/* Return the smallest chunk size multiple that can contain a certain size. */
#define CHUNK_CEILING(phd, sz) (((sz) + (phd)->uiChunkSizeMask) & ~((phd)->uiChunkSizeMask))
extern PVOID _HeapChunkAlloc(PHEAPDATA phd, SIZE_T sz, SIZE_T szAlignment, BOOL fBase, BOOL *pfZeroed);
extern void _HeapChunkUnmap(PHEAPDATA phd, PVOID pvChunk, SIZE_T sz);
extern void _HeapChunkDeAlloc(PHEAPDATA phd, PVOID pvChunk, SIZE_T sz, BOOL fUnmap);
extern HRESULT _HeapChunkSetup(PHEAPDATA phd);
extern void _HeapChunkShutdown(PHEAPDATA phd);
/*------------------------------------
* Internal base management functions
*------------------------------------
*/
CDECL_BEGIN
extern PVOID _HeapBaseAlloc(PHEAPDATA phd, SIZE_T sz);
extern PEXTENT_NODE _HeapBaseNodeAlloc(PHEAPDATA phd);
extern void _HeapBaseNodeDeAlloc(PHEAPDATA phd, PEXTENT_NODE pexn);
extern HRESULT _HeapBaseSetup(PHEAPDATA phd);
extern void _HeapBaseShutdown(PHEAPDATA phd);
CDECL_END
#endif /* __ASM__ */
#endif /* __COMROGUE_INTERNALS__ */

View File

@@ -34,12 +34,14 @@
*/
#include <comrogue/compiler_macros.h>
#include <comrogue/types.h>
#include <comrogue/scode.h>
#include <comrogue/str.h>
#include <comrogue/objhelp.h>
#include <comrogue/stdobj.h>
#include <comrogue/allocator.h>
#include <comrogue/heap.h>
#include <comrogue/internals/seg.h>
#include <comrogue/internals/mmu.h>
#include "heap_internals.h"
#include "enumgeneric.h"
@@ -93,6 +95,23 @@ static UINT32 malloc_AddRef(IUnknown *pThis)
return ++(((PHEAPDATA)pThis)->uiRefCount);
}
/*
* Shuts down everything that was allocated at top level.
*
* Parameters:
* - phd = Pointer to HEAPDATA structure.
*
* Returns:
* Nothing.
*/
static void toplevel_shutdown(PHEAPDATA phd)
{
ObjHlpFixedCpTeardown(&(phd->fcpMallocSpy));
ObjHlpFixedCpTeardown(&(phd->fcpSequentialStream));
IUnknown_Release(phd->pMutexFactory);
IUnknown_Release(phd->pChunkAllocator);
}
/*
* Removes a reference from the heap data object. The object is freed when its reference count reaches 0.
*
@@ -112,9 +131,8 @@ static UINT32 malloc_Release(IUnknown *pThis)
if ((rc == 0) && !(phd->uiFlags & PHDFLAGS_DELETING))
{
phd->uiFlags |= PHDFLAGS_DELETING;
ObjHlpFixedCpTeardown(&(phd->fcpMallocSpy));
ObjHlpFixedCpTeardown(&(phd->fcpSequentialStream));
IUnknown_Release(phd->pChunkAllocator);
_HeapBaseShutdown(phd);
toplevel_shutdown(phd);
if (phd->pfnFreeRawHeapData)
{
pfnFree = phd->pfnFreeRawHeapData;
@@ -138,6 +156,9 @@ static PVOID malloc_Alloc(IMalloc *pThis, SIZE_T cb)
return NULL; /* simulated memory failure */
}
if (cbActual == 0)
cbActual = 1; /* allocate at least SOMETHING */
rc = NULL; /* TODO */
/* handle PostAlloc call */
@@ -184,6 +205,9 @@ static void malloc_Free(IMalloc *pThis, PVOID pv)
PVOID pvActual = pv; /* actual heap block pointer */
BOOL fSpyed; /* were we allocated while currently spyed on? */
if (!pv)
return; /* no effect if pointer is NULL */
/* handle PreFree call */
if (phd->pMallocSpy)
{
@@ -431,15 +455,18 @@ static const SEG_RODATA struct IConnectionPointContainerVTable vtblConnectionPoi
* "prhd" block. May be NULL.
* - pChunkAllocator = Pointer to the IChunkAllocator interface used by the heap to allocate chunks of memory
* for carving up by the heap.
* - pMutexFactory = Pointer to the IMutexFactory interface used to allocate IMutex objects.
* - nChunkBits = Number of "bits" in a memory chunk that gets allocated.
* - ppHeap = Pointer location that will receive a pointer to the heap's IMalloc interface.
*
* Returns:
* Standard HRESULT success/failure.
*/
HRESULT HeapCreate(PRAWHEAPDATA prhd, PFNRAWHEAPDATAFREE pfnFree, IChunkAllocator *pChunkAllocator,
IMalloc **ppHeap)
IMutexFactory *pMutexFactory, UINT32 nChunkBits, IMalloc **ppHeap)
{
PHEAPDATA phd; /* pointer to actual heap data */
HRESULT hr; /* HRESULT of intermediate operations */
if (sizeof(RAWHEAPDATA) < sizeof(HEAPDATA))
return MEMMGR_E_BADHEAPDATASIZE; /* bogus size of raw heap data */
@@ -454,12 +481,37 @@ HRESULT HeapCreate(PRAWHEAPDATA prhd, PFNRAWHEAPDATAFREE pfnFree, IChunkAllocato
phd->uiRefCount = 1;
phd->uiFlags = 0;
phd->pfnFreeRawHeapData = pfnFree;
phd->nChunkBits = nChunkBits;
phd->szChunk = 1 << nChunkBits;
if (phd->szChunk < SYS_PAGE_SIZE)
return MEMMGR_E_BADCHUNKSIZE;
phd->uiChunkSizeMask = phd->szChunk - 1;
phd->cpgChunk = phd->szChunk >> SYS_PAGE_BITS;
/* Set up the top-level data. */
phd->pChunkAllocator = pChunkAllocator;
IUnknown_AddRef(phd->pChunkAllocator);
phd->pMutexFactory = pMutexFactory;
IUnknown_AddRef(phd->pMutexFactory);
ObjHlpFixedCpSetup(&(phd->fcpMallocSpy), (PUNKNOWN)phd, &IID_IMallocSpy, (IUnknown **)(&(phd->pMallocSpy)), 1, NULL);
ObjHlpFixedCpSetup(&(phd->fcpSequentialStream), (PUNKNOWN)phd, &IID_ISequentialStream,
(IUnknown **)(&(phd->pDebugStream)), 1, NULL);
/* Setup base pointers. */
hr = _HeapBaseSetup(phd);
if (FAILED(hr))
goto error0;
*ppHeap = (IMalloc *)phd;
return S_OK;
/*error1:*/
_HeapBaseShutdown(phd);
error0:
toplevel_shutdown(phd);
*ppHeap = NULL;
return hr;
}