1 /*
2  * Copyright (c) 2012, 2013 ARM Ltd
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the company may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "nano-malloc.h"
30 
31 /*
32  * Implement either by merging adjacent free memory
33  * or by calling malloc/memcpy
34  */
35 void *
realloc(void * ptr,size_t size)36 realloc(void * ptr, size_t size)
37 {
38     void * mem;
39 
40     if (ptr == NULL)
41 	return malloc(size);
42 
43     if (size == 0)
44     {
45         free(ptr);
46         return NULL;
47     }
48 
49     if (size > MALLOC_MAXSIZE)
50     {
51         errno = ENOMEM;
52         return NULL;
53     }
54 
55     size_t new_size = chunk_size(size);
56     chunk_t *p_to_realloc = ptr_to_chunk(ptr);
57 
58 #if MALLOC_DEBUG
59     __malloc_validate_block(p_to_realloc);
60 #endif
61 
62     size_t old_size = _size(p_to_realloc);
63 
64     /* See if we can avoid allocating new memory
65      * when increasing the size
66      */
67     if (new_size > old_size)
68     {
69 	void *chunk_e = chunk_end(p_to_realloc);
70 
71 	MALLOC_LOCK;
72 
73 	if (__malloc_grow_chunk(p_to_realloc, new_size))
74 	{
75 	    /* clear new memory */
76 	    memset(chunk_e, '\0', new_size - old_size);
77 	    /* adjust chunk_t size */
78 	    old_size = new_size;
79 	}
80 	else
81 	{
82 	    chunk_t **p, *r;
83 
84 	    /* Check to see if there's a chunk_t of free space just past
85 	     * the current block, merge it in in case that's useful
86 	     */
87 	    for (p = &__malloc_free_list; (r = *p) != NULL; p = &r->next)
88 	    {
89 		if (r == chunk_e)
90 		{
91 		    size_t r_size = _size(r);
92 
93 		    /* remove R from the free list */
94 		    *p = r->next;
95 
96 		    /* clear the memory from r */
97 		    memset(r, '\0', r_size);
98 
99 		    /* add it's size to our block */
100 		    old_size += r_size;
101 		    _set_size(p_to_realloc, old_size);
102 		    break;
103 		}
104 		if (p_to_realloc < r)
105 		    break;
106 	    }
107 	}
108 
109 	MALLOC_UNLOCK;
110     }
111 
112     if (new_size <= old_size)
113     {
114 	size_t extra = old_size - new_size;
115 
116 	/* If there's enough space left over, split it out
117 	 * and free it
118 	 */
119 	if (extra >= MALLOC_MINSIZE) {
120 	    _set_size(p_to_realloc, new_size);
121 	    make_free_chunk(chunk_after(p_to_realloc), extra);
122 	}
123 	return ptr;
124     }
125 
126     /* No short cuts, allocate new memory and copy */
127 
128     mem = malloc(size);
129     if (!mem)
130         return NULL;
131 
132     memcpy(mem, ptr, old_size - MALLOC_HEAD);
133     free(ptr);
134 
135     return mem;
136 }
137