Write a completed interior page to disk and record it as a parent. Returns updated parent_count, or -1 on allocation failure.
| 695 | // Write a completed interior page to disk and record it as a parent. |
| 696 | // Returns updated parent_count, or -1 on allocation failure. |
| 697 | static int write_interior_page(PageBuilder *pb, uint8_t *page, int cell_count, int content_offset, |
| 698 | uint32_t right_child_page, const PageRef *children, |
| 699 | int right_child_idx, bool is_index, PageRef **parents, |
| 700 | int parent_count, int *parent_cap) { |
| 701 | pb->next_page = cbm_skip_pending_byte(pb->next_page); |
| 702 | uint32_t pnum = pb->next_page++; |
| 703 | page[0] = is_index ? INTERIOR_INDEX_FLAG : INTERIOR_TABLE_FLAG; |
| 704 | put_u16(page + HDR_FREEBLOCK_OFF, 0); |
| 705 | put_u16(page + HDR_CELLCOUNT_OFF, (uint16_t)cell_count); |
| 706 | put_u16(page + HDR_CONTENT_OFF, (uint16_t)content_offset); |
| 707 | page[HDR_FRAGBYTES_OFF] = 0; |
| 708 | put_u32(page + HDR_RIGHTCHILD_OFF, right_child_page); |
| 709 | |
| 710 | (void)fseek(pb->fp, (long)(pnum - SKIP_ONE) * CBM_PAGE_SIZE, SEEK_SET); |
| 711 | (void)fwrite(page, SKIP_ONE, CBM_PAGE_SIZE, pb->fp); |
| 712 | |
| 713 | if (parent_count >= *parent_cap) { |
| 714 | int old_pcap = *parent_cap; |
| 715 | *parent_cap = old_pcap == 0 ? INITIAL_PARENT_CAP : old_pcap * GROWTH_FACTOR; |
| 716 | PageRef *tmp = (PageRef *)realloc(*parents, *parent_cap * sizeof(PageRef)); |
| 717 | if (!tmp) { |
| 718 | free(*parents); |
| 719 | *parents = NULL; |
| 720 | return CBM_NOT_FOUND; |
| 721 | } |
| 722 | *parents = tmp; |
| 723 | memset(&(*parents)[old_pcap], 0, |
| 724 | ((size_t)*parent_cap - (size_t)old_pcap) * sizeof(PageRef)); |
| 725 | } |
| 726 | (*parents)[parent_count].page_num = pnum; |
| 727 | (*parents)[parent_count].max_key = children[right_child_idx].max_key; |
| 728 | if (is_index && children[right_child_idx].sep_cell) { |
| 729 | int slen = children[right_child_idx].sep_cell_len; |
| 730 | (*parents)[parent_count].sep_cell = (uint8_t *)malloc(slen); |
| 731 | memcpy((*parents)[parent_count].sep_cell, children[right_child_idx].sep_cell, slen); |
| 732 | (*parents)[parent_count].sep_cell_len = slen; |
| 733 | } else { |
| 734 | (*parents)[parent_count].sep_cell = NULL; |
| 735 | (*parents)[parent_count].sep_cell_len = 0; |
| 736 | } |
| 737 | return parent_count + SKIP_ONE; |
| 738 | } |
| 739 | |
| 740 | // Free a PageRef array (sep_cell allocations), unless it's the original leaves. |
| 741 | static void free_children(PageRef *children, int child_count, const PageRef *leaves) { |
no test coverage detected