Hi guys,
I'm starting to develop my own small operating system, and so far it's able to print messages on the screen. Well, at least it's a start
What I need to implement now is memory management (mallocs, frees, etc) so I can implement the rest of the operating system.
I know the theory, but my question is.. how do I get started with actual code? I tried searching the web, but found nothing... So what tips can you give me so I can start implementing a paged memory management system?
Thank you in advance!
anog
How to start memory management?
Re: How to start memory management?
this is mm.c bitmap memory management unit for physicalMM
typedef struct {
dword lin;
dword bit;
}tprior_col;
dword m_bitmap[131072]; // endress to 512 MB of memory
tprior_col prior_col;
dword endereco_encontrado; //address found
dword tamanho_encontrado; // size_found
dword numero_de_paginas; // number of pages
byte erro;
void m_bitmap_initialize() {
dword ln;
for(ln=0;ln<131072;ln++) {
m_bitmap[ln] = 0;
}
}
void get_line_and_col(dword endereco, dword lin, byte col) {
dword var1;
var1 = (endereco / 4096); // endereco = address
lin = ((var1 / 32));
col = ((var1 % 32) - 1);
}
dword getaddress(dword l, byte c) {
dword var1, var2, var3 = 0;
// l++;
if (!l) var3 = (4096 * c);
if (l) {
var1 = (32 * 4096);
var2 = (l * var1);
var3 = (var2 + (4096 * c));
}
return var3;
}
dword get_amount_of_pages(dword tamanho_requerido_kb) {
while ((tamanho_requerido_kb % 0x1000) != 0) {
tamanho_requerido_kb = (tamanho_requerido_kb + (0x1000 - (tamanho_requerido_kb % 0x1000)));
}
return (tamanho_requerido_kb / 0x1000);
} // tamanho_requerido_kb = riquerid_size_kb
dword bit_invert(dword value) {
// dword number;
// number = value;
return (value & ~value);
// number = (number & ~128);
// return number;
}
dword getbit(byte col) {
dword r;
r = (1 << col);
return r;
}
// tamanho_requerido = required_size
dword kalloc(dword tamanho_requerido) {
byte str[128];
byte coluna = 0; // col
dword linha = 0; // line
dword bit = 1;
byte erro = false;
tamanho_encontrado = 0;
endereco_encontrado = 0;
while ((tamanho_encontrado < tamanho_requerido) || (erro == true)) {
if ((m_bitmap[linha] & bit) != 0) { // o bit estah busy
if (tamanho_encontrado) {
// libera o bit anterior porque nao foi encontrado bits livres sequenciais
m_bitmap[prior_col.lin] = (m_bitmap[prior_col.lin] & ~prior_col.bit);
}
// zera o tamanho encontrado e o endereco_encontrado
tamanho_encontrado = endereco_encontrado = 0;
}
if ((m_bitmap[linha] & bit) == 0) { // o bit esta livre
// eh o primeiro item da busca?
if (tamanho_encontrado == 0) { // sim eh
// calcula o endereco a ser retornado
endereco_encontrado = getaddress(linha, coluna);
}
// entao nesse caso setamos o bit com 1 e salvamos sua posicao
// para liberar caso o proximo bit nao esteja livre
prior_col.lin = linha;
prior_col.bit = bit;
m_bitmap[linha] = (m_bitmap[linha] | bit);
tamanho_encontrado++;
}
coluna++;
bit = (bit << 1);
if (coluna == 32) {
bit = 1;
coluna = 0;
linha++;
if (linha == 131071) erro = true;
}
}
return endereco_encontrado;
}
dword dealloc(dword address_to_free, dword amount_of_pages) {
dword lin, col, n;
dword bit;
dword linha_inicial;
byte coluna_inicial;
n = 0;
get_line_and_col(address_to_free, lin, col);
bit = getbit(col);
while (n < amount_of_pages) {
m_bitmap[lin] = (m_bitmap[lin] & ~bit);
bit = (bit << 1);
n++;
col++;
if (col == 32) {
bit = 1;
col = 0;
lin++;
}
}
return n;
}
is that
typedef struct {
dword lin;
dword bit;
}tprior_col;
dword m_bitmap[131072]; // endress to 512 MB of memory
tprior_col prior_col;
dword endereco_encontrado; //address found
dword tamanho_encontrado; // size_found
dword numero_de_paginas; // number of pages
byte erro;
void m_bitmap_initialize() {
dword ln;
for(ln=0;ln<131072;ln++) {
m_bitmap[ln] = 0;
}
}
void get_line_and_col(dword endereco, dword lin, byte col) {
dword var1;
var1 = (endereco / 4096); // endereco = address
lin = ((var1 / 32));
col = ((var1 % 32) - 1);
}
dword getaddress(dword l, byte c) {
dword var1, var2, var3 = 0;
// l++;
if (!l) var3 = (4096 * c);
if (l) {
var1 = (32 * 4096);
var2 = (l * var1);
var3 = (var2 + (4096 * c));
}
return var3;
}
dword get_amount_of_pages(dword tamanho_requerido_kb) {
while ((tamanho_requerido_kb % 0x1000) != 0) {
tamanho_requerido_kb = (tamanho_requerido_kb + (0x1000 - (tamanho_requerido_kb % 0x1000)));
}
return (tamanho_requerido_kb / 0x1000);
} // tamanho_requerido_kb = riquerid_size_kb
dword bit_invert(dword value) {
// dword number;
// number = value;
return (value & ~value);
// number = (number & ~128);
// return number;
}
dword getbit(byte col) {
dword r;
r = (1 << col);
return r;
}
// tamanho_requerido = required_size
dword kalloc(dword tamanho_requerido) {
byte str[128];
byte coluna = 0; // col
dword linha = 0; // line
dword bit = 1;
byte erro = false;
tamanho_encontrado = 0;
endereco_encontrado = 0;
while ((tamanho_encontrado < tamanho_requerido) || (erro == true)) {
if ((m_bitmap[linha] & bit) != 0) { // o bit estah busy
if (tamanho_encontrado) {
// libera o bit anterior porque nao foi encontrado bits livres sequenciais
m_bitmap[prior_col.lin] = (m_bitmap[prior_col.lin] & ~prior_col.bit);
}
// zera o tamanho encontrado e o endereco_encontrado
tamanho_encontrado = endereco_encontrado = 0;
}
if ((m_bitmap[linha] & bit) == 0) { // o bit esta livre
// eh o primeiro item da busca?
if (tamanho_encontrado == 0) { // sim eh
// calcula o endereco a ser retornado
endereco_encontrado = getaddress(linha, coluna);
}
// entao nesse caso setamos o bit com 1 e salvamos sua posicao
// para liberar caso o proximo bit nao esteja livre
prior_col.lin = linha;
prior_col.bit = bit;
m_bitmap[linha] = (m_bitmap[linha] | bit);
tamanho_encontrado++;
}
coluna++;
bit = (bit << 1);
if (coluna == 32) {
bit = 1;
coluna = 0;
linha++;
if (linha == 131071) erro = true;
}
}
return endereco_encontrado;
}
dword dealloc(dword address_to_free, dword amount_of_pages) {
dword lin, col, n;
dword bit;
dword linha_inicial;
byte coluna_inicial;
n = 0;
get_line_and_col(address_to_free, lin, col);
bit = getbit(col);
while (n < amount_of_pages) {
m_bitmap[lin] = (m_bitmap[lin] & ~bit);
bit = (bit << 1);
n++;
col++;
if (col == 32) {
bit = 1;
col = 0;
lin++;
}
}
return n;
}
is that
Re: How to start memory management?
it is important to understand that the kernel doesnt usually handle malloc/free at all -- those are library functions supplied by the compiler (but is OS specific -- and must be replaced with one appropriate for your OS environment)
those functions do, however, call kernel functions which allocate large blocks (usually 4KB -- 1 page)
the library function then sub-divides the blocks as necessary for the application only calling the kernel again when it runs out of memory in existing blocks -- and calls the kernel free when it has entire blocks to free (in the case of the kernel, it is possible to combine these, as the kernel has access to everything, but its not usually a good idea, since you need the page allocator for application code anyway)
so there are actually at least 3 memory allocation programs to write (i am assuming you are/intend using paging):
physical allocator
virtual allocator
malloc
malloc requires the virtual allocator
and the virtual allocator requires the physical allocator
those functions do, however, call kernel functions which allocate large blocks (usually 4KB -- 1 page)
the library function then sub-divides the blocks as necessary for the application only calling the kernel again when it runs out of memory in existing blocks -- and calls the kernel free when it has entire blocks to free (in the case of the kernel, it is possible to combine these, as the kernel has access to everything, but its not usually a good idea, since you need the page allocator for application code anyway)
so there are actually at least 3 memory allocation programs to write (i am assuming you are/intend using paging):
physical allocator
virtual allocator
malloc
malloc requires the virtual allocator
and the virtual allocator requires the physical allocator