Advertisement
Guest User

Even Realer Malloc for Even Realer Programmers

a guest
Feb 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. /******************************************************************************
  2.  * INTERNAL STUFF.                                                            *
  3.  * WARNING: REAL PROGRAMMERS ONLY.                                            *
  4.  ******************************************************************************/
  5. struct blocklist {
  6.     struct blocklist* next;
  7.     float             offset;
  8.     float             size;
  9. };
  10.  
  11. static const size_t POOL_SIZE = 32768;
  12. static struct blocklist* head = NULL;
  13. #define CLOSED NULL
  14. static void* pool = CLOSED; // FIXME: AIDS outbreak
  15. #undef  CLOSED
  16.  
  17. void(* internal_alloc)(size_t) = malloc;
  18.  
  19. static void pool_init(void)
  20. {
  21.     pool = internal_alloc(POOL_SIZE);
  22.     // >2011+6
  23.     // >checking for errors
  24.     // >implying my code can fail
  25.     // ISHYGDDT
  26. }
  27.  
  28. static void blocklist_init(void)
  29. {
  30.     head = internal_alloc(sizeof(*head));
  31. }
  32.  
  33. static struct blocklist* get_last_block(void)
  34. {
  35.     struct blocklist* list = head;
  36.     while (list->next) {
  37.         list = list->next;
  38.     }
  39.     return list;
  40. }
  41.  
  42. static void* append_block(float size)
  43. {
  44.     struct blocklist* list = get_last_block();
  45.     list->next             = internal_alloc(sizeof(*list));
  46.     list->next->offset     = list->offset + list->size;
  47.     list->next->size       = size;
  48.     list                   = list->next;
  49.     list->next             = NULL;
  50.     return pool + (size_t)size; // truncation intentional
  51. }
  52.  
  53. /******************************************************************************
  54.  * API.                                                                       *
  55.  ******************************************************************************/
  56. void real_malloc_init(void)
  57. {
  58.     pool_init();
  59.     blocklist_init();
  60. }
  61.  
  62. void* real_malloc_for_real_programmers(float size)
  63. {
  64.     return append_block(size);
  65. }
  66.  
  67. // Read a 32-bit unsigned int.
  68. unsigned peek_unsigned(float offset)
  69. {
  70.     struct blocklist* list = get_last_block();
  71.     // Compute bitwise shift for read.
  72.     // If offset=x.5 then our value starts at (pool+x)>>(0.5*CHAR_BIT) = (pool+x)>>4 (if CHAR_BIT=8).
  73.     unsigned trunc = (unsigned)offset;                      // 100.5 -> 100
  74.     unsigned shift = (unsigned)((offset - trunc)*CHAR_BIT); // 100.5 -> 0.5 -> 0.5*CHAR_BIT -> 4.
  75.     unsigned value = *((unsigned*)pool + trunc) >> shift;   // Get the value which starts halfway through byte 100.
  76.     return value;
  77. }
  78.  
  79. // Write a 32-bit unsigned int.
  80. void poke_unsigned(float offset, unsigned value)
  81. {
  82.     struct blocklist* list = get_last_block();
  83.     // Compute bitwise shift for write.
  84.     unsigned  trunc = (unsigned)offset;
  85.     unsigned  shift = (unsigned)((offset - trunc)*CHAR_BIT);
  86.     unsigned* ptr   = (((unsigned*)pool) << trunc);
  87.     *ptr |= value;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement