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
|
|
|
*
|
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>
|
|
|
|
|
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"
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-14 18:47:12 +01:00
|
|
|
* Name: gran_reserve
|
2014-08-23 20:43:21 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reserve memory in the granule heap. This will reserve the granules
|
|
|
|
* that contain the start and end addresses plus all of the granules
|
|
|
|
* in between. This should be done early in the initialization sequence
|
|
|
|
* before any other allocations are made.
|
|
|
|
*
|
|
|
|
* Reserved memory can never be allocated (it can be freed however which
|
|
|
|
* essentially unreserves the memory).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-11-14 18:47:12 +01:00
|
|
|
* handle - The handle previously returned by gran_initialize
|
|
|
|
* start - The address of the beginning of the region to be reserved.
|
|
|
|
* size - The size of the region to be reserved
|
2014-08-23 20:43:21 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2022-11-17 12:39:17 +01:00
|
|
|
* On success, a non-NULL pointer to the allocated memory is returned;
|
|
|
|
* NULL is returned on failure.
|
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
|
|
|
{
|
2017-11-14 18:47:12 +01:00
|
|
|
FAR struct gran_s *priv = (FAR struct gran_s *)handle;
|
2022-11-17 12:39:17 +01:00
|
|
|
FAR void *ret = NULL;
|
2017-11-14 18:47:12 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
2014-08-23 20:43:21 +02:00
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
uintptr_t mask = (1 << priv->log2gran) - 1;
|
|
|
|
uintptr_t end = start + size - 1;
|
|
|
|
unsigned int ngranules;
|
|
|
|
|
|
|
|
/* Get the aligned (down) start address and the aligned (up) end
|
|
|
|
* address
|
|
|
|
*/
|
|
|
|
|
|
|
|
start &= ~mask;
|
|
|
|
end = (end + mask) & ~mask;
|
|
|
|
|
2014-08-24 00:35:52 +02:00
|
|
|
/* Calculate the new size in granules */
|
2014-08-23 20:43:21 +02:00
|
|
|
|
|
|
|
ngranules = ((end - start) >> priv->log2gran) + 1;
|
|
|
|
|
2022-11-17 12:39:17 +01:00
|
|
|
/* Must lock the granule allocator */
|
|
|
|
|
|
|
|
if (gran_enter_critical(priv) < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-23 20:43:21 +02:00
|
|
|
/* And reserve the granules */
|
|
|
|
|
2022-11-17 12:39:17 +01:00
|
|
|
ret = gran_mark_allocated(priv, start, ngranules);
|
|
|
|
gran_leave_critical(priv);
|
2014-08-23 20:43:21 +02:00
|
|
|
}
|
2022-11-17 12:39:17 +01:00
|
|
|
|
|
|
|
return ret;
|
2014-08-23 20:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_GRAN */
|