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