Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish step10 : add memory management
- Loading branch information
Showing
11 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "defines.h" | ||
#include "kozos.h" | ||
#include "lib.h" | ||
#include "memory.h" | ||
|
||
typedef struct _kzmem_block { | ||
struct _kzmem_block *next; | ||
int size; | ||
} kzmem_block; | ||
|
||
typedef struct _kzmem_pool { | ||
int size; | ||
int num; | ||
kzmem_block *free; | ||
} kzmem_pool; | ||
|
||
static kzmem_pool pool[] = { | ||
{16, 8, NULL}, | ||
{32, 8, NULL}, | ||
{64, 4, NULL}, | ||
}; | ||
|
||
#define MEMORY_AREA_NUM (sizeof(pool) / sizeof(*pool)) | ||
|
||
static int kzmem_init_pool(kzmem_pool *p) | ||
{ | ||
int i; | ||
kzmem_block *mp; | ||
kzmem_block **mpp; | ||
extern char freearea; | ||
static char *area = &freearea; | ||
|
||
mp = (kzmem_block *)area; | ||
|
||
mpp = &p->free; | ||
for (i = 0; i < p->num; i++) { | ||
*mpp = mp; | ||
memset(mp, 0, sizeof(*mp)); | ||
mp->size = p->size; | ||
mpp = &(mp->next); | ||
mp = (kzmem_block *)((char *)mp + p->size); | ||
area += p->size; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int kzmem_init(void) | ||
{ | ||
int i; | ||
for (i = 0; i < MEMORY_AREA_NUM; i++) { | ||
kzmem_init_pool(&pool[i]); | ||
} | ||
return 0; | ||
} | ||
|
||
void *kzmem_alloc(int size) | ||
{ | ||
kzmem_block *mp; | ||
kzmem_pool *p; | ||
int i; | ||
|
||
for (i = 0; i < MEMORY_AREA_NUM; i++) { | ||
p = &pool[i]; | ||
if (size <= p->size - sizeof(kzmem_block)) { | ||
if (p->free == NULL) { | ||
kz_sysdown(); | ||
return NULL; | ||
} | ||
mp = p->free; | ||
p->free = p->free->next; | ||
mp->next = NULL; | ||
return mp + 1; | ||
} | ||
} | ||
kz_sysdown(); | ||
return NULL; | ||
} | ||
|
||
void kzmem_free(void *mem) | ||
{ | ||
kzmem_block *mp; | ||
kzmem_pool *p; | ||
int i; | ||
|
||
mp = ((kzmem_block *)mem -1); | ||
|
||
for (i = 0; i < MEMORY_AREA_NUM; i++) { | ||
p = &pool[i]; | ||
if (mp->size == p->size) { | ||
mp->next = p->free; | ||
p->free = mp; | ||
return; | ||
} | ||
} | ||
kz_sysdown(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef _KOZOS_MEMORY_H_INCLUDED_ | ||
#define _KOZOS_MEMORY_H_INCLUDED_ | ||
|
||
int kzmem_init(void); | ||
void *kzmem_alloc(int size); | ||
void kzmem_free(void *mem); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "defines.h" | ||
#include "kozos.h" | ||
#include "lib.h" | ||
|
||
int test10_1_main(int argc, char *argv[]) | ||
{ | ||
char *p1, *p2; | ||
int i, j; | ||
puts("test10_1 started.\n"); | ||
|
||
for (i = 4; i <= 56; i += 4) { | ||
p1 = kz_kmalloc(i); | ||
p2 = kz_kmalloc(i); | ||
for (j = 0; j < i - 1; j++) { | ||
p1[j] = 'a'; | ||
p2[j] = 'b'; | ||
} | ||
p1[j] = '\0'; | ||
p2[j] = '\0'; | ||
|
||
putxval((unsigned long)p1, 8); | ||
puts(" "); | ||
puts(p1); | ||
puts("\n"); | ||
putxval((unsigned long)p2, 8); | ||
puts(" "); | ||
puts(p2); | ||
puts("\n"); | ||
|
||
kz_kmfree(p1); | ||
kz_kmfree(p2); | ||
} | ||
puts("test10_1 exit.\n"); | ||
return 0; | ||
} |