nuttx/fs/shm/shmfs_alloc.c
chenrun1 3f47fd767a fs/xxfs:Replace kmm with fs heap
Summary:
  1.Add configuration to allocate memory from the specified section
  2.Replace all memory operations (kmm_) in the vfs with
    fs_heap_. When FS_HEAPSIZE > 0, memory is requested for the file system by specifying a configured heap location. By default (i.e. FS_HEAPSIZE=0) fs_heap_ is equivalent to kmm_

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2024-10-10 15:30:41 +02:00

163 lines
4.0 KiB
C

/****************************************************************************
* fs/shm/shmfs_alloc.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/pgalloc.h>
#include "shm/shmfs.h"
#include "fs_heap.h"
/****************************************************************************
* Public Functions
****************************************************************************/
FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
{
FAR struct shmfs_object_s *object;
bool allocated = false;
#if defined(CONFIG_BUILD_FLAT)
/* in FLAT build, allocate the object metadata and the data in the same
* chunk in kernel heap
*/
size_t alloc_size = sizeof(struct shmfs_object_s) + length;
if (alloc_size < length)
{
/* There must have been an integer overflow */
return NULL;
}
object = fs_heap_zalloc(alloc_size);
if (object)
{
object->paddr = (FAR char *)(object + 1);
allocated = true;
}
#elif defined(CONFIG_BUILD_PROTECTED)
/* in PROTECTED build, allocate the shm object in kernel heap, and shared
* memory in user heap
*/
object = fs_heap_zalloc(sizeof(struct shmfs_object_s));
if (object)
{
object->paddr = kumm_zalloc(length);
if (object->paddr)
{
allocated = true;
}
}
#elif defined(CONFIG_BUILD_KERNEL)
/* in KERNEL build, allocate the shared memory from page pool and store the
* physical address
*/
size_t i = 0;
FAR void **pages;
size_t n_pages = MM_NPAGES(length);
object = fs_heap_zalloc(sizeof(struct shmfs_object_s) +
(n_pages - 1) * sizeof(object->paddr));
if (object)
{
pages = &object->paddr;
for (; i < n_pages; i++)
{
pages[i] = (FAR void *)mm_pgalloc(1);
if (!pages[i])
{
break;
}
else
{
/* Clear the page memory (requirement for truncate) */
up_addrenv_page_wipe((uintptr_t)pages[i]);
}
}
}
if (i == n_pages)
{
allocated = true;
}
#endif
if (allocated)
{
object->length = length;
}
else
{
/* delete any partial allocation */
shmfs_free_object(object);
object = NULL;
}
return object;
}
void shmfs_free_object(FAR struct shmfs_object_s *object)
{
#if defined(CONFIG_BUILD_KERNEL)
size_t i;
size_t n_pages = MM_NPAGES(object->length);
FAR void **pages;
#endif
if (object)
{
#if defined (CONFIG_BUILD_PROTECTED)
kumm_free(object->paddr);
#elif defined(CONFIG_BUILD_KERNEL)
pages = &object->paddr;
for (i = 0; i < n_pages; i++)
{
if (pages[i])
{
mm_pgfree((uintptr_t)pages[i], 1);
}
}
#endif
/* Delete the object metadata
* (and the shared memory in case of FLAT build)
*/
fs_heap_free(object);
}
}