|
| 1 | +/************************************************************ |
| 2 | + * mm/mm_realloc.c |
| 3 | + * |
| 4 | + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. |
| 5 | + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in |
| 15 | + * the documentation and/or other materials provided with the |
| 16 | + * distribution. |
| 17 | + * 3. Neither the name NuttX nor the names of its contributors may be |
| 18 | + * used to endorse or promote products derived from this software |
| 19 | + * without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 28 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 29 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + * POSSIBILITY OF SUCH DAMAGE. |
| 33 | + * |
| 34 | + ************************************************************/ |
| 35 | + |
| 36 | +#include "mm_environment.h" |
| 37 | +#include "mm_internal.h" |
| 38 | + |
| 39 | +/************************************************************ |
| 40 | + * realloc |
| 41 | + * |
| 42 | + * Description: |
| 43 | + * If the reallocation is for less space, then: |
| 44 | + * (1) the current allocation is reduced in size |
| 45 | + * (2) the remainder at the end of the allocation is |
| 46 | + * returned to the free list. |
| 47 | + * |
| 48 | + * If the request is for more space and the current |
| 49 | + * allocation can be extended, it will be extended by: |
| 50 | + * (1) Taking the additional space from the following |
| 51 | + * free chunk, or |
| 52 | + * (2) Taking the additional space from the preceding |
| 53 | + * free chunk. |
| 54 | + * (3) Or both |
| 55 | + * |
| 56 | + * If the request is for more space but the current chunk |
| 57 | + * cannot be extended, then malloc a new buffer, copy the |
| 58 | + * data into the new buffer, and free the old buffer. |
| 59 | + * |
| 60 | + ************************************************************/ |
| 61 | + |
| 62 | +void *realloc(void *oldmem, size_t size) |
| 63 | +{ |
| 64 | + struct mm_allocnode_s *oldnode; |
| 65 | + struct mm_freenode_s *prev; |
| 66 | + struct mm_freenode_s *next; |
| 67 | + size_t oldsize; |
| 68 | + size_t prevsize = 0; |
| 69 | + size_t nextsize = 0; |
| 70 | + void *newmem; |
| 71 | + |
| 72 | + /* If oldmem is NULL, then realloc is equivalent to malloc */ |
| 73 | + |
| 74 | + if (!oldmem) |
| 75 | + { |
| 76 | + return malloc(size); |
| 77 | + } |
| 78 | + |
| 79 | + /* If size is zero, then realloc is equivalent to free */ |
| 80 | + |
| 81 | + if (size <= 0) |
| 82 | + { |
| 83 | + free(oldmem); |
| 84 | + return NULL; |
| 85 | + } |
| 86 | + |
| 87 | + /* Adjust the size to account for (1) the size of the allocated |
| 88 | + * node and (2) to make sure that it is an even multiple of |
| 89 | + * our granule size. |
| 90 | + */ |
| 91 | + |
| 92 | + size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE); |
| 93 | + |
| 94 | + /* Map the memory chunk into an allocated node structure */ |
| 95 | + |
| 96 | + oldnode = (struct mm_allocnode_s *)((char*)oldmem - SIZEOF_MM_ALLOCNODE); |
| 97 | + |
| 98 | + /* Check if this is a request to reduce the size of the allocation. */ |
| 99 | + |
| 100 | + oldsize = oldnode->size; |
| 101 | + if (size <= oldsize) |
| 102 | + { |
| 103 | + /* Handle the special case where we are not going to change the |
| 104 | + * size of the allocation. |
| 105 | + */ |
| 106 | + if (size < oldsize) |
| 107 | + { |
| 108 | + mm_shrinkchunk(oldnode, size); |
| 109 | + } |
| 110 | + |
| 111 | + /* Then return the original address */ |
| 112 | + return oldmem; |
| 113 | + } |
| 114 | + |
| 115 | + /* This is a request to increase the size of the allocation, Get the |
| 116 | + * available sizes before and after the oldnode so that we can make |
| 117 | + * the best decision |
| 118 | + */ |
| 119 | + |
| 120 | + next = (struct mm_freenode_s *)((char*)oldnode + oldnode->size); |
| 121 | + if ((next->preceding & MM_ALLOC_BIT) == 0) |
| 122 | + { |
| 123 | + nextsize = next->size; |
| 124 | + } |
| 125 | + |
| 126 | + prev = (struct mm_freenode_s *)((char*)oldnode - (oldnode->preceding & ~MM_ALLOC_BIT)); |
| 127 | + if ((prev->preceding & MM_ALLOC_BIT) == 0) |
| 128 | + { |
| 129 | + prevsize = prev->size; |
| 130 | + } |
| 131 | + |
| 132 | + /* Now, check if we can extend the current allocation or not */ |
| 133 | + |
| 134 | + if (nextsize + prevsize + oldsize >= size) |
| 135 | + { |
| 136 | + size_t needed = size - oldsize; |
| 137 | + size_t takeprev = 0; |
| 138 | + size_t takenext = 0; |
| 139 | + |
| 140 | + /* Check if we can extend into the previous chunk and if the |
| 141 | + * previous chunk is smaller than the next chunk. |
| 142 | + */ |
| 143 | + |
| 144 | + if (prevsize > 0 && (nextsize >= prevsize || nextsize <= 0)) |
| 145 | + { |
| 146 | + /* Can we get everything we need from the previous chunk? */ |
| 147 | + |
| 148 | + if (needed > prevsize) |
| 149 | + { |
| 150 | + /* No, take the whole previous chunk and get the |
| 151 | + * rest that we need from the next chunk. |
| 152 | + */ |
| 153 | + |
| 154 | + takeprev = prevsize; |
| 155 | + takenext = needed - prevsize; |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + /* Yes, take what we need from the previous chunk */ |
| 160 | + |
| 161 | + takeprev = needed; |
| 162 | + takenext = 0; |
| 163 | + } |
| 164 | + |
| 165 | + needed = 0; |
| 166 | + } |
| 167 | + |
| 168 | + /* Check if we can extend into the next chunk and if we still |
| 169 | + * need more memory. |
| 170 | + */ |
| 171 | + |
| 172 | + if (nextsize > 0 && needed) |
| 173 | + { |
| 174 | + /* Can we get everything we need from the next chunk? */ |
| 175 | + |
| 176 | + if (needed > nextsize) |
| 177 | + { |
| 178 | + /* No, take the whole next chunk and get the |
| 179 | + * rest that we need from the previous chunk. |
| 180 | + */ |
| 181 | + |
| 182 | + takeprev = needed - nextsize; |
| 183 | + takenext = nextsize; |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + /* Yes, take what we need from the previous chunk */ |
| 188 | + |
| 189 | + takeprev = 0; |
| 190 | + takenext = needed; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /* Extend into the previous free chunk */ |
| 195 | + |
| 196 | + newmem = oldmem; |
| 197 | + if (takeprev) |
| 198 | + { |
| 199 | + struct mm_allocnode_s *newnode; |
| 200 | + |
| 201 | + /* Remove the previous node. There must be a predecessor, |
| 202 | + * but there may not be a successor node. |
| 203 | + */ |
| 204 | + |
| 205 | + DEBUGASSERT(prev->blink); |
| 206 | + prev->blink->flink = prev->flink; |
| 207 | + if (prev->flink) |
| 208 | + { |
| 209 | + prev->flink->blink = prev->blink; |
| 210 | + } |
| 211 | + |
| 212 | + /* Extend the node into the previous free chunk */ |
| 213 | + |
| 214 | + newnode = (struct mm_allocnode_s *)((char*)oldnode - takeprev); |
| 215 | + |
| 216 | + /* Did we consume the entire preceding chunk? */ |
| 217 | + |
| 218 | + if (takeprev < prevsize) |
| 219 | + { |
| 220 | + /* No.. just take what we need from the previous chunk |
| 221 | + * and put it back into the free list |
| 222 | + */ |
| 223 | + |
| 224 | + prev->size -= takeprev; |
| 225 | + newnode->size = oldsize + takeprev; |
| 226 | + newnode->preceding = prev->size | MM_ALLOC_BIT; |
| 227 | + next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT); |
| 228 | + |
| 229 | + /* Return the previous free node to the nodelist (with the new size) */ |
| 230 | + |
| 231 | + mm_addfreechunk(prev); |
| 232 | + |
| 233 | + /* Now we want to return newnode */ |
| 234 | + |
| 235 | + oldnode = newnode; |
| 236 | + } |
| 237 | + else |
| 238 | + { |
| 239 | + /* Yes.. update its size (newnode->preceding is already set) */ |
| 240 | + |
| 241 | + newnode->size += oldsize; |
| 242 | + newnode->preceding |= MM_ALLOC_BIT; |
| 243 | + next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT); |
| 244 | + } |
| 245 | + |
| 246 | + oldnode = newnode; |
| 247 | + oldsize = newnode->size; |
| 248 | + |
| 249 | + /* Now we have to move the user contents 'down' in memory. memcpy should |
| 250 | + * should be save for this. |
| 251 | + */ |
| 252 | + |
| 253 | + newmem = (void*)((char*)newnode + SIZEOF_MM_ALLOCNODE); |
| 254 | + memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE); |
| 255 | + } |
| 256 | + |
| 257 | + /* Extend into the next free chunk */ |
| 258 | + |
| 259 | + if (takenext) |
| 260 | + { |
| 261 | + struct mm_freenode_s *newnode; |
| 262 | + struct mm_allocnode_s *andbeyond; |
| 263 | + |
| 264 | + /* Get the chunk following the next node (which could be the tail chunk) */ |
| 265 | + |
| 266 | + andbeyond = (struct mm_allocnode_s*)((char*)next + nextsize); |
| 267 | + |
| 268 | + /* Remove the next node. There must be a predecessor, |
| 269 | + * but there may not be a successor node. |
| 270 | + */ |
| 271 | + |
| 272 | + DEBUGASSERT(next->blink); |
| 273 | + next->blink->flink = next->flink; |
| 274 | + if (next->flink) |
| 275 | + { |
| 276 | + next->flink->blink = next->blink; |
| 277 | + } |
| 278 | + |
| 279 | + /* Extend the node into the next chunk */ |
| 280 | + |
| 281 | + oldnode->size = oldsize + takenext; |
| 282 | + newnode = (struct mm_freenode_s *)((char*)oldnode + oldnode->size); |
| 283 | + |
| 284 | + /* Did we consume the entire preceding chunk? */ |
| 285 | + |
| 286 | + if (takenext < nextsize) |
| 287 | + { |
| 288 | + /* No, take what we need from the next chunk and return it |
| 289 | + * to the free nodelist. |
| 290 | + */ |
| 291 | + |
| 292 | + newnode->size = nextsize - takenext; |
| 293 | + newnode->preceding = oldnode->size; |
| 294 | + andbeyond->preceding = newnode->size | (andbeyond->preceding & MM_ALLOC_BIT); |
| 295 | + |
| 296 | + /* Add the new free node to the nodelist (with the new size) */ |
| 297 | + |
| 298 | + mm_addfreechunk(newnode); |
| 299 | + } |
| 300 | + else |
| 301 | + { |
| 302 | + /* Yes, just update some pointers. */ |
| 303 | + |
| 304 | + andbeyond->preceding = oldnode->size | (andbeyond->preceding & MM_ALLOC_BIT); |
| 305 | + } |
| 306 | + } |
| 307 | + return newmem; |
| 308 | + } |
| 309 | + |
| 310 | + /* The current chunk cannot be extended. Just allocate a new chunk and copy */ |
| 311 | + |
| 312 | + else |
| 313 | + { |
| 314 | + /* Allocate a new block. On failure, realloc must return NULL but |
| 315 | + * leave the original memory in place. |
| 316 | + */ |
| 317 | + newmem = (void*)malloc(size); |
| 318 | + if (newmem) |
| 319 | + { |
| 320 | + memcpy(newmem, oldmem, oldsize); |
| 321 | + free(oldmem); |
| 322 | + } |
| 323 | + |
| 324 | + return newmem; |
| 325 | + } |
| 326 | + |
| 327 | +} |
| 328 | + |
0 commit comments