2010-03-22 07:49:57 +08:00
|
|
|
/*
|
2013-03-30 16:14:38 +08:00
|
|
|
* heap management in finsh shell.
|
2010-03-22 07:49:57 +08:00
|
|
|
*
|
2013-03-30 16:14:38 +08:00
|
|
|
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* This file is part of RT-Thread (http://www.rt-thread.org)
|
|
|
|
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2010-03-22 07:49:57 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2010-03-22 Bernard first version
|
|
|
|
*/
|
2009-07-03 06:48:23 +08:00
|
|
|
#include <finsh.h>
|
|
|
|
|
|
|
|
#include "finsh_var.h"
|
|
|
|
|
2012-09-20 13:07:14 +08:00
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
2017-10-10 14:27:34 +08:00
|
|
|
uint8_t finsh_heap[FINSH_HEAP_MAX];
|
2009-07-03 06:48:23 +08:00
|
|
|
struct finsh_block_header
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
uint32_t length;
|
|
|
|
struct finsh_block_header* next;
|
2009-07-03 06:48:23 +08:00
|
|
|
};
|
|
|
|
#define BLOCK_HEADER(x) (struct finsh_block_header*)(x)
|
2017-10-10 14:27:34 +08:00
|
|
|
#define finsh_block_get_header(data) (struct finsh_block_header*)((uint8_t*)data - sizeof(struct finsh_block_header))
|
|
|
|
#define finsh_block_get_data(header) (uint8_t*)((struct finsh_block_header*)header + 1)
|
2018-03-01 21:01:20 +08:00
|
|
|
#define HEAP_ALIGN_SIZE(size) (((size) + HEAP_ALIGNMENT - 1) & ~(HEAP_ALIGNMENT-1))
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
static struct finsh_block_header* free_list;
|
|
|
|
static struct finsh_block_header* allocate_list;
|
|
|
|
|
|
|
|
static void finsh_heap_gc(void);
|
|
|
|
|
|
|
|
static void finsh_block_insert(struct finsh_block_header** list, struct finsh_block_header* header);
|
|
|
|
static void finsh_block_remove(struct finsh_block_header** list, struct finsh_block_header* header);
|
|
|
|
static void finsh_block_split(struct finsh_block_header* header, size_t size);
|
|
|
|
static void finsh_block_merge(struct finsh_block_header** list, struct finsh_block_header* header);
|
|
|
|
|
2010-04-14 08:40:20 +08:00
|
|
|
int finsh_heap_init(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
/* clear heap to zero */
|
|
|
|
memset(&finsh_heap[0], 0, sizeof(finsh_heap));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* init free and alloc list */
|
2009-07-03 06:48:23 +08:00
|
|
|
free_list = BLOCK_HEADER(&finsh_heap[0]);
|
2018-03-01 21:01:20 +08:00
|
|
|
free_list->length = FINSH_HEAP_MAX - sizeof(struct finsh_block_header);
|
2009-07-03 06:48:23 +08:00
|
|
|
free_list->next = NULL;
|
|
|
|
|
|
|
|
allocate_list = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* allocate a block from heap
|
|
|
|
*/
|
|
|
|
void* finsh_heap_allocate(size_t size)
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
struct finsh_block_header* header;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
size = HEAP_ALIGN_SIZE(size);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* find the first fit block */
|
|
|
|
for (header = free_list;
|
|
|
|
((header != NULL) && (header->length <= size + sizeof(struct finsh_block_header)));
|
|
|
|
header = header->next) ;
|
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
if (header == NULL)
|
|
|
|
{
|
|
|
|
finsh_heap_gc();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* find the first fit block */
|
|
|
|
for (header = free_list;
|
|
|
|
((header != NULL) && (header->length < size + sizeof(struct finsh_block_header)));
|
|
|
|
header = header->next) ;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* there is no memory */
|
|
|
|
if (header == NULL) return NULL;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* split block */
|
2018-03-01 21:01:20 +08:00
|
|
|
finsh_block_split(header, size);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* remove from free list */
|
|
|
|
finsh_block_remove(&free_list, header);
|
|
|
|
header->next = NULL;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* insert to allocate list */
|
|
|
|
finsh_block_insert(&allocate_list, header);
|
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
memset(finsh_block_get_data(header), 0, size);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
return finsh_block_get_data(header);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* release the allocated block
|
|
|
|
*/
|
|
|
|
void finsh_heap_free(void*ptr)
|
|
|
|
{
|
|
|
|
struct finsh_block_header* header;
|
|
|
|
|
|
|
|
/* get block header */
|
2018-03-01 21:01:20 +08:00
|
|
|
header = finsh_block_get_header(ptr);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* remove from allocate list */
|
2018-03-01 21:01:20 +08:00
|
|
|
finsh_block_remove(&allocate_list, header);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* insert to free list */
|
|
|
|
finsh_block_insert(&free_list, header);
|
|
|
|
finsh_block_merge(&free_list, header);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* garbage collector
|
|
|
|
*/
|
|
|
|
static void finsh_heap_gc(void)
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
int i;
|
|
|
|
struct finsh_block_header *header, *temp;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
temp = NULL;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* find the first fit block */
|
|
|
|
for (header = allocate_list; header != NULL; )
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
|
|
|
|
{
|
|
|
|
if (global_variable[i].type != finsh_type_unknown)
|
|
|
|
{
|
|
|
|
if (global_variable[i].value.ptr == finsh_block_get_data(header))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = header;
|
|
|
|
header = header->next;
|
|
|
|
|
|
|
|
/* this block is an unused block, release it */
|
|
|
|
if (i == FINSH_VARIABLE_MAX)
|
|
|
|
{
|
|
|
|
finsh_heap_free(finsh_block_get_data(temp));
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* insert a block to list
|
|
|
|
*/
|
|
|
|
void finsh_block_insert(struct finsh_block_header** list, struct finsh_block_header* header)
|
|
|
|
{
|
|
|
|
struct finsh_block_header* node;
|
|
|
|
|
|
|
|
if (*list == NULL)
|
|
|
|
{
|
|
|
|
*list = header;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find out insert point */
|
2018-03-01 21:01:20 +08:00
|
|
|
node = *list;
|
|
|
|
|
|
|
|
if (node > header)
|
|
|
|
{
|
|
|
|
/* insert node in the header of list */
|
|
|
|
header->next = node;
|
|
|
|
*list = header;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (node = *list; node; node = node->next)
|
|
|
|
{
|
|
|
|
if (node->next > header) break;
|
|
|
|
|
|
|
|
if (node->next == NULL) break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* insert node */
|
|
|
|
if (node->next != NULL) header->next = node->next;
|
|
|
|
node->next = header;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove block from list
|
|
|
|
*/
|
|
|
|
void finsh_block_remove(struct finsh_block_header** list, struct finsh_block_header* header)
|
|
|
|
{
|
|
|
|
struct finsh_block_header* node;
|
|
|
|
|
|
|
|
node = *list;
|
|
|
|
if (node == header)
|
|
|
|
{
|
|
|
|
/* remove list header */
|
|
|
|
*list = header->next;
|
|
|
|
header->next = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (node = *list; node != NULL; node = node->next)
|
|
|
|
{
|
|
|
|
if (node->next == header)
|
|
|
|
{
|
|
|
|
node->next = header->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* split block
|
|
|
|
*/
|
|
|
|
void finsh_block_split(struct finsh_block_header* header, size_t size)
|
|
|
|
{
|
|
|
|
struct finsh_block_header* next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* split header into two node:
|
|
|
|
* header->next->...
|
|
|
|
*/
|
2017-10-10 14:27:34 +08:00
|
|
|
next = BLOCK_HEADER((uint8_t*)header + sizeof(struct finsh_block_header) + size);
|
2009-07-03 06:48:23 +08:00
|
|
|
next->length = header->length - sizeof(struct finsh_block_header) - size;
|
|
|
|
header->length = size;
|
|
|
|
next->next = header->next;
|
|
|
|
|
|
|
|
header->next = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void finsh_block_merge(struct finsh_block_header** list, struct finsh_block_header* header)
|
|
|
|
{
|
|
|
|
struct finsh_block_header* prev_node;
|
|
|
|
struct finsh_block_header* next_node;
|
|
|
|
|
|
|
|
next_node = header->next;
|
|
|
|
|
|
|
|
if (*list == header) prev_node = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* find out the previous header */
|
|
|
|
for (prev_node = *list; prev_node; prev_node =prev_node->next)
|
|
|
|
{
|
|
|
|
if (prev_node->next == header)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try merge node */
|
|
|
|
|
|
|
|
/* merge to previous node */
|
|
|
|
if (prev_node != NULL &&
|
2017-10-10 14:27:34 +08:00
|
|
|
((uint8_t*)prev_node + prev_node->length + sizeof(struct finsh_block_header)
|
|
|
|
== (uint8_t*)header))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
/* is it close to next node? */
|
|
|
|
if ((next_node != NULL) &&
|
2017-10-10 14:27:34 +08:00
|
|
|
((uint8_t*)header + header->length + sizeof(struct finsh_block_header)
|
|
|
|
== (uint8_t*)next_node))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
/* merge three node */
|
2018-03-01 21:01:20 +08:00
|
|
|
prev_node->length += header->length + next_node->length +
|
2009-07-03 06:48:23 +08:00
|
|
|
2 * sizeof(struct finsh_block_header);
|
|
|
|
|
|
|
|
prev_node->next = next_node->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_node->length += header->length + sizeof(struct finsh_block_header);
|
|
|
|
prev_node->next = header->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* merge to last node */
|
|
|
|
if ( (next_node != NULL) &&
|
2017-10-10 14:27:34 +08:00
|
|
|
((uint8_t*)header + header->length + sizeof(struct finsh_block_header)
|
|
|
|
== (uint8_t*)next_node))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
header->length += next_node->length + sizeof(struct finsh_block_header);
|
|
|
|
header->next = next_node->next;
|
|
|
|
}
|
|
|
|
}
|