Skip to content

Commit 045685d

Browse files
authored
Merge pull request #4 from SidoShiro/command-queue
Command queue
2 parents 75fa5f8 + acd3130 commit 045685d

4 files changed

Lines changed: 119 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(DistributedMalloc)
88
find_package(MPI REQUIRED)
99

1010
# Vars with files for compilation
11-
set(SRCS src/main/main.c src/utils/utils.c src/cli/cli.c src/network/message.c src/network/block.c include/block.h)
11+
set(SRCS src/main/main.c src/utils/utils.c src/cli/cli.c src/network/message.c src/network/block.c include/block.h src/utils/command_queue.c)
1212
include_directories("include/")
1313
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
1414

doc/user_commands.dot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
digraph User_Command_Queue {
2+
3+
Command_1 [shape=box]
4+
Command_2 [shape=box]
5+
Command_3 [shape=box]
6+
Leader [shape=diamond]
7+
8+
Command_1 -> Command_2 -> Command_3;
9+
User -> Command_1;
10+
Command_3 -> Leader;
11+
}

include/command_queue.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef DISTRIBUTEDMALLOC_COMMAND_QUEUE_H
2+
#define DISTRIBUTEDMALLOC_COMMAND_QUEUE_H
3+
4+
#include <stdlib.h>
5+
6+
enum user_command {
7+
MALLOC, // Data => Size
8+
FREE, // Data => Address
9+
WRITE, // Data => Address; Data size; Data
10+
READ, // Data => Address; Data size
11+
DUMP, // Data => Address
12+
SNAP,
13+
DNET,
14+
KILL, // Data => id
15+
REVIVE, // Data => id
16+
NONE
17+
};
18+
19+
struct command_queue {
20+
enum user_command command;
21+
struct command_queue *next;
22+
void *data; // Contain the structure depending of the command
23+
};
24+
25+
// Delete first command queue, return head
26+
struct command_queue *pop_command(struct command_queue *head);
27+
28+
// Push command in queue, return head, can have head=NULL for init queue
29+
struct command_queue *push_command(struct command_queue *head, struct command_queue *new_elt);
30+
31+
// Get command queue data, you should known how to cast it
32+
void *peek_command(struct command_queue *head);
33+
34+
// Get user command enum of the first of the queue
35+
enum user_command peek_user_command(struct command_queue *head);
36+
37+
struct data_write {
38+
size_t address;
39+
size_t size;
40+
void *data;
41+
};
42+
43+
struct data_read {
44+
size_t address;
45+
size_t size;
46+
};
47+
48+
struct data_id {
49+
unsigned short id;
50+
};
51+
52+
struct data_address {
53+
size_t address;
54+
};
55+
56+
struct data_size {
57+
size_t size;
58+
};
59+
60+
#endif /* !DISTRIBUTEDMALLOC_COMMAND_QUEUE_H */

src/utils/command_queue.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "command_queue.h"
2+
3+
// Delete first command queue, return head
4+
struct command_queue *pop_command(struct command_queue *head) {
5+
struct command_queue *new_head = NULL;
6+
if (!head)
7+
return NULL;
8+
else if (!head->next) {
9+
if (head->data)
10+
free(head->data);
11+
free(head);
12+
return NULL;
13+
} else {
14+
new_head = head->next;
15+
if (head->data)
16+
free(head->data);
17+
free(head);
18+
return new_head;
19+
}
20+
}
21+
22+
// Push command in queue, return head, can have head=NULL for init queue
23+
struct command_queue *push_command(struct command_queue *head, struct command_queue *new_elt) {
24+
if (!head) {
25+
return new_elt;
26+
}
27+
struct command_queue *iter = head;
28+
while (iter->next) {
29+
iter = iter->next;
30+
}
31+
iter->next = new_elt;
32+
return head;
33+
}
34+
35+
// Get command queue data, you should known how to cast it
36+
void *peek_command(struct command_queue *head) {
37+
if (head)
38+
return head->data;
39+
return NULL;
40+
}
41+
42+
// Get user command enum of the first of the queue
43+
enum user_command peek_user_command(struct command_queue *head) {
44+
if (head)
45+
return head->command;
46+
return NONE;
47+
}

0 commit comments

Comments
 (0)