2012-09-11 20:22:27 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:33:23 +02:00
|
|
|
* mm/mm_gran/mm_granalloc.c
|
2012-09-11 20:22:27 +02:00
|
|
|
*
|
2024-09-12 16:44:55 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-29 19:57:53 +02: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
|
2012-09-11 20:22:27 +02:00
|
|
|
*
|
2020-03-29 19:57:53 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-09-11 20:22:27 +02:00
|
|
|
*
|
2020-03-29 19:57:53 +02: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.
|
2012-09-11 20:22:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
#include <assert.h>
|
2024-04-08 08:48:17 +02:00
|
|
|
#include <debug.h>
|
2012-09-11 22:33:58 +02:00
|
|
|
|
2014-09-24 14:55:26 +02:00
|
|
|
#include <nuttx/mm/gran.h>
|
2012-09-11 20:22:27 +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"
|
2012-09-11 22:33:58 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_GRAN
|
|
|
|
|
2012-09-11 20:22:27 +02:00
|
|
|
/****************************************************************************
|
2017-11-14 18:47:12 +01:00
|
|
|
* Public Functions
|
2012-09-11 20:22:27 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-14 18:47:12 +01:00
|
|
|
FAR void *gran_alloc(GRAN_HANDLE handle, size_t size)
|
2012-09-11 20:22:27 +02:00
|
|
|
{
|
2024-04-08 08:48:17 +02:00
|
|
|
FAR gran_t *gran = (FAR gran_t *)handle;
|
|
|
|
size_t ngran;
|
|
|
|
int posi;
|
|
|
|
int ret;
|
|
|
|
uintptr_t retp;
|
|
|
|
|
|
|
|
DEBUGASSERT(gran);
|
|
|
|
ngran = NGRANULE(gran, size);
|
|
|
|
if (!ngran || ngran > gran->ngranules)
|
2012-09-11 20:22:27 +02:00
|
|
|
{
|
2024-04-08 08:48:17 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-09-11 20:22:27 +02:00
|
|
|
|
2024-04-08 08:48:17 +02:00
|
|
|
ret = gran_enter_critical(gran);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-09-11 20:22:27 +02:00
|
|
|
|
2024-04-08 08:48:17 +02:00
|
|
|
posi = gran_search(gran, ngran);
|
|
|
|
if (posi >= 0)
|
|
|
|
{
|
|
|
|
gran_set(gran, posi, ngran);
|
|
|
|
}
|
2015-01-19 19:57:25 +01:00
|
|
|
|
2024-04-08 08:48:17 +02:00
|
|
|
gran_leave_critical(gran);
|
|
|
|
if (posi < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
2012-09-11 20:22:27 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 08:48:17 +02:00
|
|
|
retp = gran->heapstart + (posi << gran->log2gran);
|
|
|
|
graninfo("heap=%"PRIxPTR" posi=%d retp=%"PRIxPTR" size=%zu n=%zu\n",
|
|
|
|
gran->heapstart, posi, retp, size, ngran);
|
|
|
|
DEBUGASSERT(retp >= gran->heapstart);
|
|
|
|
DEBUGASSERT(retp < gran->heapstart + GRANBYTE(gran));
|
|
|
|
return (FAR void *)retp;
|
2012-09-11 20:22:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
#endif /* CONFIG_GRAN */
|