Wed, 02 May 2018 21:45:52 +0200
adds more convenience macros for sstr
This commit also increases the UCX version number towards the next release.
- New product version: 1.1
- New library version: 2.0.1 - we are backwards, but not forward compatible.
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 | * |
259 | 4 | * Copyright 2017 Mike Becker, 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 | ||
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
29 | #include "ucx/stack.h" |
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
30 | |
13 | 31 | #include <string.h> |
32 | ||
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
33 | 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
|
34 | 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
|
35 | 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
|
36 | 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
|
37 | } |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
38 | 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
|
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 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | stack->top = NULL; |
182 | 45 | |
185
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.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
|
47 | 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
|
48 | 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
|
49 | 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
|
50 | stack->allocator.free = (ucx_allocator_free) ucx_stack_free; |
13 | 51 | } |
52 | ||
182 | 53 | void *ucx_stack_malloc(UcxStack *stack, size_t n) { |
54 | ||
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
55 | 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
|
56 | return NULL; |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
57 | } 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
|
58 | 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
|
59 | 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
|
60 | 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
|
61 | } else { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
62 | 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
|
63 | } |
182 | 64 | |
185
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)->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
|
66 | ((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
|
67 | stack->top += sizeof(struct ucx_stack_metadata); |
182 | 68 | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
69 | return stack->top; |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
70 | } |
13 | 71 | } |
72 | ||
182 | 73 | void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) { |
74 | void *mem = ucx_stack_malloc(stack, nelem*elsize); | |
75 | memset(mem, 0, nelem*elsize); | |
76 | return mem; | |
13 | 77 | } |
78 | ||
182 | 79 | 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
|
80 | 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
|
81 | 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
|
82 | 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
|
83 | } else { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
84 | ((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
|
85 | 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
|
86 | } |
182 | 87 | } 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
|
88 | 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
|
89 | ucx_stack_align(n)) { |
182 | 90 | void *nptr = ucx_stack_malloc(stack, n); |
91 | 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
|
92 | memcpy(nptr, ptr, n); |
182 | 93 | 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
|
94 | |
182 | 95 | return nptr; |
96 | } else { | |
97 | return NULL; | |
14 | 98 | } |
182 | 99 | } 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
|
100 | ((struct ucx_stack_metadata*)ptr - 1)->size = n; |
182 | 101 | return ptr; |
13 | 102 | } |
103 | } | |
104 | } | |
105 | ||
182 | 106 | 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
|
107 | 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
|
108 | stack->top = ((struct ucx_stack_metadata*) stack->top - 1)->prev; |
182 | 109 | } 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
|
110 | 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
|
111 | (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
|
112 | 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
|
113 | ); |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
114 | next->prev = ((struct ucx_stack_metadata*) ptr - 1)->prev; |
113
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 | } |
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
117 | |
185
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
118 | 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
|
119 | 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
|
120 | return; |
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 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
123 | 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
|
124 | 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
|
125 | 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
|
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 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
128 | 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
|
129 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
130 | ucx_stack_free(stack, stack->top); |
13 | 131 | } |
132 | ||
185
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 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
|
134 | 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
|
135 | - (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
|
136 | - 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
|
137 | : 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
|
138 | |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
139 | 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
|
140 | 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
|
141 | } else { |
a48428642b4e
added stack implementation + added g++ config and added some fixes for C++
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
142 | 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
|
143 | } |
13 | 144 | } |