# MemoryBackend类层次结构

作为一个设备类型，MemoryBackend自然也有自己的类层次结构。

经过这么多代码的洗礼，我就不多说什么了，直接上类继承关系图。

```
       +------------------+                                       +----------------------+
       |                  |                                       |                      |
       |   ObjectClass    |  ------------------------------       |   Object             |
       |     class_init   |                                       |                      |
       |                  |                                       |                      |
       +------------------+                                       +----------------------+
                 |                                                            |
                 |                                                            |
                 |                                                            |
                 v                                                            v
  +--------------------------+                                       +----------------------+
  |                          |                                       |                      |
  |HostMemoryBackendClass    |  ------------------------------------ |   HostMemoryBackend  |
  |    class_init            |  host_memory_backend_class_init       |      instance_init   |  host_memory_backend_init
  |                          |                                       |                      |
  |  +-----------------------+                                       |                      |
  |  |UserCreatableClass     |                                       |                      |
  |  |    complete           |  host_memory_backend_memory_complete  |                      |
  +--+-----------------------+                                       +---+------------------+
          |                                                              |
          |                                                              |
          |                                                              |
          |                                                              |
          |     TYPE_MEMORY_BACKEND_RAM                                  |
          |     +-----------------------+                                |      +----------------------+
          |     |                       |                                |      |                      |
          +---  |HostMemoryBackendClass |                                +---   |HostMemoryBackend     |
          |     |     class_init        |  ram_backend_class_init        |      |      instance_init   |
          |     |     bc->alloc         |  ram_backend_memory_alloc      |      |                      |
          |     +-----------------------+                                |      +----------------------+
          |                                                              |     
          |     TYPE_MEMORY_BACKEND_FILE                                 |     
          |     +-----------------------+                                |      +----------------------+
          |     |                       |                                |      |                      |
          +---  |HostMemoryBackendClass |                                +---   |HostMemoryBackendFile |
          |     |     class_init        |  file_backend_class_init       |      |      instance_init   |
          |     |     bc->alloc         |  file_backend_memory_alloc     |      |                      |
          |     +-----------------------+                                |      +----------------------+
          |                                                              |
          |     TYPE_MEMORY_BACKEND_RAM                                  |
          |     +-----------------------+                                |      +----------------------+
          |     |                       |                                |      |                      |
          +---  |HostMemoryBackendClass |                                +---   |HostMemoryBackendMemfd|
                |     class_init        |  memfd_backend_class_init             |      instance_init   |   memfd_backend_instance_init
                |     bc->alloc         |  memfd_backend_memory_alloc           |                      |
                +-----------------------+                                       +----------------------+
```

可以看到，现在的MemoryBackend一共有三种具体的实现 ram, file, memfd。他们大同小异，主要的区别就在于bc->alloc函数的实现不同。

那这个bc->alloc究竟是如何起作用的，请看下节\[初始化流程]\[/memory\_backend/02-init\_flow\.md]
