#define FOOLOS_MODULE_NAME "mem" #define MEM_PRINT_MEMORYMAP #include "lib/int/stdint.h" #include "lib/logger/log.h" // logger facilities //! 8 blocks per byte #define PMMNGR_BLOCKS_PER_BYTE 8 //! block size (4k) #define PMMNGR_BLOCK_SIZE 4096 //! block alignment #define PMMNGR_BLOCK_ALIGN PMMNGR_BLOCK_SIZE //memory map bit array. Each bit represents a 4KB memory block static uint32_t *_mmngr_memory_map; static uint32_t mem_free_blocks; static uint32_t mem_array_size; // bit funcs! void mmap_set(int bit) { _mmngr_memory_map[bit / 32] |= (1 << (bit % 32)); } void mmap_unset(int bit) { _mmngr_memory_map[bit / 32] &= ~ (1 << (bit % 32)); } int mmap_test(int bit) { return _mmngr_memory_map[bit / 32] & (1 << (bit % 32)); } void pmmngr_init () { // By default, all of memory is in use for(int i=0;i0; blocks--) { mmap_unset (align++); mem_free_blocks++; } } void pmmngr_deinit_region (uint32_t base, uint32_t size) { uint32_t align = base / PMMNGR_BLOCK_SIZE; uint32_t blocks = size / PMMNGR_BLOCK_SIZE; for (; blocks>0; blocks--) { mmap_set (align++); mem_free_blocks--; } } void* pmmngr_alloc_block () { int frame = mmap_first_free (); if (frame == -1) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"OUT OF MEMORY"); return 0; //out of memory } mmap_set (frame); uint32_t addr = frame * PMMNGR_BLOCK_SIZE; mem_free_blocks--; return (void*)addr; } void pmmngr_free_block (void* p) { uint32_t addr = (uint32_t*)p; int frame = addr / PMMNGR_BLOCK_SIZE; if(mmap_test(frame)) { mmap_unset (frame); mem_free_blocks++; } } void mem_init(uint16_t *memmap,uint16_t entries) { // init free blocks counter mem_free_blocks=0; // count available mem uint32_t total_mem=0, highest_end=0, high_end_low=0; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"the memory map contains %d entries.",entries); // preserve pointer uint16_t save=memmap; //print memory map and calc blocks. for(int i=0;ihighest_end){ highest_end=high_end; high_end_low=low_end; } } memmap+=12; } // restore pointer memmap=save; int blocks=highest_end/4096+1; mem_array_size=blocks/32+1; if(highest_end-high_end_low