Mon, 14 Jul 2014 16:54:10 +0200
merged sstrcat function
olaf@13 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@177 | 4 | * Copyright 2014 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@13 | 27 | */ |
olaf@13 | 28 | |
universe@182 | 29 | #include "stack.h" |
olaf@13 | 30 | #include <string.h> |
olaf@13 | 31 | |
universe@182 | 32 | UcxStack ucx_stack_new(char* space, size_t size) { |
universe@182 | 33 | UcxStack stack; |
universe@182 | 34 | stack.size = size; |
universe@182 | 35 | stack.space = stack.top = space; |
universe@182 | 36 | |
universe@182 | 37 | UcxAllocator alloc; |
universe@182 | 38 | alloc.pool = &stack; |
universe@182 | 39 | alloc.malloc = (ucx_allocator_malloc) ucx_stack_malloc; |
universe@182 | 40 | alloc.calloc = (ucx_allocator_calloc) ucx_stack_calloc; |
universe@182 | 41 | alloc.realloc = (ucx_allocator_realloc) ucx_stack_realloc; |
universe@182 | 42 | alloc.free = (ucx_allocator_free) ucx_stack_free; |
universe@182 | 43 | |
universe@182 | 44 | stack.allocator = alloc; |
universe@182 | 45 | |
universe@182 | 46 | return stack; |
olaf@13 | 47 | } |
olaf@13 | 48 | |
universe@182 | 49 | void *ucx_stack_malloc(UcxStack *stack, size_t n) { |
universe@182 | 50 | n += n % sizeof(void*); |
universe@182 | 51 | |
universe@182 | 52 | if (stack->top + n + sizeof(size_t) > stack->space + stack->size) { |
universe@135 | 53 | return NULL; |
universe@135 | 54 | } else { |
universe@182 | 55 | void *ptr = stack->top; |
universe@182 | 56 | |
universe@182 | 57 | *((size_t*) (stack->top + n)) = n; |
universe@182 | 58 | stack->top += n + sizeof(size_t); |
universe@182 | 59 | |
universe@182 | 60 | return ptr; |
universe@15 | 61 | } |
olaf@13 | 62 | } |
olaf@13 | 63 | |
universe@182 | 64 | void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) { |
universe@182 | 65 | void *mem = ucx_stack_malloc(stack, nelem*elsize); |
universe@182 | 66 | memset(mem, 0, nelem*elsize); |
universe@182 | 67 | return mem; |
olaf@13 | 68 | } |
olaf@13 | 69 | |
universe@182 | 70 | void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n) { |
universe@182 | 71 | if (ptr == stack->top - sizeof(size_t) - *((size_t*) stack->top - 1)) { |
olaf@13 | 72 | |
universe@182 | 73 | stack->top = (char*)ptr + n; |
universe@182 | 74 | *((size_t*)stack->top) = n; |
universe@182 | 75 | stack->top += sizeof(size_t); |
universe@182 | 76 | |
universe@182 | 77 | return ptr; |
universe@182 | 78 | } else { |
universe@182 | 79 | size_t* sptr = (size_t*) (((char*) ptr)-sizeof(size_t)); |
universe@182 | 80 | if (*sptr < n) { |
universe@182 | 81 | void *nptr = ucx_stack_malloc(stack, n); |
universe@182 | 82 | if (nptr) { |
universe@182 | 83 | memcpy(nptr, ptr, *sptr); |
universe@182 | 84 | ucx_stack_free(stack, ptr); |
universe@182 | 85 | return nptr; |
universe@182 | 86 | } else { |
universe@182 | 87 | return NULL; |
olaf@14 | 88 | } |
universe@182 | 89 | } else { |
universe@182 | 90 | *sptr = n; |
universe@182 | 91 | return ptr; |
olaf@13 | 92 | } |
olaf@13 | 93 | } |
olaf@13 | 94 | } |
olaf@13 | 95 | |
universe@182 | 96 | void ucx_stack_free(UcxStack *stack, void *ptr) { |
universe@182 | 97 | if (ptr == stack->top+sizeof(size_t)) { |
universe@182 | 98 | |
universe@182 | 99 | } else { |
universe@182 | 100 | |
olaf@113 | 101 | } |
olaf@113 | 102 | } |
olaf@113 | 103 | |
universe@182 | 104 | void ucx_stack_pop(UcxStack *stack, void *dest) { |
olaf@13 | 105 | } |
olaf@13 | 106 | |
universe@182 | 107 | void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) { |
olaf@13 | 108 | } |