2022-03-24 07:52:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-02-01 14:23:09 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2022-03-24 07:52:46 +01:00
|
|
|
#include <stdbool.h>
|
2024-02-01 14:23:09 +01:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2022-03-24 07:52:46 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/pgalloc.h>
|
|
|
|
|
|
|
|
#include "shm/shmfs.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
|
|
|
|
*/
|
|
|
|
|
|
|
|
object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
|
|
|
|
if (object)
|
|
|
|
{
|
2023-01-26 00:36:14 +01:00
|
|
|
object->paddr = (FAR char *)(object + 1);
|
2022-03-24 07:52:46 +01:00
|
|
|
allocated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(CONFIG_BUILD_PROTECTED)
|
|
|
|
/* in PROTECTED build, allocate the shm object in kernel heap, and shared
|
|
|
|
* memory in user heap
|
|
|
|
*/
|
|
|
|
|
|
|
|
object = kmm_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 = kmm_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;
|
|
|
|
}
|
2024-02-01 14:23:09 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Clear the page memory (requirement for truncate) */
|
|
|
|
|
|
|
|
up_addrenv_page_wipe((uintptr_t)pages[i]);
|
|
|
|
}
|
2022-03-24 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
|
|
|
|
kmm_free(object);
|
|
|
|
}
|
|
|
|
}
|