00001 #include <stdio.h>
00002 #include <stdlib.h>
00003
00004 #include "hvmem.h"
00005 #include "hvmemptr.h"
00006 #include "hlist.h"
00007
00008
00009 #define INSTR_BUF_SIZE 10
00010 #define POOL_SIZE 16
00011 #define BACKEND "/var/id_backend"
00012
00014 #define BRAM_SIZE 10
00015
00018 static struct hlist* get_bram_entries_();
00020 static int create_bram_entries_();
00022 static const int get_first_bram_();
00024 static struct hlorphan* create_entry_(int address);
00025
00026 static struct hlist* bram_entries = NULL;
00027 static struct hlist* get_bram_entries_()
00028 {
00029 return bram_entries;
00030 }
00031
00032 static int create_bram_entries_()
00033 {
00034 bram_entries = hlist_create();
00035
00036 return 0;
00037 }
00038
00039 static const int first_bram = 0;
00040 static const int get_first_bram_()
00041 {
00042 return first_bram;
00043 }
00044
00045
00046 static int get_entry_address_(struct hlelement* element)
00047 {
00048 if (element == NULL)
00049 return -1;
00050
00051 return *(int*)hlorphan_get_data( hlelement_get_data(element) );
00052 }
00053
00054
00055 static struct hlorphan* create_entry_(int address)
00056 {
00057 int* entry = malloc(sizeof(int));
00058 *entry = address;
00059 return hlorphan_create(entry);
00060 }
00061
00062
00063 int hvmem_get_free_count()
00064 {
00065 struct hlelement* bram_i = hlist_get_first(get_bram_entries_());
00066 int bytes_free = 0;
00067 int last_alloc_bram = get_first_bram_() - 1;
00068 while (bram_i != NULL) {
00069 bytes_free += get_entry_address_(bram_i) - last_alloc_bram - 1;
00070 last_alloc_bram = get_entry_address_(bram_i);
00071 bram_i = hlelement_get_next(bram_i);
00072 }
00073
00074 if ( get_entry_address_(hlist_get_last(get_bram_entries_())) < BRAM_SIZE - 1 )
00075 bytes_free += BRAM_SIZE - get_entry_address_(hlist_get_last(get_bram_entries_())) - 1;
00076
00077 return bytes_free;
00078 }
00079
00080
00081 void hvmem_print_entries()
00082 {
00083 struct hlelement* bram_i = hlist_get_first(get_bram_entries_());
00084 while (bram_i != NULL) {
00085
00086 bram_i = hlelement_get_next(bram_i);
00087 }
00088 }
00089
00090
00091 struct hvmemptr* hvmem_allocate(struct hprocess* process, int size)
00092 {
00093 if (process == NULL || size < 1)
00094 return NULL;
00095
00096 if (get_bram_entries_() == NULL)
00097 create_bram_entries_();
00098
00099 struct hlelement* bram_i = hlist_get_first(get_bram_entries_());
00100 struct hlelement* new_entry = NULL;
00101 struct hlorphan* bram_entry = NULL;
00102 int bytes_allocated = 0;
00103
00104 if (bram_i == NULL) {
00105 bram_entry = create_entry_(get_first_bram_());
00106 bytes_allocated += 1;
00107 bram_i = hlist_enqueue(get_bram_entries_(), bram_entry);
00108 new_entry = bram_i;
00109
00110 }
00111
00112 struct hlelement* bram_j = bram_i;
00113
00114
00115 while ( get_entry_address_(bram_i) > get_first_bram_() && bytes_allocated < size) {
00116 bram_entry = create_entry_(get_entry_address_(bram_i) - 1);
00117 bytes_allocated += 1;
00118 bram_i = hlist_insert_before(get_bram_entries_(), bram_i, bram_entry);
00119
00120 }
00121
00122 if (bytes_allocated > 0)
00123 new_entry = bram_i;
00124
00125 struct hlelement* bram_k = NULL;
00126
00127 int bram_j_next_addr = -1;
00128 while (bram_j != NULL && bytes_allocated < size) {
00129 bram_k = bram_j;
00130 if (hlelement_get_next(bram_j) == NULL)
00131 bram_j_next_addr = BRAM_SIZE;
00132 else
00133 bram_j_next_addr = get_entry_address_(hlelement_get_next(bram_j));
00134
00135 if ( get_entry_address_(bram_j) + (size - bytes_allocated) + 1 < bram_j_next_addr)
00136 bram_j_next_addr = get_entry_address_(bram_j) + (size - bytes_allocated) + 1;
00137
00138
00139 while ( bram_j_next_addr > get_entry_address_(bram_k) + 1) {
00140 bram_entry = create_entry_(get_entry_address_(bram_k) + 1);
00141 bram_k = hlist_insert_after(get_bram_entries_(), bram_k, bram_entry);
00142 if (bytes_allocated == 0)
00143 new_entry = bram_k;
00144 bytes_allocated += 1;
00145
00146 }
00147 bram_j = hlelement_get_next(bram_j);
00148 }
00149
00150
00151 if (new_entry != NULL && bytes_allocated != size) {
00152 free(new_entry);
00153 return NULL;
00154 } else if (new_entry != NULL) {
00155 return hvmemptr_create(new_entry, size);
00156 } else
00157 return NULL;
00158 }
00159
00160
00161 int hvmem_free(struct hprocess* process, struct hvmemptr* pointer)
00162 {
00163 if (process == NULL || pointer == NULL)
00164 return -1;
00165
00166 struct hlelement* bram_i = hvmemptr_get_bramentry(pointer);
00167 int size = hvmemptr_get_size(pointer);
00168 int bytes_removed = 0;
00169 struct hlelement* bram_next = NULL;
00170 while (bram_i != NULL && bytes_removed < size) {
00171 bram_next = hlelement_get_next(bram_i);
00172 hlelement_remove(bram_i, get_bram_entries_());
00173 bram_i = bram_next;
00174 bytes_removed += 1;
00175 }
00176
00177 if (bytes_removed != size)
00178 return -2;
00179 else
00180 return bytes_removed;
00181 }
00182
00183