Allocate a new heap
| 867 | |
| 868 | //! Allocate a new heap |
| 869 | static heap_t* |
| 870 | _memory_allocate_heap(void) { |
| 871 | heap_t* heap; |
| 872 | heap_t* next_heap; |
| 873 | //Try getting an orphaned heap |
| 874 | atomic_thread_fence_acquire(); |
| 875 | do { |
| 876 | heap = atomic_load_ptr(&_memory_orphan_heaps); |
| 877 | if (!heap) |
| 878 | break; |
| 879 | next_heap = heap->next_orphan; |
| 880 | } |
| 881 | while (!atomic_cas_ptr(&_memory_orphan_heaps, next_heap, heap)); |
| 882 | |
| 883 | if (heap) { |
| 884 | heap->next_orphan = 0; |
| 885 | return heap; |
| 886 | } |
| 887 | |
| 888 | //Map in pages for a new heap |
| 889 | heap = _memory_map(2); |
| 890 | memset(heap, 0, sizeof(heap_t)); |
| 891 | |
| 892 | //Get a new heap ID |
| 893 | do { |
| 894 | heap->id = atomic_incr32(&_memory_heap_id); |
| 895 | if (_memory_heap_lookup(heap->id)) |
| 896 | heap->id = 0; |
| 897 | } |
| 898 | while (!heap->id); |
| 899 | |
| 900 | //Link in heap in heap ID map |
| 901 | size_t list_idx = heap->id % HEAP_ARRAY_SIZE; |
| 902 | do { |
| 903 | next_heap = atomic_load_ptr(&_memory_heaps[list_idx]); |
| 904 | heap->next_heap = next_heap; |
| 905 | } |
| 906 | while (!atomic_cas_ptr(&_memory_heaps[list_idx], heap, next_heap)); |
| 907 | |
| 908 | return heap; |
| 909 | } |
| 910 | |
| 911 | //! Add a span to a double linked list |
| 912 | static void |
no test coverage detected