#define FOOLOS_MODULE_NAME "mem" #include "config.h" #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 ??? TODO: what is this!? #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) { panic(FOOLOS_MODULE_NAME,"OUT OF MEMORY (alloc_block)"); return 0; //out of memory } mmap_set (frame); mem_free_blocks--; uint32_t addr = frame * PMMNGR_BLOCK_SIZE; return (void*)addr; } void* pmmngr_alloc_blocks (uint32_t size) { int frame = mmap_first_free_s (size); if (frame == -1) { panic(FOOLOS_MODULE_NAME,"OUT OF MEMORY (alloc_blocks)"); return 0; //out of memory } for (int i=0; ihighest_end) { highest_end=high_end; high_end_low=last_end; } } else { last_end=high_end+1; } memmap+=12; } // restore pointer memmap=save; int blocks=highest_end/4096+1; mem_array_size=blocks/32+1; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Highest end area: 0x%08X - 0x%08X",high_end_low,highest_end); if(highest_end-high_end_low