Wed, 07 Aug 2019 21:14:58 +0200
ucx_array_sort() uses qsort_r(), if available
olaf@13 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, 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@251 | 29 | #include "ucx/stack.h" |
universe@251 | 30 | |
olaf@13 | 31 | #include <string.h> |
olaf@13 | 32 | |
universe@185 | 33 | static size_t ucx_stack_align(size_t n) { |
universe@185 | 34 | int align = n % sizeof(void*); |
universe@185 | 35 | if (align) { |
universe@185 | 36 | n += sizeof(void*) - align; |
universe@185 | 37 | } |
universe@185 | 38 | return n; |
universe@185 | 39 | } |
universe@185 | 40 | |
universe@185 | 41 | void ucx_stack_init(UcxStack *stack, char* space, size_t size) { |
universe@185 | 42 | stack->size = size - size % sizeof(void*); |
universe@185 | 43 | stack->space = space; |
universe@185 | 44 | stack->top = NULL; |
universe@182 | 45 | |
universe@185 | 46 | stack->allocator.pool = stack; |
universe@185 | 47 | stack->allocator.malloc = (ucx_allocator_malloc) ucx_stack_malloc; |
universe@185 | 48 | stack->allocator.calloc = (ucx_allocator_calloc) ucx_stack_calloc; |
universe@185 | 49 | stack->allocator.realloc = (ucx_allocator_realloc) ucx_stack_realloc; |
universe@185 | 50 | stack->allocator.free = (ucx_allocator_free) ucx_stack_free; |
olaf@13 | 51 | } |
olaf@13 | 52 | |
universe@182 | 53 | void *ucx_stack_malloc(UcxStack *stack, size_t n) { |
universe@182 | 54 | |
universe@185 | 55 | if (ucx_stack_avail(stack) < ucx_stack_align(n)) { |
universe@135 | 56 | return NULL; |
universe@135 | 57 | } else { |
universe@185 | 58 | char *prev = stack->top; |
universe@185 | 59 | if (stack->top) { |
universe@185 | 60 | stack->top += ucx_stack_align(ucx_stack_topsize(stack)); |
universe@185 | 61 | } else { |
universe@185 | 62 | stack->top = stack->space; |
universe@185 | 63 | } |
universe@182 | 64 | |
universe@185 | 65 | ((struct ucx_stack_metadata*)stack->top)->prev = prev; |
universe@185 | 66 | ((struct ucx_stack_metadata*)stack->top)->size = n; |
universe@185 | 67 | stack->top += sizeof(struct ucx_stack_metadata); |
universe@182 | 68 | |
universe@185 | 69 | return stack->top; |
universe@15 | 70 | } |
olaf@13 | 71 | } |
olaf@13 | 72 | |
universe@182 | 73 | void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) { |
universe@182 | 74 | void *mem = ucx_stack_malloc(stack, nelem*elsize); |
universe@182 | 75 | memset(mem, 0, nelem*elsize); |
universe@182 | 76 | return mem; |
olaf@13 | 77 | } |
olaf@13 | 78 | |
universe@182 | 79 | void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n) { |
universe@185 | 80 | if (ptr == stack->top) { |
universe@185 | 81 | if (stack->size - (stack->top - stack->space) < ucx_stack_align(n)) { |
universe@185 | 82 | return NULL; |
universe@185 | 83 | } else { |
universe@185 | 84 | ((struct ucx_stack_metadata*)stack->top - 1)->size = n; |
universe@185 | 85 | return ptr; |
universe@185 | 86 | } |
universe@182 | 87 | } else { |
universe@185 | 88 | if (ucx_stack_align(((struct ucx_stack_metadata*)ptr - 1)->size) < |
universe@185 | 89 | ucx_stack_align(n)) { |
universe@182 | 90 | void *nptr = ucx_stack_malloc(stack, n); |
universe@182 | 91 | if (nptr) { |
universe@185 | 92 | memcpy(nptr, ptr, n); |
universe@182 | 93 | ucx_stack_free(stack, ptr); |
universe@185 | 94 | |
universe@182 | 95 | return nptr; |
universe@182 | 96 | } else { |
universe@182 | 97 | return NULL; |
olaf@14 | 98 | } |
universe@182 | 99 | } else { |
universe@185 | 100 | ((struct ucx_stack_metadata*)ptr - 1)->size = n; |
universe@182 | 101 | return ptr; |
olaf@13 | 102 | } |
olaf@13 | 103 | } |
olaf@13 | 104 | } |
olaf@13 | 105 | |
universe@182 | 106 | void ucx_stack_free(UcxStack *stack, void *ptr) { |
universe@185 | 107 | if (ptr == stack->top) { |
universe@185 | 108 | stack->top = ((struct ucx_stack_metadata*) stack->top - 1)->prev; |
universe@182 | 109 | } else { |
universe@185 | 110 | struct ucx_stack_metadata *next = (struct ucx_stack_metadata*)( |
universe@185 | 111 | (char*)ptr + |
universe@185 | 112 | ucx_stack_align(((struct ucx_stack_metadata*) ptr - 1)->size) |
universe@185 | 113 | ); |
universe@185 | 114 | next->prev = ((struct ucx_stack_metadata*) ptr - 1)->prev; |
olaf@113 | 115 | } |
olaf@113 | 116 | } |
olaf@113 | 117 | |
universe@185 | 118 | void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) { |
universe@185 | 119 | if (ucx_stack_empty(stack)) { |
universe@185 | 120 | return; |
universe@185 | 121 | } |
universe@185 | 122 | |
universe@301 | 123 | if (dest) { |
universe@301 | 124 | size_t len = ucx_stack_topsize(stack); |
universe@301 | 125 | if (len > n) { |
universe@301 | 126 | len = n; |
universe@301 | 127 | } |
universe@301 | 128 | |
universe@301 | 129 | memcpy(dest, stack->top, len); |
universe@185 | 130 | } |
universe@185 | 131 | |
universe@185 | 132 | ucx_stack_free(stack, stack->top); |
olaf@13 | 133 | } |
olaf@13 | 134 | |
universe@185 | 135 | size_t ucx_stack_avail(UcxStack *stack) { |
universe@185 | 136 | size_t avail = ((stack->top ? (stack->size |
universe@185 | 137 | - (stack->top - stack->space) |
universe@185 | 138 | - ucx_stack_align(ucx_stack_topsize(stack))) |
universe@185 | 139 | : stack->size)); |
universe@185 | 140 | |
universe@185 | 141 | if (avail > sizeof(struct ucx_stack_metadata)) { |
universe@185 | 142 | return avail - sizeof(struct ucx_stack_metadata); |
universe@185 | 143 | } else { |
universe@185 | 144 | return 0; |
universe@185 | 145 | } |
olaf@13 | 146 | } |
universe@301 | 147 | |
universe@301 | 148 | void *ucx_stack_push(UcxStack *stack, size_t n, const void *data) { |
universe@301 | 149 | void *space = ucx_stack_malloc(stack, n); |
universe@301 | 150 | if (space) { |
universe@301 | 151 | memcpy(space, data, n); |
universe@301 | 152 | } |
universe@301 | 153 | return space; |
universe@301 | 154 | } |
universe@301 | 155 | |
universe@301 | 156 | void *ucx_stack_pusharr(UcxStack *stack, |
universe@301 | 157 | size_t nelem, size_t elsize, const void *data) { |
universe@301 | 158 | |
universe@301 | 159 | // skip the memset by using malloc |
universe@301 | 160 | void *space = ucx_stack_malloc(stack, nelem*elsize); |
universe@301 | 161 | if (space) { |
universe@301 | 162 | memcpy(space, data, nelem*elsize); |
universe@301 | 163 | } |
universe@301 | 164 | return space; |
universe@301 | 165 | } |