static bool init_stream(MyStream *v, PaStreamParameters *in, PaStreamParameters *out, long size) { memset(v, 0, sizeof(MyStream)); if (in) { long bufsize = round_pow2(Pa_GetSampleSize(in->sampleFormat)* in->channelCount*size); if (!init_buf(&v->in_buf, &v->in_data, bufsize)) return false; } if (out) { long bufsize = round_pow2(Pa_GetSampleSize(out->sampleFormat)* out->channelCount*size); if (!init_buf(&v->out_buf, &v->out_data, bufsize)) { if (in) fini_buf(&v->in_buf, &v->in_data); return false; } #if 0 { /* fake a full write buffer */ long bytes = MyRingBuffer_GetWriteAvailable(&v->out_buf); MyRingBuffer_AdvanceWriteIndex(&v->out_buf, bytes); } #endif } v->time = 0.0; #ifdef TIMER_KLUDGE v->delta = 0.0; #endif pthread_mutex_init(&v->data_mutex, NULL); if (in) { pthread_mutex_init(&v->in_mutex, NULL); pthread_cond_init(&v->in_cond, NULL); } if (out) { pthread_mutex_init(&v->out_mutex, NULL); pthread_cond_init(&v->out_cond, NULL); } if (current) current->prev = v; v->prev = NULL; v->next = current; current = v; return true; }
void *gw_check_realloc(void *p, size_t size, const char *filename, long lineno, const char *function) { struct area *area; if (p == NULL) return gw_check_malloc(size, filename, lineno, function); gw_assert(initialized); gw_assert(size > 0); lock(); area = find_area(p); if (!area) { unlock(); panic(0, "Realloc called on non-allocated area"); } if (size == area->area_size) { /* No changes */ } else if (size <= area->max_size) { change_total_size(size - area->area_size); area->area_size = size; endmark(p, size); } else if (size > area->max_size) { /* The current block is not large enough for the reallocation. * We will allocate a new block, copy the data over, and free * the old block. We round the size up to a power of two, * to prevent frequent reallocations. */ struct area *new_area; size_t new_size; unsigned char *new_p; new_size = round_pow2(size + 2 * MARKER_SIZE); new_p = malloc(new_size); new_size -= 2 * MARKER_SIZE; new_p += MARKER_SIZE; memcpy(new_p, p, area->area_size); fill(new_p + area->area_size, size - area->area_size, NEW_AREA_PATTERN); new_area = record_allocation(new_p, size, area->allocator.filename, area->allocator.lineno, area->allocator.function); new_area->max_size = new_size; free_area(area); p = new_p; area = new_area; } area->reallocator.filename = filename; area->reallocator.lineno = lineno; area->reallocator.function = function; unlock(); return p; }
/* Fill random buffer. */ void clib_random_buffer_fill (clib_random_buffer_t * b, uword n_words) { uword * w, n = n_words; if (n < 256) n = 256; n = round_pow2 (n, 2 << ISAAC_LOG2_SIZE); vec_add2 (b->buffer, w, n); do { isaac2 (b->ctx, w); w += 2 * ISAAC_SIZE; n -= 2 * ISAAC_SIZE; } while (n > 0); }
void vnet_rewrite_copy_slow_path (vnet_rewrite_data_t * p0, vnet_rewrite_data_t * rw0, word n_left, uword most_likely_size) { uword n_done = round_pow2 (most_likely_size, sizeof (rw0[0])) / sizeof (rw0[0]); p0 -= n_done; rw0 -= n_done; /* As we enter the cleanup loop, p0 and rw0 point to the last chunk written by the fast path. Hence, the constant 1, which the vnet_rewrite_copy_one macro renders as p0[-1] = rw0[-1]. */ while (n_left > 0) { vnet_rewrite_copy_one (p0, rw0, 1); p0--; rw0--; n_left--; } }
struct cdf *cdf_create(uint32_t n_vals, int socket_id) { struct cdf *ret; size_t mem_size = 0; uint32_t n_vals_round = round_pow2(n_vals); if (0 == n_vals_round) return NULL; mem_size += sizeof(struct cdf); mem_size += sizeof(((struct cdf *)(0))->elems[0]) * n_vals_round * 2; ret = prox_zmalloc(mem_size, socket_id); ret->elems[0] = n_vals; /* leafs are [n_vals, 2 * n_vals[. During cdf_add() and cdf_setup(), rand_max refers to the index of the next leaf to be added. */ ret->rand_max = n_vals_round; ret->first_child = n_vals_round; ret->seed = rte_rdtsc(); return ret; }
void Image::upload_texture() { if (tex != 0 || image == NULL) return; #ifndef CHOWDREN_IS_WIIU // create alpha mask int size = width * height; BaseBitArray::word_t * data; data = (BaseBitArray::word_t*)malloc(GET_BITARRAY_SIZE(size) * 4); int i = 0; int ii = 0; unsigned char c; while (i < size) { BaseBitArray::word_t word = 0; for (BaseBitArray::word_t m = 1UL; m != 0UL; m <<= 1UL) { c = ((unsigned char*)(((unsigned int*)image) + i))[3]; if (c != 0) word |= m; ++i; if (i >= size) break; } data[ii++] = word; } alpha.data = data; #endif int gl_width, gl_height; #ifdef CHOWDREN_NO_NPOT pot_w = std::max(8, round_pow2(width)); pot_h = std::max(8, round_pow2(height)); if (pot_w >= 1024 || pot_h >= 1024) { pot_w = std::min<short>(1024, pot_w); pot_h = std::min<short>(1024, pot_h); } gl_width = pot_w; gl_height = pot_h; if (pot_w != width || pot_h != height) { unsigned int * old_image =(unsigned int*)image; image = (unsigned char*)malloc(pot_w * pot_h * 4); unsigned int * image_arr = (unsigned int*)image; // in case the image is being cut off due to dimension restrictions int ww = std::min(width, pot_w); int hh = std::min(height, pot_h); for (int x = 0; x < ww; x++) for (int y = 0; y < hh; y++) { image_arr[x + y * pot_w] = old_image[x + y * width]; } stbi_image_free(old_image); } #else gl_width = width; gl_height = height; #endif tex = Render::create_tex(image, Render::RGBA, gl_width, gl_height); Render::set_filter(tex, (flags & LINEAR_FILTER) != 0); if (flags & KEEP) return; // for memory reasons, we delete the image and access the alpha or // the texture directly stbi_image_free(image); image = NULL; }
int cdf_setup(struct cdf *cdf) { uint32_t last_leaf, first_leaf; uint32_t first_parent, last_parent; uint32_t total, multiplier, cur, end; if (cdf->elems[0] == 1) { cdf->rand_max = RAND_MAX; cdf->elems[1] = RAND_MAX; cdf->elems[0] = 2; return 0; } last_leaf = cdf->rand_max; first_leaf = round_pow2(cdf->elems[0]); /* Failed to add all elements through cdf_add() */ if (last_leaf - first_leaf != cdf->elems[0]) return -1; total = 0; for (uint32_t i = first_leaf; i < last_leaf; ++i) { total += cdf->elems[i]; } multiplier = RAND_MAX / total; if (multiplier * total == RAND_MAX) multiplier--; cdf->rand_max = multiplier * total; total = 0; for (uint32_t i = first_leaf; i < last_leaf; ++i) { uint32_t cur = cdf->elems[i]; /* Each element represents the range between previous total (non-inclusive) and new total (inclusive). */ total += cur * multiplier - 1; cdf->elems[i] = total; total += 1; } end = round_pow2(first_leaf) << 1; for (uint32_t i = last_leaf; i < end; ++i) { cdf->elems[i] = RAND_MAX; } cdf->first_child = first_leaf; cdf->elems[0] = end; /* Build the binary tree used at run-time. */ last_leaf = end - 1; do { first_parent = first_leaf/2; last_parent = last_leaf/2; for (uint32_t i = first_parent; i <= last_parent; ++i) { /* The current nodes value should be the biggest value accessible through its left child. This value is stored in the right most child of the left child. The left most child of the right child is the first value that can not be accessed through the left child. */ cdf->elems[i] = get_r_max(cdf, i * 2); } first_leaf = first_parent; last_leaf = last_parent; } while (first_parent != last_parent); return 0; }
int test_mheap_main (unformat_input_t * input) { int i, j, k, n_iterations; void *h, *h_mem; uword *objects = 0; u32 objects_used, really_verbose, n_objects, max_object_size; u32 check_mask, seed, trace, use_vm; u32 print_every = 0; u32 *data; mheap_t *mh; /* Validation flags. */ check_mask = 0; #define CHECK_VALIDITY 1 #define CHECK_DATA 2 #define CHECK_ALIGN 4 #define TEST1 8 n_iterations = 10; seed = 0; max_object_size = 100; n_objects = 1000; trace = 0; really_verbose = 0; use_vm = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (0 == unformat (input, "iter %d", &n_iterations) && 0 == unformat (input, "count %d", &n_objects) && 0 == unformat (input, "size %d", &max_object_size) && 0 == unformat (input, "seed %d", &seed) && 0 == unformat (input, "print %d", &print_every) && 0 == unformat (input, "validdata %|", &check_mask, CHECK_DATA | CHECK_VALIDITY) && 0 == unformat (input, "valid %|", &check_mask, CHECK_VALIDITY) && 0 == unformat (input, "verbose %=", &really_verbose, 1) && 0 == unformat (input, "trace %=", &trace, 1) && 0 == unformat (input, "vm %=", &use_vm, 1) && 0 == unformat (input, "align %|", &check_mask, CHECK_ALIGN) && 0 == unformat (input, "test1 %|", &check_mask, TEST1)) { clib_warning ("unknown input `%U'", format_unformat_error, input); return 1; } } /* Zero seed means use default. */ if (!seed) seed = random_default_seed (); if (check_mask & TEST1) { return test1 (); } if_verbose ("testing %d iterations, %d %saligned objects, max. size %d, seed %d", n_iterations, n_objects, (check_mask & CHECK_ALIGN) ? "randomly " : "un", max_object_size, seed); vec_resize (objects, n_objects); if (vec_bytes (objects) > 0) /* stupid warning be gone */ clib_memset (objects, ~0, vec_bytes (objects)); objects_used = 0; /* Allocate initial heap. */ { uword size = max_pow2 (2 * n_objects * max_object_size * sizeof (data[0])); h_mem = clib_mem_alloc (size); if (!h_mem) return 0; h = mheap_alloc (h_mem, size); } if (trace) mheap_trace (h, trace); mh = mheap_header (h); if (use_vm) mh->flags &= ~MHEAP_FLAG_DISABLE_VM; else mh->flags |= MHEAP_FLAG_DISABLE_VM; if (check_mask & CHECK_VALIDITY) mh->flags |= MHEAP_FLAG_VALIDATE; for (i = 0; i < n_iterations; i++) { while (1) { j = random_u32 (&seed) % vec_len (objects); if (objects[j] != ~0 || i + objects_used < n_iterations) break; } if (objects[j] != ~0) { mheap_put (h, objects[j]); objects_used--; objects[j] = ~0; } else { uword size, align, align_offset; size = (random_u32 (&seed) % max_object_size) * sizeof (data[0]); align = align_offset = 0; if (check_mask & CHECK_ALIGN) { align = 1 << (random_u32 (&seed) % 10); align_offset = round_pow2 (random_u32 (&seed) & (align - 1), sizeof (u32)); } h = mheap_get_aligned (h, size, align, align_offset, &objects[j]); if (align > 0) ASSERT (0 == ((objects[j] + align_offset) & (align - 1))); ASSERT (objects[j] != ~0); objects_used++; /* Set newly allocated object with test data. */ if (check_mask & CHECK_DATA) { uword len; data = (void *) h + objects[j]; len = mheap_len (h, data); ASSERT (size <= mheap_data_bytes (h, objects[j])); data[0] = len; for (k = 1; k < len; k++) data[k] = objects[j] + k; } } /* Verify that all used objects have correct test data. */ if (check_mask & 2) { for (j = 0; j < vec_len (objects); j++) if (objects[j] != ~0) { u32 *data = h + objects[j]; uword len = data[0]; for (k = 1; k < len; k++) ASSERT (data[k] == objects[j] + k); } } if (print_every != 0 && i > 0 && (i % print_every) == 0) fformat (stderr, "iteration %d: %U\n", i, format_mheap, h, really_verbose); } if (verbose) fformat (stderr, "%U\n", format_mheap, h, really_verbose); mheap_free (h); clib_mem_free (h_mem); vec_free (objects); return 0; }