struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / pic14 / libc / memmisc.c
blobb9a2e0978f77c7cbe9dae85f84fe4f54f4909071
1 /*-------------------------------------------------------------------------
2 memmisc.c - heap handling functions
4 Copyright (C) 2005, Vangelis Rokas <vrokas at otenet.gr>
6 Modifications for PIC14 by
7 Copyright (C) 2019 Gonzalo Pérez de Olaguer Córdoba <salo@gpoc.es>
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.
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.
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.
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 -------------------------------------------------------------------------*/
32 #include <pic14_malloc.h>
33 #include <string.h>
35 _malloc_rec __data *_malloc_heap = NULL;
37 void _initHeap (void __data *ptr, size_t size)
39 _malloc_rec __data *head;
40 size_t used_size = 0;
41 size_t block_size;
43 if (ptr == NULL || size == 0)
45 _malloc_heap = NULL;
46 return;
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--;
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;
60 /* now we can create the block */
61 head->datum = block_size;
62 head = NEXTREC(head);
64 used_size += block_size;
67 /* mark end of block list */
68 head->datum = 0;
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.
75 void __data *_allocateHeap (void __data *ptr, size_t size)
77 _malloc_rec __data *current_head, *next_head, *realloc_head;
78 size_t block_size;
80 if (_malloc_heap == NULL)
81 return ((void __data *)NULL);
83 if (size >= MAX_BLOCK_SIZE)
84 return ((void __data *)NULL);
86 if (ptr)
87 realloc_head = PTR2REC(ptr);
88 else
89 realloc_head = NULL;
91 current_head = next_head = _malloc_heap;
92 block_size = 0;
93 size++;
94 while (next_head->datum != 0)
96 if (next_head->bits.alloc == 0 || next_head == realloc_head)
98 block_size += next_head->bits.count;
99 if (block_size >= size)
101 /* block found */
102 if (realloc_head)
104 memmove (REC2PTR(current_head), ptr, realloc_head->bits.count - 1);
105 if (next_head != realloc_head)
106 realloc_head->bits.alloc = 0;
108 current_head->datum = ALLOC_FLAG + size;
109 if (block_size > size)
111 next_head = NEXTREC(current_head);
112 next_head->datum = block_size - size;
114 return REC2PTR(current_head);
116 next_head = NEXTREC(next_head);
118 else
120 current_head = next_head = NEXTREC(next_head);
121 block_size = 0;
124 return ((void __data *)NULL);