You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.3 KiB

  1. unsigned int global_heap_start = 0;
  2. unsigned int global_heap_end = 0;
  3. unsigned int global_heap_next = 0;
  4. void* malloc(num_bytes);
  5. void* calloc(number_elements, size);
  6. int set_heap_settings(param_start, param_end);
  7. unsigned int get_heap_next();
  8. void* malloc(num_bytes)
  9. int num_bytes;
  10. {
  11. unsigned int allocated_pointer = global_heap_next;
  12. global_heap_next = global_heap_next + num_bytes;
  13. if (global_heap_next > global_heap_end)
  14. {
  15. return 0; /* No more memory */
  16. }
  17. return allocated_pointer;
  18. }
  19. void* calloc(number_elements, size)
  20. unsigned int number_elements;
  21. unsigned int size;
  22. {
  23. int i;
  24. char* temp_pointer;
  25. unsigned int num_bytes = number_elements * size;
  26. void* allocated_pointer = global_heap_next;
  27. global_heap_next = global_heap_next + num_bytes;
  28. if (global_heap_next > global_heap_end)
  29. {
  30. return 0; /* No more memory */
  31. }
  32. /* Since this is calloc, we set the memory to zero */
  33. temp_pointer = allocated_pointer;
  34. for (i = 0; i < num_bytes; ++i)
  35. {
  36. *temp_pointer = 0;
  37. temp_pointer++;
  38. }
  39. return allocated_pointer;
  40. }
  41. int set_heap_settings(param_start, param_end)
  42. unsigned int param_start;
  43. unsigned int param_end;
  44. {
  45. global_heap_start = param_start;
  46. global_heap_next = param_start;
  47. global_heap_end = param_end;
  48. return 0;
  49. }
  50. unsigned int get_heap_next()
  51. {
  52. return global_heap_next;
  53. }