olaf@13: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@13: * universe@259: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@13: */ olaf@13: universe@251: #include "ucx/stack.h" universe@251: olaf@13: #include olaf@13: universe@185: static size_t ucx_stack_align(size_t n) { universe@185: int align = n % sizeof(void*); universe@185: if (align) { universe@185: n += sizeof(void*) - align; universe@185: } universe@185: return n; universe@185: } universe@185: universe@185: void ucx_stack_init(UcxStack *stack, char* space, size_t size) { universe@185: stack->size = size - size % sizeof(void*); universe@185: stack->space = space; universe@185: stack->top = NULL; universe@182: universe@185: stack->allocator.pool = stack; universe@185: stack->allocator.malloc = (ucx_allocator_malloc) ucx_stack_malloc; universe@185: stack->allocator.calloc = (ucx_allocator_calloc) ucx_stack_calloc; universe@185: stack->allocator.realloc = (ucx_allocator_realloc) ucx_stack_realloc; universe@185: stack->allocator.free = (ucx_allocator_free) ucx_stack_free; olaf@13: } olaf@13: universe@182: void *ucx_stack_malloc(UcxStack *stack, size_t n) { universe@182: universe@185: if (ucx_stack_avail(stack) < ucx_stack_align(n)) { universe@135: return NULL; universe@135: } else { universe@185: char *prev = stack->top; universe@185: if (stack->top) { universe@185: stack->top += ucx_stack_align(ucx_stack_topsize(stack)); universe@185: } else { universe@185: stack->top = stack->space; universe@185: } universe@182: universe@185: ((struct ucx_stack_metadata*)stack->top)->prev = prev; universe@185: ((struct ucx_stack_metadata*)stack->top)->size = n; universe@185: stack->top += sizeof(struct ucx_stack_metadata); universe@182: universe@185: return stack->top; universe@15: } olaf@13: } olaf@13: universe@182: void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) { universe@182: void *mem = ucx_stack_malloc(stack, nelem*elsize); universe@182: memset(mem, 0, nelem*elsize); universe@182: return mem; olaf@13: } olaf@13: universe@182: void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n) { universe@185: if (ptr == stack->top) { universe@185: if (stack->size - (stack->top - stack->space) < ucx_stack_align(n)) { universe@185: return NULL; universe@185: } else { universe@185: ((struct ucx_stack_metadata*)stack->top - 1)->size = n; universe@185: return ptr; universe@185: } universe@182: } else { universe@185: if (ucx_stack_align(((struct ucx_stack_metadata*)ptr - 1)->size) < universe@185: ucx_stack_align(n)) { universe@182: void *nptr = ucx_stack_malloc(stack, n); universe@182: if (nptr) { universe@185: memcpy(nptr, ptr, n); universe@182: ucx_stack_free(stack, ptr); universe@185: universe@182: return nptr; universe@182: } else { universe@182: return NULL; olaf@14: } universe@182: } else { universe@185: ((struct ucx_stack_metadata*)ptr - 1)->size = n; universe@182: return ptr; olaf@13: } olaf@13: } olaf@13: } olaf@13: universe@182: void ucx_stack_free(UcxStack *stack, void *ptr) { universe@185: if (ptr == stack->top) { universe@185: stack->top = ((struct ucx_stack_metadata*) stack->top - 1)->prev; universe@182: } else { universe@185: struct ucx_stack_metadata *next = (struct ucx_stack_metadata*)( universe@185: (char*)ptr + universe@185: ucx_stack_align(((struct ucx_stack_metadata*) ptr - 1)->size) universe@185: ); universe@185: next->prev = ((struct ucx_stack_metadata*) ptr - 1)->prev; olaf@113: } olaf@113: } olaf@113: universe@185: void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) { universe@185: if (ucx_stack_empty(stack)) { universe@185: return; universe@185: } universe@185: universe@185: size_t len = ucx_stack_topsize(stack); universe@185: if (len > n) { universe@185: len = n; universe@185: } universe@185: universe@185: memcpy(dest, stack->top, len); universe@185: universe@185: ucx_stack_free(stack, stack->top); olaf@13: } olaf@13: universe@185: size_t ucx_stack_avail(UcxStack *stack) { universe@185: size_t avail = ((stack->top ? (stack->size universe@185: - (stack->top - stack->space) universe@185: - ucx_stack_align(ucx_stack_topsize(stack))) universe@185: : stack->size)); universe@185: universe@185: if (avail > sizeof(struct ucx_stack_metadata)) { universe@185: return avail - sizeof(struct ucx_stack_metadata); universe@185: } else { universe@185: return 0; universe@185: } olaf@13: }