[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 hvmemptr * | hvmem_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. |
[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.
Allocate virtual memory of the given size.
Note that the allocated memory may or may not be stored contiguous in BRAM.
process | Process that should own the memory. | |
size Allocate | memory of the given size. |
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; }
Free virtual memory pointed to by the virtual pointer.
process | Process that owns the memory. | |
size Allocate | memory of the given size. |
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.
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; }
Read data from the virtual memory.
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. |
int hvmem_write | ( | struct hprocess * | process, | |
struct hvmemptr * | pointer, | |||
int | offset, | |||
int | bytes, | |||
char * | data | |||
) |
Write data to the virtual memory.
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. |