-
Notifications
You must be signed in to change notification settings - Fork 0
/
datstr.c
74 lines (72 loc) · 1.83 KB
/
datstr.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
#include "datstr.h"
struct User* createUser(char *username,int uid,double initialCredits)
{
struct User* newUser =(struct User *) calloc(1,sizeof(struct User));
newUser->username = username;
newUser->uid = uid;
newUser->remainingCredits = initialCredits;
newUser->userMutex = (pthread_mutex_t*)calloc(1,sizeof(pthread_mutex_t));
pthread_mutex_init(newUser->userMutex,NULL);
return newUser;
}
void freeUsers(struct User **array, int size){
int i = 0;
struct Node *ptr, *temp;
if(array!=NULL){
for(i = 0 ; i<size ; i++){
if(array[i]!=NULL){
if(array[i]->username!=NULL){
free(array[i]->username);
}
pthread_mutex_destroy(array[i]->userMutex);
free(array[i]->userMutex);
if(array[i]->success!=NULL){
ptr = array[i]->success->next;
while(ptr!=array[i]->success){
if (ptr->data!=NULL){
if(((struct Order*)(ptr->data))->bookTitle!=NULL){
free(((struct Order*)ptr->data)->bookTitle);
}
free(ptr->data);
}
temp=ptr;
ptr=ptr->next;
free(temp);
}
if (ptr->data!=NULL){
if(((struct Order*)(ptr->data))->bookTitle!=NULL){
free(((struct Order*)ptr->data)->bookTitle);
}
free(ptr->data);
}
free(ptr);
}
if(array[i]->fail!=NULL){
ptr = array[i]->fail->next;
while(ptr!=array[i]->fail){
if (ptr->data!=NULL){
if(((struct Order*)ptr->data)->bookTitle!=NULL){
free(((struct Order*)ptr->data)->bookTitle);
}
free(ptr->data);
}
temp=ptr;
ptr=ptr->next;
free(temp);
}
if (ptr->data!=NULL){
if(((struct Order*)ptr->data)->bookTitle!=NULL){
free(((struct Order*)ptr->data)->bookTitle);
}
free(ptr->data);
}
free(ptr);
}
free(array[i]);
}
}
free(array);
}
}