Fixed context passing errors for the heap and added dynamic parameter allocation for parameterized tests

This commit is contained in:
Snaipe 2015-09-21 07:55:40 -07:00
parent 40f7646e22
commit e2cf03c327
3 changed files with 37 additions and 19 deletions

View file

@ -57,17 +57,17 @@ namespace criterion {
typename std::enable_if<std::is_fundamental<T>::value>::type*
new_arr(size_t len) {
void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len);
*(static_cast<size_t*>(ptr)) = len;
T* arr = static_cast<T*>(static_cast<size_t*>(ptr) + 1);
*(reinterpret_cast<size_t*>(ptr)) = len;
T* arr = reinterpret_cast<T*>(reinterpret_cast<size_t*>(ptr) + 1);
return arr;
}
template<typename T>
T* new_arr(size_t len) {
void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len);
*(static_cast<size_t*>(ptr)) = len;
*(reinterpret_cast<size_t*>(ptr)) = len;
T* arr = static_cast<T*>(static_cast<size_t*>(ptr) + 1);
T* arr = reinterpret_cast<T*>(reinterpret_cast<size_t*>(ptr) + 1);
for (size_t i = 0; i < len; ++i)
new (arr + i) T();
return arr;
@ -86,8 +86,8 @@ namespace criterion {
template<typename T>
void delete_arr(T* ptr) {
size_t len = *(static_cast<size_t*>(ptr));
T* arr = static_cast<T*>(static_cast<size_t*>(ptr) + 1);
size_t len = *(reinterpret_cast<size_t*>(ptr));
T* arr = reinterpret_cast<T*>(reinterpret_cast<size_t*>(ptr) + 1);
for (int i = 0; i < len; ++i) {
arr[i].~T();
}

View file

@ -38,24 +38,40 @@ ParameterizedTest(struct parameter_tuple *tup, params, multiple) {
// Cleaning up dynamically generated parameters
// Note: Do **NOT** embed dynamically allocated pointers inside of structures
// or this will fail on windows
// you **MUST** use cr_malloc, cr_free, cr_realloc, and cr_calloc instead of their
// unprefixed counterparts to allocate dynamic memory in parameters, otherwise
// this will crash on Windows builds of the test.
struct parameter_tuple_dyn {
int i;
double *d;
};
void free_params(struct criterion_test_params *crp) {
free(crp->params);
for (size_t i = 0; i < crp->length; ++i) {
struct parameter_tuple_dyn *tup = (struct parameter_tuple_dyn*) crp->params + i;
cr_free(tup->d);
}
cr_free(crp->params);
}
double *gen_double(double val) {
double *ptr = cr_malloc(sizeof (double));
*ptr = val;
return ptr;
}
ParameterizedTestParameters(params, cleanup) {
const size_t nb_tuples = 3;
struct parameter_tuple *params = malloc(sizeof(struct parameter_tuple) * nb_tuples);
params[0] = (struct parameter_tuple) { 1, 2 };
params[1] = (struct parameter_tuple) { 3, 4 };
params[2] = (struct parameter_tuple) { 5, 6 };
struct parameter_tuple_dyn *params = cr_malloc(sizeof (struct parameter_tuple_dyn) * nb_tuples);
params[0] = (struct parameter_tuple_dyn) { 1, gen_double(2) };
params[1] = (struct parameter_tuple_dyn) { 3, gen_double(4) };
params[2] = (struct parameter_tuple_dyn) { 5, gen_double(6) };
return cr_make_param_array(struct parameter_tuple, params, nb_tuples, free_params);
return cr_make_param_array(struct parameter_tuple_dyn, params, nb_tuples, free_params);
}
ParameterizedTest(struct parameter_tuple *tup, params, cleanup) {
cr_assert_fail("Parameters: (%d, %f)", tup->i, tup->d);
ParameterizedTest(struct parameter_tuple_dyn *tup, params, cleanup) {
cr_assert_fail("Parameters: (%d, %f)", tup->i, *tup->d);
}

View file

@ -63,17 +63,19 @@ int inherit_heap(HANDLE child_process) {
if (!(entry.wFlags & PROCESS_HEAP_REGION))
continue;
DWORD region_size = entry.Region.dwCommittedSize;
if (!VirtualAllocEx(child_process,
entry.lpData,
entry.cbData + entry.cbOverhead,
MEM_COMMIT,
region_size,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE))
return -1;
if (!WriteProcessMemory(child_process,
entry.lpData,
entry.lpData,
entry.cbData + entry.cbOverhead,
region_size,
NULL))
return -1;
}