1 /*-------------------------------------------------------------------------
2    memmisc.c - heap handling functions
3 
4    Copyright (C) 2005, Vangelis Rokas <vrokas at otenet.gr>
5 
6    Modifications for PIC14 by
7    Copyright (C) 2019 Gonzalo Pérez de Olaguer Córdoba <salo@gpoc.es>
8 
9    This library is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by the
11    Free Software Foundation; either version 2, or (at your option) any
12    later version.
13 
14    This library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this library; see the file COPYING. If not, write to the
21    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
22    MA 02110-1301, USA.
23 
24    As a special exception, if you link this library with other files,
25    some of which are compiled with SDCC, to produce an executable,
26    this library does not by itself cause the resulting executable to
27    be covered by the GNU General Public License. This exception does
28    not however invalidate any other reasons why the executable file
29    might be covered by the GNU General Public License.
30 -------------------------------------------------------------------------*/
31 
32 #include <pic14_malloc.h>
33 #include <string.h>
34 
35 _malloc_rec __data *_malloc_heap = NULL;
36 
_initHeap(void __data * ptr,size_t size)37 void _initHeap (void __data *ptr, size_t size)
38 {
39     _malloc_rec __data *head;
40     size_t used_size = 0;
41     size_t block_size;
42 
43     if (ptr == NULL || size == 0)
44     {
45         _malloc_heap = NULL;
46         return;
47     }
48 
49     _malloc_heap = (_malloc_rec __data *)ptr;
50     head = _malloc_heap;
51     /* we need one byte as the end of block list marker */
52     size--;
53 
54     while (used_size < size) {
55         /* a guess of the next block size */
56         block_size = (size - used_size); /* thus: block_size > 0 */
57         if (block_size > MAX_BLOCK_SIZE)
58 	    block_size = MAX_BLOCK_SIZE;
59 
60         /* now we can create the block */
61         head->datum = block_size;
62         head = NEXTREC(head);
63 
64         used_size += block_size;
65     }
66 
67     /* mark end of block list */
68     head->datum = 0;
69 }
70 
71 /* try to allocate a block from the heap of at least size bytes,
72  * merging adjacent blocks if necessary.
73  * if ptr is not NULL, it may be included into the merged blocks.
74  */
_allocateHeap(void __data * ptr,size_t size)75 void __data *_allocateHeap (void __data *ptr, size_t size)
76 {
77     _malloc_rec __data *current_head, *next_head, *realloc_head;
78     size_t block_size;
79 
80     if (_malloc_heap == NULL)
81         return ((void __data *)NULL);
82 
83     if (size >= MAX_BLOCK_SIZE)
84         return ((void __data *)NULL);
85 
86     if (ptr)
87         realloc_head = PTR2REC(ptr);
88     else
89         realloc_head = NULL;
90 
91     current_head = next_head = _malloc_heap;
92     block_size = 0;
93     size++;
94     while (next_head->datum != 0)
95     {
96         if (next_head->bits.alloc == 0 || next_head == realloc_head)
97         {
98             block_size += next_head->bits.count;
99             if (block_size >= size)
100             {
101                 /* block found */
102                 if (realloc_head)
103                 {
104                     memmove (REC2PTR(current_head), ptr, realloc_head->bits.count - 1);
105                     if (next_head != realloc_head)
106                         realloc_head->bits.alloc = 0;
107                 }
108                 current_head->datum = ALLOC_FLAG + size;
109                 if (block_size > size)
110                 {
111                     next_head = NEXTREC(current_head);
112                     next_head->datum = block_size - size;
113                 }
114                 return REC2PTR(current_head);
115             }
116             next_head = NEXTREC(next_head);
117         }
118         else
119         {
120             current_head = next_head = NEXTREC(next_head);
121             block_size = 0;
122         }
123     }
124     return ((void __data *)NULL);
125 }
126