| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2019, 2021 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <lbm_memory.h> | ||
| 19 | #include <lbm_types.h> | ||
| 20 | #include <string.h> | ||
| 21 | |||
| 22 | #include "stack.h" | ||
| 23 | #include "print.h" | ||
| 24 | |||
| 25 | 18228 | int lbm_stack_allocate(lbm_stack_t *s, lbm_uint stack_size) { | |
| 26 | 18228 | s->data = lbm_memory_allocate(stack_size); | |
| 27 | 18228 | s->sp = 0; | |
| 28 | 18228 | s->size = stack_size; | |
| 29 | 18228 | s->max_sp = 0; | |
| 30 | |||
| 31 |
1/2✓ Branch 0 taken 18228 times.
✗ Branch 1 not taken.
|
18228 | if (s->data) return 1; |
| 32 | ✗ | return 0; | |
| 33 | } | ||
| 34 | |||
| 35 | 34888 | int lbm_stack_create(lbm_stack_t *s, lbm_uint* data, lbm_uint size) { | |
| 36 | 34888 | s->data = data; | |
| 37 | 34888 | s->sp = 0; | |
| 38 | 34888 | s->size = size; | |
| 39 | 34888 | s->max_sp = 0; | |
| 40 | 34888 | return 1; | |
| 41 | } | ||
| 42 | |||
| 43 | 18019 | void lbm_stack_free(lbm_stack_t *s) { | |
| 44 |
1/2✓ Branch 0 taken 18019 times.
✗ Branch 1 not taken.
|
18019 | if (s->data) { |
| 45 | 18019 | lbm_memory_free(s->data); | |
| 46 | } | ||
| 47 | 18019 | } | |
| 48 | |||
| 49 | 36719 | void lbm_stack_clear(lbm_stack_t *s) { | |
| 50 | 36719 | s->sp = 0; | |
| 51 | 36719 | } | |
| 52 | |||
| 53 | ✗ | lbm_uint *lbm_get_stack_ptr(lbm_stack_t *s, lbm_uint n) { | |
| 54 | ✗ | if (n > s->sp) return NULL; | |
| 55 | ✗ | lbm_uint index = s->sp - n; | |
| 56 | ✗ | return &s->data[index]; | |
| 57 | } | ||
| 58 | |||
| 59 | 834437435 | int lbm_stack_drop(lbm_stack_t *s, lbm_uint n) { | |
| 60 | |||
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 834437435 times.
|
834437435 | if (n > s->sp) return 0; |
| 62 | |||
| 63 | 834437435 | s->sp -= n; | |
| 64 | 834437435 | return 1; | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | lbm_uint *lbm_stack_reserve(lbm_stack_t *s, lbm_uint n) { | |
| 68 | |||
| 69 | ✗ | if (s->sp + n >= s->size) { | |
| 70 | ✗ | return NULL; | |
| 71 | } | ||
| 72 | ✗ | lbm_uint *ptr = &s->data[s->sp]; | |
| 73 | ✗ | s->sp += n; | |
| 74 | ✗ | return ptr; | |
| 75 | } | ||
| 76 | |||
| 77 | 58185100 | int lbm_push(lbm_stack_t *s, lbm_uint val) { | |
| 78 | 58185100 | int res = 1; | |
| 79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58185100 times.
|
58185100 | if (s->sp == s->size) { |
| 80 | ✗ | return 0; | |
| 81 | } | ||
| 82 | 58185100 | s->data[s->sp++] = val; | |
| 83 |
2/2✓ Branch 0 taken 24865 times.
✓ Branch 1 taken 58160235 times.
|
58185100 | if (s->sp > s->max_sp) s->max_sp = s->sp; |
| 84 | 58185100 | return res; | |
| 85 | } | ||
| 86 | |||
| 87 | 1871737558 | int lbm_pop(lbm_stack_t *s, lbm_uint *val) { | |
| 88 | 1871737558 | s->sp--; | |
| 89 | 1871737558 | *val = s->data[s->sp]; | |
| 90 | 1871737558 | return 1; | |
| 91 | } | ||
| 92 | |||
| 93 | 34086448 | int lbm_pop_2(lbm_stack_t *s, lbm_uint *r0, lbm_uint *r1) { | |
| 94 | 34086448 | s->sp--; | |
| 95 | 34086448 | *r0 = s->data[s->sp--]; | |
| 96 | 34086448 | *r1 = s->data[s->sp]; | |
| 97 | 34086448 | return 1; | |
| 98 | } | ||
| 99 | |||
| 100 | 8796 | int lbm_pop_3(lbm_stack_t *s, lbm_uint *r0, lbm_uint *r1, lbm_uint *r2) { | |
| 101 | 8796 | s->sp--; | |
| 102 | 8796 | *r0 = s->data[s->sp--]; | |
| 103 | 8796 | *r1 = s->data[s->sp--]; | |
| 104 | 8796 | *r2 = s->data[s->sp]; | |
| 105 | 8796 | return 1; | |
| 106 | } | ||
| 107 | |||
| 108 |