forked from TMaxElectronics/MidiStick_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLL.c
130 lines (107 loc) · 3.82 KB
/
DLL.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright (C) 2020,2021 TMax-Electronics.de
This file is part of the MidiStick Firmware.
the MidiStick Firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
the MidiStick Firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the MidiStick Firmware. If not, see <https://www.gnu.org/licenses/>.
*/
#include <xc.h>
#include <stdint.h>
#include "DLL.h"
#include "ADSREngine.h"
typedef struct{
uint32_t nextUID;
} HeadData;
DLLObject * createNewDll(){
DLLObject * head = malloc(sizeof(DLLObject));
HeadData * headData = malloc(sizeof(HeadData));
headData->nextUID = 0;
head->uid = 0;
head->data = (void*) headData;
head->next = head;
head->prev = head;
return head;
}
void DLL_clear(DLLObject * listHead){
DLLObject * currObject = listHead->next;
while(currObject != listHead){
currObject = currObject->next;
DLL_remove(currObject->prev);
}
}
uint32_t DLL_add(void * data, DLLObject * listHead){
//allocate memory for new object
DLLObject * newObject = malloc(sizeof(DLLObject));
//relink objects
listHead->prev->next = newObject;
newObject->prev = listHead->prev;
listHead->prev = newObject;
newObject->next = listHead;
//set new object's data
newObject->uid = ((HeadData*) listHead->data)->nextUID++;
newObject->data = data;
return newObject->uid;
}
void * DLL_find(void * dataPtr, DLLObject * listHead){
DLLObject * currObject = listHead->next;
while(currObject != listHead){
if(currObject->data == dataPtr) return currObject->data; //target object found
currObject = currObject->next;
}
return 0; //object not found
}
void * DLL_get(uint16_t index, DLLObject * listHead){
DLLObject * currObject = listHead->next;
if(currObject == listHead) return 0;
uint16_t currIndex = 0;
for(; currIndex < index; currIndex ++){
currObject = listHead->next;
if(currObject == listHead) return 0;
}
return currObject->data;
}
unsigned DLL_isEmpty(DLLObject * listHead){
return listHead->next == listHead;
}
/*void removeListEntry(void * targetDataPtr, DLLObject * listHead){
DLLObject * currObject = listHead->next;
while(currObject->data != targetDataPtr){
currObject = currObject.next;
if(currObject == listHead) return;
}
removeListEntry(currObject);
}*/
void DLL_remove(DLLObject * target){
//UART_print("removed 0x%08x from a DLL\r\n", target);
target->prev->next = target->next;
target->next->prev = target->prev;
free(target);
}
void DLL_pop(DLLObject * listHead){
DLL_remove(listHead->next);
}
void DLL_moveToEnd(DLLObject * currObject, DLLObject * listHead){
currObject->prev->next = currObject->next;
currObject->next->prev = currObject->prev;
listHead->prev->next = currObject;
currObject->prev = listHead->prev;
listHead->prev = currObject;
currObject->next = listHead;
}
void DLL_dump(DLLObject * listHead){
UART_sendString("\r\nDLL Dump:", 1);
UART_print("Head (%d) : 0x%08x (data: 0x%08x)\r\n", listHead->uid, listHead, listHead->data);
DLLObject * currObject = listHead->next;
while(currObject != listHead){
UART_print("%d: 0x%08x (data: 0x%08x)\r\n", currObject->uid, currObject, currObject->data);
currObject = currObject->next;
}
UART_sendString("-------------\r\n", 1);
}