2016-09-28 14:40:37 +00:00
|
|
|
/*
|
2018-02-07 14:13:13 +00:00
|
|
|
Copyright (c) 2016-2018 Chung, Hyung-Hwan. All rights reserved.
|
2016-09-28 14:40:37 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hcl-prv.h"
|
|
|
|
|
2020-12-31 17:48:47 +00:00
|
|
|
static void* xma_alloc (hcl_mmgr_t* mmgr, hcl_oow_t size)
|
|
|
|
{
|
|
|
|
return hcl_xma_alloc(mmgr->ctx, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* xma_realloc (hcl_mmgr_t* mmgr, void* ptr, hcl_oow_t size)
|
|
|
|
{
|
|
|
|
return hcl_xma_realloc(mmgr->ctx, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xma_free (hcl_mmgr_t* mmgr, void* ptr)
|
|
|
|
{
|
2021-02-28 06:43:22 +00:00
|
|
|
hcl_xma_free (mmgr->ctx, ptr);
|
2020-12-31 17:48:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 14:40:37 +00:00
|
|
|
hcl_heap_t* hcl_makeheap (hcl_t* hcl, hcl_oow_t size)
|
|
|
|
{
|
|
|
|
hcl_heap_t* heap;
|
2021-02-09 14:54:54 +00:00
|
|
|
hcl_oow_t alloc_size;
|
2016-09-28 14:40:37 +00:00
|
|
|
|
2021-02-11 09:35:38 +00:00
|
|
|
if (size <= HCL_SIZEOF(*heap)) /* 0 or smaller than the heap header */
|
|
|
|
{
|
|
|
|
/* make a zero-sized heap using the default memory manager.
|
|
|
|
* this zero-sized heap contains only the heap header */
|
|
|
|
size = 0;
|
|
|
|
alloc_size = HCL_SIZEOF(*heap);
|
|
|
|
heap = (hcl_heap_t*)hcl_allocmem(hcl, alloc_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if a non-zero heap size is given, create the heap with
|
|
|
|
* the dedicated heap allocator which is allowed to create
|
|
|
|
* a bigger heap than requested */
|
|
|
|
alloc_size = size;
|
|
|
|
heap = (hcl_heap_t*)hcl->vmprim.alloc_heap(hcl, &alloc_size);
|
|
|
|
}
|
2020-12-31 17:48:47 +00:00
|
|
|
if (HCL_UNLIKELY(!heap))
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2020-12-31 17:48:47 +00:00
|
|
|
const hcl_ooch_t* oldmsg = hcl_backuperrmsg(hcl);
|
|
|
|
hcl_seterrbfmt (hcl, hcl_geterrnum(hcl), "unable to allocate a heap - %js", oldmsg);
|
2016-09-28 14:40:37 +00:00
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:54:54 +00:00
|
|
|
/* the vmprim.alloc_heap() function is allowed to create a bigger heap than the requested size.
|
|
|
|
* if the created heap is bigger than requested, the heap will be utilized in full. */
|
2021-02-11 09:35:38 +00:00
|
|
|
HCL_ASSERT (hcl, alloc_size >= HCL_SIZEOF(*heap));
|
2021-02-09 14:54:54 +00:00
|
|
|
HCL_MEMSET (heap, 0, alloc_size);
|
2016-09-28 14:40:37 +00:00
|
|
|
|
2021-02-11 09:35:38 +00:00
|
|
|
alloc_size -= HCL_SIZEOF(*heap); /* exclude the header size */
|
2016-09-28 14:40:37 +00:00
|
|
|
heap->base = (hcl_uint8_t*)(heap + 1);
|
2021-02-09 14:54:54 +00:00
|
|
|
heap->size = alloc_size;
|
2016-09-28 14:40:37 +00:00
|
|
|
|
2021-02-11 09:35:38 +00:00
|
|
|
if (size == 0)
|
2020-12-31 17:48:47 +00:00
|
|
|
{
|
|
|
|
/* use the existing memory allocator */
|
2021-02-11 09:35:38 +00:00
|
|
|
HCL_ASSERT (hcl, alloc_size == 0);
|
2020-12-31 17:48:47 +00:00
|
|
|
heap->xmmgr = *hcl_getmmgr(hcl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* create a new memory allocator over the allocated heap */
|
|
|
|
heap->xma = hcl_xma_open(hcl_getmmgr(hcl), 0, heap->base, heap->size);
|
|
|
|
if (HCL_UNLIKELY(!heap->xma))
|
|
|
|
{
|
|
|
|
hcl->vmprim.free_heap (hcl, heap);
|
2021-02-09 14:54:54 +00:00
|
|
|
hcl_seterrbfmt (hcl, HCL_ESYSMEM, "unable to allocate a memory manager over a heap");
|
2020-12-31 17:48:47 +00:00
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-22 02:53:29 +00:00
|
|
|
heap->xmmgr.allocmem = xma_alloc;
|
|
|
|
heap->xmmgr.reallocmem = xma_realloc;
|
|
|
|
heap->xmmgr.freemem = xma_free;
|
2020-12-31 17:48:47 +00:00
|
|
|
heap->xmmgr.ctx = heap->xma;
|
|
|
|
}
|
2016-09-28 14:40:37 +00:00
|
|
|
|
|
|
|
return heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hcl_killheap (hcl_t* hcl, hcl_heap_t* heap)
|
|
|
|
{
|
2021-02-11 09:35:38 +00:00
|
|
|
if (heap->size == 0)
|
|
|
|
{
|
|
|
|
hcl_freemem (hcl, heap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (heap->xma) hcl_xma_close (heap->xma);
|
|
|
|
hcl->vmprim.free_heap (hcl, heap);
|
|
|
|
}
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 17:48:47 +00:00
|
|
|
void* hcl_callocheapmem (hcl_t* hcl, hcl_heap_t* heap, hcl_oow_t size)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2020-12-31 17:48:47 +00:00
|
|
|
void* ptr;
|
2016-09-28 14:40:37 +00:00
|
|
|
|
2020-12-31 17:48:47 +00:00
|
|
|
ptr = HCL_MMGR_ALLOC(&heap->xmmgr, size);
|
2023-11-09 15:03:03 +00:00
|
|
|
if (HCL_UNLIKELY(!ptr))
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2020-12-31 17:48:47 +00:00
|
|
|
HCL_DEBUG2 (hcl, "Cannot callocate %zd bytes from heap - ptr %p\n", size, heap);
|
2018-02-05 10:43:25 +00:00
|
|
|
hcl_seterrnum (hcl, HCL_EOOMEM);
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
2020-12-31 17:48:47 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
HCL_MEMSET (ptr, 0, size);
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
2016-09-28 14:40:37 +00:00
|
|
|
|
2021-01-17 17:45:39 +00:00
|
|
|
void* hcl_callocheapmem_noseterr (hcl_t* hcl, hcl_heap_t* heap, hcl_oow_t size)
|
2020-12-31 17:48:47 +00:00
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
ptr = HCL_MMGR_ALLOC(&heap->xmmgr, size);
|
|
|
|
if (HCL_LIKELY(ptr)) HCL_MEMSET (ptr, 0, size);
|
2016-09-28 14:40:37 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
2020-12-31 17:48:47 +00:00
|
|
|
|
|
|
|
void hcl_freeheapmem (hcl_t* hcl, hcl_heap_t* heap, void* ptr)
|
|
|
|
{
|
|
|
|
HCL_MMGR_FREE (&heap->xmmgr, ptr);
|
|
|
|
}
|