src/atsc3_utils.c
block_t* block_Resize(block_t* src, uint32_t src_size_requested) {
	if(!__block_check_bounaries(__FUNCTION__, src)) return NULL;

	uint32_t src_size_required = __MAX(64, src_size_requested);

	//always over alloc by 1 byte for a null pad
	void* new_block = realloc(src->p_buffer, src_size_required + 8);
	if(!new_block) {
		_ATSC3_UTILS_ERROR("block_Resize: block: %p resize to %u failed, returning NULL", src, src_size_required);
		return NULL;
	} else {
		_ATSC3_UTILS_TRACE("block_Resize: block: %p, p_buffer was: %p, now: %p, resize from %u to %u", src, src->p_buffer, new_block, src->p_size, src_size_required);
		src->p_buffer = (uint8_t*) new_block;
		src->p_size = src_size_required;
		uint32_t to_check_new_i_pos = __MIN(src->p_size - 1, src->i_pos);
		if(to_check_new_i_pos != src->i_pos) {
			_ATSC3_UTILS_WARN("block_Resize: block: %p resize to %u, old pos %u past end of new size, updating to %u", src, src->p_size, src->i_pos, to_check_new_i_pos);
			src->i_pos = to_check_new_i_pos;
		} else {
			_ATSC3_UTILS_TRACE("block_Resize: block: %p, zeroing out from: %u to %u", src, src->i_pos, src->p_size);
			uint32_t to_scrub_len = __MAX(0, (src->p_size - 1 - src->i_pos));
			memset(&src->p_buffer[src->i_pos], 0, to_scrub_len + 8);
		}
	}

	return src;
}
//return will be NULL if realloc failed, but src will still be valid
//this has not been tested with shrinking down the size...
return will be NULL if realloc failed, but src will still be valid
this has not been tested with shrinking down the size
return will be NULL if realloc failed but src will still be valid this has not been tested with shrinking down the size
block_t* block_Resize(block_t* src, uint32_t src_size_requested) {
	if(!__block_check_bounaries(__FUNCTION__, src)) return NULL;
	uint32_t src_size_required = __MAX(64, src_size_requested);
	void* new_block = realloc(src->p_buffer, src_size_required + 8);
	if(!new_block) {
		_ATSC3_UTILS_ERROR("block_Resize: block: %p resize to %u failed, returning NULL", src, src_size_required);
		return NULL;
	} else {
		_ATSC3_UTILS_TRACE("block_Resize: block: %p, p_buffer was: %p, now: %p, resize from %u to %u", src, src->p_buffer, new_block, src->p_size, src_size_required);
		src->p_buffer = (uint8_t*) new_block;
		src->p_size = src_size_required;
		uint32_t to_check_new_i_pos = __MIN(src->p_size - 1, src->i_pos);
		if(to_check_new_i_pos != src->i_pos) {
			_ATSC3_UTILS_WARN("block_Resize: block: %p resize to %u, old pos %u past end of new size, updating to %u", src, src->p_size, src->i_pos, to_check_new_i_pos);
			src->i_pos = to_check_new_i_pos;
		} else {
			_ATSC3_UTILS_TRACE("block_Resize: block: %p, zeroing out from: %u to %u", src, src->i_pos, src->p_size);
			uint32_t to_scrub_len = __MAX(0, (src->p_size - 1 - src->i_pos));
			memset(&src->p_buffer[src->i_pos], 0, to_scrub_len + 8);
		}
	}
	return src;
}
block_t * block_Resize ( block_t * src , uint32_t src_size_requested ) { if ( ! __block_check_bounaries ( __FUNCTION__ , src ) ) return NULL ; uint32_t src_size_required = __MAX ( 64 , src_size_requested ) ; void * new_block = realloc ( src -> p_buffer , src_size_required + 8 ) ; if ( ! new_block ) { _ATSC3_UTILS_ERROR ( " " , src , src_size_required ) ; return NULL ; } else { _ATSC3_UTILS_TRACE ( " " , src , src -> p_buffer , new_block , src -> p_size , src_size_required ) ; src -> p_buffer = ( uint8_t * ) new_block ; src -> p_size = src_size_required ; uint32_t to_check_new_i_pos = __MIN ( src -> p_size - 1 , src -> i_pos ) ; if ( to_check_new_i_pos != src -> i_pos ) { _ATSC3_UTILS_WARN ( " " , src , src -> p_size , src -> i_pos , to_check_new_i_pos ) ; src -> i_pos = to_check_new_i_pos ; } else { _ATSC3_UTILS_TRACE ( " " , src , src -> i_pos , src -> p_size ) ; uint32_t to_scrub_len = __MAX ( 0 , ( src -> p_size - 1 - src -> i_pos ) ) ; memset ( & src -> p_buffer [ src -> i_pos ] , 0 , to_scrub_len + 8 ) ; } } return src ; }
return will be NULL if realloc failed, but src will still be valid
this has not been tested with shrinking down the size...
return will be NULL if realloc failed but src will still be valid this has not been tested with shrinking down the size ...
//always over alloc by 1 byte for a null pad