Implement prerequisites
This commit is contained in:
		
							
								
								
									
										9
									
								
								src/consumer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/consumer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
#include "list.hpp"
 | 
			
		||||
#include "globals.hpp"
 | 
			
		||||
#include "consumer.hpp"
 | 
			
		||||
 | 
			
		||||
void consumer () {
 | 
			
		||||
   block* c = list_unlink(list2);
 | 
			
		||||
   //TODO: consume information in block c
 | 
			
		||||
   list_link(freelist, c);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										89
									
								
								src/list.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								src/list.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,89 @@
 | 
			
		||||
#include "list.hpp"
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
 | 
			
		||||
// pop the last block
 | 
			
		||||
block* list_unlink (list* l) {
 | 
			
		||||
   // we need a list
 | 
			
		||||
   if (!l) return nullptr;
 | 
			
		||||
   block* b = l->end;
 | 
			
		||||
   // can't unlink from an empty list
 | 
			
		||||
   if (!b) return nullptr;
 | 
			
		||||
   // remove b
 | 
			
		||||
   if (b->prev) {
 | 
			
		||||
      // b is not the only element of the list
 | 
			
		||||
      // chop of the tail
 | 
			
		||||
      l->end = b->prev;
 | 
			
		||||
      // bandage the wound
 | 
			
		||||
      l->end->next = nullptr;
 | 
			
		||||
      // wrap the severed end
 | 
			
		||||
      b->prev = nullptr;
 | 
			
		||||
   } else {
 | 
			
		||||
      // b is the only element of the list
 | 
			
		||||
      l->start = nullptr;
 | 
			
		||||
      l->end = nullptr;
 | 
			
		||||
   }
 | 
			
		||||
   // if we get here, we've removed element b from the list
 | 
			
		||||
   l->length--;
 | 
			
		||||
   return b;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// push a block
 | 
			
		||||
void list_link (list* l, block* b) {
 | 
			
		||||
   // can't link to a nonexistent list
 | 
			
		||||
   if (!l) return;
 | 
			
		||||
   // can't append nullptr;
 | 
			
		||||
   if (!b) return;
 | 
			
		||||
   if (l->start == nullptr) {
 | 
			
		||||
      // b is the only element of the list
 | 
			
		||||
      l->start = b;
 | 
			
		||||
      l->end   = b;
 | 
			
		||||
   } else {
 | 
			
		||||
      // make b the next element of the list
 | 
			
		||||
      b->prev = l->end;
 | 
			
		||||
      l->end->next = b;
 | 
			
		||||
      // terminate the list
 | 
			
		||||
      b->next = nullptr;
 | 
			
		||||
      // update end
 | 
			
		||||
      l->end = b;
 | 
			
		||||
   }
 | 
			
		||||
   l->length++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// push a lot of blocks
 | 
			
		||||
// this is slow, but the pointer arithmetic *has* to be done for each block
 | 
			
		||||
// so it's not really as slow as it would be, were that not the case.
 | 
			
		||||
void list_init (list *l, block *m, int size) {
 | 
			
		||||
   // move each block of memory into the freelist, by linking it to the end
 | 
			
		||||
   for (int b = 0; b < size; b++) {
 | 
			
		||||
      list_link(l, &(m[b]));
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// YAML-like, because I like YAML
 | 
			
		||||
void list_print(list *l) {
 | 
			
		||||
   if (!l) return;
 | 
			
		||||
   std::printf("l_%p:\n  - start:  '%p',\n  - end:    '%p',\n  - length: '%d',\n  - blocks:",l,l->start,l->end,l->length);
 | 
			
		||||
   block* b = l->start;
 | 
			
		||||
   if (b) {
 | 
			
		||||
      do {
 | 
			
		||||
         std::printf("\n    - b_%p: [prev: '%p', next: '%p', data: '%d']",b,b->prev,b->next,b->data);
 | 
			
		||||
      } while (b = b->next);
 | 
			
		||||
      std::printf("\n");
 | 
			
		||||
   } else {
 | 
			
		||||
      std::printf(" []\n");
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_concise (list *l) {
 | 
			
		||||
   if (!l) return;
 | 
			
		||||
   std::printf("%lx: {", (__uint64_t)l&0xffff);
 | 
			
		||||
   block *b = l->start;
 | 
			
		||||
   if (b) {
 | 
			
		||||
      do {
 | 
			
		||||
         std::printf("%lx:%d, ", (__uint64_t)b&0xfff, b->data);
 | 
			
		||||
      } while (b = b->next);
 | 
			
		||||
      std::printf("\b\b} \n");
 | 
			
		||||
   } else {
 | 
			
		||||
      std::printf("}\n");
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								src/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include "list.hpp"
 | 
			
		||||
#include "globals.hpp"
 | 
			
		||||
#include "producer.hpp"
 | 
			
		||||
 | 
			
		||||
// Create all of memory
 | 
			
		||||
block memory[N] = {0};
 | 
			
		||||
// create the three lists
 | 
			
		||||
list lists[3] = {0};
 | 
			
		||||
list *freelist = &lists[0], *list1 = &lists[1], *list2 = &lists[2];
 | 
			
		||||
 | 
			
		||||
int main (int argc, char* argv[]) {
 | 
			
		||||
   // initialize the freelist
 | 
			
		||||
   list_init(freelist, memory, N);
 | 
			
		||||
   //TODO: Use pthreads to split execution
 | 
			
		||||
   //TODO: Implement a semaphore solution to the problem
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/producer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/producer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
#include "list.hpp"
 | 
			
		||||
#include "globals.hpp"
 | 
			
		||||
#include "producer.hpp"
 | 
			
		||||
 | 
			
		||||
void producer () {
 | 
			
		||||
   block* b = list_unlink(freelist);
 | 
			
		||||
   //TODO: Produce information in block b
 | 
			
		||||
   list_link(list1, b);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										83
									
								
								src/test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								src/test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include "list.hpp"
 | 
			
		||||
#include "globals.hpp"
 | 
			
		||||
#include "producer.hpp"
 | 
			
		||||
#include "consumer.hpp"
 | 
			
		||||
#include "transformer.hpp"
 | 
			
		||||
 | 
			
		||||
// Create all of memory
 | 
			
		||||
block memory[N] = {0};
 | 
			
		||||
// create the three lists
 | 
			
		||||
list lists[3] = {0};
 | 
			
		||||
list *freelist = &lists[0],
 | 
			
		||||
     *list1 = &lists[1],
 | 
			
		||||
     *list2 = &lists[2];
 | 
			
		||||
 | 
			
		||||
void producer_test();
 | 
			
		||||
void transfer_test();
 | 
			
		||||
void consumer_test();
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[]) {
 | 
			
		||||
   list_init(freelist, memory, N);
 | 
			
		||||
   list_concise(freelist);
 | 
			
		||||
   producer_test();
 | 
			
		||||
   transfer_test();
 | 
			
		||||
   consumer_test();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void producer_test() {
 | 
			
		||||
   printf("Starting producer_test:\n");\
 | 
			
		||||
   producer();
 | 
			
		||||
   list_concise(freelist);
 | 
			
		||||
   list_concise(list1);
 | 
			
		||||
   list_concise(list2);
 | 
			
		||||
   printf("Ending producer_test\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void transfer_test() {
 | 
			
		||||
   printf("Starting transfer_test:\n");
 | 
			
		||||
   transformer();
 | 
			
		||||
   list_concise(freelist);
 | 
			
		||||
   list_concise(list1);
 | 
			
		||||
   list_concise(list2);
 | 
			
		||||
   printf("Ending transfer_test\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void consumer_test() {
 | 
			
		||||
   printf("Starting consumer_test:\n");
 | 
			
		||||
   consumer();
 | 
			
		||||
   list_concise(freelist);
 | 
			
		||||
   list_concise(list1);
 | 
			
		||||
   list_concise(list2);
 | 
			
		||||
   printf("Ending consumer_test\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
int movement_test () {
 | 
			
		||||
 | 
			
		||||
   // Create all of memory
 | 
			
		||||
   block memory[N] = {0};
 | 
			
		||||
   // create the three lists
 | 
			
		||||
   list freelist = {0}, list1 = {0};
 | 
			
		||||
   // initialize the freelist
 | 
			
		||||
   list_init(&freelist, memory, N);
 | 
			
		||||
 | 
			
		||||
   // print the lists
 | 
			
		||||
   std::printf("Lists:\n");
 | 
			
		||||
   list_print(&freelist);
 | 
			
		||||
   list_print(&list1);
 | 
			
		||||
 | 
			
		||||
   // move a block
 | 
			
		||||
   block *b;
 | 
			
		||||
   while (b = list_unlink(&freelist))
 | 
			
		||||
   {
 | 
			
		||||
      list_link(&list1, b);
 | 
			
		||||
   }
 | 
			
		||||
   // print the lists again (should be reversed order)
 | 
			
		||||
   std::printf("Lists, again:\n");
 | 
			
		||||
   list_print(&freelist);
 | 
			
		||||
   list_print(&list1);
 | 
			
		||||
   return 1;
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										11
									
								
								src/transformer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/transformer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
#include "list.hpp"
 | 
			
		||||
#include "globals.hpp"
 | 
			
		||||
#include "transformer.hpp"
 | 
			
		||||
 | 
			
		||||
void transformer () {
 | 
			
		||||
   block* x = list_unlink(list1);
 | 
			
		||||
   block* y = list_unlink(freelist);
 | 
			
		||||
   //TODO: use block x to produce info in y
 | 
			
		||||
   list_link(freelist, x);
 | 
			
		||||
   list_link(list2, y);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user