diff mbox series

[2/6] memory: Introduce address_space_create()

Message ID 20210819142039.2825366-3-philmd@redhat.com
State New
Headers show
Series memory: Introduce address_space_create(), re-use &address_space_memory | expand

Commit Message

Philippe Mathieu-Daudé Aug. 19, 2021, 2:20 p.m. UTC
Introduce address_space_create(). In is similar to
address_space_init() but returns a pointer to a heap
allocated  AddressSpace.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 include/exec/memory.h | 14 ++++++++++++++
 softmmu/memory.c      | 10 ++++++++++
 2 files changed, 24 insertions(+)

Comments

Peter Maydell Aug. 19, 2021, 2:24 p.m. UTC | #1
On Thu, 19 Aug 2021 at 15:20, Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
>
> Introduce address_space_create(). In is similar to
> address_space_init() but returns a pointer to a heap
> allocated  AddressSpace.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  include/exec/memory.h | 14 ++++++++++++++
>  softmmu/memory.c      | 10 ++++++++++
>  2 files changed, 24 insertions(+)
>
> diff --git a/include/exec/memory.h b/include/exec/memory.h
> index c3d417d317f..b353a48c25f 100644
> --- a/include/exec/memory.h
> +++ b/include/exec/memory.h
> @@ -2418,6 +2418,20 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
>   */
>  void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
>
> +/**
> + * address_space_create: Create and initializes an address space
> + *
> + * @root: a #MemoryRegion that routes addresses for the address space
> + * @name: an address space name.  The name is only used for debugging
> + *        output.
> + *
> + * Returns pointer to initialized #AddressSpace.
> + *
> + * The caller is responsible for releasing the pointer returned
> + * with address_space_destroy() after use.
> + */
> +AddressSpace *address_space_create(MemoryRegion *root, const char *name);
> +

I'm not really a fan of this as an API -- almost always I think
devices would do better to have an AddressSpace foo field in
their device struct and call address_space_init() on that.
Hiding the heap allocation inside this function makes it harder
to notice it during code review, I think.

thanks
-- PMM
Philippe Mathieu-Daudé Aug. 19, 2021, 2:36 p.m. UTC | #2
On 8/19/21 4:24 PM, Peter Maydell wrote:
> On Thu, 19 Aug 2021 at 15:20, Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
>>
>> Introduce address_space_create(). In is similar to
>> address_space_init() but returns a pointer to a heap
>> allocated  AddressSpace.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>> ---
>>  include/exec/memory.h | 14 ++++++++++++++
>>  softmmu/memory.c      | 10 ++++++++++
>>  2 files changed, 24 insertions(+)
>>
>> diff --git a/include/exec/memory.h b/include/exec/memory.h
>> index c3d417d317f..b353a48c25f 100644
>> --- a/include/exec/memory.h
>> +++ b/include/exec/memory.h
>> @@ -2418,6 +2418,20 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
>>   */
>>  void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
>>
>> +/**
>> + * address_space_create: Create and initializes an address space
>> + *
>> + * @root: a #MemoryRegion that routes addresses for the address space
>> + * @name: an address space name.  The name is only used for debugging
>> + *        output.
>> + *
>> + * Returns pointer to initialized #AddressSpace.
>> + *
>> + * The caller is responsible for releasing the pointer returned
>> + * with address_space_destroy() after use.
>> + */
>> +AddressSpace *address_space_create(MemoryRegion *root, const char *name);
>> +
> 
> I'm not really a fan of this as an API -- almost always I think
> devices would do better to have an AddressSpace foo field in
> their device struct and call address_space_init() on that.
> Hiding the heap allocation inside this function makes it harder
> to notice it during code review, I think.

So I understand you rather I discard this (simple) approach and
rather modify 'info mtree' "was designed on the assumption that
there's really only one or two interesting address spaces." [*]

[*] https://www.mail-archive.com/qemu-devel@nongnu.org/msg829821.html
diff mbox series

Patch

diff --git a/include/exec/memory.h b/include/exec/memory.h
index c3d417d317f..b353a48c25f 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -2418,6 +2418,20 @@  MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
  */
 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
 
+/**
+ * address_space_create: Create and initializes an address space
+ *
+ * @root: a #MemoryRegion that routes addresses for the address space
+ * @name: an address space name.  The name is only used for debugging
+ *        output.
+ *
+ * Returns pointer to initialized #AddressSpace.
+ *
+ * The caller is responsible for releasing the pointer returned
+ * with address_space_destroy() after use.
+ */
+AddressSpace *address_space_create(MemoryRegion *root, const char *name);
+
 /**
  * address_space_destroy: destroy an address space
  *
diff --git a/softmmu/memory.c b/softmmu/memory.c
index 185f978c925..16a2b518d8d 100644
--- a/softmmu/memory.c
+++ b/softmmu/memory.c
@@ -2937,6 +2937,16 @@  void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
     address_space_update_ioeventfds(as);
 }
 
+AddressSpace *address_space_create(MemoryRegion *root, const char *name)
+{
+    AddressSpace *as;
+
+    as = g_new(AddressSpace, 1);
+    address_space_init(as, root, name);
+
+    return as;
+}
+
 static void do_address_space_destroy(AddressSpace *as)
 {
     assert(QTAILQ_EMPTY(&as->listeners));