include/nuttx/pgalloc.h and mm/mm_pgalloc.c: Add a simple page allocator based on the existing NuttX granule allocator. I am not certain if the granule allocator is sufficiently deterministic for long range use, but it gets get a page allocator in place for testing very quickly.

This commit is contained in:
Gregory Nutt 2014-08-23 16:35:52 -06:00
parent ce225dfc93
commit b028fb31e9
9 changed files with 503 additions and 29 deletions

View File

@ -52,7 +52,7 @@
* Pre-Processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* CONFIG_GRAN - Enable granual allocator support
/* CONFIG_GRAN - Enable granule allocator support
* CONFIG_GRAN_SINGLE - Select if there is only one instance of the
* granule allocator (i.e., gran_initialize will be called only once.
* In this case, (1) there are a few optimizations that can can be done
@ -62,7 +62,7 @@
* mutual exclusion logic will disable interrupts. While this options is
* more invasive to system performance, it will also support use of the
* granule allocator from interrupt level logic.
* CONFIG_DEBUG_GRAN - Just like CONFIG_DEBUG_MM, but only generates ouput
* CONFIG_DEBUG_GRAN - Just like CONFIG_DEBUG_MM, but only generates output
* from the gran allocation logic.
*/
@ -80,7 +80,8 @@ typedef FAR void *GRAN_HANDLE;
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C" {
extern "C"
{
#else
#define EXTERN extern
#endif
@ -94,16 +95,17 @@ extern "C" {
* granule size (log2gran). Larger granules will give better performance
* and less overhead but more losses of memory due to quantization waste.
* Additional memory waste can occur from alignment; log2align should be
* set to 0 unless you are using the granule allocator to manage DMA memory
* and your hardware has specific memory alignment requirements.
* set to 0 unless you are using the granule allocator to manage DMA
* or page-aligned memory and your hardware has specific memory alignment
* requirements.
*
* Geneneral Usage Summary. This is an example using the GCC section
* General Usage Summary. This is an example using the GCC section
* attribute to position a DMA heap in memory (logic in the linker script
* would assign the section .dmaheap to the DMA memory.
*
* FAR uint32_t g_dmaheap[DMAHEAP_SIZE] __attribute__((section(.dmaheap)));
*
* The heap is created by calling gran_initialize. Here the granual size
* The heap is created by calling gran_initialize. Here the granule size
* is set to 64 bytes and the alignment to 16 bytes:
*
* GRAN_HANDLE handle = gran_initialize(g_dmaheap, DMAHEAP_SIZE, 6, 4);

201
include/nuttx/pgalloc.h Normal file
View File

@ -0,0 +1,201 @@
/****************************************************************************
* include/nuttx/pgalloc.h
* Page memory allocator.
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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 OWNER 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_PGALLOC_H
#define __INCLUDE_NUTTX_PGALLOC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#ifdef CONFIG_MM_PGALLOC
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* CONFIG_MM_PGALLOC - Enable page allocator support
* CONFIG_MM_PGSIZE - The page size. Must be one of {1024, 2048,
* 4096, 8192, or 16384}. This is easily extensible, but only those
* values are currently support.
* CONFIG_MM_PGPOOL_PADDR - Physical address of the start of the page
* memory pool. This will be aligned to the page size if it is not
* already aligned.
* CONFIG_MM_PGPOOL_SIZE - The size of the page memory pool in bytes. This
* will be aligned if it is not already aligned.
* CONFIG_DEBUG_PGALLOC - Just like CONFIG_DEBUG_MM, but only generates
* output from the page allocation logic.
*
* Dependencies: CONFIG_ARCH_HAVE_MMU and CONFIG_GRAN
*/
#ifndef CONFIG_MM_PGALLOC_PGSIZE
# define CONFIG_MM_PGALLOC_PGSIZE 4096
#endif
#ifndef CONFIG_MM_PGPOOL_PADDR
# error CONFIG_MM_PGPOOL_PADDR must be defined
#endif
#ifndef CONFIG_MM_PGPOOL_SIZE
# error CONFIG_MM_PGPOOL_SIZE must be defined
#endif
#if CONFIG_MM_PGSIZE == 1024
# define MM_PGSIZE 1024
# define MM_PGSHIFT 10
#elif CONFIG_MM_PGSIZE == 2048
# define MM_PGSIZE 2048
# define MM_PGSHIFT 11
#elif CONFIG_MM_PGSIZE == 4096
# define MM_PGSIZE 4096
# define MM_PGSHIFT 12
#elif CONFIG_MM_PGSIZE == 8192
# define MM_PGSIZE 8192
# define MM_PGSHIFT 13
#elif CONFIG_MM_PGSIZE == 16384
# define MM_PGSIZE 16384
# define MM_PGSHIFT 14
#else
# error CONFIG_MM_PGSIZE not supported
#endif
#define MM_PGMASK (MM_PGSIZE - 1)
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: mm_pginitialize
*
* Description:
* Initialize the page allocator.
*
* Input Parameters:
* None
*
* Returned Value:
* Mpme
*
****************************************************************************/
void mm_pginitialize(void);
/****************************************************************************
* Name: mm_pgreserve
*
* Description:
* Reserve memory in the page memory pool. This will reserve the pages
* that contain the start and end addresses plus all of the pages
* in between. This should be done early in the initialization sequence
* before any other allocations are made.
*
* Reserved memory can never be allocated (it can be freed however which
* essentially unreserves the memory).
*
* Input Parameters:
* start - The address of the beginning of the region to be reserved.
* size - The size of the region to be reserved
*
* Returned Value:
* None
*
****************************************************************************/
void mm_pgreserve(uintptr_t start, size_t size);
/****************************************************************************
* Name: mm_pgalloc
*
* Description:
* Allocate page memory from the page memory pool.
*
* Input Parameters:
* npages - The number of pages to allocate, each of size CONFIG_MM_PGSIZE.
*
* Returned Value:
* On success, a non-zero, physical address of the allocated page memory
* is returned. Zero is returned on failure. NOTE: This is an unmapped
* physical address and cannot be used until it is appropriately mapped.
*
****************************************************************************/
uintptr_t mm_pgalloc(unsigned int npages);
/****************************************************************************
* Name: mm_pgfree
*
* Description:
* Return page memory to the page memory pool.
*
* Input Parameters:
* paddr - A physical address to a page in the page memory pool previously
* allocated by mm_pgalloc.
* npages - The number of contiguous pages to be return to the page memory
* pool, beginning with the page at paddr;
*
* Returned Value:
* None
*
****************************************************************************/
void mm_pgfree(uintptr_t paddr, unsigned int npages);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_MM_PGALLOC */
#endif /* __INCLUDE_NUTTX_PGALLOC_H */

View File

@ -101,7 +101,7 @@ config GRAN
bool "Enable Granule Allocator"
default n
---help---
Enable granual allocator support. Allocations will be aligned to the
Enable granule allocator support. Allocations will be aligned to the
granule size; allocations will be in units of the granule size.
Larger granules will give better performance and less overhead but
more losses of memory due to alignment and quantization waste.
@ -136,5 +136,46 @@ config DEBUG_GRAN
default n
depends on GRAN && DEBUG
---help---
Just like DEBUG_MM, but only generates ouput from the gran
Just like DEBUG_MM, but only generates output from the gran
allocation logic.
config MM_PGALLOC
bool "Enable Page Allocator"
default n
depends on ARCH_HAVE_MMU
select GRAN
---help---
Enable support for a MMU physical page allocator based on the
granule allocator.
if MM_PGALLOC
config MM_PGSIZE
int "Page Size"
default 4096
--help---
The MMU page size. Must be one of {1024, 2048, 4096, 8192, or
16384}. This is easily extensible, but only those values are
currently support.
config MM_PGPOOL_PADDR
hex "Page Memory Pool Start"
---help---
Physical address of the start of the page memory pool. This
will be aligned to the page size if it is not already aligned.
config MM_PGPOOL_SIZE
int "Page Memory Pool Size"
---help---
The size of the page memory pool in bytes. This will be aligned
if it is not already aligned.
config DEBUG_PGALLOC
bool "Page Allocator Debug"
default n
depends on DEBUG
---help---
Just like DEBUG_MM, but only generates output from the page
allocation logic.
endif # MM_PGALLOC

View File

@ -69,6 +69,12 @@ endif
ifeq ($(CONFIG_GRAN),y)
CSRCS += mm_graninit.c mm_granreserve.c mm_granalloc.c mm_granmark.c
CSRCS += mm_granfree.c mm_grancritical.c
# A page allocator based on the granule allocator
ifeq ($(CONFIG_MM_PGALLOC),y)
CSRCS += mm_pgalloc.c
endif
endif
BINDIR ?= bin

View File

@ -63,19 +63,19 @@
#ifdef CONFIG_CPP_HAVE_VARARGS
# ifdef CONFIG_DEBUG_GRAM
# define gramdbg(format, ...) dbg(format, ##__VA_ARGS__)
# define gramvdbg(format, ...) vdbg(format, ##__VA_ARGS__)
# define grandbg(format, ...) dbg(format, ##__VA_ARGS__)
# define granvdbg(format, ...) vdbg(format, ##__VA_ARGS__)
# else
# define gramdbg(format, ...) mdbg(format, ##__VA_ARGS__)
# define gramvdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
# define grandbg(format, ...) mdbg(format, ##__VA_ARGS__)
# define granvdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
# endif
#else
# ifdef CONFIG_DEBUG_GRAM
# define gramdbg dbg
# define gramvdbg vdbg
# define grandbg dbg
# define granvdbg vdbg
# else
# define gramdbg (void)
# define gramvdbg (void)
# define grandbg (void)
# define granvdbg (void)
# endif
#endif

View File

@ -235,7 +235,7 @@ static inline FAR void *gran_common_alloc(FAR struct gran_s *priv, size_t size)
}
/* Set up for the next time through the loop. Perform a 64
* bit shift to move to the next gram position andi ncrement
* bit shift to move to the next gran position andi ncrement
* to the next candidate allocation address.
*/

View File

@ -102,7 +102,7 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
unsigned int alignedsize;
unsigned int ngranules;
/* Check parameters if debug is on. Note the size of a granual is
/* Check parameters if debug is on. Note the size of a granule is
* limited to 2**31 bytes and that the size of the granule must be greater
* than the alignment size.
*/
@ -159,16 +159,17 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
* granule size (log2gran). Larger granules will give better performance
* and less overhead but more losses of memory due to quantization waste.
* Additional memory waste can occur from alignment; log2align should be
* set to 0 unless you are using the granule allocator to manage DMA memory
* and your hardware has specific memory alignment requirements.
* set to 0 unless you are using the granule allocator to manage DMA
* or page-aligned memory and your hardware has specific memory alignment
* requirements.
*
* Geneneral Usage Summary. This is an example using the GCC section
* General Usage Summary. This is an example using the GCC section
* attribute to position a DMA heap in memory (logic in the linker script
* would assign the section .dmaheap to the DMA memory.
*
* FAR uint32_t g_dmaheap[DMAHEAP_SIZE] __attribute__((section(.dmaheap)));
*
* The heap is created by calling gran_initialize(). Here the granual size
* The heap is created by calling gran_initialize(). Here the granule size
* is set to 64 bytes (2**6) and the alignment to 16 bytes (2**4):
*
* GRAN_HANDLE handle = gran_initialize(g_dmaheap, DMAHEAP_SIZE, 6, 4);
@ -199,7 +200,7 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
*
* Returned Value:
* On success, a non-NULL handle is returned that may be used with other
* granual allocator interfaces.
* granule allocator interfaces.
*
****************************************************************************/

View File

@ -93,7 +93,7 @@ static inline void gran_common_reserve(FAR struct gran_s *priv,
start &= ~mask;
end = (end + mask) & ~mask;
/* Calculate the new size in granuales */
/* Calculate the new size in granules */
ngranules = ((end - start) >> priv->log2gran) + 1;

223
mm/mm_pgalloc.c Normal file
View File

@ -0,0 +1,223 @@
/****************************************************************************
* mm/mm_pgalloc.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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 OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <nuttx/gran.h>
#include <nuttx/pgalloc.h>
#include "mm_gran.h"
#ifdef CONFIG_MM_PGALLOC
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* CONFIG_MM_PGALLOC - Enable page allocator support
* CONFIG_MM_PGSIZE - The page size. Must be one of {1024, 2048,
* 4096, 8192, or 16384}. This is easily extensible, but only those
* values are currently support.
* CONFIG_MM_PGPOOL_PADDR - Physical address of the start of the page
* memory pool. This will be aligned to the page size if it is not
* already aligned.
* CONFIG_MM_PGPOOL_SIZE - The size of the page memory pool in bytes. This
* will be aligned if it is not already aligned.
* CONFIG_DEBUG_PGALLOC - Just like CONFIG_DEBUG_MM, but only generates
* output from the page allocation logic.
*
* Dependencies: CONFIG_ARCH_HAVE_MMU and CONFIG_GRAN
*/
/* Debug */
#ifdef CONFIG_CPP_HAVE_VARARGS
# ifdef CONFIG_DEBUG_PGALLOC
# define pgadbg(format, ...) dbg(format, ##__VA_ARGS__)
# define pgavdbg(format, ...) vdbg(format, ##__VA_ARGS__)
# else
# define pgadbg(format, ...) mdbg(format, ##__VA_ARGS__)
# define pgavdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
# endif
#else
# ifdef CONFIG_DEBUG_PGALLOC
# define pgadbg dbg
# define pgavdbg vdbg
# else
# define pgadbg (void)
# define pgavdbg (void)
# endif
#endif
/****************************************************************************
* Private Data
****************************************************************************/
#ifndef CONFIG_GRAN_SINGLE
/* The state of the page allocator */
static GRAN_HANDLE g_pgalloc;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mm_pginitialize
*
* Description:
* Initialize the page allocator.
*
* Input Parameters:
* None
*
* Returned Value:
* Mpme
*
****************************************************************************/
void mm_pginitialize(void)
{
#ifdef CONFIG_GRAN_SINGLE
int ret;
ret = gran_initialize((FAR void *)CONFIG_MM_PGPOOL_PADDR,
CONFIG_MM_PGPOOL_SIZE,
MM_PGSHIFT, MM_PGSHIFT);
DEBUGASSERT(ret == OK);
UNUSED(ret);
#else
g_pgalloc = gran_initialize((FAR void *)CONFIG_MM_PGPOOL_PADDR,
CONFIG_MM_PGPOOL_SIZE,
MM_PGSHIFT, MM_PGSHIFT);
DEBUGASSERT(pg_alloc != NULL);
#endif
}
/****************************************************************************
* Name: mm_pgreserve
*
* Description:
* Reserve memory in the page memory pool. This will reserve the pages
* that contain the start and end addresses plus all of the pages
* in between. This should be done early in the initialization sequence
* before any other allocations are made.
*
* Reserved memory can never be allocated (it can be freed however which
* essentially unreserves the memory).
*
* Input Parameters:
* start - The address of the beginning of the region to be reserved.
* size - The size of the region to be reserved
*
* Returned Value:
* None
*
****************************************************************************/
void mm_pgreserve(uintptr_t start, size_t size)
{
#ifdef CONFIG_GRAN_SINGLE
gran_reserve(start, size);
#else
gran_reserve(g_pgalloc, start, size);
#endif
}
/****************************************************************************
* Name: mm_pgalloc
*
* Description:
* Allocate page memory from the page memory pool.
*
* Input Parameters:
* npages - The number of pages to allocate, each of size CONFIG_MM_PGSIZE.
*
* Returned Value:
* On success, a non-zero, physical address of the allocated page memory
* is returned. Zero is returned on failure. NOTE: This is an unmapped
* physical address and cannot be used until it is appropriately mapped.
*
****************************************************************************/
uintptr_t mm_pgalloc(unsigned int npages)
{
#ifdef CONFIG_GRAN_SINGLE
return (uintptr_t)gran_alloc((size_t)1 << MM_PGSHIFT);
#else
return (uintptr_t)gran_alloc(g_pgalloc, (size_t)1 << MM_PGSHIFT);
#endif
}
/****************************************************************************
* Name: mm_pgfree
*
* Description:
* Return page memory to the page memory pool.
*
* Input Parameters:
* paddr - A physical address to a page in the page memory pool previously
* allocated by mm_pgalloc.
* npages - The number of contiguous pages to be return to the page memory
* pool, beginning with the page at paddr;
*
* Returned Value:
* None
*
****************************************************************************/
void mm_pgfree(uintptr_t paddr, unsigned int npages)
{
#ifdef CONFIG_GRAN_SINGLE
gran_free((FAR void*)paddr, (size_t)npages << MM_PGSHIFT);
#else
gran_free(g_pgalloc, (FAR void*)paddr, (size_t)npages << MM_PGSHIFT);
#endif
}
#endif /* CONFIG_MM_PGALLOC */