FunctionMap.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "utils/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 &&
  19. strcmp(fm->data[i].name, name) == 0) {
  20. return fm->data + i;
  21. }
  22. }
  23. return NULL;
  24. }
  25. bool fmAdd(FunctionMap* fm, const char* name, int arguments, int address,
  26. bool returns) {
  27. if(fmSearch(fm, name, arguments) != NULL) {
  28. return false;
  29. }
  30. if(fm->entries >= fm->capacity) {
  31. fm->capacity *= 2;
  32. fm->data = realloc(fm->data, sizeof(Function) * fm->capacity);
  33. }
  34. int index = fm->entries++;
  35. fm->data[index].name = name;
  36. fm->data[index].arguments = arguments;
  37. fm->data[index].address = address;
  38. fm->data[index].returns = returns;
  39. return true;
  40. }
  41. void fmEnqueue(FunctionMap* fm, const char* name, int arguments, int line,
  42. int reserved, bool noReturn) {
  43. if(fm->queueEntries >= fm->queueCapacity) {
  44. fm->queueCapacity *= 2;
  45. fm->queue =
  46. realloc(fm->queue, sizeof(LingeringFunction) * fm->queueCapacity);
  47. }
  48. int index = fm->queueEntries++;
  49. fm->queue[index].name = name;
  50. fm->queue[index].arguments = arguments;
  51. fm->queue[index].line = line;
  52. fm->queue[index].reserved = reserved;
  53. fm->queue[index].noReturn = noReturn;
  54. }