项目作者: lastweek

项目描述 :
a userspace buddy allocator.
高级语言: C
项目地址: git://github.com/lastweek/source-minimal-buddy-allocator.git
创建时间: 2020-05-04T10:16:59Z
项目社区:https://github.com/lastweek/source-minimal-buddy-allocator

开源协议:

下载


Userspace Buddy Allocator

During my recent work, I need to build an allocator
to manage a certain device’s on-board DRAM (couple of GBs).
Since everything in our system is page-sized, we used a buddy allocator for that purpose.

Due to time limitation, I directly ported a simplified version from linux kernel (mm/page_allo.c) into userspace.
Algorithm-wise, a buddy allocator is simple. But a practical implementation adds some complications,
as is the case for the kernel one.

I did some modificatons, e.g., remove a lot zone and accounting code, but kept its core.
I decided to make it public, its linux’s code anyway.

Why?

  1. This port is generic/small, can be embedded into other designs.
  2. Good start if you want to learn how Linux manages physical memory.

Tuning

Managed Physical Memory Range (without holes now):

  1. /*
  2. * The managed physical memory range.
  3. * We do not allow any holes at this point.
  4. */
  5. unsigned long buddy_mem_start = PAGE_SIZE;
  6. unsigned long buddy_mem_end = 1024 * 1024 * 1024;

Page Size:

  1. #define PAGE_SHIFT (12)

The maximum orders [0, MAX_ORDER):

  1. #define MAX_ORDER (3)

Debugging:

  1. (e.g., 16GB memory, MAX_ORDER=11)
  2. Buddy Allocator
  3. -------- ---------- --------------------
  4. order size (kb) nr_free
  5. -------- ---------- --------------------
  6. 0 4 1
  7. 1 8 1
  8. 2 16 1
  9. 3 32 1
  10. 4 64 1
  11. 5 128 1
  12. 6 256 1
  13. 7 512 1
  14. 8 1024 1
  15. 9 2048 1
  16. 10 4096 4095
  17. -------- ---------- --------------------
  18. Total Free: 16777212 kb, or around 16383 mb