-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack.h
More file actions
43 lines (37 loc) · 838 Bytes
/
stack.h
File metadata and controls
43 lines (37 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "definitions.h"
struct Stack
{
int size;
int top;
int* items;
CRITICAL_SECTION lock;
};
inline void stack_init(struct Stack* stack, int size)
{
stack->size = size;
stack->top = -1;
stack->items = (int *)((void* (__cdecl*)(size_t))MLF_MALLOC_CRT)(stack->size * sizeof(int));
InitializeCriticalSection(&stack->lock);
}
inline void stack_push(struct Stack *stack, int value)
{
EnterCriticalSection(&stack->lock);
if (stack->top < stack->size - 1)
stack->items[++stack->top] = value;
LeaveCriticalSection(&stack->lock);
}
inline int stack_pop(struct Stack* stack)
{
int ret;
EnterCriticalSection(&stack->lock);
if (stack->top > -1) {
ret = stack->items[stack->top--];
goto end;
}
ret = stack->size;
end:
LeaveCriticalSection(&stack->lock);
return ret;
}