Advertisement
Guest User

Real Malloc for Real Programmers

a guest
Feb 22nd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 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(*head);
  31. }
  32.  
  33. static void* append_block(float size)
  34. {
  35.     struct blocklist* list = head;
  36.     while (list->next) {
  37.         list = list->next;
  38.     }
  39.     list->next = internal_alloc(sizeof(*list));
  40.     list->next->offset = list->offset + list->size;
  41.     list->next->size   = size;
  42.     list = list->next;
  43.     list->next = NULL;
  44.     return pool + (size_t)size; // truncation intentional
  45. }
  46. /******************************************************************************
  47.  * API.                                                                       *
  48.  ******************************************************************************/
  49. void real_malloc_init(void)
  50. {
  51.     pool_init();
  52.     blocklist_init();
  53. }
  54.  
  55. void* real_malloc_for_real_programmers(float size)
  56. {
  57.     return append_block(size);
  58. }
  59.  
  60. // Read a 32-bit unsigned int.
  61. unsigned peek_unsigned(float offset)
  62. {
  63.     struct blocklist* list = head;
  64.     while (list->next && offset < head->offset) {
  65.         list = list->next;
  66.     }
  67.     // Compute bitwise shift for read.
  68.     // If offset=x.5 then our value starts at (pool+x)>>(0.5*CHAR_BIT) = (pool+x)>>4 (if CHAR_BIT=8).
  69.     unsigned trunc = (unsigned)offset;                      // 100.5 -> 100
  70.     unsigned shift = (unsigned)((offset - trunc)*CHAR_BIT); // 100.5 -> 0.5 -> 0.5*CHAR_BIT -> 4.
  71.     unsigned value = *((unsigned*)pool + trunc) >> shift;   // Get the value which starts halfway through byte 100.
  72.     return value;
  73. }
  74.  
  75. // Write a 32-bit unsigned int.
  76. void poke_unsigned(float offset, unsigned value)
  77. {
  78.     struct blocklist* list = head;
  79.     while (list->next && offset < head->offset) {
  80.         list = list->next;
  81.     }
  82.     // Compute bitwise shift for write.
  83.     unsigned trunc = (unsigned)offset;
  84.     unsigned shift = (unsigned)((offset - trunc)*CHAR_BIT);
  85.     *(((unsigned*)pool) << trunc) |= value;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement