Пример #1
0
/**
 *	Ensure the bytes buffer can handle `capacity` bytes.
 *		
 *	If `resize` is true and `capacity` exceeds the capacity of the bytes's 
 *	buffer, then resize the capacity of the buffer to `capacity` bytes. If the 
 *	buffer was heap allocated, then `cf_realloc()` will be used to resize. If the 
 *	buffer was stack allocated, it will be converted to a heap allocated buffer 
 *	using cf_malloc() and then its contents will be copied into the new heap 
 *	allocated  buffer.
 *
 *	If `resize` is false, and if the capacity is not sufficient, then return
 *	false.
 *	
 *	~~~~~~~~~~{.c}
 *	as_bytes_ensure(&bytes, 100, true);
 *	~~~~~~~~~~
 *	
 *	@param bytes	The bytes to ensure the capacity of.
 *	@param capacity	The total number of bytes to ensure bytes can handle.
 *	@param resize	If true and capacity is not sufficient, then resize the buffer.
 *
 *	@return On success, true. Otherwise an error occurred.
 */
bool as_bytes_ensure(as_bytes * bytes, uint32_t capacity, bool resize)
{
	if ( capacity <= bytes->capacity ) return true;
	if ( !resize ) return false;

	uint8_t * buffer = NULL;

	if ( bytes->free ) {
		// this is a previously cf_malloc'd value
		buffer = cf_realloc(bytes->value, capacity);
		if ( !buffer ) {
			// allocation failed, so return false.
			return false;
		}
	}
	else {
		// this is a previously stack alloc'd value
		buffer = cf_malloc(capacity);
		if ( !buffer ) {
			// allocation failed, so return false.
			return false;
		}
		// copy the bytes
		memcpy(buffer, bytes->value, bytes->size);
	}

	bytes->free = true;
	bytes->value = buffer;
	bytes->capacity = capacity;

	return true;
}
Пример #2
0
as_particle *as_particle_set_blob(as_particle *p, as_particle_type type, void *data, uint32_t sz, bool data_in_memory)
{
	as_particle_blob *pb = (as_particle_blob *)p;
	if (data_in_memory) {
		if (pb && (sz != pb->sz)) {
			if (sz > pb->sz) {
				cf_free(pb);
				pb = 0;
			}
			else {
				pb = cf_realloc(pb, sizeof(as_particle_blob) + sz);
			}
		}

		if (! pb) {
			pb = cf_malloc(sizeof(as_particle_blob) + sz);
		}
	}

	pb->type = type;
	pb->sz = sz;

	memcpy(pb->data, data, sz);

	return((as_particle *)pb);
}
Пример #3
0
//
// Make sure the buf has enough bytes for whatever you're up to.
int
cf_buf_builder_reserve_internal(cf_buf_builder **bb_r, size_t sz)
{
	cf_buf_builder *bb = *bb_r;

	// see if we need more space
	size_t new_sz = cf_dyn_buf_get_newsize(bb->alloc_sz, bb->used_sz, sz);
	if (new_sz > bb->alloc_sz) {
		if (bb->alloc_sz - bb->used_sz < MAX_BACKOFF) {
			bb = cf_realloc(bb, new_sz);
			if (!bb)	return(-1);
		}
		else {
			// Only possible if buffer was reset. Avoids potential expensive
			// copy within realloc.
			cf_buf_builder	*_t = cf_malloc(new_sz);
			if (!_t)	return(-1);
			memcpy(_t->buf, bb->buf, bb->used_sz);
			_t->used_sz = bb->used_sz;
			cf_free(bb);
			bb = _t;
		}
		bb->alloc_sz = new_sz - sizeof(cf_buf_builder);
		*bb_r = bb;
	}
	return(0);
}
Пример #4
0
as_particle *as_particle_set_string(as_particle *p, as_particle_type type, void *data, uint32_t sz, bool data_in_memory)
{
	as_particle_string *ps = (as_particle_string *)p;

	if (data_in_memory) {
		if (ps && (sz != ps->sz)) {
			if (sz > ps->sz) {
				cf_free(ps);
				ps = 0;
			}
			else {
				ps = cf_realloc(ps, sizeof(as_particle_string) + sz);
			}
		}

		if (! ps) {
			ps = cf_malloc(sizeof(as_particle_string) + sz);
		}
	}

	ps->type = AS_PARTICLE_TYPE_STRING;
	ps->sz = sz;

	memcpy(ps->data, data, sz);

	return((as_particle *)ps);
}
Пример #5
0
static int cf_vector_resize(cf_vector *v, uint32_t new_sz) {
	if (v->flags & VECTOR_FLAG_BIGRESIZE) {
		if (new_sz < 50)	new_sz = 50;
	}
	else if (new_sz == 0) {
		new_sz = 2;
	}
	uint8_t *_t;
	if (v->vector == 0 || v->stack_vector) {
		_t = cf_malloc(new_sz * v->value_len);
		if (!_t)	return(-1);
		if (v->stack_vector) {
			memcpy(_t, v->vector, v->alloc_len * v->value_len);
			v->stack_vector = false;
		}
	}
	else
		_t = cf_realloc(v->vector, (new_sz) * v->value_len);
	if (!_t)	return(-1);
	v->vector = _t;
	if (v->flags & VECTOR_FLAG_INITZERO)
		memset(v->vector + (v->alloc_len * v->value_len), 0, (new_sz - v->alloc_len) * v->value_len);
	v->alloc_len = new_sz;
	return(0);
}
Пример #6
0
void insert_as_predexp_array(as_predexp_array *a, as_predexp_base * element) {
  if (a->capacity == a->size) {
    a->size *= 2;
    a->array = (as_predexp_base **) cf_realloc(a->array, a->size * sizeof(as_bin_name));
  }
  a->array[a->capacity++] = element;
}
Пример #7
0
void cf_vector_compact(cf_vector *v) {
	if (v->flags & VECTOR_FLAG_BIGLOCK)
		VECTOR_LOCK(v);
	if (v->alloc_len && (v->len != v->alloc_len)) {
		v->vector = cf_realloc(v->vector, v->len * v->alloc_len);
		v->alloc_len = v->len;
	}
	if (v->flags & VECTOR_FLAG_BIGLOCK)
		VECTOR_UNLOCK(v);
	return;
}
Пример #8
0
/*
 * Returns
 *      arr pointer in case of successful operation
 *      NULL in case of failure
 */
