olaf@13: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@13: * universe@177: * Copyright 2014 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@182: #include "stack.h" olaf@13: #include olaf@13: universe@182: UcxStack ucx_stack_new(char* space, size_t size) { universe@182: UcxStack stack; universe@182: stack.size = size; universe@182: stack.space = stack.top = space; universe@182: universe@182: UcxAllocator alloc; universe@182: alloc.pool = &stack; universe@182: alloc.malloc = (ucx_allocator_malloc) ucx_stack_malloc; universe@182: alloc.calloc = (ucx_allocator_calloc) ucx_stack_calloc; universe@182: alloc.realloc = (ucx_allocator_realloc) ucx_stack_realloc; universe@182: alloc.free = (ucx_allocator_free) ucx_stack_free; universe@182: universe@182: stack.allocator = alloc; universe@182: universe@182: return stack; olaf@13: } olaf@13: universe@182: void *ucx_stack_malloc(UcxStack *stack, size_t n) { universe@182: n += n % sizeof(void*); universe@182: universe@182: if (stack->top + n + sizeof(size_t) > stack->space + stack->size) { universe@135: return NULL; universe@135: } else { universe@182: void *ptr = stack->top; universe@182: universe@182: *((size_t*) (stack->top + n)) = n; universe@182: stack->top += n + sizeof(size_t); universe@182: universe@182: return ptr; 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@182: if (ptr == stack->top - sizeof(size_t) - *((size_t*) stack->top - 1)) { olaf@13: universe@182: stack->top = (char*)ptr + n; universe@182: *((size_t*)stack->top) = n; universe@182: stack->top += sizeof(size_t); universe@182: universe@182: return ptr; universe@182: } else { universe@182: size_t* sptr = (size_t*) (((char*) ptr)-sizeof(size_t)); universe@182: if (*sptr < n) { universe@182: void *nptr = ucx_stack_malloc(stack, n); universe@182: if (nptr) { universe@182: memcpy(nptr, ptr, *sptr); universe@182: ucx_stack_free(stack, ptr); universe@182: return nptr; universe@182: } else { universe@182: return NULL; olaf@14: } universe@182: } else { universe@182: *sptr = n; universe@182: return ptr; olaf@13: } olaf@13: } olaf@13: } olaf@13: universe@182: void ucx_stack_free(UcxStack *stack, void *ptr) { universe@182: if (ptr == stack->top+sizeof(size_t)) { universe@182: universe@182: } else { universe@182: olaf@113: } olaf@113: } olaf@113: universe@182: void ucx_stack_pop(UcxStack *stack, void *dest) { olaf@13: } olaf@13: universe@182: void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) { olaf@13: }