Cache for virtual memory pages.
Virtual memory pages are allocated in big chunks with help of init_vmpage. This component makes calling into the operating system unnecessary every time you need a new page or want ot free one.
It offers pages of size 4096 bytes no matter of the page size of the operating system.
Other page sizes could be added supported if necessary.
MemoryPageCache | Cache for virtual memory pages. |
Copyright | This program is free software. |
Files | |
C-kern/ | Header file MemoryPageCache. |
C-kern/ | Implementation file MemoryPageCache impl. |
Types | |
struct pagecache_impl_t | Export pagecache_impl_t into global namespace. |
struct pagecache_block_t | Export opaque type pagecache_block_t into global namespace. |
struct pagecache_blockmap_t | Export type pagecache_blockmap_t into global namespace. |
Functions | |
test | |
unittest_memory_pagecacheimpl | Test pagecache_impl_t functionality. |
pagecache_impl_t | Allocates and frees virtual memory pages and caches them. |
blocklist | A list of pagecache_block_t. |
freelist | An array of list of allocated blocks which contain free pages. |
staticpagelist | A list of static memory blocks. |
sizeallocated | Number of allocated bytes. |
sizestatic | Number of allocated bytes from static memory. |
init | |
initthread_pagecacheimpl | Calls init_pagecacheimpl and adds interface pagecache_it to object. |
freethread_pagecacheimpl | Calls free_pagecacheimpl with for object pointer in pagecache_t. |
lifetime | |
pagecache_impl_FREE | Static initializer. |
init_pagecacheimpl | Preallocates at least 1MB of memory and initializes pgcache. |
free_pagecacheimpl | Frees all allocated memory pages even if they are in use. |
query | |
sizeallocated_pagecacheimpl | Returns the sum of the size of all allocated pages. |
sizestatic_pagecacheimpl | Returns wize of memory allocated with <allocstatic>. |
isfree_pagecacheimpl | Returns true if pgcache equals pagecache_impl_FREE. |
alloc | |
allocpage_pagecacheimpl | Allocates a single memory page of size pgsize. |
releasepage_pagecacheimpl | Releases a single memory page. |
allocstatic_pagecacheimpl | Allocates static memory which should live as long as pgcache. |
freestatic_pagecacheimpl | Frees static memory. |
cache | |
emptycache_pagecacheimpl | Releases all unused memory blocks back to the operating system. |
pagecache_blockmap_t | A simple shared hash map which maps an index pagecache_block_t. |
config | |
pagecache_blockmap_ARRAYSIZE | Static configuration of number of bytes used for the hash table. |
lifetime | |
pagecache_blockmap_FREE | Static initializer. |
init_pagecacheblockmap | Allocates a shared hash table of size pagecache_blockmap_FREE. |
free_pagecacheblockmap | Frees allocated hash table. |
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2013 Jörg Seebohn
Header file MemoryPageCache.
Implementation file MemoryPageCache impl.
typedef struct pagecache_impl_t pagecache_impl_t
Export pagecache_impl_t into global namespace.
typedef struct pagecache_block_t pagecache_block_t
Export opaque type pagecache_block_t into global namespace. This type stores information about a large block of memory of at least 1MB.
typedef struct pagecache_blockmap_t pagecache_blockmap_t
Export type pagecache_blockmap_t into global namespace. It serves as hashtable for a set of pagecache_block_t. The hashtable is shared between all pagecache_impl_t.
test | |
unittest_memory_pagecacheimpl | Test pagecache_impl_t functionality. |
int unittest_memory_pagecacheimpl( void )
Test pagecache_impl_t functionality.
struct pagecache_impl_t
Allocates and frees virtual memory pages and caches them. The pagecache_impl_t allocates always blocks of memory pages pagecache_block_t. Every pagecache_block_t maintains a list of free pages of the same size. For every pagesize a new pagecache_block_t is allocated. If several pagecache_block_t are allocated and one of them is not used at all it is returned to the operating system.
TODO: To prevent fragmentation of large blocks introduce page lifetime (long, short lifetime, ...). This helps to place longer living pages on one big block and short living pages on another. Blocks with pages with short lifetime can be reclaimed faster.
blocklist | A list of pagecache_block_t. |
freelist | An array of list of allocated blocks which contain free pages. |
staticpagelist | A list of static memory blocks. |
sizeallocated | Number of allocated bytes. |
sizestatic | Number of allocated bytes from static memory. |
init | |
initthread_pagecacheimpl | Calls init_pagecacheimpl and adds interface pagecache_it to object. |
freethread_pagecacheimpl | Calls free_pagecacheimpl with for object pointer in pagecache_t. |
lifetime | |
pagecache_impl_FREE | Static initializer. |
init_pagecacheimpl | Preallocates at least 1MB of memory and initializes pgcache. |
free_pagecacheimpl | Frees all allocated memory pages even if they are in use. |
query | |
sizeallocated_pagecacheimpl | Returns the sum of the size of all allocated pages. |
sizestatic_pagecacheimpl | Returns wize of memory allocated with <allocstatic>. |
isfree_pagecacheimpl | Returns true if pgcache equals pagecache_impl_FREE. |
alloc | |
allocpage_pagecacheimpl | Allocates a single memory page of size pgsize. |
releasepage_pagecacheimpl | Releases a single memory page. |
allocstatic_pagecacheimpl | Allocates static memory which should live as long as pgcache. |
freestatic_pagecacheimpl | Frees static memory. |
cache | |
emptycache_pagecacheimpl | Releases all unused memory blocks back to the operating system. |
struct { struct dlist_node_t * last ; } blocklist
A list of pagecache_block_t. This list contains all allocated blocks and serves to free all allocated blocks. Objects of type pagecache_block_t are managed on the memory heap and are not part of the block of the allocated pages.
struct { struct dlist_node_t * last ; } staticpagelist
A list of static memory blocks. Functions allocstatic_pagecacheimpl and freestatic_pagecacheimpl could change this list.
size_t sizeallocated
Number of allocated bytes. This number is incremented by every call to allocpage_pagecacheimpl and decremented by every call to releasepage_pagecacheimpl.
size_t sizestatic
Number of allocated bytes from static memory. Value is incremented by every call to allocstatic_pagecacheimpl and decremented by every call to freestatic_pagecacheimpl.
int initthread_pagecacheimpl( /*out*/struct pagecache_t * pagecache )
Calls init_pagecacheimpl and adds interface pagecache_it to object. This function is called from <init_threadcontext>.
int freethread_pagecacheimpl( struct pagecache_t * pagecache )
Calls free_pagecacheimpl with for object pointer in pagecache_t. This function is called from <free_threadcontext>.
int free_pagecacheimpl( pagecache_impl_t * pgcache )
Frees all allocated memory pages even if they are in use. All allocated memory pages are returned to the operating system. Make sure that every component has called releasepage_pagecacheimpl before this function is called. Or that allocated pages are never used after calling this function. In not all memory has been freed the error ENOTEMPTY is returned but memory is freed nevertheless.
size_t sizeallocated_pagecacheimpl( const pagecache_impl_t * pgcache )
Returns the sum of the size of all allocated pages. Static memory is also allocated with help of allocpage_pagecacheimpl.
bool isfree_pagecacheimpl( const pagecache_impl_t * pgcache )
Returns true if pgcache equals pagecache_impl_FREE.
int releasepage_pagecacheimpl( pagecache_impl_t * pgcache, struct memblock_t * page )
Releases a single memory page. It is kept in the cache and only returned to the operating system if a big chunk of memory is not in use. After return page is set to <memblock_FREE>. Calling this function with page set to <memblock_FREE> does nothing.
int allocstatic_pagecacheimpl( pagecache_impl_t * pgcache, size_t bytesize, /*out*/struct memblock_t * memblock )
Allocates static memory which should live as long as pgcache. These blocks can only be freed in the reverse order they hae been allocated. The size of memory is set in bytes with parameter bytesize. The allocated memory block (aligned to KONFIG_MEMALIGN) is returned in memblock. In case of no memory ENOMEM is returned. The size of the block if restricted to 128 bytes. Calling this function increases sizeallocated_pagecacheimpl or not if bytesize bytes fits on a previously allocated page.
int freestatic_pagecacheimpl( pagecache_impl_t * pgcache, struct memblock_t * memblock )
Frees static memory. If this function is not called in the reverse order of the call sequence of allocstatic_pagecacheimpl the value EINVAL is returned and nothing is done. After return memblock is set to <memblock_FREE>. Calling this function with memblock set to <memblock_FREE> does nothing.
struct pagecache_blockmap_t
A simple shared hash map which maps an index pagecache_block_t. The array size (table size) is static which allows for efficient access without rwlock_t or any other type of locks. Atomic operations to read and write pagecache_block_t.threadcontext ensure that an entry is used by only one thread.
Use pagecache_blockmap_ARRAYSIZE to configure the static size of the array.
config | |
pagecache_blockmap_ARRAYSIZE | Static configuration of number of bytes used for the hash table. |
lifetime | |
pagecache_blockmap_FREE | Static initializer. |
init_pagecacheblockmap | Allocates a shared hash table of size pagecache_blockmap_FREE. |
free_pagecacheblockmap | Frees allocated hash table. |
#define pagecache_blockmap_ARRAYSIZE ( 2*1024*1024 )
Static configuration of number of bytes used for the hash table. The number of supported pagecache_block_t is fixed cause only one entry per hash bucket is supported with no overflow handling. pagecache_blockmap_t is considered to be implemented in hardware.
int init_pagecacheblockmap( /*out*/pagecache_blockmap_t * blockmap )
Allocates a shared hash table of size pagecache_blockmap_FREE.
int free_pagecacheblockmap( pagecache_blockmap_t * blockmap )
Frees allocated hash table. Make sure that no pagecache_block_t is in use.
Export pagecache_impl_t into global namespace.
typedef struct pagecache_impl_t pagecache_impl_t
Allocates and frees virtual memory pages and caches them.
struct pagecache_impl_t
Export opaque type pagecache_block_t into global namespace.
typedef struct pagecache_block_t pagecache_block_t
Stores information about a block of memory pages.
struct pagecache_block_t
Export type pagecache_blockmap_t into global namespace.
typedef struct pagecache_blockmap_t pagecache_blockmap_t
A simple shared hash map which maps an index pagecache_block_t.
struct pagecache_blockmap_t
Test pagecache_impl_t functionality.
int unittest_memory_pagecacheimpl( void )
A list of pagecache_block_t.
struct { struct dlist_node_t * last ; } blocklist
A list of static memory blocks.
struct { struct dlist_node_t * last ; } staticpagelist
Number of allocated bytes.
size_t sizeallocated
Number of allocated bytes from static memory.
size_t sizestatic
Calls init_pagecacheimpl and adds interface pagecache_it to object.
int initthread_pagecacheimpl( /*out*/struct pagecache_t * pagecache )
Preallocates at least 1MB of memory and initializes pgcache.
int init_pagecacheimpl( /*out*/pagecache_impl_t * pgcache )
Interface which allows to allocate and relase pages of memory.
struct pagecache_it
Calls free_pagecacheimpl with for object pointer in pagecache_t.
int freethread_pagecacheimpl( struct pagecache_t * pagecache )
Frees all allocated memory pages even if they are in use.
int free_pagecacheimpl( pagecache_impl_t * pgcache )
Uses iobj_DECLARE to declare interfaceable object.
iobj_DECLARE( pagecache_t, pagecache )
Static initializer.
#define pagecache_impl_FREE { { 0 }, { { 0 } }, { 0 }, 0, 0 }
Returns the sum of the size of all allocated pages.
size_t sizeallocated_pagecacheimpl( const pagecache_impl_t * pgcache )
Returns wize of memory allocated with allocstatic.
size_t sizestatic_pagecacheimpl( const pagecache_impl_t * pgcache )
Returns true if pgcache equals pagecache_impl_FREE.
bool isfree_pagecacheimpl( const pagecache_impl_t * pgcache )
Allocates a single memory page of size pgsize.
int allocpage_pagecacheimpl( pagecache_impl_t * pgcache, uint8_t pgsize, /*out*/struct memblock_t * page )
Releases a single memory page.
int releasepage_pagecacheimpl( pagecache_impl_t * pgcache, struct memblock_t * page )
Allocates static memory which should live as long as pgcache.
int allocstatic_pagecacheimpl( pagecache_impl_t * pgcache, size_t bytesize, /*out*/struct memblock_t * memblock )
Frees static memory.
int freestatic_pagecacheimpl( pagecache_impl_t * pgcache, struct memblock_t * memblock )
Releases all unused memory blocks back to the operating system.
int emptycache_pagecacheimpl( pagecache_impl_t * pgcache )
Static configuration of number of bytes used for the hash table.
#define pagecache_blockmap_ARRAYSIZE ( 2*1024*1024 )
Static initializer.
#define pagecache_blockmap_FREE { 0, 0, 0, 0 }
Allocates a shared hash table of size pagecache_blockmap_FREE.
int init_pagecacheblockmap( /*out*/pagecache_blockmap_t * blockmap )
Frees allocated hash table.
int free_pagecacheblockmap( pagecache_blockmap_t * blockmap )
Implements vmpage_t.init_vmpage.
#define init_vmpage( vmpage, size_in_bytes ) (init2_vmpage((vmpage), (size_in_bytes), accessmode_RDWR|accessmode_PRIVATE))
Alignment of allocated memory.
#define KONFIG_MEMALIGN 4
Thread which allocated the memory block.
threadcontext_t * threadcontext