Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/* Project:     OSLib
2
 * Description: The OS Construction Kit
3
 * Date:                1.6.2000
4
 * Idea by:             Luca Abeni & Gerardo Lamastra
5
 *
6
 * OSLib is an SO project aimed at developing a common, easy-to-use
7
 * low-level infrastructure for developing OS kernels and Embedded
8
 * Applications; it partially derives from the HARTIK project but it
9
 * currently is independently developed.
10
 *
11
 * OSLib is distributed under GPL License, and some of its code has
12
 * been derived from the Linux kernel source; also some important
13
 * ideas come from studying the DJGPP go32 extender.
14
 *
15
 * We acknowledge the Linux Community, Free Software Foundation,
16
 * D.J. Delorie and all the other developers who believe in the
17
 * freedom of software and ideas.
18
 *
19
 * For legalese, check out the included GPL license.
20
 */
21
 
22
/*      Basic Memory Manager Functions:
23
        A classic fixed partition allocator!    */
24
 
25
#include <ll/i386/stdlib.h>
26
#include <ll/i386/string.h>
27
#include <ll/i386/mem.h>
28
#include <ll/i386/cons.h>
29
#include <ll/i386/error.h>
30
 
31
#include <ll/sys/ll/ll-mem.h>
32
 
33
FILE(Memory);
34
 
35
#define MAX_PARTITION   50      /* Max available partition */
36
 
37
static struct {
38
    BYTE used;
39
#ifdef __WC16__
40
    BYTE __huge *addr;
41
#else
42
    BYTE *addr;
43
#endif
44
    DWORD size;
45
} mem_table[MAX_PARTITION];
46
 
47
void ll_mem_init(void *base, DWORD size)
48
{
49
    register int i;
50
    mem_table[0].used = 1;
51
    mem_table[0].addr = base;
52
    mem_table[0].size = size;
53
    for (i = 1; i < MAX_PARTITION; i++)
54
        mem_table[i].used = 0;
55
}
56
 
57
void *ll_alloc(DWORD s)
58
{
59
    void *p = NULL;
60
    int i = 0;
61
 
62
    while (i < MAX_PARTITION && p == NULL) {
63
        if (mem_table[i].used && (mem_table[i].size >= s))
64
            p = mem_table[i].addr;
65
        else
66
            i++;
67
    }
68
    if (p != NULL) {
69
        if (mem_table[i].size > s) {
70
            mem_table[i].size -= s;
71
            mem_table[i].addr += s;
72
        } else
73
            mem_table[i].used = FALSE;
74
    }
75
    return (p);
76
}
77
 
78
WORD ll_free(void *p, DWORD s)
79
{
80
    register int i = 1;
81
    unsigned i1 = 0, i2 = 0;
82
 
83
    while (i < MAX_PARTITION && ((i1 == 0) || (i2 == 0))) {
84
        if (mem_table[i].used) {
85
            if (mem_table[i].addr + mem_table[i].size == p)
86
                i1 = i;
87
            if (mem_table[i].addr == (BYTE *) (p) + s)
88
                i2 = i;
89
        }
90
        i++;
91
    }
92
    if (i1 != 0 && i2 != 0) {
93
        mem_table[i1].size += mem_table[i2].size + s;
94
        mem_table[i2].used = FALSE;
95
    } else if (i1 == 0 && i2 != 0) {
96
        mem_table[i2].addr = p;
97
        mem_table[i2].size += s;
98
    } else if (i1 != 0 && i2 == 0)
99
        mem_table[i1].size += s;
100
    else {
101
        i = 0;
102
        while (i < MAX_PARTITION && (mem_table[i].used == TRUE))
103
            i++;
104
        if (i == MAX_PARTITION)
105
            return (FALSE);
106
        mem_table[i].addr = p;
107
        mem_table[i].size = s;
108
        mem_table[i].used = TRUE;
109
    }
110
    return (TRUE);
111
}
112
 
113
void ll_mem_dump(void)
114
{
115
    register int i;
116
    for (i = 0; i < MAX_PARTITION; i++) {
117
        if (mem_table[i].used)
118
            message("Entry : [%d] Addr : %p Size : %ld\n", i,
119
                    mem_table[i].addr, mem_table[i].size);
120
    }
121
}