2012-09-11 20:22:27 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:33:23 +02:00
|
|
|
* mm/mm_gran/mm_granfree.c
|
2012-09-11 20:22:27 +02:00
|
|
|
*
|
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>
|
|
|
|
|
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"
|
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
|
|
|
* Name: gran_free
|
2012-09-11 20:22:27 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return memory to the granule heap.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - The handle previously returned by gran_initialize
|
|
|
|
* memory - A pointer to memory previoiusly allocated by gran_alloc.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-14 18:47:12 +01:00
|
|
|
void gran_free(GRAN_HANDLE handle, FAR void *memory, size_t size)
|
2012-09-11 20:22:27 +02:00
|
|
|
{
|
2017-11-14 18:47:12 +01:00
|
|
|
FAR struct gran_s *priv = (FAR struct gran_s *)handle;
|
2012-09-11 20:22:27 +02:00
|
|
|
unsigned int granno;
|
|
|
|
unsigned int gatidx;
|
|
|
|
unsigned int gatbit;
|
2012-09-11 22:33:58 +02:00
|
|
|
unsigned int granmask;
|
|
|
|
unsigned int ngranules;
|
2012-09-11 20:22:27 +02:00
|
|
|
unsigned int avail;
|
2012-09-12 17:18:56 +02:00
|
|
|
uint32_t gatmask;
|
2020-03-29 19:57:53 +02:00
|
|
|
int ret;
|
2012-09-11 22:33:58 +02:00
|
|
|
|
2017-11-14 18:47:12 +01:00
|
|
|
DEBUGASSERT(priv != NULL && memory && size <= 32 * (1 << priv->log2gran));
|
2012-09-11 20:22:27 +02:00
|
|
|
|
2012-09-11 23:39:39 +02:00
|
|
|
/* Get exclusive access to the GAT */
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = gran_enter_critical(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Should happen only on task cancellation */
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == -ECANCELED);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-11 23:39:39 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
/* Determine the granule number of the first granule in the allocation */
|
2012-09-11 20:22:27 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
granno = ((uintptr_t)memory - priv->heapstart) >> priv->log2gran;
|
2012-09-11 20:22:27 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
/* Determine the GAT table index and bit number associated with the
|
|
|
|
* allocation.
|
|
|
|
*/
|
2012-09-11 20:22:27 +02:00
|
|
|
|
|
|
|
gatidx = granno >> 5;
|
|
|
|
gatbit = granno & 31;
|
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
/* Determine the number of granules in the allocation */
|
|
|
|
|
|
|
|
granmask = (1 << priv->log2gran) - 1;
|
|
|
|
ngranules = (size + granmask) >> priv->log2gran;
|
|
|
|
|
2012-09-12 19:55:03 +02:00
|
|
|
/* Clear bits in the GAT entry or entries */
|
2012-09-11 20:22:27 +02:00
|
|
|
|
|
|
|
avail = 32 - gatbit;
|
|
|
|
if (ngranules > avail)
|
|
|
|
{
|
2012-09-12 19:55:03 +02:00
|
|
|
/* Clear bits in the first GAT entry */
|
|
|
|
|
|
|
|
gatmask = (0xffffffff << gatbit);
|
|
|
|
DEBUGASSERT((priv->gat[gatidx] & gatmask) == gatmask);
|
|
|
|
|
|
|
|
priv->gat[gatidx] &= ~gatmask;
|
2012-09-11 20:22:27 +02:00
|
|
|
ngranules -= avail;
|
2012-09-11 23:39:39 +02:00
|
|
|
|
|
|
|
/* Clear bits in the second GAT entry */
|
|
|
|
|
|
|
|
gatmask = 0xffffffff >> (32 - ngranules);
|
2020-02-13 14:58:07 +01:00
|
|
|
DEBUGASSERT((priv->gat[gatidx + 1] & gatmask) == gatmask);
|
2012-09-12 19:55:03 +02:00
|
|
|
|
2020-02-13 14:58:07 +01:00
|
|
|
priv->gat[gatidx + 1] &= ~gatmask;
|
2012-09-11 20:22:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 23:39:39 +02:00
|
|
|
/* Handle the case where where all of the granules came from one entry */
|
2012-09-11 20:22:27 +02:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2012-09-12 19:55:03 +02:00
|
|
|
/* Clear bits in a single GAT entry */
|
|
|
|
|
|
|
|
gatmask = 0xffffffff >> (32 - ngranules);
|
|
|
|
gatmask <<= gatbit;
|
|
|
|
DEBUGASSERT((priv->gat[gatidx] & gatmask) == gatmask);
|
|
|
|
|
|
|
|
priv->gat[gatidx] &= ~gatmask;
|
2012-09-11 20:22:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 17:18:56 +02:00
|
|
|
gran_leave_critical(priv);
|
2012-09-11 20:22:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
#endif /* CONFIG_GRAN */
|