#define FOOLOS_MODULE_NAME "mem" #include #include "config.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; char *memmap_type_to_string[]= { "Usable", "Reserved", "ACPI reclaimable", "ACPI NVS", "Bad Memory" }; // 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)); } // // By default, Set all of memory is in use void pmmngr_init () { mem_free_blocks=0; 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; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"alloc block (%d) 0x%08X)",frame,addr); 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; i