mem.c 13.7 KB
Newer Older
1
/*
B
bernard.xiong 已提交
2
 * File      : mem.c
3
 * This file is part of RT-Thread RTOS
B
bernard.xiong 已提交
4
 * COPYRIGHT (C) 2008 - 2009, RT-Thread Development Team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2008-7-12     Bernard      the first version
 */

/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Adam Dunkels <adam@sics.se>
 *         Simon Goldschmidt
 *
 */

#include <rtthread.h>

/* #define RT_MEM_DEBUG */
51
#define RT_MEM_STATS
52 53 54 55 56 57 58 59 60 61 62 63

#if defined (RT_USING_HEAP) && defined (RT_USING_SMALL_MEM)
#ifdef RT_USING_HOOK
static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
static void (*rt_free_hook)(void *ptr);

/**
 * @addtogroup Hook
 */
/*@{*/

/**
B
bernard.xiong 已提交
64
 * This function will set a hook function, which will be invoked when a memory
65
 * block is allocated from heap memory.
B
bernard.xiong 已提交
66
 *
67 68 69 70 71 72 73 74
 * @param hook the hook function
 */
void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
{
	rt_malloc_hook = hook;
}

/**
B
bernard.xiong 已提交
75
 * This function will set a hook function, which will be invoked when a memory
76
 * block is released to heap memory.
B
bernard.xiong 已提交
77
 *
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 * @param hook the hook function
 */
void rt_free_sethook(void (*hook)(void *ptr))
{
	rt_free_hook = hook;
}

/*@}*/

#endif

#define HEAP_MAGIC 0x1ea0
struct heap_mem
{
	/* magic and used flag */
	rt_uint16_t magic;
	rt_uint16_t used;

	rt_size_t next, prev;
};

/** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
static rt_uint8_t *heap_ptr;

/** the last entry, always unused! */
static struct heap_mem *heap_end;

#define MIN_SIZE 12
#define MIN_SIZE_ALIGNED     RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE)
#define SIZEOF_STRUCT_MEM    RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE)

static struct heap_mem *lfree;   /* pointer to the lowest free block */

static struct rt_semaphore heap_sem;
B
bernard.xiong 已提交
112 113
static rt_size_t mem_size_aligned;

114
#ifdef RT_MEM_STATS
B
bernard.xiong 已提交
115 116
static rt_size_t used_mem, max_mem;
#endif
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

