<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-cn">
		<id>http://wiki.ywrobot.net/index.php?action=history&amp;feed=atom&amp;title=Realloc.c</id>
		<title>Realloc.c - 版本历史</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.ywrobot.net/index.php?action=history&amp;feed=atom&amp;title=Realloc.c"/>
		<link rel="alternate" type="text/html" href="http://wiki.ywrobot.net/index.php?title=Realloc.c&amp;action=history"/>
		<updated>2026-05-14T11:01:15Z</updated>
		<subtitle>本wiki的该页面的版本历史</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://wiki.ywrobot.net/index.php?title=Realloc.c&amp;diff=51&amp;oldid=prev</id>
		<title>YWrobot WM：创建页面，内容为“&lt;pre style=&quot;color:blue&quot;&gt; /* Copyright (c) 2004, 2010 Joerg Wunsch    All rights reserved.     Redistribution and use in source and binary forms, with or without    m...”</title>
		<link rel="alternate" type="text/html" href="http://wiki.ywrobot.net/index.php?title=Realloc.c&amp;diff=51&amp;oldid=prev"/>
				<updated>2016-04-25T01:57:43Z</updated>
		
		<summary type="html">&lt;p&gt;创建页面，内容为“&amp;lt;pre style=&amp;quot;color:blue&amp;quot;&amp;gt; /* Copyright (c) 2004, 2010 Joerg Wunsch    All rights reserved.     Redistribution and use in source and binary forms, with or without    m...”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre style=&amp;quot;color:blue&amp;quot;&amp;gt;&lt;br /&gt;
/* Copyright (c) 2004, 2010 Joerg Wunsch&lt;br /&gt;
   All rights reserved.&lt;br /&gt;
&lt;br /&gt;
   Redistribution and use in source and binary forms, with or without&lt;br /&gt;
   modification, are permitted provided that the following conditions are met:&lt;br /&gt;
&lt;br /&gt;
   * Redistributions of source code must retain the above copyright&lt;br /&gt;
     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
&lt;br /&gt;
   * Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
     notice, this list of conditions and the following disclaimer in&lt;br /&gt;
     the documentation and/or other materials provided with the&lt;br /&gt;
     distribution.&lt;br /&gt;
&lt;br /&gt;
   * Neither the name of the copyright holders nor the names of&lt;br /&gt;
     contributors may be used to endorse or promote products derived&lt;br /&gt;
     from this software without specific prior written permission.&lt;br /&gt;
&lt;br /&gt;
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS IS&amp;quot;&lt;br /&gt;
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE&lt;br /&gt;
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE&lt;br /&gt;
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE&lt;br /&gt;
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR&lt;br /&gt;
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF&lt;br /&gt;
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS&lt;br /&gt;
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN&lt;br /&gt;
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)&lt;br /&gt;
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE&lt;br /&gt;
  POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
*/&lt;br /&gt;
/* $Id: realloc.c 2127 2010-06-07 14:49:37Z joerg_wunsch $ */&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;quot;sectionname.h&amp;quot;&lt;br /&gt;
#include &amp;quot;stdlib_private.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;avr/io.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ATTRIBUTE_CLIB_SECTION&lt;br /&gt;
void *&lt;br /&gt;
realloc(void *ptr, size_t len)&lt;br /&gt;
{&lt;br /&gt;
	struct __freelist *fp1, *fp2, *fp3, *ofp3;&lt;br /&gt;
	char *cp, *cp1;&lt;br /&gt;
	void *memp;&lt;br /&gt;
	size_t s, incr;&lt;br /&gt;
&lt;br /&gt;
	/* Trivial case, required by C standard. */&lt;br /&gt;
	if (ptr == 0)&lt;br /&gt;
		return malloc(len);&lt;br /&gt;
&lt;br /&gt;
	cp1 = (char *)ptr;&lt;br /&gt;
	cp1 -= sizeof(size_t);&lt;br /&gt;
	fp1 = (struct __freelist *)cp1;&lt;br /&gt;
&lt;br /&gt;
	cp = (char *)ptr + len; /* new next pointer */&lt;br /&gt;
	if (cp &amp;lt; cp1)&lt;br /&gt;
		/* Pointer wrapped across top of RAM, fail. */&lt;br /&gt;
		return 0;&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * See whether we are growing or shrinking.  When shrinking,&lt;br /&gt;
	 * we split off a chunk for the released portion, and call&lt;br /&gt;
	 * free() on it.  Therefore, we can only shrink if the new&lt;br /&gt;
	 * size is at least sizeof(struct __freelist) smaller than the&lt;br /&gt;
	 * previous size.&lt;br /&gt;
	 */&lt;br /&gt;
	if (len &amp;lt;= fp1-&amp;gt;sz) {&lt;br /&gt;
		/* The first test catches a possible unsigned int&lt;br /&gt;
		 * rollover condition. */&lt;br /&gt;
		if (fp1-&amp;gt;sz &amp;lt;= sizeof(struct __freelist) ||&lt;br /&gt;
		    len &amp;gt; fp1-&amp;gt;sz - sizeof(struct __freelist))&lt;br /&gt;
			return ptr;&lt;br /&gt;
		fp2 = (struct __freelist *)cp;&lt;br /&gt;
		fp2-&amp;gt;sz = fp1-&amp;gt;sz - len - sizeof(size_t);&lt;br /&gt;
		fp1-&amp;gt;sz = len;&lt;br /&gt;
		free(&amp;amp;(fp2-&amp;gt;nx));&lt;br /&gt;
		return ptr;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * If we get here, we are growing.  First, see whether there&lt;br /&gt;
	 * is space in the free list on top of our current chunk.&lt;br /&gt;
	 */&lt;br /&gt;
	incr = len - fp1-&amp;gt;sz;&lt;br /&gt;
	cp = (char *)ptr + fp1-&amp;gt;sz;&lt;br /&gt;
	fp2 = (struct __freelist *)cp;&lt;br /&gt;
	for (s = 0, ofp3 = 0, fp3 = __flp;&lt;br /&gt;
	     fp3;&lt;br /&gt;
	     ofp3 = fp3, fp3 = fp3-&amp;gt;nx) {&lt;br /&gt;
		if (fp3 == fp2 &amp;amp;&amp;amp; fp3-&amp;gt;sz + sizeof(size_t) &amp;gt;= incr) {&lt;br /&gt;
			/* found something that fits */&lt;br /&gt;
			if (fp3-&amp;gt;sz + sizeof(size_t) - incr &amp;gt; sizeof(struct __freelist)) {&lt;br /&gt;
				/* split off a new freelist entry */&lt;br /&gt;
				cp = (char *)ptr + len;&lt;br /&gt;
				fp2 = (struct __freelist *)cp;&lt;br /&gt;
				fp2-&amp;gt;nx = fp3-&amp;gt;nx;&lt;br /&gt;
				fp2-&amp;gt;sz = fp3-&amp;gt;sz - incr;&lt;br /&gt;
				fp1-&amp;gt;sz = len;&lt;br /&gt;
			} else {&lt;br /&gt;
				/* it just fits, so use it entirely */&lt;br /&gt;
				fp1-&amp;gt;sz += fp3-&amp;gt;sz + sizeof(size_t);&lt;br /&gt;
				fp2 = fp3-&amp;gt;nx;&lt;br /&gt;
			}&lt;br /&gt;
			if (ofp3)&lt;br /&gt;
				ofp3-&amp;gt;nx = fp2;&lt;br /&gt;
			else&lt;br /&gt;
				__flp = fp2;&lt;br /&gt;
			return ptr;&lt;br /&gt;
		}&lt;br /&gt;
		/*&lt;br /&gt;
		 * Find the largest chunk on the freelist while&lt;br /&gt;
		 * walking it.&lt;br /&gt;
		 */&lt;br /&gt;
		if (fp3-&amp;gt;sz &amp;gt; s)&lt;br /&gt;
			s = fp3-&amp;gt;sz;&lt;br /&gt;
	}&lt;br /&gt;
	/*&lt;br /&gt;
	 * If we are the topmost chunk in memory, and there was no&lt;br /&gt;
	 * large enough chunk on the freelist that could be re-used&lt;br /&gt;
	 * (by a call to malloc() below), quickly extend the&lt;br /&gt;
	 * allocation area if possible, without need to copy the old&lt;br /&gt;
	 * data.&lt;br /&gt;
	 */&lt;br /&gt;
	if (__brkval == (char *)ptr + fp1-&amp;gt;sz &amp;amp;&amp;amp; len &amp;gt; s) {&lt;br /&gt;
		cp1 = __malloc_heap_end;&lt;br /&gt;
		cp = (char *)ptr + len;&lt;br /&gt;
		if (cp1 == 0)&lt;br /&gt;
			cp1 = STACK_POINTER() - __malloc_margin;&lt;br /&gt;
		if (cp &amp;lt; cp1) {&lt;br /&gt;
			__brkval = cp;&lt;br /&gt;
			fp1-&amp;gt;sz = len;&lt;br /&gt;
			return ptr;&lt;br /&gt;
		}&lt;br /&gt;
		/* If that failed, we are out of luck. */&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * Call malloc() for a new chunk, then copy over the data, and&lt;br /&gt;
	 * release the old region.&lt;br /&gt;
	 */&lt;br /&gt;
	if ((memp = malloc(len)) == 0)&lt;br /&gt;
		return 0;&lt;br /&gt;
	memcpy(memp, ptr, fp1-&amp;gt;sz);&lt;br /&gt;
	free(ptr);&lt;br /&gt;
	return memp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>YWrobot WM</name></author>	</entry>

	</feed>