Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. // # memory: heap & stack
  2. // - rlyeh, public domain.
  3. //
  4. // # heap: easy arenas (~ stack-scope allocator)
  5. // memory is split in two: bottom-to-top is heap, top-to-bottom is stack.
  6. // heap pointer can be restarted (to bottom) for quick level loading.
  7. // stack pointer can be restarted (to top) when entering a new frame.
  8. //
  9. // |bottom>>|ui|spinners|mecommon|level1|level2|..>>>HEAP_PTR  free-space   STACK_PTR<<<< top|
  10. //
  11. // @todo: odd/even stack frames support
  12. // @todo: void *remark[256]; push_heap(); pop_heap();
  13. //
  14. // [ref] double ended allocator
  15. // [ref] Game Engine Architecture p211..213, p291
  16.  
  17. #include <stdint.h>
  18.  
  19. struct heap_t {
  20.     char *bottom, *heap; // bottom >> ... heap >>
  21.     char *stack, *top;   // << stack  ...  << top
  22. } heap_[32];
  23. void heap_create(int heap_id, void *buffer, int sz) {
  24.     struct heap_t *M = &heap_[ heap_id ];
  25.     M->bottom = M->heap = (char*)buffer;
  26.     M->stack = M->top = (char*)buffer + sz;
  27. }
  28. void* heap_malloc(int heap_id, int sz, int align) {
  29.     struct heap_t *M = &heap_[ heap_id ];
  30.     align = align ? (align - ((uintptr_t)M->heap & (align - 1)) & (align-1)) : 0;
  31.     sz += align;
  32.     if( M->heap + sz >= M->stack ) {
  33.         return 0;
  34.     }
  35.     void *ret = M->heap + align + 0 /*fence*/;
  36.     M->heap += sz;
  37.     return ret;
  38. }
  39. void* heap_alloca(int heap_id, int sz) {
  40.     struct heap_t *M = &heap_[ heap_id ];
  41.     if( M->heap + sz >= M->stack ) {
  42.         return 0;
  43.     }
  44.     M->stack -= sz;
  45.     return M->stack;
  46. }
  47. void heap_clear_heap(int heap_id) {
  48.     struct heap_t *M = &heap_[ heap_id ];
  49.     M->heap = M->bottom; // clear heap
  50. }
  51. void heap_clear_stack(int heap_id) {
  52.     struct heap_t *M = &heap_[ heap_id ];
  53.     M->stack = M->top; // likely every frame
  54. }
  55. int heap_avail(int heap_id) {
  56.     struct heap_t *M = &heap_[ heap_id ];
  57.     return M->stack - M->heap;
  58. }
  59. int heap_used(int heap_id) {
  60.     struct heap_t *M = &heap_[ heap_id ];
  61.     return (M->heap - M->bottom) + (M->top - M->stack);
  62. }
  63.  
  64.  
  65. #include <stdio.h>
  66. char *heap_debug(int heap_id) {
  67.     static char buffer[256];
  68.     sprintf(buffer, "bottom:%p | heap:%p >> ... << stack:%p | top:%p", heap_[heap_id].bottom, heap_[heap_id].heap, heap_[heap_id].stack, heap_[heap_id].top);
  69.     return buffer;
  70. }
  71.  
  72. #ifdef MEMHEAPDEMO
  73. #include <stdlib.h>
  74. int main() {
  75.     enum {
  76.         HEAP_AUDIO = 3,
  77.         HEAP_GRAPHICS = 10,
  78.     };
  79.     void *buffer;
  80.     heap_create(HEAP_AUDIO, buffer = (void*)malloc(1*1024*1024), 1*1024*1024);
  81.  
  82.     void *ptr1 = heap_malloc(HEAP_AUDIO, 515, 0x10000);
  83.     void *ptr2 = heap_malloc(HEAP_AUDIO, 515, 0x10000);
  84.     void *temp = heap_alloca(HEAP_AUDIO, 0x100);
  85.  
  86.     printf("ptr1[%p] ptr2[%p] temp[%p]\n", ptr1, ptr2, temp);
  87.  
  88.     char buf[256];
  89.     puts( heap_debug(HEAP_AUDIO) );
  90.  
  91.     free( buffer );
  92. }
  93. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement