2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 #include "sysemu/hostmem.h"
13 #include "qom/object_interfaces.h"
17 * @TYPE_MEMORY_BACKEND_FILE:
18 * name of backend that uses mmap on a file descriptor
20 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
22 #define MEMORY_BACKEND_FILE(obj) \
23 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
25 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
27 struct HostMemoryBackendFile
{
28 HostMemoryBackend parent_obj
;
33 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
35 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
38 error_setg(errp
, "can't create backend with size 0");
42 error_setg(errp
, "mem_path property not set");
46 error_setg(errp
, "-mem-path not supported on this host");
48 if (!memory_region_size(&backend
->mr
)) {
49 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
50 object_get_canonical_path(OBJECT(backend
)),
58 file_backend_class_init(ObjectClass
*oc
, void *data
)
60 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
62 bc
->alloc
= file_backend_memory_alloc
;
65 static char *get_mem_path(Object
*o
, Error
**errp
)
67 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
69 return g_strdup(fb
->mem_path
);
72 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
74 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
75 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
77 if (memory_region_size(&backend
->mr
)) {
78 error_setg(errp
, "cannot change property value");
84 fb
->mem_path
= g_strdup(str
);
88 file_backend_instance_init(Object
*o
)
90 object_property_add_str(o
, "mem-path", get_mem_path
,
94 static const TypeInfo file_backend_info
= {
95 .name
= TYPE_MEMORY_BACKEND_FILE
,
96 .parent
= TYPE_MEMORY_BACKEND
,
97 .class_init
= file_backend_class_init
,
98 .instance_init
= file_backend_instance_init
,
99 .instance_size
= sizeof(HostMemoryBackendFile
),
102 static void register_types(void)
104 type_register_static(&file_backend_info
);
107 type_init(register_types
);