3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #define UMEM_HEAP_ADDR 0x800000 // MEMORY HEAP is placed at 8MB address
29 #define UMEM_MALLOC_MAGIC 0xebc9 // MALLOC STRUCTURE signature
31 #define UMEM_MALLOC_STATE_FREE 0xaa // MEMORY is FREE at moment
32 #define UMEM_MALLOC_STATE_USED 0xcc // MEMORY is USED at moment
39 } __attribute__ ((__packed__
)) malloc_t
;
42 void *addr
; /* heap structure is covered by own heap structure address */
43 void *first
; /* first malloc structure */
44 void *last
; /* last malloc structure - usefully for calculate memory usage */
45 void *curr
; /* current malloc structure */
46 } __attribute__ ((__packed__
)) heap_t
;
48 static heap_t
*heap
; /* heap structure of memory */
49 static MUTEX_CREATE (mutex_umalloc
);
50 static MUTEX_CREATE (mutex_ufree
);
52 void *umalloc (proc_t
*proc
, size_t size
)
57 mutex_lock (&mutex_umalloc
);
59 unsigned malloc_len
= sizeof (malloc_t
); /* check malloc structure size */
60 unsigned need_alloc
= 0;
62 /* let's find new or free malloc structure in memory */
63 malloc_t
*malloc
= (malloc_t
*) heap
->first
;
66 //DPRINT (DBG_MM | DBG_UMEM, "malloc: 0x%x\n", malloc);
69 DPRINT (DBG_MM
| DBG_UMEM
, "!malloc");
74 if (malloc
->magic
!= UMEM_MALLOC_MAGIC
) {
75 DPRINT (DBG_MM
| DBG_UMEM
, "malloc->magic != UMEM_MALLOC_MAGIC");
80 if (malloc
->state
== UMEM_MALLOC_STATE_USED
) { /* It's OK */
81 DPRINT (DBG_MM
| DBG_UMEM
, "malloc->state == UMEM_MALLOC_STATE_USED");
83 malloc
= (malloc_t
*) malloc
->next
;
87 /* we've found FREE memory block, at moment it is not used */
88 if (malloc
->state
== UMEM_MALLOC_STATE_FREE
) {
89 /* read next malloc structure, right behind free block */
90 malloc_t
*next
= (malloc_t
*) malloc
;
92 DPRINT (DBG_MM
| DBG_UMEM
, "malloc->state == UMEM_MALLOC_STATE_FREE");
95 next
= (malloc_t
*) next
->next
;
97 if (next
->magic
!= UMEM_MALLOC_MAGIC
) {
98 DPRINT (DBG_MM
| DBG_UMEM
, "2: next->magic != UMEM_MALLOC_MAGIC");
102 if ((unsigned) ((void *) malloc
+ malloc_len
+ size
) <= (unsigned) next
) {
103 //kprintf ("Je mensi\n");
106 if (next
->state
== UMEM_MALLOC_STATE_USED
)
109 if (next
->state
== UMEM_MALLOC_STATE_USED
)
112 if (next
->state
!= UMEM_MALLOC_STATE_FREE
)
119 unsigned diff = (unsigned) ((void *) next - (void *) malloc);
121 DPRINT (DBG_MM | DBG_UMEM, "diff: %d", diff);
124 malloc = (malloc_t *) malloc->next;
131 malloc
= (malloc_t
*) malloc
->next
;
135 /* when old malloc structures are full, let's grab new block of memory */
138 malloc
->magic
= UMEM_MALLOC_MAGIC
;
140 malloc
->next
= (void *) ((void *) malloc
+ malloc_len
+ size
);
142 heap
->curr
= malloc
->next
;
145 malloc
->state
= UMEM_MALLOC_STATE_USED
;
149 /* calculate size of process image (binary file) */
150 unsigned p
= (unsigned) palign ((void *) (proc
->end
- proc
->start
));
152 /* save last malloc structure pointer to heap */
153 if ((unsigned) heap
->curr
> (unsigned) heap
->last
)
154 heap
->last
= heap
->curr
;
157 unsigned s
= proc
->heap
;
158 for (l
= s
; l
< (unsigned) ((char *) malloc
+ malloc_len
+ size
); l
+= 0x1000) {
160 page_mmap (proc
->task
->page_cover
, (void *) proc
->heap
+p
, (void *) proc
->heap
+p
+ 0x1000, 0, 1);
162 //kprintf ("OD: 0x%x DO: 0x%x | 0x%x | 0x%x\n", (void *) proc->heap + p, (void *) proc->heap + 0x1000 + p, s, size);
163 proc
->heap
+= 0x1000;
166 mutex_unlock (&mutex_umalloc
);
168 return (void *) ((void *) malloc
+ malloc_len
+ p
);
171 void ufree (proc_t
*proc
, void *ptr
)
176 mutex_lock (&mutex_ufree
);
178 /* calculate size of process image (binary file) */
179 unsigned p
= (unsigned) palign ((void *) (proc
->end
- proc
->start
));
181 /* we need find malloc structure - it could be risk working with lower memory address */
182 malloc_t
*malloc
= (void *) ((void *) ptr
- sizeof (malloc_t
) - p
);
184 /* check malloc signature, when not agree, ptr pointing to wrong memory address */
185 if (malloc
->magic
!= UMEM_MALLOC_MAGIC
) {
186 kprintf ("free: malloc->magic != UMEM_MALLOC_MAGIC\n");
190 /* is this memory block our ? */
191 if (malloc
->proc
!= proc
) {
192 kprintf ("free: malloc->proc != proc\n");
196 if (malloc
->state
== UMEM_MALLOC_STATE_FREE
) {
197 kprintf ("kfree () - memory is FREE - double free !\n");
201 if (malloc
->state
!= UMEM_MALLOC_STATE_USED
) {
202 kprintf ("malloc->state != UMEM_MALLOC_STATE_USED\n");
206 /* match malloc as FREE */
207 malloc
->state
= UMEM_MALLOC_STATE_FREE
;
210 mutex_unlock (&mutex_ufree
);
213 void *urealloc (proc_t
*proc
, void *ptr
, size_t size
)
219 return umalloc (proc
, size
);
226 /* calculate size of process image (binary file) */
227 unsigned p
= (unsigned) palign ((void *) (proc
->end
- proc
->start
));
229 /* we need find malloc structure - it could be risk working with lower memory address */
230 malloc_t
*malloc
= (void *) ((char *) ptr
- sizeof (malloc_t
) - p
);
232 /* check malloc signature, when not agree, ptr pointing to wrong memory address */
233 if (malloc
->magic
!= UMEM_MALLOC_MAGIC
) {
234 kprintf ("urealloc: malloc->magic != UMEM_MALLOC_MAGIC\n");
238 int s
= (int) (malloc
->next
- ptr
) + p
;
239 printf ("s: %d : %d\n", s
, size
);
243 char *ptr_new
= umalloc (proc
, size
);
244 printf ("s: ptr_new: 0x%x\n", ptr_new
);
251 /* copy old memory block to new one (-p because ptr and ptr_new got virtual mapped address) */
252 memcpy (ptr_new
- p
, ptr
- p
, s
);
259 /* free all memory blocks allocated by process */
260 unsigned ufree_proc (proc_t
*proc
)
268 for (malloc
= (malloc_t
*) heap
->first
; malloc
->magic
== UMEM_MALLOC_MAGIC
; malloc
= (malloc_t
*) malloc
->next
)
269 if (malloc
->proc
== proc
)
270 if (malloc
->state
== UMEM_MALLOC_STATE_USED
)
271 malloc
->state
= UMEM_MALLOC_STATE_FREE
;
278 /* calculate used memory by process */
279 unsigned umem_used_proc (proc_t
*proc
)
287 for (malloc
= (malloc_t
*) heap
->first
; malloc
->magic
== UMEM_MALLOC_MAGIC
; malloc
= (malloc_t
*) malloc
->next
)
288 if (malloc
->proc
== proc
)
289 if (malloc
->state
== UMEM_MALLOC_STATE_USED
)
290 used
+= (int) ((unsigned) malloc
->next
- (unsigned) malloc
);
297 /* calculate used memory from all apps */
298 unsigned umem_used ()
306 for (malloc
= (malloc_t
*) heap
->first
; malloc
->magic
== UMEM_MALLOC_MAGIC
; malloc
= (malloc_t
*) malloc
->next
)
307 if (malloc
->state
== UMEM_MALLOC_STATE_USED
)
308 used
+= (int) ((unsigned) malloc
->next
- (unsigned) malloc
);
315 unsigned umem_init ()
317 heap
= (heap_t
*) UMEM_HEAP_ADDR
;
319 /* we rather clear heap structure */
320 heap
->addr
= (void *) heap
;
321 heap
->first
= (void *) ((void *) heap
+ sizeof (heap_t
)); /* first malloc structure should be right behind heap structure */
322 heap
->last
= heap
->first
; /* last malloc structure - it is first at init */
323 heap
->curr
= heap
->first
; /* point to first malloc structure - well, it is clear and not used yet */