Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

Kernel Memory Allocator
[Memory Manager]


Data Structures

struct  MEM_BLOCK
 A memory block structure. More...


Defines

#define M_MAGIC   ((dword)"$MB$")
 Magic number to identify a memory block.

#define M_FREE   0
 Free block.

#define M_ALLOC   1
 Allocated block.


Typedefs

typedef MEM_BLOCK mem_block_t
 A memory block structure.


Functions

void kmalloc_init ()
 Initialize the kernel virtual memory area.

void * kmalloc (dword size)
void kfree (void *ptr)
 Frees the memory space pointed by ptr, which must have been returned by a previous call to kmalloc(size_t size).
Parameters:
ptr  The pointer you want to free.


int mem_sizeof (void *ptr)
 Return the size of the pointer ptr passed as argument.
Parameters:
ptr  The pointer you want to know the size.
Exceptions:
NULL  Invalid pointer ptr.
Returns:
The size of the pointer ptr passed as argument.


void * kmemalign (size_t alignment, size_t size)
 Allocate some memory aligned to a boundary.
Parameters:
alignment  The boundary.
size  The size you want to allocate.
Exceptions:
NULL  Out-of-memory.
Returns:
A pointer to a memory area aligned to the boundary. The pointer is a aligned_mem_block_t pointer, so if you want to access to the data area of this pointer you must specify the p->start filed.
Note:
Use kfree(void *ptr) to free the allocated block.



Detailed Description

The memory operators for the kernel.

Define Documentation

#define M_ALLOC   1
 

Allocated block.

Definition at line 24 of file kmalloc.h.

#define M_FREE   0
 

Free block.

Definition at line 22 of file kmalloc.h.

#define M_MAGIC   ((dword)"$MB$")
 

Magic number to identify a memory block.

Definition at line 20 of file kmalloc.h.


Typedef Documentation

typedef struct MEM_BLOCK mem_block_t
 

A memory block structure.


Function Documentation

void kfree void *    ptr
 

Frees the memory space pointed by ptr, which must have been returned by a previous call to kmalloc(size_t size).

Parameters:
ptr  The pointer you want to free.

Definition at line 223 of file kmalloc.c.

00224 {
00225         mem_block_t *p, *p2;
00226         dword IF = GET_IF();
00227 
00228         if (ptr == NULL) return;
00229 
00230         disable();
00231 
00232         // Point to the header                                          //
00233         p = (void *)ptr - sizeof(mem_block_t);
00234 
00235         if (p->magic != M_MAGIC) return; // Is the process crazy?! :)   //
00236         if (p->flags != M_ALLOC) return; // Crazy again?! :)            //
00237 
00238         // Free the block                                               //
00239         p->flags = M_FREE;
00240         p->owner = NULL;
00241 
00242         // Try to combine the block wih the next one...                 //
00243         p2 = (void *)p + p->size + sizeof(mem_block_t);
00244 
00245         if (p2 < (mem_block_t *)K_HEAP_END)
00246                 if (p2->flags == M_FREE)
00247                 {
00248                         // Let's merge the two blocks!                  //
00249                         p->size += p2->size + sizeof(mem_block_t);
00250                         p2->magic = NULL;
00251                 }
00252 
00253         // Try to combine the block with the previous one...            //
00254         if (p != (mem_block_t *)K_HEAP_START)
00255         {
00256                 // Find the previous block                              //
00257                 p2 = (void *)K_HEAP_START;
00258                 for(;;)
00259                 {
00260                         if (((void *)p2 + p2->size + sizeof(mem_block_t)) == p)
00261                         {
00262                                 // Block found!                         //
00263                                 // Check if it's a free block           //
00264                                 if (p2->flags == M_FREE)
00265                                 {
00266                                         // Let's merge the two blocks!  //
00267                                         p2->size += (p->size + sizeof(mem_block_t));
00268                                         p->magic = NULL;
00269                                         p = p2;
00270                                 }
00271                                 break;
00272                         }
00273                         p2 = (void *)p2 + p2->size + sizeof(mem_block_t);
00274                 }
00275         }
00276         // Release the physical space occupied by the block             //
00277         deallocate(p);
00278 
00279         SET_IF(IF);
00280 }

void* kmalloc dword    size
 

void kmalloc_init  
 

Initialize the kernel virtual memory area.

Definition at line 22 of file kmalloc.c.

00023 {
00024         mem_block_t *p;
00025 
00026         // Create only one hudge block                                  //
00027         p = (mem_block_t *)K_HEAP_START;
00028         p->magic = M_MAGIC;
00029         p->flags = M_FREE;
00030         p->size = K_HEAP_END - K_HEAP_START - sizeof(mem_block_t);
00031         p->owner = NULL;
00032 }

void* kmemalign size_t    alignment,
size_t    size
 

Allocate some memory aligned to a boundary.

Parameters:
alignment  The boundary.
size  The size you want to allocate.
Exceptions:
NULL  Out-of-memory.
Returns:
A pointer to a memory area aligned to the boundary. The pointer is a aligned_mem_block_t pointer, so if you want to access to the data area of this pointer you must specify the p->start filed.
Note:
Use kfree(void *ptr) to free the allocated block.

Definition at line 308 of file kmalloc.c.

00309 {
00310         mem_block_t *p, *p2;
00311         dword IF = GET_IF();
00312 
00313         // Cannot allocate memory with a size of zero                   //
00314         if ( !size )
00315                 return(NULL);
00316 
00317         // Allocate the pointer                                         //
00318         p = kmalloc(size + alignment + 2*sizeof(mem_block_t));
00319         if ( p == NULL )
00320                 return(NULL);
00321 
00322         // An alignment of zero is intended as a simple kmalloc(size)   //
00323         if ( alignment == 0 )
00324         {
00325                 return(p);
00326         }
00327 
00328         // Check if the pointer is correcly aligned                     //
00329         if ( (size_t)(p) % alignment )
00330         {
00331                 // Align the pointer p to the boundary.                 //
00332                 disable();
00333 
00334                 // Allocate the new block                               //
00335                 p2 = p + 2;
00336                 p2 = (mem_block_t *)ALIGN_UP((size_t)p2, alignment)  - 1;
00337                 p2->magic = M_MAGIC;
00338                 p2->flags = (p-1)->flags;
00339                 p2->owner = (p-1)->owner;
00340                 p2->size = mem_sizeof(p) - ((size_t)(p2+1) - (size_t)p);
00341 
00342                 // Free the unused space                                //
00343                 (p-1)->size = (size_t)p2 - (size_t)p;
00344                 kfree(p);
00345 
00346                 SET_IF(IF);
00347 
00348                 return(p2+1);
00349         }
00350         else
00351                 // The pointer p is already aligned to the boudary      //
00352                 return(p);
00353 }

int mem_sizeof void *    ptr
 

Return the size of the pointer ptr passed as argument.

Parameters:
ptr  The pointer you want to know the size.
Exceptions:
NULL  Invalid pointer ptr.
Returns:
The size of the pointer ptr passed as argument.

Definition at line 287 of file kmalloc.c.

00288 {
00289 // Return the size of the pointer "ptr", or NULL if an error occurs     //
00290         mem_block_t *p = ((mem_block_t *)ptr-1);
00291 
00292         if ( (p->magic == M_MAGIC) && (p->flags == M_ALLOC) )
00293                 return( p->size );
00294         else
00295                 return(NULL);
00296 }


Generated on Fri Feb 20 15:32:22 2004 for Minirighi by doxygen1.2.18