2014-08-24 02:43:21 +08:00
|
|
|
/****************************************************************************
|
2014-09-23 00:33:23 +08:00
|
|
|
* mm/mm_gran/mm_granreserve.c
|
2014-08-24 02:43:21 +08:00
|
|
|
*
|
2024-09-12 22:44:55 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 20:50:10 +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-08-24 02:43:21 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-08-24 02:43:21 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +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-08-24 02:43:21 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2024-04-08 14:48:17 +08:00
|
|
|
#include <debug.h>
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2014-09-24 20:55:26 +08:00
|
|
|
#include <nuttx/mm/gran.h>
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2014-09-23 00:33:23 +08:00
|
|
|
#include "mm_gran/mm_gran.h"
|
2024-04-08 14:48:17 +08:00
|
|
|
#include "mm_gran/mm_grantable.h"
|
2014-08-24 02:43:21 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_GRAN
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-15 01:47:12 +08:00
|
|
|
* Public Functions
|
2014-08-24 02:43:21 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-17 19:39:17 +08:00
|
|
|
FAR void *gran_reserve(GRAN_HANDLE handle, uintptr_t start, size_t size)
|
2014-08-24 02:43:21 +08:00
|
|
|
{
|
2024-04-08 14:48:17 +08:00
|
|
|
FAR gran_t *gran = (FAR gran_t *)handle;
|
|
|
|
uintptr_t end;
|
|
|
|
size_t ngran;
|
|
|
|
size_t posi;
|
|
|
|
bool avail;
|
|
|
|
|
|
|
|
DEBUGASSERT(gran);
|
|
|
|
if (!size || size > GRANBYTE(gran))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-15 01:47:12 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
/* align down/up start/ending addresses */
|
2017-11-15 01:47:12 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
end = END_RSRV(gran, start, size);
|
|
|
|
if (!GRAN_INRANGE(gran, end))
|
2014-08-24 02:43:21 +08:00
|
|
|
{
|
2024-04-08 14:48:17 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
start = MEM_RSRV(gran, start);
|
|
|
|
if (!GRAN_INRANGE(gran, start))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
/* convert unit to granule */
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
posi = MEM2GRAN(gran, start);
|
|
|
|
ngran = ((end - start) >> gran->log2gran) + 1;
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
/* lock the granule allocator */
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
if (gran_enter_critical(gran) < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-17 19:39:17 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
avail = gran_match(gran, posi, ngran, 0, NULL);
|
|
|
|
if (avail)
|
|
|
|
{
|
|
|
|
gran_set(gran, posi, ngran);
|
|
|
|
}
|
2022-11-17 19:39:17 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
gran_leave_critical(gran);
|
2014-08-24 02:43:21 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
graninfo("%s posi=%zu retp=%zx size=%zu n=%zu\n",
|
|
|
|
avail ? " done" : " error", posi, (size_t)start, size, ngran);
|
2022-11-17 19:39:17 +08:00
|
|
|
|
2024-04-08 14:48:17 +08:00
|
|
|
return avail ? (FAR void *)start : NULL;
|
2014-08-24 02:43:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_GRAN */
|