Tue, 19 Sep 2017 15:32:53 +0200
removes unusedFunction check for now (we may add it later to get a further indicator for test coverage)
13 | 1 | /* |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
13 | 3 | * |
225
a1a068c2c4ef
updates documenting comments
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
4 | * Copyright 2016 Olaf Wintermann. All rights reserved. |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
13 | 27 | */ |
28 | ||
182 | 29 | #include "stack.h" |
13 | 30 | #include <string.h> |
31 | ||
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
32 | static size_t ucx_stack_align(size_t n) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
33 | int align = n % sizeof(void*); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
34 | if (align) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
35 | n += sizeof(void*) - align; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
36 | } |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
37 | return n; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
38 | } |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
39 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
40 | void ucx_stack_init(UcxStack *stack, char* space, size_t size) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
41 | stack->size = size - size % sizeof(void*); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
42 | stack->space = space; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
43 | stack->top = NULL; |
182 | 44 | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
45 | stack->allocator.pool = stack; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
46 | stack->allocator.malloc = (ucx_allocator_malloc) ucx_stack_malloc; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
47 | stack->allocator.calloc = (ucx_allocator_calloc) ucx_stack_calloc; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
48 | stack->allocator.realloc = (ucx_allocator_realloc) ucx_stack_realloc; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
49 | stack->allocator.free = (ucx_allocator_free) ucx_stack_free; |
13 | 50 | } |
51 | ||
182 | 52 | void *ucx_stack_malloc(UcxStack *stack, size_t n) { |
53 | ||
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
54 | if (ucx_stack_avail(stack) < ucx_stack_align(n)) { |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
55 | return NULL; |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
56 | } else { |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
57 | char *prev = stack->top; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
58 | if (stack->top) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
59 | stack->top += ucx_stack_align(ucx_stack_topsize(stack)); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
60 | } else { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
61 | stack->top = stack->space; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
62 | } |
182 | 63 | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
64 | ((struct ucx_stack_metadata*)stack->top)->prev = prev; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
65 | ((struct ucx_stack_metadata*)stack->top)->size = n; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
66 | stack->top += sizeof(struct ucx_stack_metadata); |
182 | 67 | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
68 | return stack->top; |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
69 | } |
13 | 70 | } |
71 | ||
182 | 72 | void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) { |
73 | void *mem = ucx_stack_malloc(stack, nelem*elsize); | |
74 | memset(mem, 0, nelem*elsize); | |
75 | return mem; | |
13 | 76 | } |
77 | ||
182 | 78 | void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n) { |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
79 | if (ptr == stack->top) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
80 | if (stack->size - (stack->top - stack->space) < ucx_stack_align(n)) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
81 | return NULL; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
82 | } else { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
83 | ((struct ucx_stack_metadata*)stack->top - 1)->size = n; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
84 | return ptr; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
85 | } |
182 | 86 | } else { |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
87 | if (ucx_stack_align(((struct ucx_stack_metadata*)ptr - 1)->size) < |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
88 | ucx_stack_align(n)) { |
182 | 89 | void *nptr = ucx_stack_malloc(stack, n); |
90 | if (nptr) { | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
91 | memcpy(nptr, ptr, n); |
182 | 92 | ucx_stack_free(stack, ptr); |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
93 | |
182 | 94 | return nptr; |
95 | } else { | |
96 | return NULL; | |
14 | 97 | } |
182 | 98 | } else { |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
99 | ((struct ucx_stack_metadata*)ptr - 1)->size = n; |
182 | 100 | return ptr; |
13 | 101 | } |
102 | } | |
103 | } | |
104 | ||
182 | 105 | void ucx_stack_free(UcxStack *stack, void *ptr) { |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
106 | if (ptr == stack->top) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
107 | stack->top = ((struct ucx_stack_metadata*) stack->top - 1)->prev; |
182 | 108 | } else { |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
109 | struct ucx_stack_metadata *next = (struct ucx_stack_metadata*)( |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
110 | (char*)ptr + |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
111 | ucx_stack_align(((struct ucx_stack_metadata*) ptr - 1)->size) |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
112 | ); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
113 | next->prev = ((struct ucx_stack_metadata*) ptr - 1)->prev; |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
114 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
115 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
116 | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
117 | void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
118 | if (ucx_stack_empty(stack)) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
119 | return; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
120 | } |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
121 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
122 | size_t len = ucx_stack_topsize(stack); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
123 | if (len > n) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
124 | len = n; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
125 | } |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
126 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
127 | memcpy(dest, stack->top, len); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
128 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
129 | ucx_stack_free(stack, stack->top); |
13 | 130 | } |
131 | ||
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
132 | size_t ucx_stack_avail(UcxStack *stack) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
133 | size_t avail = ((stack->top ? (stack->size |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
134 | - (stack->top - stack->space) |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
135 | - ucx_stack_align(ucx_stack_topsize(stack))) |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
136 | : stack->size)); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
137 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
138 | if (avail > sizeof(struct ucx_stack_metadata)) { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
139 | return avail - sizeof(struct ucx_stack_metadata); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
140 | } else { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
141 | return 0; |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
142 | } |
13 | 143 | } |