static ai_arr *
ai_arr_expand(ai_arr *arr)
{
	int size = arr->capacity * 2;

	if (size > AI_ARR_MAX_SIZE) {
		cf_crash(AS_SINDEX, "Refusing to expand ai_arr to %d (beyond limit of %d)", size, AI_ARR_MAX_SIZE);
	}

	arr = cf_realloc(arr, sizeof(ai_arr) + (size * CF_DIGEST_KEY_SZ));
	//cf_info(AS_SINDEX, "EXPAND REALLOC to %d", size);
	arr->capacity = size;
	return arr;
}
Пример #9
0
//
// Make sure the buf has enough bytes for whatever you're up to.
int
cf_buf_builder_reserve_internal(cf_buf_builder **bb_r, size_t sz)
{
	cf_buf_builder *bb = *bb_r;

	// see if we need more space
	size_t new_sz = cf_dyn_buf_get_newsize(bb->alloc_sz, bb->used_sz, sz);
	if (new_sz > bb->alloc_sz) {
		bb = cf_realloc(bb, new_sz);
		if (!bb)	return(-1);
		bb->alloc_sz = new_sz - sizeof(cf_buf_builder);
		*bb_r = bb;
	}
	return(0);
}
Пример #10
0
void
as_vector_increase_capacity(as_vector* vector)
{
	if (vector->flags & FLAGS_HEAP) {
		vector->capacity *= 2;
		vector->list = cf_realloc(vector->list, vector->capacity * vector->item_size);
	}
	else {
		uint32_t capacity = vector->capacity * 2;
		void* tmp = cf_malloc(capacity * vector->item_size);
		memcpy(tmp, vector->list, vector->capacity * vector->item_size);
		vector->list = tmp;
		vector->capacity = capacity;
		vector->flags |= FLAGS_HEAP;
	}
}
Пример #11
0
static ai_arr *
ai_arr_shrink(ai_arr *arr)
{
	int size = arr->capacity / 2;

	// Do not shrink if the capacity not greater than 4
	// or if the halving capacity is not a extra level
	// over currently used
	if ((arr->capacity <= 4) ||
			(size < arr->used * 2)) {
		return arr;
	}

	ai_arr * temp_arr = cf_realloc(arr, sizeof(ai_arr) + (size * CF_DIGEST_KEY_SZ));
	temp_arr->capacity = size;
	return temp_arr;
}
Пример #12
0
void
as_bin_allocate_bin_space(as_record *r, as_storage_rd *rd, int32_t delta) {
	if (rd->n_bins == 0) {
		rd->n_bins = (uint16_t)delta;

		as_bin_space* bin_space = (as_bin_space*)
				cf_malloc(sizeof(as_bin_space) + (rd->n_bins * sizeof(as_bin)));

		rd->bins = bin_space->bins;
		as_bin_set_all_empty(rd);

		bin_space->n_bins = rd->n_bins;
		as_index_set_bin_space(r, bin_space);
	}
	else {
		uint16_t new_n_bins = (uint16_t)((int32_t)rd->n_bins + delta);

		if (delta < 0) {
			as_record_clean_bins_from(rd, new_n_bins);
		}

		uint16_t old_n_bins = rd->n_bins;

		rd->n_bins = new_n_bins;

		if (new_n_bins != 0) {
			as_bin_space* bin_space = (as_bin_space*)
					cf_realloc((void*)as_index_get_bin_space(r), sizeof(as_bin_space) + (rd->n_bins * sizeof(as_bin)));

			rd->bins = bin_space->bins;

			if (delta > 0) {
				as_bin_set_empty_from(rd, old_n_bins);
			}

			bin_space->n_bins = rd->n_bins;
			as_index_set_bin_space(r, bin_space);
		}
		else {
			cf_free((void*)as_index_get_bin_space(r));
			as_index_set_bin_space(r, NULL);
			rd->bins = NULL;
		}
	}
}
Пример #13
0
static bool
as_string_builder_append_increase(as_string_builder* sb, const char* src)
{
	uint32_t remaining_len = (uint32_t)strlen(src);
	uint32_t capacity = sb->capacity * 2;
	uint32_t initial_len = sb->capacity - 1;
	uint32_t total_len = initial_len + remaining_len;
	uint32_t capacity_min = total_len + 1;
	
	if (capacity < capacity_min) {
		capacity = capacity_min;
	}
	
	if (sb->free) {
		// String already allocated on heap.  Realloc.
		char* data = cf_realloc(sb->data, capacity);
		
		if (data) {
			memcpy(&data[initial_len], src, remaining_len);
			data[total_len] = 0;
			sb->data = data;
			sb->capacity = capacity;
			sb->length = total_len;
			return true;
		}
	}
	else {
		// String allocated on stack.  Transfer to heap.
		char* data = cf_malloc(capacity);
		
		if (data) {
			memcpy(data, sb->data, initial_len);
			memcpy(&data[initial_len], src, remaining_len);
			data[total_len] = 0;
			sb->data = data;
			sb->capacity = capacity;
			sb->length = total_len;
			sb->free = true;
			return true;
		}
	}
	return false;
}
Пример #14
0
//------------------------------------------------
// Clear, re-scale/re-size a linear histogram.
//
void
linear_hist_reset(linear_hist *h, uint32_t start, uint32_t max_offset,
		uint32_t num_buckets)
{
	if (h->num_buckets == num_buckets) {
		linear_hist_clear(h, start, max_offset);
		return;
	}

	uint32_t *counts = cf_realloc(h->counts, sizeof(uint32_t) * num_buckets);

	if (! counts) {
		cf_warning(AS_INFO, "failed linear_hist_reset - realloc failed");
		linear_hist_clear(h, start, max_offset);
		return;
	}

	h->num_buckets = num_buckets;
	h->counts = counts;
	linear_hist_clear(h, start, max_offset);
}
Пример #15
0
void
cf_dyn_buf_reserve_internal(cf_dyn_buf *db, size_t sz)
{
	size_t new_sz = get_new_size(db->alloc_sz, db->used_sz, sz);

	if (new_sz > db->alloc_sz) {
		uint8_t	*_t;

		if (db->is_stack) {
			_t = cf_malloc(new_sz);
			memcpy(_t, db->buf, db->used_sz);
			db->is_stack = false;
		}
		else {
			_t = cf_realloc(db->buf, new_sz);
		}

		db->buf = _t;
		db->alloc_sz = new_sz;
	}
}
Пример #16
0
//
// Make sure the buf has enough bytes for whatever you're up to.
int
cf_dyn_buf_reserve_internal(cf_dyn_buf *db, size_t sz)
{
	// see if we need more space
	size_t new_sz = cf_dyn_buf_get_newsize(db->alloc_sz, db->used_sz, sz);
	if (new_sz > db->alloc_sz) {
		uint8_t	*_t;
		if (db->is_stack) {
			_t = cf_malloc(new_sz);
			if (!_t)	return(-1);
			memcpy(_t, db->buf, db->used_sz);
			db->is_stack = false;
			db->buf = _t;
		}
		else {
			_t = cf_realloc(db->buf, new_sz);
			if (!_t)	return(-1);
			db->buf = _t;
		}
		db->alloc_sz = new_sz;
	}
	return(0);
}
Пример #17
0
int
expand_chunk(ldt_record *lrecord)
{
	uint64_t   new_size  = lrecord->max_chunks + 1;
	void *old_chunk      = lrecord->chunk;

	if (lrecord->max_chunks) {
		lrecord->chunk = cf_realloc(lrecord->chunk, sizeof(ldt_slot_chunk) * new_size);
	} else {
		lrecord->chunk = cf_malloc(sizeof(ldt_slot_chunk) * new_size);
	}

	if (lrecord->chunk == NULL) {
		cf_warning(AS_LDT, "expand_chunk: Allocation Error !! [Chunk cannot be allocated ]... Fail");
		lrecord->chunk = old_chunk;
		return -1;
	}

	lrecord->chunk[lrecord->max_chunks].slots = create_slot();
	if (lrecord->chunk[lrecord->max_chunks].slots == NULL) {
		cf_warning(AS_LDT, "expand_chunk: Allocation Error !! [Slot cannot be allocated ]... Fail");
		cf_free(lrecord->chunk);
		lrecord->chunk = old_chunk;
		return -1;
	}

	for (int i = lrecord->max_chunks; i < new_size; i++) {
		for(int j = 0; j < LDT_SLOT_CHUNK_SIZE; j++) {
			lrecord->chunk[i].slots[j].inuse = false;
		}
	}

	cf_detail(AS_LDT, "Bumping up chunks from %"PRIu64" to %"PRIu64"", lrecord->max_chunks, new_size);
	lrecord->max_chunks = new_size;
	return 0;
}
Пример #18
0
static void* cert_blacklist_read(const char * path)
{
	FILE* fp = fopen(path, "r");
	if (fp == NULL) {
		as_log_warn("Failed to open cert blacklist '%s': %s",
					path, strerror(errno));
		return NULL;
	}

	size_t capacity = 32;
	size_t sz = sizeof(cert_blacklist) + (capacity * sizeof(cert_spec));
	cert_blacklist* cbp = cf_malloc(sz);
	cbp->ncerts = 0;

	char buffer[1024];
	while (true) {
		char* line = fgets(buffer, sizeof(buffer), fp);
		if (! line) {
			break;
		}

		// Lines begining with a '#' are comments.
		if (line[0] == '#') {
			continue;
		}
		
		char* saveptr = NULL;
		char* hex_serial = strtok_r(line, " \t\r\n", &saveptr);
		if (! hex_serial) {
			continue;
		}

		// Skip all additional whitespace.
		while (isspace(*saveptr)) {
			++saveptr;
		}

		// Everything to the end of the line is issuer name.  Note we
		// do not consider whitespace a separator anymore.
		char* issuer_name = strtok_r(NULL, "\r\n", &saveptr);

		// Do we need more room?
		if (cbp->ncerts == capacity) {
			capacity *= 2;
			size_t sz = sizeof(cert_blacklist) + (capacity * sizeof(cert_spec));
			cbp = cf_realloc(cbp, sz);
		}

		cbp->certs[cbp->ncerts].hex_serial = cf_strdup(hex_serial);
		cbp->certs[cbp->ncerts].issuer_name =
			issuer_name ? cf_strdup(issuer_name) : NULL;

		cbp->ncerts++;
	}

	qsort(cbp->certs, cbp->ncerts, sizeof(cert_spec), cert_spec_compare);

	fclose(fp);
	
	return cbp;
}