annotate src/parallel_execution/stack.c @ 118:32773f506410

remove code stack, add continuation of rotateL/totateR
author ikkun
date Mon, 26 Sep 2016 19:54:24 +0900
parents f57e9ffa7960
children 2bb5e4f0fd35
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <string.h>
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include "stack.h"
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 stack_ptr stack_init(size_t size, int max) {
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
5 stack_ptr p;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
7 if ((p = calloc(1, sizeof(stack))) == NULL)
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 return NULL;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
10 if ((p->data = calloc(max, size)) == NULL) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
11 free(p);
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 return NULL;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
15 p->size = size;
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
16 p->max = max;
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
17 p->num = 0;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
19 return p;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
22 stack_ptr stack_realloc(stack_ptr p, int max) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
23 if (p == NULL)
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 return NULL;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
26 if ((p->data = realloc(p->data, p->size*max)) == NULL)
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 return NULL;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
29 p->max = max;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
31 return p;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
34 void stack_free(stack_ptr p) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
35 if (p != NULL && p->data != NULL) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
36 free(p->data);
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
37 free(p);
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
41 int stack_push(stack_ptr p, void* data) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
42 if (p->max <= p->num)
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 return -1;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
45 memcpy((char*)p->data+p->num*p->size, data, p->size);
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
46 p->num++;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 return 0;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
51 int stack_pop(stack_ptr p, void* data) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
52 if (p->num == 0)
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 return -1;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
55 p->num--;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
57 memcpy(data, (char*)p->data+p->num*p->size, p->size);
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 return 0;
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
62 int isMax(const stack_ptr p) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
63 return p->max<=p->num;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 }
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
113
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
66 int isEmpty(const stack_ptr p) {
f57e9ffa7960 add comment rb_tree
ikkun
parents: 87
diff changeset
67 return p->num<=0;
86
e06e1a9e569e create worker
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 }