2014-09-23 04:53:56 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* mm/shm/shmdt.c
|
|
|
|
*
|
2020-05-09 23:05:29 +08:00
|
|
|
* 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
|
2014-09-23 04:53:56 +08:00
|
|
|
*
|
2020-05-09 23:05:29 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-23 04:53:56 +08:00
|
|
|
*
|
2020-05-09 23:05:29 +08:00
|
|
|
* 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.
|
2014-09-23 04:53:56 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-24 06:07:12 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/shm.h>
|
2021-06-09 02:00:55 +08:00
|
|
|
#include <assert.h>
|
2022-11-16 20:30:18 +08:00
|
|
|
#include <debug.h>
|
2014-09-24 06:07:12 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/sched.h>
|
2022-11-16 20:30:18 +08:00
|
|
|
#include <nuttx/pgalloc.h>
|
2022-04-12 20:34:50 +08:00
|
|
|
#include <nuttx/mm/map.h>
|
2014-09-24 06:07:12 +08:00
|
|
|
|
2024-07-15 17:52:00 +08:00
|
|
|
#include "sched/sched.h"
|
2014-09-24 06:07:12 +08:00
|
|
|
#include "shm/shm.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_MM_SHM
|
|
|
|
|
2014-09-23 04:53:56 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-01-10 21:03:04 +08:00
|
|
|
* Name: shmdt
|
2014-09-23 04:53:56 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2023-01-10 21:03:04 +08:00
|
|
|
* The shmdt() function detaches the shared memory segment located at
|
2022-04-12 20:34:50 +08:00
|
|
|
* the address specified by shmaddr from the address space of the calling
|
2014-09-23 04:53:56 +08:00
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* shmid - Shared memory identifier
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2023-01-10 21:03:04 +08:00
|
|
|
* Upon successful completion, shmdt() will decrement the value of
|
2014-09-23 04:53:56 +08:00
|
|
|
* shm_nattch in the data structure associated with the shared memory ID
|
|
|
|
* of the attached shared memory segment and return 0.
|
|
|
|
*
|
2023-01-10 21:03:04 +08:00
|
|
|
* Otherwise, the shared memory segment will not be detached, shmdt()
|
2015-10-05 05:04:00 +08:00
|
|
|
* will return -1, and errno will be set to indicate the error.
|
2014-09-23 04:53:56 +08:00
|
|
|
*
|
2014-09-24 06:07:12 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-12 20:34:50 +08:00
|
|
|
int shmdt(FAR const void *shmaddr)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *tcb;
|
|
|
|
FAR struct mm_map_entry_s *entry;
|
|
|
|
FAR struct task_group_s *group;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the TCB and group containing our virtual memory allocator */
|
|
|
|
|
2024-07-15 17:52:00 +08:00
|
|
|
tcb = this_task();
|
2022-04-12 20:34:50 +08:00
|
|
|
DEBUGASSERT(tcb && tcb->group);
|
|
|
|
group = tcb->group;
|
|
|
|
|
|
|
|
/* Get exclusive access to process' mm_map */
|
|
|
|
|
|
|
|
ret = mm_map_lock();
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Perform the reverse lookup to get the shmid corresponding to this
|
|
|
|
* shmaddr. The mapping is matched with just shmaddr == map->vaddr.
|
|
|
|
*/
|
|
|
|
|
2023-04-20 20:37:45 +08:00
|
|
|
entry = mm_map_find(get_current_mm(), shmaddr, 1);
|
2023-01-10 21:03:04 +08:00
|
|
|
if (entry && entry->vaddr == shmaddr)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(entry->munmap);
|
|
|
|
ret = entry->munmap(group, entry, entry->vaddr, entry->length);
|
|
|
|
}
|
|
|
|
else
|
2022-04-12 20:34:50 +08:00
|
|
|
{
|
|
|
|
shmerr("ERROR: No region matching this virtual address: %p\n",
|
|
|
|
shmaddr);
|
|
|
|
|
2023-01-10 21:03:04 +08:00
|
|
|
ret = -EINVAL;
|
2022-04-12 20:34:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mm_map_unlock();
|
2023-01-10 21:03:04 +08:00
|
|
|
}
|
2022-04-12 20:34:50 +08:00
|
|
|
|
2023-01-10 21:03:04 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = -1;
|
2022-04-12 20:34:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-23 04:53:56 +08:00
|
|
|
#endif /* CONFIG_MM_SHM */
|