static void plug_holes(struct heap_mem *mem)
{
	struct heap_mem *nmem;
	struct heap_mem *pmem;

	RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);
	RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
	RT_ASSERT(mem->used == 0);

	/* plug hole forward */
	nmem = (struct heap_mem *)&heap_ptr[mem->next];
	if (mem != nmem && nmem->used == 0 && (rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
	{
		/* if mem->next is unused and not end of heap_ptr, combine mem and mem->next */
		if (lfree == nmem)
		{
			lfree = mem;
		}
		mem->next = nmem->next;
		((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
	}

	/* plug hole backward */
	pmem = (struct heap_mem *)&heap_ptr[mem->prev];
	if (pmem != mem && pmem->used == 0)
	{
		/* if mem->prev is unused, combine mem and mem->prev */
		if (lfree == mem)
		{
			lfree = pmem;
		}
		pmem->next = mem->next;
		((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
	}
}

/**
 * @ingroup SystemInit
 *
 * This function will init system heap
 *
 * @param begin_addr the beginning address of system page
 * @param end_addr the end address of system page
 *
 */
void rt_system_heap_init(void* begin_addr, void* end_addr)
{
	struct heap_mem *mem;

	/* alignment addr */
	begin_addr = (void*)RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);

	/* calculate the aligned memory size */
	mem_size_aligned = RT_ALIGN((rt_uint32_t)end_addr - (rt_uint32_t)begin_addr, RT_ALIGN_SIZE) - 2 * sizeof(struct heap_mem);

	/* point to begin address of heap */
	heap_ptr = begin_addr;

#ifdef RT_MEM_DEBUG
	rt_kprintf("mem init, heap begin address 0x%x, size %d\n", (rt_uint32_t)heap_ptr, mem_size_aligned);
#endif

	/* initialize the start of the heap */
	mem = (struct heap_mem *)heap_ptr;
	mem->magic= HEAP_MAGIC;
	mem->next = mem_size_aligned;
	mem->prev = 0;
	mem->used = 0;

	/* initialize the end of the heap */
	heap_end = (struct heap_mem *)&heap_ptr[mem_size_aligned];
	heap_end->magic= HEAP_MAGIC;
	heap_end->used = 1;
	heap_end->next = mem_size_aligned;
	heap_end->prev = mem_size_aligned;

	rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);

	/* initialize the lowest-free pointer to the start of the heap */
B
bernard.xiong 已提交
197
	lfree = (struct heap_mem *)heap_ptr;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

/**
 * @addtogroup MM
 */

/*@{*/

/**
 * Allocate a block of memory with a minimum of 'size' bytes.
 *
 * @param size is the minimum size of the requested block in bytes.
 *
 * @return pointer to allocated memory or NULL if no free memory was found.
 */
void *rt_malloc(rt_size_t size)
{
	rt_size_t ptr, ptr2;
	struct heap_mem *mem, *mem2;

	if (size == 0) return RT_NULL;

#ifdef RT_MEM_DEBUG
	rt_kprintf("malloc size %d, but align to %d\n", size, RT_ALIGN(size, RT_ALIGN_SIZE));
#endif

	/* alignment size */
	size = RT_ALIGN(size, RT_ALIGN_SIZE);

	if (size > mem_size_aligned)
	{
#ifdef RT_MEM_DEBUG
		rt_kprintf("no memory\n");
#endif
		return RT_NULL;
	}

	/* every data block must be at least MIN_SIZE_ALIGNED long */
	if(size < MIN_SIZE_ALIGNED) size = MIN_SIZE_ALIGNED;

	/* take memory semaphore */
	rt_sem_take(&heap_sem, RT_WAITING_FOREVER);

B
bernard.xiong 已提交
241
	for (ptr = (rt_uint8_t *)lfree - heap_ptr; ptr < mem_size_aligned - size;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
	{
		mem = (struct heap_mem *)&heap_ptr[ptr];

		if ((!mem->used) &&
				(mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
		{
			/* mem is not used and at least perfect fit is possible:
			 * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */

			if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
			{
				/* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
				 * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
				 * -> split large block, create empty remainder,
				 * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
				 * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
				 * struct heap_mem would fit in but no data between mem2 and mem2->next
				 * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
				 *       region that couldn't hold data, but when mem->next gets freed,
				 *       the 2 regions would be combined, resulting in more free memory
				 */
				ptr2 = ptr + SIZEOF_STRUCT_MEM + size;

				/* create mem2 struct */
				mem2 = (struct heap_mem *)&heap_ptr[ptr2];
				mem2->magic = HEAP_MAGIC;
				mem2->used = 0;
				mem2->next = mem->next;
				mem2->prev = ptr;

				/* and insert it between mem and mem->next */
				mem->next = ptr2;
				mem->used = 1;

				if (mem2->next != mem_size_aligned)
				{
					((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
B
bernard.xiong 已提交
280 281 282 283
				}
#ifdef RT_MEM_STATS
				used_mem += (size + SIZEOF_STRUCT_MEM);
				if (max_mem < used_mem) max_mem = used_mem;
284 285 286 287 288 289 290 291 292 293 294
#endif
			}
			else
			{
				/* (a mem2 struct does no fit into the user data space of mem and mem->next will always
				 * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
				 * take care of this).
				 * -> near fit or excact fit: do not split, no mem2 creation
				 * also can't move mem->next directly behind mem, since mem->next
				 * will always be used at this point!
				 */
B
bernard.xiong 已提交
295 296 297 298
				mem->used = 1;
#ifdef RT_MEM_STATS
				used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
				if (max_mem < used_mem) max_mem = used_mem;
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
#endif
			}

			if (mem == lfree)
			{
				/* Find next free block after mem and update lowest free pointer */
				while (lfree->used && lfree != heap_end) lfree = (struct heap_mem *)&heap_ptr[lfree->next];

				RT_ASSERT(((lfree == heap_end) || (!lfree->used)));
			}

			rt_sem_release(&heap_sem);
			RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
 			RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
			RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);

#ifdef RT_MEM_DEBUG
			rt_kprintf("allocate memory at 0x%x\n", (rt_uint32_t)((rt_uint8_t*)mem + SIZEOF_STRUCT_MEM));
#endif

#ifdef RT_USING_HOOK
			if (rt_malloc_hook != RT_NULL)
321
				rt_malloc_hook((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM, size);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#endif
			/* return the memory data except mem struct */
			return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
		}
	}

	rt_sem_release(&heap_sem);
	return RT_NULL;
}

/**
 * This function will change the previously allocated memory block.
 *
 * @param rmem pointer to memory allocated by rt_malloc
 * @param newsize the required new size
B
bernard.xiong 已提交
337
 *
338 339 340 341 342 343 344
 * @return the changed memory block address
 */
void *rt_realloc(void *rmem, rt_size_t newsize)
{
	rt_size_t size;
	rt_size_t ptr, ptr2;
	struct heap_mem *mem, *mem2;
B
bernard.xiong 已提交
345
	void* nmem;
346 347 348 349 350 351 352 353 354 355 356 357 358

	/* alignment size */
	newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
	if (newsize > mem_size_aligned)
	{
#ifdef RT_MEM_DEBUG
		rt_kprintf("no memory\n");
#endif
		return RT_NULL;
	}

	rt_sem_take(&heap_sem, RT_WAITING_FOREVER);

B
bernard.xiong 已提交
359
	if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
360 361 362 363 364 365 366 367 368 369 370 371 372
		(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
	{
		/* illegal memory */
		rt_sem_release(&heap_sem);
		return rmem;
	}

	mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);

	ptr = (rt_uint8_t *)mem - heap_ptr;
	size = mem->next - ptr - SIZEOF_STRUCT_MEM;

	if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
B
bernard.xiong 已提交
373
	{
374
#ifdef RT_MEM_STATS
B
bernard.xiong 已提交
375
  		used_mem -= (size - newsize);
376
#endif
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

		ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
		mem2 = (struct heap_mem *)&heap_ptr[ptr2];
		mem2->magic= HEAP_MAGIC;
		mem2->used = 0;
		mem2->next = mem->next;
		mem2->prev = ptr;
		mem->next = ptr2;
		if (mem2->next != mem_size_aligned)
		{
			((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
		}

		plug_holes(mem2);

		rt_sem_release(&heap_sem);
		return rmem;
	}
	rt_sem_release(&heap_sem);

	/* expand memory */
B
bernard.xiong 已提交
398
	nmem = rt_malloc(newsize);
399
	rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
400

B
bernard.xiong 已提交
401
	rt_free(rmem);
402

B
bernard.xiong 已提交
403
	return nmem;
404 405 406
}

/**
B
bernard.xiong 已提交
407 408
 * This function will contiguously allocate enough space for count objects
 * that are size bytes of memory each and returns a pointer to the allocated
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
 * memory.
 *
 * The allocated memory is filled with bytes of value zero.
 *
 * @param count number of objects to allocate
 * @param size size of the objects to allocate
 *
 * @return pointer to allocated memory / NULL pointer if there is an error
 */
void *rt_calloc(rt_size_t count, rt_size_t size)
{
	void *p;

	/* allocate 'count' objects of size 'size' */
	p = rt_malloc(count * size);

	/* zero the memory */
	if (p) rt_memset(p, 0, count * size);

	return p;
}

/**
B
bernard.xiong 已提交
432
 * This function will release the previously allocated memory block by rt_malloc.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
 * The released memory block is taken back to system heap.
 *
 * @param rmem the address of memory which will be released
 */
void rt_free(void *rmem)
{
	struct heap_mem *mem;

	if (rmem == RT_NULL) return;
	RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0);

#ifdef RT_USING_HOOK
	if (rt_free_hook != RT_NULL) rt_free_hook(rmem);
#endif

#ifdef RT_MEM_DEBUG
	rt_kprintf("release memory 0x%x\n", (rt_uint32_t)rmem);
#endif

	/* protect the heap from concurrent access */
	rt_sem_take(&heap_sem, RT_WAITING_FOREVER);

	RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr &&
			  (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);

	if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
	{
#ifdef RT_MEM_DEBUG
		rt_kprintf("illegal memory\n");
#endif
		/* illegal memory */
		rt_sem_release(&heap_sem);
		return;
	}

	/* Get the corresponding struct heap_mem ... */
	mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
	/* ... which has to be in a used state ... */
	RT_ASSERT(mem->used);
	/* ... and is now unused. */
	mem->used = 0;

	if (mem < lfree)
	{
		/* the newly freed struct is now the lowest */
		lfree = mem;
B
bernard.xiong 已提交
479
	}
480

B
bernard.xiong 已提交
481 482 483
#ifdef RT_MEM_STATS
	used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));
#endif
484 485 486 487

	/* finally, see if prev or next are free also */
	plug_holes(mem);
	rt_sem_release(&heap_sem);
B
bernard.xiong 已提交
488
}
489

490
#ifdef RT_MEM_STATS
B
bernard.xiong 已提交
491
void rt_memory_info(rt_uint32_t *total,
492 493 494 495 496 497 498
	rt_uint32_t *used,
	rt_uint32_t *max_used)
{
	if (total != RT_NULL) *total = mem_size_aligned;
	if (used  != RT_NULL) *used = used_mem;
	if (max_used != RT_NULL) *max_used = max_mem;
}
B
bernard.xiong 已提交
499

500 501 502 503 504 505 506 507 508 509
#ifdef RT_USING_FINSH
#include <finsh.h>
void list_mem()
{
	rt_kprintf("total memory: %d\n", mem_size_aligned);
	rt_kprintf("used memory : %d\n", used_mem);
	rt_kprintf("maximum allocated memory: %d\n", max_mem);
}
FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
#endif
B
bernard.xiong 已提交
510
#endif
511 512 513 514

/*@}*/

#endif