static void *dll_u_realloc(const void *context, void *mem, size_t size) { mps_pool_t *pool_ptr = (mps_pool_t *)context ; header_s *header; size_t oldSize; void *newBlock = NULL ; HQASSERT(pool_ptr != NULL, "ICU realloc context invalid") ; if (mem == NULL) return dll_u_alloc(context, size); /* Get the size and check that it's not a free block. */ header = BLOCK_HEADER(mem); oldSize = header->size; HQASSERT(oldSize != 0, "ICU Realloc block was zero"); if (size == oldSize - HEADER_SIZE) /* Same size, return existing */ return mem; if (size != 0) { newBlock = dll_u_alloc(context, size); if (newBlock != NULL) HqMemCpy(newBlock, mem, min(oldSize - HEADER_SIZE, size)); } header->size = 0; mps_free(*pool_ptr, header, oldSize); return newBlock; }
int finsh_heap_init(void) { /* clear heap to zero */ memset(&finsh_heap[0], 0, sizeof(finsh_heap)); /* init free and alloc list */ free_list = BLOCK_HEADER(&finsh_heap[0]); free_list->length = FINSH_HEAP_MAX - sizeof(struct finsh_block_header); free_list->next = NULL; allocate_list = NULL; return 0; }
/** * split block */ void finsh_block_split(struct finsh_block_header* header, size_t size) { struct finsh_block_header* next; /* * split header into two node: * header->next->... */ next = BLOCK_HEADER((u_char*)header + sizeof(struct finsh_block_header) + size); next->length = header->length - sizeof(struct finsh_block_header) - size; header->length = size; next->next = header->next; header->next = next; }
static void dll_u_free(const void *context, void *mem) { mps_pool_t *pool_ptr = (mps_pool_t *)context ; size_t size; header_s *header; HQASSERT(pool_ptr != NULL, "ICU realloc context invalid") ; if ( *pool_ptr == NULL || mem == NULL ) return ; header = BLOCK_HEADER(mem); size = header->size; /* detect double frees */ HQASSERT(size != 0, "Double free of ICU data"); header->size = 0; mps_free(*pool_ptr, header, size); }
void data_chunks_rgb( uint32_t width, uint32_t height, const uint32_t *data, FILE *fp ) { uint32_t max_line_pos = 3 * width; // each pixel consists of a red byte, a green byte, and a blue byte uint64_t data_len = (uint64_t)height * (max_line_pos + 1); // the data transfered to the file will contain an additional 0 byte at // the start of each line uint32_t line_pos = max_line_pos; uint32_t cur_pixel = *data++; uint32_t pixel_pos = 2; // 2 = red, 1 = green, 0 = blue uint32_t crc, adler_sum1, adler_sum2, mod_delay, num_blocks, block_pos, cur_byte; while (data_len >= MAX_DATA_LEN) { num_blocks = MAX_NUM_BLOCKS; PUTC_INT(MAX_CHUNK_LEN, fp); PUTC_INT(IDAT, fp); crc = IDAT_CRC; PUTC_AND_CRC(0x78, fp, crc); PUTC_AND_CRC(0x01, fp, crc); adler_sum1 = 1; adler_sum2 = 0; mod_delay = 0; while (--num_blocks) { BLOCK_HEADER(fp, crc); block_pos = MAX_BLOCK_LEN; while (block_pos--) { if (line_pos == max_line_pos) { PUTC_AND_CRC(0, fp, crc); adler_sum2 += adler_sum1; line_pos = 0; } else { cur_byte = cur_pixel >> 8 * pixel_pos & 0xFF; PUTC_AND_CRC(cur_byte, fp, crc); ADLER(cur_byte, adler_sum1, adler_sum2); line_pos++; if (pixel_pos-- == 0) { cur_pixel = *data++; pixel_pos = 2; } } if (++mod_delay == MAX_ADDITIONS) { MOD_SUMS(adler_sum1, adler_sum2); mod_delay = 0; } } } MAX_CHUNK_LAST_BLOCK_HEADER(fp, crc); block_pos = MAX_CHUNK_LAST_BLOCK_LEN; while (block_pos--) { if (line_pos == max_line_pos) { PUTC_AND_CRC(0, fp, crc); adler_sum2 += adler_sum1; line_pos = 0; } else { cur_byte = cur_pixel >> 8 * pixel_pos & 0xFF; PUTC_AND_CRC(cur_byte, fp, crc); ADLER(cur_byte, adler_sum1, adler_sum2); line_pos++; if (pixel_pos-- == 0) { cur_pixel = *data++; pixel_pos = 2; } } if (++mod_delay == MAX_ADDITIONS) { MOD_SUMS(adler_sum1, adler_sum2); mod_delay = 0; } } MOD_SUMS(adler_sum1, adler_sum2); PUTC_AND_CRC(adler_sum2 >> 8 & 0xFF, fp, crc); PUTC_AND_CRC(adler_sum2 & 0xFF, fp, crc); PUTC_AND_CRC(adler_sum1 >> 8 & 0xFF, fp, crc); PUTC_AND_CRC(adler_sum1 & 0xFF, fp, crc); crc ^= 0xFFFFFFFF; PUTC_INT(crc, fp); data_len -= MAX_DATA_LEN; } if (data_len) { num_blocks = 1 + ((uint32_t)data_len - 1) / MAX_BLOCK_LEN; // ceil(data_len / MAX_BLOCK_LEN) crc = 6 + 5 * num_blocks + (uint32_t)data_len; // (using crc to avoid making new variable) // 2-byte header "\x78\x01", plus a 5-byte header per deflate // block, plus the remaining data, plus a 4-byte Adler-32 // checksum of the remaining data PUTC_INT(crc, fp); PUTC_INT(IDAT, fp); crc = IDAT_CRC; PUTC_AND_CRC(0x78, fp, crc); PUTC_AND_CRC(0x01, fp, crc); adler_sum1 = 1; adler_sum2 = 0; mod_delay = 0; while (--num_blocks) { BLOCK_HEADER(fp, crc); block_pos = MAX_BLOCK_LEN; while (block_pos--) { if (line_pos == max_line_pos) { PUTC_AND_CRC(0, fp, crc); adler_sum2 += adler_sum1; line_pos = 0; } else { cur_byte = cur_pixel >> 8 * pixel_pos & 0xFF; PUTC_AND_CRC(cur_byte, fp, crc); ADLER(cur_byte, adler_sum1, adler_sum2); line_pos++; if (pixel_pos-- == 0) { cur_pixel = *data++; pixel_pos = 2; } } if (++mod_delay == MAX_ADDITIONS) { MOD_SUMS(adler_sum1, adler_sum2); mod_delay = 0; } } data_len -= MAX_BLOCK_LEN; } LAST_BLOCK_HEADER(fp, crc, data_len); while (data_len--) { if (line_pos == max_line_pos) { PUTC_AND_CRC(0, fp, crc); adler_sum2 += adler_sum1; line_pos = 0; } else { cur_byte = cur_pixel >> 8 * pixel_pos & 0xFF; PUTC_AND_CRC(cur_byte, fp, crc); ADLER(cur_byte, adler_sum1, adler_sum2); line_pos++; if (pixel_pos-- == 0) { cur_pixel = *data++; pixel_pos = 2; } } if (++mod_delay == MAX_ADDITIONS) { MOD_SUMS(adler_sum1, adler_sum2); mod_delay = 0; } } MOD_SUMS(adler_sum1, adler_sum2); PUTC_AND_CRC(adler_sum2 >> 8 & 0xFF, fp, crc); PUTC_AND_CRC(adler_sum2 & 0xFF, fp, crc); PUTC_AND_CRC(adler_sum1 >> 8 & 0xFF, fp, crc); PUTC_AND_CRC(adler_sum1 & 0xFF, fp, crc); crc ^= 0xFFFFFFFF; PUTC_INT(crc, fp); } }