From 35d1afaa00a16182413b81dc501cc1304f91b155 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Thu, 12 Dec 2024 12:22:39 +0200 Subject: [PATCH] fs/shm/shmfs_alloc.c: Allocate zero-initialized memory in flat build POSIX requires that the shm objects are zero-initialized. This has been broken in some earlier commits (starting from 9af5fc5d09724a5d300bc07ce85b9ba5c01ffadd) Also fix the flat build memory allocation to allocate both object data and payload in the same chunk (as the comment also suggests). This saves allocations and memory in a system with lots of shm objects. Signed-off-by: Jukka Laitinen --- fs/shm/shmfs_alloc.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/fs/shm/shmfs_alloc.c b/fs/shm/shmfs_alloc.c index 4764c901e6849..740113d7c1b56 100644 --- a/fs/shm/shmfs_alloc.c +++ b/fs/shm/shmfs_alloc.c @@ -51,24 +51,34 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length) * chunk in kernel heap */ - object = fs_heap_zalloc(sizeof(struct shmfs_object_s)); - if (object) + size_t alloc_size; + size_t cachesize = up_get_dcache_linesize(); + + if (cachesize > 0) { - size_t cachesize = up_get_dcache_linesize(); - if (cachesize > 0) + alloc_size = + ALIGN_UP(sizeof(struct shmfs_object_s), cachesize) + + ALIGN_UP(length, cachesize); + object = fs_heap_memalign(cachesize, alloc_size); + if (object) { - object->paddr = fs_heap_memalign(cachesize, - ALIGN_UP(length, cachesize)); + memset(object, 0, alloc_size); + object->paddr = (void *)((uintptr_t)object + cachesize); } - else + } + else + { + alloc_size = sizeof(struct shmfs_object_s) + length; + object = fs_heap_zalloc(alloc_size); + if (object) { - object->paddr = fs_heap_malloc(length); + object->paddr = (FAR char *)(object + 1); } + } - if (object->paddr) - { - allocated = true; - } + if (object->paddr) + { + allocated = true; } #elif defined(CONFIG_BUILD_PROTECTED) @@ -152,9 +162,7 @@ void shmfs_free_object(FAR struct shmfs_object_s *object) { if (object) { -#if defined(CONFIG_BUILD_FLAT) - fs_heap_free(object->paddr); -#elif defined(CONFIG_BUILD_PROTECTED) +#if defined(CONFIG_BUILD_PROTECTED) kumm_free(object->paddr); #elif defined(CONFIG_BUILD_KERNEL) size_t i;