Lines Matching defs:heap

237 dump_bin_list(heap_allocator *heap)
239 for (uint32 i = 0; i < heap->bin_count; i++)
240 dump_bin(&heap->bins[i]);
246 dump_allocator_areas(heap_allocator *heap)
248 heap_area *area = heap->all_areas;
263 dump_allocator(heap_allocator *heap, bool areas, bool bins)
267 "empty_areas: %" B_PRIu32 "\n", heap, heap->name, heap->page_size,
268 heap->bin_count, heap->total_pages, heap->total_free_pages,
269 heap->empty_areas);
272 dump_allocator_areas(heap);
274 dump_bin_list(heap);
284 heap_allocator *heap = sHeaps[classIndex];
287 heap_area *area = heap->all_areas;
295 addr_t base = area->base + i * heap->page_size;
296 if (page->bin_index < heap->bin_count) {
300 = heap->bins[page->bin_index].element_size;
337 == heap->bin_count
343 * heap->page_size - sizeof(heap_leak_check_info));
375 heap_allocator *heap = sHeaps[classIndex];
376 ReadLocker areaReadLocker(heap->area_lock);
377 for (uint32 i = 0; i < heap->bin_count; i++)
378 mutex_lock(&heap->bins[i].lock);
379 MutexLocker pageLocker(heap->page_lock);
382 heap_area *area = heap->all_areas;
390 addr_t base = area->base + i * heap->page_size;
391 if (page->bin_index < heap->bin_count) {
395 = heap->bins[page->bin_index].element_size;
437 == heap->bin_count
443 * heap->page_size - sizeof(heap_leak_check_info));
445 if (info->size > pageCount * heap->page_size
449 pageCount, pageCount * heap->page_size);
472 for (uint32 i = 0; i < heap->bin_count; i++)
473 mutex_unlock(&heap->bins[i].lock);
479 heap_validate_heap(heap_allocator *heap)
481 ReadLocker areaReadLocker(heap->area_lock);
482 for (uint32 i = 0; i < heap->bin_count; i++)
483 mutex_lock(&heap->bins[i].lock);
484 MutexLocker pageLocker(heap->page_lock);
488 heap_area *area = heap->all_areas;
541 area = heap->areas;
557 area = heap->all_areas;
567 for (uint32 i = 0; i < heap->bin_count; i++) {
568 heap_bin *bin = &heap->bins[i];
573 area = heap->all_areas;
618 addr_t pageBase = area->base + page->index * heap->page_size;
621 || (addr_t)element >= pageBase + heap->page_size)
648 for (uint32 i = 0; i < heap->bin_count; i++)
649 mutex_unlock(&heap->bins[i].lock);
657 heap_add_area(heap_allocator *heap, area_id areaID, addr_t base, size_t size)
665 uint32 pageCount = size / heap->page_size;
676 pageCount = area->size / heap->page_size;
696 WriteLocker areaWriteLocker(heap->area_lock);
697 MutexLocker pageLocker(heap->page_lock);
698 if (heap->areas == NULL) {
699 // it's the only (empty) area in that heap
701 heap->areas = area;
704 heap_area *lastArea = heap->areas;
713 if (heap->all_areas == NULL || heap->all_areas->base < area->base) {
714 area->all_next = heap->all_areas;
715 heap->all_areas = area;
717 heap_area *insert = heap->all_areas;
725 heap->total_pages += area->page_count;
726 heap->total_free_pages += area->free_page_count;
731 heap->empty_areas++;
740 heap_remove_area(heap_allocator *heap, heap_area *area)
743 panic("tried removing heap area that has still pages in use");
748 panic("tried removing the last non-full heap area");
752 if (heap->areas == area)
753 heap->areas = area->next;
759 if (heap->all_areas == area)
760 heap->all_areas = area->all_next;
762 heap_area *previous = heap->all_areas;
773 panic("removing heap area that is not in all list");
776 heap->total_pages -= area->page_count;
777 heap->total_free_pages -= area->free_page_count;
786 heap_allocator *heap = (heap_allocator *)base;
790 heap->name = name;
791 heap->page_size = heapClass->page_size;
792 heap->total_pages = heap->total_free_pages = heap->empty_areas = 0;
793 heap->areas = heap->all_areas = NULL;
794 heap->bins = (heap_bin *)base;
796 heap->bin_count = 0;
798 uint32 count = heap->page_size / heapClass->min_bin_size;
800 if (heap->bin_count >= 32)
801 panic("heap configuration invalid - max bin count reached\n");
803 binSize = (heap->page_size / count) & ~(heapClass->bin_alignment - 1);
806 if (heap->page_size - count * binSize > heapClass->max_waste_per_page)
809 heap_bin *bin = &heap->bins[heap->bin_count];
810 mutex_init(&bin->lock, "heap bin lock");
812 bin->max_free_count = heap->page_size / binSize;
814 heap->bin_count++;
817 base += heap->bin_count * sizeof(heap_bin);
818 size -= heap->bin_count * sizeof(heap_bin);
820 rw_lock_init(&heap->area_lock, "heap area lock");
821 mutex_init(&heap->page_lock, "heap page lock");
823 heap_add_area(heap, -1, base, size);
824 return heap;
829 heap_free_pages_added(heap_allocator *heap, heap_area *area, uint32 pageCount)
832 heap->total_free_pages += pageCount;
835 // we need to add ourselfs to the area list of the heap
837 area->next = heap->areas;
840 heap->areas = area;
854 if (heap->areas == area)
855 heap->areas = area->next;
866 heap->empty_areas++;
871 heap_free_pages_removed(heap_allocator *heap, heap_area *area, uint32 pageCount)
875 heap->empty_areas--;
879 heap->total_free_pages -= pageCount;
887 if (heap->areas == area)
888 heap->areas = area->next;
908 if (heap->areas == insert)
909 heap->areas = area;
943 heap_allocate_contiguous_pages(heap_allocator *heap, uint32 pageCount,
946 heap_area *area = heap->areas;
957 if (alignment > heap->page_size) {
959 / heap->page_size;
960 step = alignment / heap->page_size;
990 page->bin_index = heap->bin_count;
999 heap_free_pages_removed(heap, area, pageCount);
1022 heap_raw_alloc(heap_allocator *heap, size_t size, size_t alignment)
1024 INFO(("heap %p: allocate %" B_PRIuSIZE " bytes from raw pages with"
1025 " alignment %" B_PRIuSIZE "\n", heap, size, alignment));
1027 uint32 pageCount = (size + heap->page_size - 1) / heap->page_size;
1029 MutexLocker pageLocker(heap->page_lock);
1030 heap_page *firstPage = heap_allocate_contiguous_pages(heap, pageCount,
1033 INFO(("heap %p: found no contiguous pages to allocate %" B_PRIuSIZE " bytes\n",
1034 heap, size));
1038 addr_t address = firstPage->area->base + firstPage->index * heap->page_size;
1039 heap_add_leak_check_info(address, pageCount * heap->page_size, size);
1045 heap_allocate_from_bin(heap_allocator *heap, uint32 binIndex, size_t size)
1047 heap_bin *bin = &heap->bins[binIndex];
1048 INFO(("heap %p: allocate %" B_PRIuSIZE " bytes from bin %" B_PRIu32
1050 heap, size, binIndex, bin->element_size));
1055 MutexLocker pageLocker(heap->page_lock);
1056 heap_area *area = heap->areas;
1058 INFO(("heap %p: no free pages to allocate %" B_PRIuSIZE " bytes\n", heap,
1070 heap_free_pages_removed(heap, area, 1);
1094 address = (void *)(page->area->base + page->index * heap->page_size
1114 heap_memalign(heap_allocator *heap, size_t alignment, size_t size)
1136 for (uint32 i = 0; i < heap->bin_count; i++) {
1137 if (size <= heap->bins[i].element_size
1138 && is_valid_alignment(heap->bins[i].element_size)) {
1139 address = heap_allocate_from_bin(heap, i, size);
1144 for (uint32 i = 0; i < heap->bin_count; i++) {
1145 if (size <= heap->bins[i].element_size) {
1146 address = heap_allocate_from_bin(heap, i, size);
1154 address = heap_raw_alloc(heap, size, alignment);
1176 heap_free(heap_allocator *heap, void *address)
1181 ReadLocker areaReadLocker(heap->area_lock);
1182 heap_area *area = heap->all_areas;
1209 / heap->page_size];
1214 if (page->bin_index > heap->bin_count) {
1220 addr_t pageBase = area->base + page->index * heap->page_size;
1221 if (page->bin_index < heap->bin_count) {
1223 heap_bin *bin = &heap->bins[page->bin_index];
1280 MutexLocker pageLocker(heap->page_lock);
1284 heap_free_pages_added(heap, area, 1);
1319 MutexLocker pageLocker(heap->page_lock);
1322 if (!page[i].in_use || page[i].bin_index != heap->bin_count
1338 size_t allocationSize = pageCount * heap->page_size;
1363 heap_free_pages_added(heap, area, pageCount);
1368 if (heap->empty_areas > 1) {
1369 WriteLocker areaWriteLocker(heap->area_lock);
1370 MutexLocker pageLocker(heap->page_lock);
1372 area = heap->areas;
1373 while (area != NULL && heap->empty_areas > 1) {
1377 && heap_remove_area(heap, area) == B_OK) {
1379 heap->empty_areas--;
1391 heap_realloc(heap_allocator *heap, void *address, void **newAddress,
1394 ReadLocker areaReadLocker(heap->area_lock);
1395 heap_area *area = heap->all_areas;
1423 / heap->page_size];
1424 if (page->bin_index > heap->bin_count) {
1433 if (page->bin_index < heap->bin_count) {
1435 heap_bin *bin = &heap->bins[page->bin_index];
1438 minSize = heap->bins[page->bin_index - 1].element_size + 1;
1443 maxSize = heap->page_size;
1445 MutexLocker pageLocker(heap->page_lock);
1447 if (!page[i].in_use || page[i].bin_index != heap->bin_count
1451 minSize += heap->page_size;
1452 maxSize += heap->page_size;
1466 MutexLocker pageLocker(heap->page_lock);
1487 heap_free(heap, address);
1509 heap_get_allocation_info(heap_allocator *heap, void *address, size_t *size,
1512 ReadLocker areaReadLocker(heap->area_lock);
1513 heap_area *area = heap->all_areas;
1538 / heap->page_size];
1540 if (page->bin_index > heap->bin_count) {
1547 addr_t pageBase = area->base + page->index * heap->page_size;
1548 if (page->bin_index < heap->bin_count) {
1550 heap_bin *bin = &heap->bins[page->bin_index];
1581 MutexLocker pageLocker(heap->page_lock);
1584 if (!page[i].in_use || page[i].bin_index != heap->bin_count
1591 size_t allocationSize = pageCount * heap->page_size;
1617 heap_create_new_heap_area(heap_allocator *heap, const char *name, size_t size)
1623 INFO(("heap: couldn't allocate heap area \"%s\"\n", name));
1627 heap_add_area(heap, heapArea, (addr_t)address, size);
1629 heap_validate_heap(heap);
1655 sWallCheckThread = spawn_thread(heap_wall_checker, "heap wall checker",
1734 panic("heap: failed to create area for guarded allocation of %" B_PRIuSIZE
1741 panic("heap: failed to protect guard page: %s\n", strerror(errno));
1759 INFO(("heap: allocated area %" B_PRId32 " for guarded allocation of %" B_PRIuSIZE " bytes\n",
1774 heap_allocator *heap = sHeaps[i];
1775 if (heap_get_allocation_info(heap, address, size, thread) == B_OK)
1807 // This will locate the heap base at 384 MB and reserve the next 1152 MB
1809 // size of the heap is guaranteed until the space is really needed.
1814 area_id heapArea = create_area("heap", (void **)&heapBase,
1856 panic("heap: failed to create area for huge allocation of %" B_PRIuSIZE
1877 INFO(("heap: allocated area %" B_PRId32 " for huge allocation of %" B_PRIuSIZE " bytes\n",
1889 heap_allocator *heap = sHeaps[heapClass];
1890 void *result = heap_memalign(heap, alignment, size);
1893 heap_create_new_heap_area(heap, "additional heap area", HEAP_GROW_SIZE);
1894 result = heap_memalign(heap, alignment, size);
1898 heap_validate_heap(heap);
1901 panic("heap: heap has run out of memory trying to allocate %" B_PRIuSIZE " bytes\n",
1926 heap_allocator *heap = sHeaps[i];
1927 if (heap_free(heap, address) == B_OK) {
1929 heap_validate_heap(heap);
1968 heap_allocator *heap = sHeaps[i];
1969 if (heap_realloc(heap, address, &newAddress, newSize) == B_OK) {
1971 heap_validate_heap(heap);