Skip to content

Commit

Permalink
fs/shm/shmfs_alloc.c: Allocate zero-initialized memory in flat build
Browse files Browse the repository at this point in the history
POSIX requires that the shm objects are zero-initialized. This has been broken
in some earlier commits (starting from 9af5fc5)

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 <[email protected]>
  • Loading branch information
jlaitine committed Dec 12, 2024
1 parent 6819583 commit 35d1afa
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions fs/shm/shmfs_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 35d1afa

Please sign in to comment.