Functions

/home/sindre/dev_uclinux/suzaku_shared/hwos_sw/libhwos/hvmem.h File Reference

[Hardware OS Virtual Memory Interface] Keeps track of each process' BRAM memory. More...

#include "hstructures.h"

Go to the source code of this file.

Functions

void hvmem_print_entries ()
struct hvmemptrhvmem_allocate (struct hprocess *process, int size)
 Allocate virtual memory of the given size.
int hvmem_get_free_count ()
 Return total size of free memory.
int hvmem_free (struct hprocess *process, struct hvmemptr *pointer)
 Free virtual memory pointed to by the virtual pointer.
int hvmem_write (struct hprocess *process, struct hvmemptr *pointer, int offset, int bytes, char *data)
 Write data to the virtual memory.
int hvmem_read (struct hprocess *process, struct hvmemptr *pointer, int offset, int bytes)
 Read data from the virtual memory.

Detailed Description

[Hardware OS Virtual Memory Interface] Keeps track of each process' BRAM memory.

Based on known concepts in OS-theory, this interface for each process to allocate, read, write and free BRAM memory has been made. The memory is called virtual because allocated memory may or may not be contiguous in BRAM. There may be possible to swap memory to disk in future versions.

The underlying implementation is based on double linked lists.

NOTE: The implementation of hvmem was just started. It may be possible to use some of this code when doing further development of a virtual memory implementation.

Author (2011): Sindre Hansen

Definition in file hvmem.h.


Function Documentation

struct hvmemptr* hvmem_allocate ( struct hprocess process,
int  size 
) [read]

Allocate virtual memory of the given size.

Note that the allocated memory may or may not be stored contiguous in BRAM.

Parameters:
process Process that should own the memory.
size Allocate memory of the given size.
Returns:
Virtual pointer to the allocated memory. NULL on failure.

Definition at line 91 of file hvmem.c.

{
        if (process == NULL || size < 1)
                return NULL;

        if (get_bram_entries_() == NULL)
                create_bram_entries_();

        struct hlelement* bram_i = hlist_get_first(get_bram_entries_());
        struct hlelement* new_entry = NULL;
        struct hlorphan* bram_entry = NULL;
        int bytes_allocated = 0;

        if (bram_i == NULL) {
                bram_entry = create_entry_(get_first_bram_());
                bytes_allocated += 1;
                bram_i = hlist_enqueue(get_bram_entries_(), bram_entry);
                new_entry = bram_i;
                //printf("hvmem_allocate: adding first=%d\n", get_entry_address_(bram_i));
        }

        struct hlelement* bram_j = bram_i;

        // Insert elements before first element if possible.
        while ( get_entry_address_(bram_i) > get_first_bram_() && bytes_allocated < size) {
                bram_entry = create_entry_(get_entry_address_(bram_i) - 1);
                bytes_allocated += 1;
                bram_i = hlist_insert_before(get_bram_entries_(), bram_i, bram_entry);
                //printf("hvmem_allocate: inserting before, data=%d\n", get_entry_address_(bram_i));
        }

        if (bytes_allocated > 0)
                new_entry = bram_i;

        struct hlelement* bram_k = NULL; 

        int bram_j_next_addr = -1;
        while (bram_j != NULL && bytes_allocated < size) {
                bram_k = bram_j;
                if (hlelement_get_next(bram_j) == NULL)
                        bram_j_next_addr = BRAM_SIZE;
                else
                        bram_j_next_addr = get_entry_address_(hlelement_get_next(bram_j));

                if ( get_entry_address_(bram_j) + (size - bytes_allocated) + 1 < bram_j_next_addr)
                        bram_j_next_addr = get_entry_address_(bram_j) + (size - bytes_allocated) + 1;

                //printf("hvmem_allocate: bram_j_next_addr=%d\n", bram_j_next_addr);
                while ( bram_j_next_addr > get_entry_address_(bram_k) + 1) {
                        bram_entry = create_entry_(get_entry_address_(bram_k) + 1);
                        bram_k = hlist_insert_after(get_bram_entries_(), bram_k, bram_entry);
                        if (bytes_allocated == 0)
                                new_entry = bram_k;
                        bytes_allocated += 1;
                        //printf("hvmem_allocate: adding after, data=%d\n", get_entry_address_(bram_k));
                }
                bram_j = hlelement_get_next(bram_j);
        }
       

        if (new_entry != NULL && bytes_allocated != size) {
                free(new_entry);
                return NULL;
        } else if (new_entry != NULL) {
                return hvmemptr_create(new_entry, size);
        } else
                return NULL;
}

int hvmem_free ( struct hprocess process,
struct hvmemptr pointer 
)

Free virtual memory pointed to by the virtual pointer.

Parameters:
process Process that owns the memory.
size Allocate memory of the given size.
Returns:
Physical address to first byte in BRAM. Negative on failure.

Definition at line 161 of file hvmem.c.

{
        if (process == NULL || pointer == NULL)
                return -1;

        struct hlelement* bram_i = hvmemptr_get_bramentry(pointer);
        int size = hvmemptr_get_size(pointer);
        int bytes_removed = 0;
        struct hlelement* bram_next = NULL;
        while (bram_i != NULL && bytes_removed < size) {
                bram_next = hlelement_get_next(bram_i);
                hlelement_remove(bram_i, get_bram_entries_());
                bram_i = bram_next;
                bytes_removed += 1;
        }

        if (bytes_removed != size)
                return -2;      // Fatal error (removed some bytes, but not all the allocated ones).
        else
                return bytes_removed;
}

int hvmem_get_free_count (  ) 

Return total size of free memory.

Note that this is a quite expensive method as it will have to walk through all the allocated memory.

Returns:
Total number of free bytes in memory.

Definition at line 63 of file hvmem.c.

{
        struct hlelement* bram_i = hlist_get_first(get_bram_entries_());
        int bytes_free = 0;
        int last_alloc_bram = get_first_bram_() - 1;
        while (bram_i != NULL) {
                bytes_free += get_entry_address_(bram_i) - last_alloc_bram - 1;
                last_alloc_bram = get_entry_address_(bram_i);
                bram_i = hlelement_get_next(bram_i);
        }

        if ( get_entry_address_(hlist_get_last(get_bram_entries_())) < BRAM_SIZE - 1 )
                bytes_free += BRAM_SIZE - get_entry_address_(hlist_get_last(get_bram_entries_())) - 1;

        return bytes_free;
}

int hvmem_read ( struct hprocess process,
struct hvmemptr pointer,
int  offset,
int  bytes 
)

Read data from the virtual memory.

Parameters:
process Process that owns the memory.
pointer Virtual pointer to the memory.
offset Start writing at this offset in memory.
bytes How many bytes should be written.
Returns:
0 on success. Negative on failure.
int hvmem_write ( struct hprocess process,
struct hvmemptr pointer,
int  offset,
int  bytes,
char *  data 
)

Write data to the virtual memory.

Parameters:
process Process that owns the memory.
pointer Virtual pointer to the memory.
offset Start writing at this offset in memory.
bytes How many bytes should be written.
data Pointer to physical data to be written.
Returns:
0 on success. Negative on failure.
 All Data Structures Files Functions Variables Enumerations Enumerator Defines