universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: ucx: /home/mike/workspace/c/ucx/src/ucx/stack.h Source File universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
ucx universe@390:
universe@390:
UAP Common Extensions
universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390:
universe@390: universe@390:
universe@390: universe@390: universe@390:
universe@390:
universe@390:
universe@390:
stack.h
universe@390:
universe@390:
universe@390: Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 Mike Becker, 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 
38 #ifndef UCX_STACK_H
39 #define UCX_STACK_H
40 
41 #include "ucx.h"
42 #include "allocator.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
52 typedef struct {
55 
57  size_t size;
58 
60  char *space;
61 
63  char *top;
64 } UcxStack;
65 
73  char *prev;
74 
76  size_t size;
77 };
78 
87 void ucx_stack_init(UcxStack *stack, char* space, size_t size);
88 
98 void *ucx_stack_malloc(UcxStack *stack, size_t n);
99 
110 void *ucx_stack_push(UcxStack *stack, size_t n, const void *data);
111 
123 void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
124 
136 void *ucx_stack_pusharr(UcxStack *stack,
137  size_t nelem, size_t elsize, const void *data);
138 
150 void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
151 
164 void ucx_stack_free(UcxStack *stack, void *ptr);
165 
166 
172 #define ucx_stack_topsize(stack) ((stack)->top ? ((struct ucx_stack_metadata*)\
173  (stack)->top - 1)->size : 0)
174 
188 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (size_t)-1)
189 
203 void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
204 
211 size_t ucx_stack_avail(UcxStack *stack);
212 
219 #define ucx_stack_empty(stack) (!(stack)->top)
220 
231 #define ucx_stack_dim(size, elems) (size+sizeof(struct ucx_stack_metadata) * \
232  (elems + 1))
233 
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif /* UCX_STACK_H */
240 
size_t size
Stack size.
Definition: stack.h:57
universe@390:
UCX stack structure.
Definition: stack.h:52
universe@390:
void ucx_stack_free(UcxStack *stack, void *ptr)
Frees memory on the stack.
Definition: stack.c:106
universe@390:
Main UCX Header providing most common definitions.
universe@390:
void * ucx_stack_malloc(UcxStack *stack, size_t n)
Allocates stack memory.
Definition: stack.c:53
universe@390:
void * ucx_stack_push(UcxStack *stack, size_t n, const void *data)
Allocates memory with ucx_stack_malloc() and copies the specified data if the allocation was successf...
Definition: stack.c:148
universe@390:
Metadata for each UCX stack element.
Definition: stack.h:69
universe@390:
void * ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n)
Reallocates memory on the stack.
Definition: stack.c:79
universe@390:
char * prev
Location of the previous element (NULL if this is the first)
Definition: stack.h:73
universe@390:
size_t ucx_stack_avail(UcxStack *stack)
Returns the remaining available memory on the specified stack.
Definition: stack.c:135
universe@390:
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
universe@390:
char * space
Pointer to the bottom of the stack.
Definition: stack.h:60
universe@390:
void ucx_stack_popn(UcxStack *stack, void *dest, size_t n)
Removes the top most element from the stack and copies the content to dest.
Definition: stack.c:118
universe@390:
Allocator for custom memory management.
universe@390:
void ucx_stack_init(UcxStack *stack, char *space, size_t size)
Initializes UcxStack structure with memory.
Definition: stack.c:41
universe@390:
size_t size
Size of this element.
Definition: stack.h:76
universe@390:
void * ucx_stack_pusharr(UcxStack *stack, size_t nelem, size_t elsize, const void *data)
Allocates memory with ucx_stack_calloc() and copies the specified data if the allocation was successf...
Definition: stack.c:156
universe@390:
void * ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize)
Allocates an array of stack memory.
Definition: stack.c:73
universe@390:
char * top
Pointer to the top of the stack.
Definition: stack.h:63
universe@390:
UcxAllocator allocator
UcxAllocator based on this stack.
Definition: stack.h:54
universe@390:
universe@390: universe@390: universe@390: universe@390: