cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

malloc.c (4031B)


      1/*-------------------------------------------------------------------------
      2   malloc.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
     32#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400)
     33#define HEAPSPACE __xdata
     34#elif defined(__SDCC_pdk13) || defined(__SDCC_pdk14) || defined(__SDCC_pdk15)
     35#define HEAPSPACE __near
     36#else
     37#define HEAPSPACE
     38#endif
     39
     40typedef struct header HEAPSPACE header_t;
     41
     42struct header
     43{
     44	header_t *next; // Next block. Linked list of all blocks, terminated by pointer to end of heap (or to the byte beyond the end of the heap).
     45	header_t *next_free; // Next free block. Used in free blocks only. Overlaps with user data in non-free blocks. Linked list of free blocks, 0-terminated.
     46};
     47
     48header_t *HEAPSPACE __sdcc_heap_free; // First free block, 0 if no free blocks.
     49
     50extern header_t __sdcc_heap;
     51#define HEAP_START &__sdcc_heap
     52
     53#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400) || defined(__SDCC_hc08) || defined(__SDCC_s08)
     54
     55extern const unsigned int __sdcc_heap_size;
     56#define HEAP_END (struct header HEAPSPACE *)((char HEAPSPACE *)&__sdcc_heap + (__sdcc_heap_size - 1)) // -1 To be sure that HEAP_END is bigger than HEAP_START.
     57
     58#else
     59
     60extern header_t __sdcc_heap_end; // Just beyond the end of the heap. Must be higher in memory than _sdcc_heap_start.
     61#define HEAP_END &__sdcc_heap_end
     62
     63#endif
     64
     65void __sdcc_heap_init(void)
     66{
     67	__sdcc_heap_free = HEAP_START;
     68	__sdcc_heap_free->next = HEAP_END;
     69	__sdcc_heap_free->next_free = 0;
     70}
     71
     72#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400)
     73void HEAPSPACE *malloc(size_t size)
     74#else
     75void *malloc(size_t size)
     76#endif
     77{
     78	header_t *h;
     79	header_t *HEAPSPACE *f;
     80
     81#if defined(__SDCC_mcs51) || defined(__SDCC_ds390) || defined(__SDCC_ds400) || defined(__SDCC_hc08) || defined(__SDCC_s08)
     82	if(!__sdcc_heap_free)
     83		__sdcc_heap_init();
     84#endif
     85
     86	if(!size || size + offsetof(struct header, next_free) < size)
     87		return(0);
     88	size += offsetof(struct header, next_free);
     89	if(size < sizeof(struct header)) // Requiring a minimum size makes it easier to implement free(), and avoid memory leaks.
     90		size = sizeof(struct header);
     91
     92	for(h = __sdcc_heap_free, f = &__sdcc_heap_free; h; f = &(h->next_free), h = h->next_free)
     93	{
     94		size_t blocksize = (char HEAPSPACE *)(h->next) - (char HEAPSPACE *)h;
     95		if(blocksize >= size) // Found free block of sufficient size.
     96		{
     97			if(blocksize >= size + sizeof(struct header)) // It is worth creating a new free block
     98			{
     99				header_t *const newheader = (header_t *const)((char HEAPSPACE *)h + size);
    100				newheader->next = h->next;
    101				newheader->next_free = h->next_free;
    102				*f = newheader;
    103				h->next = newheader;
    104			}
    105			else
    106				*f = h->next_free;
    107
    108			return(&(h->next_free));
    109		}
    110	}
    111
    112	return(0);
    113}
    114