The k project

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);

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);
  /* 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);