?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*-------------------------------------------------------------------------
  2.    realloc.c - allocate memory.
  3.  
  4.    Copyright (C) 2015, Philipp Klaus Krause, pkk@spth.de
  5.  
  6.    This library is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this library; see the file COPYING. If not, write to the
  18.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  19.    MA 02110-1301, USA.
  20.  
  21.    As a special exception, if you link this library with other files,
  22.    some of which are compiled with SDCC, to produce an executable,
  23.    this library does not by itself cause the resulting executable to
  24.    be covered by the GNU General Public License. This exception does
  25.    not however invalidate any other reasons why the executable file
  26.    might be covered by the GNU General Public License.
  27. -------------------------------------------------------------------------*/
  28.  
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <string.h>
  32.  
  33. #if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400)
  34. #define HEAPSPACE __xdata
  35. #elif defined(__SDCC_pdk13) || defined(__SDCC_pdk14) || defined(__SDCC_pdk15)
  36. #define HEAPSPACE __near
  37. #else
  38. #define HEAPSPACE
  39. #endif
  40.  
  41. typedef struct header HEAPSPACE header_t;
  42.  
  43. struct header
  44. {
  45.         header_t *next;
  46.         header_t *next_free;
  47. };
  48.  
  49. extern header_t *HEAPSPACE __sdcc_heap_free;
  50.  
  51. void __sdcc_heap_init(void);
  52.  
  53. #if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400)
  54. void HEAPSPACE *realloc(void *ptr, size_t size)
  55. #else
  56. void *realloc(void *ptr, size_t size)
  57. #endif
  58. {
  59.         void HEAPSPACE *ret;
  60.         header_t *h, *next_free, *prev_free;
  61.         header_t *HEAPSPACE *f, *HEAPSPACE *pf;
  62.         size_t blocksize, oldblocksize, maxblocksize;
  63.  
  64. #if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400) || defined(__SDCC_hc08) || defined(__SDCC_s08)
  65.         if(!__sdcc_heap_free)
  66.                 __sdcc_heap_init();
  67. #endif
  68.  
  69.         if(!ptr)
  70.                 return(malloc(size));
  71.  
  72.         if(!size)
  73.         {
  74.                 free(ptr);
  75.                 return(0);
  76.         }
  77.  
  78.         prev_free = 0, pf = 0;
  79.         for(h = __sdcc_heap_free, f = &__sdcc_heap_free; h && h < ptr; prev_free = h, pf = f, f = &(h->next_free), h = h->next_free); // Find adjacent blocks in free list
  80.         next_free = h;
  81.  
  82.         if(!size || size + offsetof(struct header, next_free) < size)
  83.                 return(0);
  84.         blocksize = size + offsetof(struct header, next_free);
  85.         if(blocksize < sizeof(struct header)) // Requiring a minimum size makes it easier to implement free(), and avoid memory leaks.
  86.                 blocksize = sizeof(struct header);
  87.  
  88.         h = (void HEAPSPACE *)((char HEAPSPACE *)(ptr) - offsetof(struct header, next_free));
  89.         oldblocksize = (char HEAPSPACE *)(h->next) - (char HEAPSPACE *)h;
  90.  
  91.         maxblocksize = oldblocksize;
  92.         if(prev_free && prev_free->next == h) // Can merge with previous block
  93.                 maxblocksize += (char HEAPSPACE *)h - (char HEAPSPACE *)prev_free;
  94.         if(next_free == h->next) // Can merge with next block
  95.                 maxblocksize += (char HEAPSPACE *)(next_free->next) - (char HEAPSPACE *)next_free;
  96.  
  97.         if(blocksize <= maxblocksize) // Can resize in place.
  98.         {
  99.                 if(prev_free && prev_free->next == h) // Always move into previous block to defragment
  100.                 {
  101.                         memmove(prev_free, h, blocksize <= oldblocksize ? blocksize : oldblocksize);
  102.                         h = prev_free;
  103.                         *pf = next_free;
  104.                         f = pf;
  105.                 }
  106.  
  107.                 if(next_free && next_free == h->next) // Merge with following block
  108.                 {
  109.                         h->next = next_free->next;
  110.                         *f = next_free->next_free;
  111.                 }
  112.  
  113.                 if(maxblocksize >= blocksize + sizeof(struct header)) // Create new block from free space
  114.                 {
  115.                         header_t *const newheader = (header_t *const)((char HEAPSPACE *)h + blocksize);
  116.                         newheader->next = h->next;
  117.                         newheader->next_free = *f;
  118.                         *f = newheader;
  119.                         h->next = newheader;
  120.                 }
  121.  
  122.                 return(&(h->next_free));
  123.         }
  124.  
  125.         if(ret = malloc(size))
  126.         {
  127.                 size_t oldsize = oldblocksize - offsetof(struct header, next_free);
  128.                 memcpy(ret, ptr, size <= oldsize ? size : oldsize);
  129.                 free(ptr);
  130.                 return(ret);
  131.         }
  132.  
  133.         return(0);
  134. }
  135.  
  136.