Memory reserve API
A physical addresses reserver is provided to help during the project. Here is how it works:
void memory_dump();
void memory_init(multiboot_info_t *info);
void *memory_reserve(size_t size);
void *memory_reserve_ex(unsigned int base_addr, size_t size);
void memory_release(void *ptr);
memory_init
: this takes the multiboot info structure k_main takes as an arguments. This function will make sure the area where the kernel is located is properly reserved so that future calls tomemory_reserve
ormemory_reserve_ex
can’t allocate memory in this area.memory_dump
: pretty prints the current allocator state.memory_reserve
: reserve a memory area of sizesize
.memory_reserve_ex
: reserve a memory area of sizesize
atbase_addr
.memory_release
: ifmemory_reserve
is equivalent tomalloc
, this is equivalent tofree
.
Here is an example of its usage:
/* Memory initialization, mandatory for API use */
memory_init(info);
/* Reserves a 2048 bytes cluster */
void *cluster = memory_reserve(2048);
/* Reserves a range of bsize addresses starting at offset base_addr */
void *kernel = memory_reserve_ex(base_addr, bsize);
/* Memory Usage */
/* Release memory previously allocated */
memory_release(cluster);
memory_release(kernel);
Cache allocator API
A cache allocator is also provided. It is designed for fast allocation and deallocation of multiple same size objects (like ISO blocks). The interface is pretty straight forward:
struct cache *cache_new(void *base, size_t nmemb, size_t bsize);
void *cache_alloc(struct cache *cache);
void cache_free(struct cache *cache, void *ptr);
cache_new
: creates a new cache atbase
(this should be a memory area previously allocated withmemory_reserve
) containingnmemb
of sizebsize
.cache_alloc
: returns a pointer to a free cache incache
.-
cache_free
: release the cache slot corresponding toptr
incache
.Here is an example of cache allocator usage:
/* Reserve enough space to permits cache creation */
void *ptr = memory_reserve(nmemb * bsize);
/* Create the cache to have nmemb slots of size bsize */
struct cache *my_cache = cache_new(ptr, nmemb, bsize);
/* Get a pointer to one one the cache slots */
void *buff = cache_alloc(my_cache);
/* Use of buff */
/* Free the pointer buff and release its slot in my_cache
cache_free(my_cache, buff);