2014-08-23 20:43:21 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:33:23 +02:00
|
|
|
* mm/mm_gran/mm_granmark.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
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gran_mark_allocated
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Mark a range of granules as allocated.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - The granule heap state structure.
|
|
|
|
* alloc - The address of the allocation.
|
|
|
|
* ngranules - The number of granules allocated
|
|
|
|
*
|
|
|
|
* 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_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
|
|
|
|
unsigned int ngranules)
|
2014-08-23 20:43:21 +02:00
|
|
|
{
|
|
|
|
unsigned int granno;
|
|
|
|
unsigned int gatidx;
|
|
|
|
unsigned int gatbit;
|
|
|
|
unsigned int avail;
|
|
|
|
uint32_t gatmask;
|
|
|
|
|
|
|
|
/* Determine the granule number of the allocation */
|
|
|
|
|
|
|
|
granno = (alloc - priv->heapstart) >> priv->log2gran;
|
|
|
|
|
|
|
|
/* Determine the GAT table index associated with the allocation */
|
|
|
|
|
|
|
|
gatidx = granno >> 5;
|
|
|
|
gatbit = granno & 31;
|
|
|
|
|
|
|
|
/* Mark bits in the GAT entry or entries */
|
|
|
|
|
|
|
|
avail = 32 - gatbit;
|
|
|
|
if (ngranules > avail)
|
|
|
|
{
|
2022-11-17 12:39:17 +01:00
|
|
|
uint32_t gatmask2;
|
2014-08-23 20:43:21 +02:00
|
|
|
|
2022-11-17 12:39:17 +01:00
|
|
|
gatmask = 0xffffffff << gatbit;
|
2014-08-23 20:43:21 +02:00
|
|
|
ngranules -= avail;
|
2022-11-17 12:39:17 +01:00
|
|
|
gatmask2 = 0xffffffff >> (32 - ngranules);
|
|
|
|
|
|
|
|
/* Check that the area is free, from both mask words */
|
2014-08-23 20:43:21 +02:00
|
|
|
|
2022-11-17 12:39:17 +01:00
|
|
|
if (((priv->gat[gatidx] & gatmask) != 0) ||
|
|
|
|
((priv->gat[gatidx + 1] & gatmask2) != 0))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-23 20:43:21 +02:00
|
|
|
|
2022-11-17 12:39:17 +01:00
|
|
|
/* Mark bits in the first and second GAT entry */
|
2014-08-23 20:43:21 +02:00
|
|
|
|
2022-11-17 12:39:17 +01:00
|
|
|
priv->gat[gatidx] |= gatmask;
|
|
|
|
priv->gat[gatidx + 1] |= gatmask2;
|
2014-08-23 20:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the case where where all of the granules come from one entry */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gatmask = 0xffffffff >> (32 - ngranules);
|
|
|
|
gatmask <<= gatbit;
|
2022-11-17 12:39:17 +01:00
|
|
|
|
|
|
|
/* Check that the area is free */
|
|
|
|
|
|
|
|
if ((priv->gat[gatidx] & gatmask) != 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark bits in a single GAT entry */
|
2014-08-23 20:43:21 +02:00
|
|
|
|
|
|
|
priv->gat[gatidx] |= gatmask;
|
|
|
|
}
|
2022-11-17 12:39:17 +01:00
|
|
|
|
|
|
|
return (FAR void *)alloc;
|
2014-08-23 20:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_GRAN */
|