Skip to content

Commit 0c2aabe

Browse files
committed
Add the sources
0 parents  commit 0c2aabe

File tree

4 files changed

+237
-0
lines changed

4 files changed

+237
-0
lines changed

main.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include "stack.h"
4+
5+
#define ELM_SIZE(arr) ( sizeof(arr[0]) )
6+
#define N_ELEMS(arr) ( sizeof(arr) / ELM_SIZE(arr) )
7+
8+
int main()
9+
{
10+
typedef uint32_t Element_T;
11+
12+
Element_T buf[64];
13+
Stack_T stack = Stack(buf, ELM_SIZE(buf), N_ELEMS(buf));
14+
15+
uint32_t push_vals[] = {10, 15, 19};
16+
for (size_t i =0; i < N_ELEMS(push_vals); ++i)
17+
{
18+
Stack_Push(&stack, &push_vals[i]);
19+
}
20+
21+
uint32_t el;
22+
while (Stack_Pop(&stack, &el))
23+
{
24+
printf("%u\n",el);
25+
}
26+
27+
printf("Done!\n");
28+
return 0;
29+
}

stack.c

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*******************************************************************************
2+
* Stack.c
3+
*
4+
*
5+
*
6+
*
7+
******************************************************************************/
8+
9+
/*******************************************************************************
10+
* Includes
11+
******************************************************************************/
12+
#include "stack.h"
13+
#include "string.h"
14+
#include "stdint.h"
15+
16+
/*******************************************************************************
17+
* Define Macros
18+
******************************************************************************/
19+
#define NULL_PTR NULL
20+
21+
/*******************************************************************************
22+
* Function-Like Macros
23+
******************************************************************************/
24+
25+
/*******************************************************************************
26+
* Type Declarations
27+
******************************************************************************/
28+
29+
/*******************************************************************************
30+
* Object Declarations
31+
******************************************************************************/
32+
33+
/*******************************************************************************
34+
* Functions Declarations
35+
******************************************************************************/
36+
37+
/*******************************************************************************
38+
* Functions Definitions
39+
******************************************************************************/
40+
41+
Stack_T Stack(void * elements, size_t size_element, size_t size_max)
42+
{
43+
Stack_T stack;
44+
45+
if (elements == NULL_PTR)
46+
{
47+
size_max = 0;
48+
}
49+
50+
stack.elements = elements;
51+
stack.size_element = size_element;
52+
stack.size_max = size_max;
53+
stack.len_stack = 0;
54+
return stack;
55+
}
56+
57+
bool Stack_Push(Stack_T * stack, void *element)
58+
{
59+
if (stack == NULL_PTR || element == NULL_PTR)
60+
{
61+
return false;
62+
}
63+
else if (stack->len_stack >= stack->size_max)
64+
{
65+
return false;
66+
}
67+
else
68+
{
69+
size_t dest_byte_offset = stack->len_stack * stack->size_element;
70+
uint8_t * target = (uint8_t *) stack->elements + dest_byte_offset;
71+
72+
memcpy(target, (uint8_t *) element, stack->size_element);
73+
++(stack->len_stack);
74+
75+
return true;
76+
}
77+
}
78+
79+
80+
bool Stack_Pop(Stack_T * stack, void * element)
81+
{
82+
if (stack == NULL_PTR || element == NULL_PTR)
83+
{
84+
return false;
85+
}
86+
else if (!stack->len_stack)
87+
{
88+
return false;
89+
}
90+
else
91+
{
92+
size_t source_byte_offset = (stack->len_stack - 1)* stack->size_element;
93+
uint8_t * source = (uint8_t *) stack->elements + source_byte_offset;
94+
95+
memcpy(element, source, stack->size_element);
96+
--(stack->len_stack);
97+
return true;
98+
}
99+
}
100+
101+
102+
size_t Stack_Len(Stack_T * stack)
103+
{
104+
if (stack == NULL_PTR)
105+
{
106+
return 0;
107+
}
108+
return stack->len_stack;
109+
}
110+
111+
/*******************************************************************************
112+
* End Stack.c
113+
******************************************************************************/
114+
115+

stack.h

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************************************************
2+
* Stack.h
3+
*
4+
*
5+
*
6+
*
7+
******************************************************************************/
8+
#pragma once
9+
10+
/*******************************************************************************
11+
* Includes
12+
******************************************************************************/
13+
#include <stddef.h>
14+
#include <stdbool.h>
15+
16+
/*******************************************************************************
17+
* Define Macros
18+
******************************************************************************/
19+
20+
/*******************************************************************************
21+
* Function-Like Macros
22+
******************************************************************************/
23+
24+
/*******************************************************************************
25+
* Type Declarations
26+
******************************************************************************/
27+
typedef struct Stack_Tag
28+
{
29+
void * elements;
30+
size_t size_element;
31+
size_t size_max;
32+
size_t len_stack;
33+
34+
} Stack_T;
35+
36+
/*******************************************************************************
37+
* Object Declarations
38+
******************************************************************************/
39+
40+
/*******************************************************************************
41+
* Functions Declarations
42+
******************************************************************************/
43+
/**
44+
* @brief Stack create a new stack using memory passed as elements as a container
45+
* @param elements: pointer to the memory that will contain stack elements
46+
* @param size_element: size of a single stack element
47+
* @param size_max: the maximum amount of elements that can fit into elements pointer
48+
* @return stack instance
49+
*/
50+
Stack_T Stack(void * elements, size_t size_element, size_t size_max);
51+
52+
/**
53+
* @brief push push an element on the stack
54+
* @param stack: stack instance
55+
* @param element: element to push on the stack
56+
* @return false on overflow
57+
*/
58+
bool Stack_Push(Stack_T * stack, void * element);
59+
60+
/**
61+
* @brief Stack_Pop pop an alement from a stack
62+
* @param stack: stack instance
63+
* @param element: if ok, a popped element will go here
64+
* @return false on underflow
65+
*/
66+
bool Stack_Pop(Stack_T * stack, void * element);
67+
68+
/**
69+
* @brief Stack_Len return current stack length
70+
* @param stack: stack instance
71+
* @return stack length
72+
*/
73+
size_t Stack_Len(Stack_T* stack);
74+
75+
76+
/*******************************************************************************
77+
* Functions Definitions
78+
******************************************************************************/
79+
80+
/*******************************************************************************
81+
* End Stack.h
82+
******************************************************************************/

stack.pro

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TEMPLATE = app
2+
CONFIG += console
3+
CONFIG -= app_bundle
4+
CONFIG -= qt
5+
6+
SOURCES += \
7+
main.c \
8+
stack.c
9+
10+
HEADERS += \
11+
stack.h

0 commit comments

Comments
 (0)