-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathmemchunk.c
More file actions
127 lines (104 loc) · 3.15 KB
/
memchunk.c
File metadata and controls
127 lines (104 loc) · 3.15 KB
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
/* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "memchunk.h"
#include <stdlib.h>
struct chunk
{
uint32_t start;
uint32_t length;
struct chunk *next;
};
struct chunk *free_list = (void *)0;
struct chunk *used = (void *)0;
uint32_t max_free = 0;
// Add a chunk to a list
static void chunk_add(uint32_t start, uint32_t length, struct chunk **list)
{
struct chunk *c = (struct chunk *)malloc(sizeof(struct chunk));
c->start = start;
c->length = length;
c->next = *list;
*list = c;
}
// Return 1 if the chunk overlaps an entry in a list, otherwise 0
static int chunk_overlaps(uint32_t start, uint32_t length, struct chunk *list)
{
while(list)
{
uint32_t l_start = list->start;
uint32_t l_end = l_start + list->length;
uint32_t end = start + length;
if((start < l_end) && (end > l_start))
return 1;
list = list->next;
}
return 0;
}
// Return 1 if the chunk is wholly contained within an entry in the list, otherwise 0
static int chunk_contains(uint32_t start, uint32_t length, struct chunk *list)
{
while(list)
{
uint32_t l_start = list->start;
uint32_t l_end = l_start + list->length;
uint32_t end = start + length;
if((start >= l_start) && (end <= l_end))
return 1;
list = list->next;
}
return 0;
}
// Return 1 if can allocate, otherwise 0
static int chunk_can_allocate(uint32_t start, uint32_t length)
{
if(!chunk_contains(start, length, free_list))
return 0;
if(chunk_overlaps(start, length, used))
return 0;
return 1;
}
void chunk_register_free(uint32_t start, uint32_t length)
{
chunk_add(start, length, &free_list);
if((start + length) > max_free)
max_free = start + length;
}
uint32_t chunk_get_any_chunk(uint32_t length)
{
uint32_t test_address = 0;
while(test_address < max_free)
{
if(chunk_can_allocate(test_address, length))
{
chunk_add(test_address, length, &used);
return test_address;
}
test_address += 0x1000; // Returned page aligned chunks
}
return 0;
}
uint32_t chunk_get_chunk(uint32_t start, uint32_t length)
{
if(chunk_can_allocate(start, length))
{
chunk_add(start, length, &used);
return start;
}
return 0;
}