diff mbox series

[v3,2/3] lib: sbi: Allocate from beginning of heap blocks

Message ID 20240807181719.244099-3-gregorhaas1997@gmail.com
State Changes Requested
Headers show
Series lib: sbi: Heap improvements for SMMTT | expand

Commit Message

Gregor Haas Aug. 7, 2024, 6:17 p.m. UTC
In the next commit, we'll add a new sbi_memalign() function. In order to
allocate aligned memory, we'll sometimes need to allocate from the middle of a
heap block, effectively splitting it in two. Allocating from the beginning of a
heap block in the nonaligned case more closely matches this behavior, reducing
the complexity of understanding the heap implementation.

Signed-off-by: Gregor Haas <gregorhaas1997@gmail.com>
---
 lib/sbi/sbi_heap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Anup Patel Aug. 8, 2024, 5:33 a.m. UTC | #1
On Wed, Aug 7, 2024 at 11:47 PM Gregor Haas <gregorhaas1997@gmail.com> wrote:
>
> In the next commit, we'll add a new sbi_memalign() function. In order to
> allocate aligned memory, we'll sometimes need to allocate from the middle of a
> heap block, effectively splitting it in two. Allocating from the beginning of a
> heap block in the nonaligned case more closely matches this behavior, reducing
> the complexity of understanding the heap implementation.
>
> Signed-off-by: Gregor Haas <gregorhaas1997@gmail.com>

LGTM.

Reviewed-by: Anup Patel <anup@brainfault.org>

Regards,
Anup


> ---
>  lib/sbi/sbi_heap.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/lib/sbi/sbi_heap.c b/lib/sbi/sbi_heap.c
> index e43d77c..cc4893d 100644
> --- a/lib/sbi/sbi_heap.c
> +++ b/lib/sbi/sbi_heap.c
> @@ -63,8 +63,9 @@ void *sbi_malloc_from(struct sbi_heap_control *hpctrl, size_t size)
>                         n = sbi_list_first_entry(&hpctrl->free_node_list,
>                                                  struct heap_node, head);
>                         sbi_list_del(&n->head);
> -                       n->addr = np->addr + np->size - size;
> +                       n->addr = np->addr;
>                         n->size = size;
> +                       np->addr += size;
>                         np->size -= size;
>                         sbi_list_add_tail(&n->head, &hpctrl->used_space_list);
>                         ret = (void *)n->addr;
> --
> 2.45.2
>
diff mbox series

Patch

diff --git a/lib/sbi/sbi_heap.c b/lib/sbi/sbi_heap.c
index e43d77c..cc4893d 100644
--- a/lib/sbi/sbi_heap.c
+++ b/lib/sbi/sbi_heap.c
@@ -63,8 +63,9 @@  void *sbi_malloc_from(struct sbi_heap_control *hpctrl, size_t size)
 			n = sbi_list_first_entry(&hpctrl->free_node_list,
 						 struct heap_node, head);
 			sbi_list_del(&n->head);
-			n->addr = np->addr + np->size - size;
+			n->addr = np->addr;
 			n->size = size;
+			np->addr += size;
 			np->size -= size;
 			sbi_list_add_tail(&n->head, &hpctrl->used_space_list);
 			ret = (void *)n->addr;