Merged in issue-47 (pull request #4)

free_buffer to check_and_free_buffer
This commit is contained in:
Kevin Greenan
2014-06-04 10:21:56 -07:00

View File

@@ -228,15 +228,15 @@ void * alloc_zeroed_buffer(int size)
/**
* Deallocate memory buffer. This methods returns NULL so that you can free
* and reset a buffer using a single line as follows:
* Deallocate memory buffer if it's not NULL. This methods returns NULL so
* that you can free and reset a buffer using a single line as follows:
*
* my_ptr = free_buffer(my_ptr);
* my_ptr = check_and_free_buffer(my_ptr);
*
* @return NULL
*/
static
void * free_buffer(void * buf)
void * check_and_free_buffer(void * buf)
{
if (buf) PyMem_Free(buf);
@@ -348,7 +348,7 @@ PyObject * alloc_zero_string(int size)
/* Create the python value to return */
zero_string = PY_BUILDVALUE_OBJ_LEN(tmp_data, size);
free_buffer(tmp_data);
check_and_free_buffer(tmp_data);
return zero_string;
}
@@ -820,7 +820,7 @@ pyeclib_c_init(PyObject *self, PyObject *args)
/* Clean up the allocated memory on error, or update the ref count */
if (pyeclib_obj_handle == NULL) {
PyErr_SetString(PyECLibError, "Could not encapsulate pyeclib_handle into Python object in pyeclib.init");
free_buffer(pyeclib_handle);
check_and_free_buffer(pyeclib_handle);
} else {
Py_INCREF(pyeclib_obj_handle);
}
@@ -846,7 +846,7 @@ pyeclib_c_destructor(PyObject *obj)
if (pyeclib_handle == NULL) {
PyErr_SetString(PyECLibError, "Attempted to free an invalid reference to pyeclib_handle");
} else {
free_buffer(pyeclib_handle);
check_and_free_buffer(pyeclib_handle);
}
return;
@@ -879,22 +879,23 @@ pyeclib_c_destructor(PyObject *obj)
* @param pyeclib_obj_handle
* @param data_len integer length of data in bytes
* @param segment_size integer length of segment in bytes
* @return a python dictionary with segment information
*
*/
static PyObject *
pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
{
PyObject *pyeclib_obj_handle = NULL;
PyObject *ret_dict = NULL;
pyeclib_t *pyeclib_handle = NULL;
int data_len;
int segment_size, last_segment_size;
int num_segments;
int fragment_size, last_fragment_size;
int min_segment_size;
int aligned_segment_size;
int aligned_data_len;
PyObject *ret_dict = NULL; /* python dictionary to return */
int data_len; /* data length from user in bytes */
int segment_size, last_segment_size; /* segment sizes in bytes */
int num_segments; /* total number of segments */
int fragment_size, last_fragment_size; /* fragment sizes in bytes */
int min_segment_size; /* EC algorithm's min. size (B) */
int aligned_segment_size; /* size (B) adjusted for addr alignment */
int aligned_data_len; /* size (B) adjusted for addr alignment */
/* Obtain and validate the method parameters */
if (!PyArg_ParseTuple(args, "Oii", &pyeclib_obj_handle, &data_len, &segment_size)) {
PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.encode");
@@ -906,14 +907,10 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
return NULL;
}
/*
* The minimum segment size depends on the EC type
*/
/* The minimum segment size depends on the EC algorithm */
min_segment_size = get_minimum_encode_size(pyeclib_handle);
/*
* Get the number of segments
*/
/* Get the number of segments */
num_segments = (int)ceill((double)data_len / segment_size);
/*
@@ -924,9 +921,7 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
num_segments--;
}
/*
* Compute the fragment size from the segment size
*/
/* Compute the fragment size from the segment size */
if (num_segments == 1) {
/*
* There is one fragment, or two fragments, where the second is
@@ -939,14 +934,10 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
*/
aligned_data_len = get_aligned_data_size(pyeclib_handle, data_len);
/*
* aligned_data_len is guaranteed to be divisible by k
*/
/* aligned_data_len is guaranteed to be divisible by k */
fragment_size = aligned_data_len / pyeclib_handle->k;
/*
* Segment size is the user-provided segment size
*/
/* Segment size is the user-provided segment size */
segment_size = data_len;
last_fragment_size = fragment_size;
last_segment_size = segment_size;
@@ -958,9 +949,7 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
aligned_segment_size = get_aligned_data_size(pyeclib_handle, segment_size);
/*
* aligned_data_len is guaranteed to be divisible by k
*/
/* aligned_data_len is guaranteed to be divisible by k */
fragment_size = aligned_segment_size / pyeclib_handle->k;
last_segment_size = data_len - (segment_size * (num_segments - 1));
@@ -970,21 +959,20 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
* with the previous fragment
*/
if (last_segment_size < min_segment_size) {
// assert(num_segments > 2)?
// Add current "last segment" to second to last segment
/* Add current "last segment" to second to last segment */
num_segments--;
last_segment_size = last_segment_size + segment_size;
}
aligned_segment_size = get_aligned_data_size(pyeclib_handle, last_segment_size);
// Compute the last fragment size from the last segment size
/* Compute the last fragment size from the last segment size */
last_fragment_size = aligned_segment_size / pyeclib_handle->k;
}
// Add header to fragment sizes
/* Add header to fragment sizes */
last_fragment_size += sizeof(fragment_header_t);
fragment_size += sizeof(fragment_header_t);
@@ -1149,13 +1137,13 @@ exit:
for (i = 0; i < pyeclib_handle->k; i++) {
if (data_to_encode[i]) free_fragment_buffer(data_to_encode[i]);
}
free_buffer(data_to_encode);
check_and_free_buffer(data_to_encode);
}
if (encoded_parity) {
for (i = 0; i < pyeclib_handle->m; i++) {
if (encoded_parity[i]) free_fragment_buffer(encoded_parity[i]);
}
free_buffer(encoded_parity);
check_and_free_buffer(encoded_parity);
}
return list_of_strips;
@@ -1282,8 +1270,8 @@ pyeclib_c_fragments_to_string(PyObject *self, PyObject *args)
ret_string = PY_BUILDVALUE_OBJ_LEN(ret_cstring, ret_data_size);
exit:
free_buffer(data);
free_buffer(ret_cstring);
check_and_free_buffer(data);
check_and_free_buffer(ret_cstring);
return ret_string;
}
@@ -1450,9 +1438,9 @@ error:
return_lists = NULL;
exit:
free_buffer(data);
free_buffer(parity);
free_buffer(missing);
check_and_free_buffer(data);
check_and_free_buffer(parity);
check_and_free_buffer(missing);
return return_lists;
}
@@ -1565,8 +1553,8 @@ pyeclib_c_get_required_fragments(PyObject *self, PyObject *args)
}
exit:
free_buffer(c_missing_list);
free_buffer(fragments_needed);
check_and_free_buffer(c_missing_list);
check_and_free_buffer(fragments_needed);
return fragment_idx_list;
}
@@ -1761,11 +1749,11 @@ out:
}
}
free_buffer(missing_idxs);
free_buffer(data);
free_buffer(parity);
free_buffer(decoding_matrix);
free_buffer(dm_ids);
check_and_free_buffer(missing_idxs);
check_and_free_buffer(data);
check_and_free_buffer(parity);
check_and_free_buffer(decoding_matrix);
check_and_free_buffer(dm_ids);
return reconstructed;
}
@@ -1929,9 +1917,9 @@ exit:
}
}
free_buffer(missing_idxs);
free_buffer(data);
free_buffer(parity);
check_and_free_buffer(missing_idxs);
check_and_free_buffer(data);
check_and_free_buffer(parity);
return list_of_strips;
}
@@ -1976,7 +1964,7 @@ pyeclib_c_get_metadata(PyObject *self, PyObject *args)
get_fragment_metadata(pyeclib_handle, data, fragment_metadata);
ret_fragment_metadata = PY_BUILDVALUE_OBJ_LEN((char*)fragment_metadata,
metadata_len);
free_buffer(fragment_metadata);
check_and_free_buffer(fragment_metadata);
}
return ret_fragment_metadata;
@@ -2095,7 +2083,7 @@ pyeclib_c_check_metadata(PyObject *self, PyObject *args)
for (i = 0; i < m; i++) {
free(parity_sigs[i]);
}
free_buffer(parity_sigs);
check_and_free_buffer(parity_sigs);
for (i = 0; i < k; i++) {
free(c_fragment_signatures[i]);
}