FunctionMap.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "FunctionMap.h"
  4. void fmInit(FunctionMap* fm) {
  5. fm->capacity = 16;
  6. fm->entries = 0;
  7. fm->data = malloc(sizeof(Function) * fm->capacity);
  8. fm->queueCapacity = 16;
  9. fm->queueEntries = 0;
  10. fm->queue = malloc(sizeof(LingeringFunction) * fm->queueCapacity);
  11. }
  12. void fmDelete(FunctionMap* fm) {
  13. free(fm->data);
  14. free(fm->queue);
  15. }
  16. Function* fmSearch(FunctionMap* fm, const char* name, int arguments) {
  17. for(int i = 0; i < fm->entries; i++) {
  18. if(fm->data[i].arguments == arguments && strcmp(fm->data[i].name, name) == 0) {
  19. return fm->data + i;
  20. }
  21. }
  22. return NULL;
  23. }
  24. bool fmAdd(FunctionMap* fm, const char* name, int arguments, int address, bool returns) {
  25. if(fmSearch(fm, name, arguments) != NULL) {
  26. return false;
  27. }
  28. if(fm->entries >= fm->capacity) {
  29. fm->capacity *= 2;
  30. fm->data = realloc(fm->data, sizeof(Function) * fm->capacity);
  31. }
  32. int index = fm->entries++;
  33. fm->data[index].name = name;
  34. fm->data[index].arguments = arguments;
  35. fm->data[index].address = address;
  36. fm->data[index].returns = returns;
  37. return true;
  38. }
  39. void fmEnqueue(FunctionMap* fm, const char* name, int arguments, int line, int reserved, bool noReturn) {
  40. if(fm->queueEntries >= fm->queueCapacity) {
  41. fm->queueCapacity *= 2;
  42. fm->queue = realloc(fm->queue, sizeof(LingeringFunction) * fm->queueCapacity);
  43. }
  44. int index = fm->queueEntries++;
  45. fm->queue[index].name = name;
  46. fm->queue[index].arguments = arguments;
  47. fm->queue[index].line = line;
  48. fm->queue[index].reserved = reserved;
  49. fm->queue[index].noReturn = noReturn;
  50. }