-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmalloc.c
232 lines (186 loc) · 6.03 KB
/
mmalloc.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//=============================================================================
// mmaloc.c
// Simple managed memory allocation routines
// nyacom (C) 2020.10
//=============================================================================
#include "mmalloc.h"
#include "ficlib2.h"
mmallocInfo info;
//=============================================================================
int mmallocInit(const char *path, off_t offset, size_t size) {
int mem_fd = open(path, O_RDWR|O_SYNC|O_DIRECT);
if (mem_fd < 0) {
fprintf(stderr, "[ERROR]: can't open %s errno=%d\n", path, errno);
return mem_fd;
}
info.mmap_fd = mem_fd;
info.offset = offset;
info.area_size = size;
info.ent = (mmallocEnt *)malloc(sizeof(mmallocEnt));
info.ent->next = NULL;
info.ent->prev = NULL;
return 0;
}
//=============================================================================
void mmsync() {
mmallocEnt *ent = info.ent->next;
while (ent) {
printf("msync %p %d\n", ent->ptr, ent->p_size);
msync(ent->ptr, ent->p_size, MS_SYNC|MS_INVALIDATE);
ent = ent->next;
}
}
//=============================================================================
void mmread(void *ptr) {
mmallocEnt *ent = info.ent->next;
while (ent) {
if (ent->ptr == ptr) {
int fd = fic_gpio_open();
fic_comm_reset();
fic_hls_ddr_read(ent->ptr, ent->p_size, ent->p_addr);
fic_gpio_close(fd);
break;
}
ent = ent->next;
}
}
//=============================================================================
void *mmalloc(size_t size) {
// off_t free_offset = info.offset;
off_t free_offset = 0;
off_t free_size = (size / MMALLOC_BLOCK_SIZE + 1) * MMALLOC_BLOCK_SIZE;
// Find allocatable area
mmallocEnt *ent_prev = info.ent;
mmallocEnt *ent = info.ent->next;
while (ent) {
off_t st_addr = ent->p_addr;
off_t ed_addr = st_addr + ent->p_size;
if (free_offset >= st_addr) {
free_offset = ed_addr + MMALLOC_BLOCK_SIZE;
} else if (free_offset+free_size < st_addr) {
break;
}
ent_prev = ent;
ent = ent->next;
}
if ((free_offset + free_size) > (info.offset + info.area_size)) {
fprintf(stderr, "[ERROR]: can't allocate area \n");
return NULL; // Cant find area
}
// Create new memory entry
mmallocEnt *new_ent = malloc(sizeof(mmallocEnt));
if (new_ent == NULL) {
fprintf(stderr, "[ERROR]: malloc failed\n");
return NULL; // Cant find area
}
// printf("DEBUG: addr=%lu size=%lu\n", free_offset, free_size);
// MMap a file
void *ptr = NULL;
ptr = mmap(
NULL, // Any adddress in our space will do
free_size, // Map length
PROT_READ | PROT_WRITE, // Enable reading & writting to mapped memory
MAP_SHARED, // shared with other processes
// MAP_PRIVATE, // shared with other processes
info.mmap_fd, // File to map
free_offset // Offset to GPIO peripheral
);
if ((int)ptr < 0) {
printf("[ERROR]: mmap errno=%d\n", errno);
return NULL;
}
// printf("DEBUG: addr=%lu size=%lu ptr=%x\n", free_offset, free_size, ptr);
new_ent->p_addr = free_offset;
new_ent->p_size = free_size;
new_ent->ptr = ptr;
new_ent->prev = NULL;
new_ent->next = NULL;
// Add to list
if (ent) {
// Insert
new_ent->prev = ent->prev;
new_ent->next = ent;
if (ent->prev) {
ent->prev->next = new_ent;
}
ent->prev = new_ent;
} else {
// Append
ent_prev->next = new_ent;
new_ent->prev = ent_prev;
}
return ptr;
}
//=============================================================================
void mfree(void *ptr) {
// Find ptr area
mmallocEnt *ent_prev = info.ent;
mmallocEnt *ent = info.ent->next;
while (ent) {
if (ent->ptr == ptr) {
munmap(ent->ptr, ent->p_size);
if (ent->prev) {
ent->prev->next = ent->next;
}
if (ent->next) {
ent->next->prev = ent->prev;
}
free(ent);
return;
}
ent = ent->next;
}
return;
}
//=============================================================================
unsigned long mget_phy_addr(void *ptr) {
mmallocEnt *ent = info.ent->next;
while (ent) {
if (ent->ptr == ptr) {
return ent->p_addr;
}
ent = ent->next;
}
return 0;
}
//=============================================================================
void minfo() {
mmallocEnt *ent = info.ent->next;
printf("\n");
printf(" mmalloc Managed memory allocation information\n");
printf(" Address Size Pointer\n");
printf("--------------------------------------------------------------\n");
while (ent) {
printf(" %08llx %8u", ent->p_addr, ent->p_size);
printf(" %p\n",ent->ptr);
ent = ent->next;
}
printf("\n\n");
}
//=============================================================================
// Tests
//=============================================================================
/*
int main() {
printf("mmalloc test\n");
// Init
mmallocInit("./mmalloc.bin", 0x0, 1024*1024);
//mmallocInit("ficmem/mem", 0x0, 0xffffffff);
void *p1 = mmalloc(4096);
minfo();
*(uint32_t *)p1 = 0x12341234;
printf("Virtual:%016lx Physical:%016lx\n", p1, mget_phy_addr(p1));
void *p2 = mmalloc(8192);
minfo();
*(uint64_t *)p2 = 0xffffaaaabbbbcccc;
printf("Virtual:%016lx Physical:%016lx\n", p2, mget_phy_addr(p2));
void *p3 = mmalloc(8192);
minfo();
printf("Virtual:%016lx Physical:%016lx\n", p3, mget_phy_addr(p3));
mfree(p2);
minfo();
void *p4 = mmalloc(8192);
minfo();
printf("Virtual:%08lx Physical:%08lx\n", p4, mget_phy_addr(p4));
}
*/