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